mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
Move db related basic functions to models/db (#17075)
* Move db related basic functions to models/db * Fix lint * Fix lint * Fix test * Fix lint * Fix lint * revert unnecessary change * Fix test * Fix wrong replace string * Use *Context * Correct committer spelling and fix wrong replaced words Co-authored-by: zeripath <art27@cantab.net>
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
@@ -197,6 +198,10 @@ type Comment struct {
|
||||
IsForcePush bool `xorm:"-"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
db.RegisterModel(new(Comment))
|
||||
}
|
||||
|
||||
// PushActionContent is content of push pull comment
|
||||
type PushActionContent struct {
|
||||
IsForcePush bool `json:"is_force_push"`
|
||||
@@ -205,10 +210,10 @@ type PushActionContent struct {
|
||||
|
||||
// LoadIssue loads issue from database
|
||||
func (c *Comment) LoadIssue() (err error) {
|
||||
return c.loadIssue(x)
|
||||
return c.loadIssue(db.DefaultContext().Engine())
|
||||
}
|
||||
|
||||
func (c *Comment) loadIssue(e Engine) (err error) {
|
||||
func (c *Comment) loadIssue(e db.Engine) (err error) {
|
||||
if c.Issue != nil {
|
||||
return nil
|
||||
}
|
||||
@@ -243,7 +248,7 @@ func (c *Comment) AfterLoad(session *xorm.Session) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Comment) loadPoster(e Engine) (err error) {
|
||||
func (c *Comment) loadPoster(e db.Engine) (err error) {
|
||||
if c.PosterID <= 0 || c.Poster != nil {
|
||||
return nil
|
||||
}
|
||||
@@ -279,7 +284,7 @@ func (c *Comment) HTMLURL() string {
|
||||
log.Error("LoadIssue(%d): %v", c.IssueID, err)
|
||||
return ""
|
||||
}
|
||||
err = c.Issue.loadRepo(x)
|
||||
err = c.Issue.loadRepo(db.DefaultContext().Engine())
|
||||
if err != nil { // Silently dropping errors :unamused:
|
||||
log.Error("loadRepo(%d): %v", c.Issue.RepoID, err)
|
||||
return ""
|
||||
@@ -308,7 +313,7 @@ func (c *Comment) APIURL() string {
|
||||
log.Error("LoadIssue(%d): %v", c.IssueID, err)
|
||||
return ""
|
||||
}
|
||||
err = c.Issue.loadRepo(x)
|
||||
err = c.Issue.loadRepo(db.DefaultContext().Engine())
|
||||
if err != nil { // Silently dropping errors :unamused:
|
||||
log.Error("loadRepo(%d): %v", c.Issue.RepoID, err)
|
||||
return ""
|
||||
@@ -329,7 +334,7 @@ func (c *Comment) IssueURL() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
err = c.Issue.loadRepo(x)
|
||||
err = c.Issue.loadRepo(db.DefaultContext().Engine())
|
||||
if err != nil { // Silently dropping errors :unamused:
|
||||
log.Error("loadRepo(%d): %v", c.Issue.RepoID, err)
|
||||
return ""
|
||||
@@ -345,7 +350,7 @@ func (c *Comment) PRURL() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
err = c.Issue.loadRepo(x)
|
||||
err = c.Issue.loadRepo(db.DefaultContext().Engine())
|
||||
if err != nil { // Silently dropping errors :unamused:
|
||||
log.Error("loadRepo(%d): %v", c.Issue.RepoID, err)
|
||||
return ""
|
||||
@@ -375,7 +380,7 @@ func (c *Comment) EventTag() string {
|
||||
// LoadLabel if comment.Type is CommentTypeLabel, then load Label
|
||||
func (c *Comment) LoadLabel() error {
|
||||
var label Label
|
||||
has, err := x.ID(c.LabelID).Get(&label)
|
||||
has, err := db.DefaultContext().Engine().ID(c.LabelID).Get(&label)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if has {
|
||||
@@ -392,7 +397,7 @@ func (c *Comment) LoadLabel() error {
|
||||
func (c *Comment) LoadProject() error {
|
||||
if c.OldProjectID > 0 {
|
||||
var oldProject Project
|
||||
has, err := x.ID(c.OldProjectID).Get(&oldProject)
|
||||
has, err := db.DefaultContext().Engine().ID(c.OldProjectID).Get(&oldProject)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if has {
|
||||
@@ -402,7 +407,7 @@ func (c *Comment) LoadProject() error {
|
||||
|
||||
if c.ProjectID > 0 {
|
||||
var project Project
|
||||
has, err := x.ID(c.ProjectID).Get(&project)
|
||||
has, err := db.DefaultContext().Engine().ID(c.ProjectID).Get(&project)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if has {
|
||||
@@ -417,7 +422,7 @@ func (c *Comment) LoadProject() error {
|
||||
func (c *Comment) LoadMilestone() error {
|
||||
if c.OldMilestoneID > 0 {
|
||||
var oldMilestone Milestone
|
||||
has, err := x.ID(c.OldMilestoneID).Get(&oldMilestone)
|
||||
has, err := db.DefaultContext().Engine().ID(c.OldMilestoneID).Get(&oldMilestone)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if has {
|
||||
@@ -427,7 +432,7 @@ func (c *Comment) LoadMilestone() error {
|
||||
|
||||
if c.MilestoneID > 0 {
|
||||
var milestone Milestone
|
||||
has, err := x.ID(c.MilestoneID).Get(&milestone)
|
||||
has, err := db.DefaultContext().Engine().ID(c.MilestoneID).Get(&milestone)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if has {
|
||||
@@ -439,7 +444,7 @@ func (c *Comment) LoadMilestone() error {
|
||||
|
||||
// LoadPoster loads comment poster
|
||||
func (c *Comment) LoadPoster() error {
|
||||
return c.loadPoster(x)
|
||||
return c.loadPoster(db.DefaultContext().Engine())
|
||||
}
|
||||
|
||||
// LoadAttachments loads attachments
|
||||
@@ -449,7 +454,7 @@ func (c *Comment) LoadAttachments() error {
|
||||
}
|
||||
|
||||
var err error
|
||||
c.Attachments, err = getAttachmentsByCommentID(x, c.ID)
|
||||
c.Attachments, err = getAttachmentsByCommentID(db.DefaultContext().Engine(), c.ID)
|
||||
if err != nil {
|
||||
log.Error("getAttachmentsByCommentID[%d]: %v", c.ID, err)
|
||||
}
|
||||
@@ -458,7 +463,7 @@ func (c *Comment) LoadAttachments() error {
|
||||
|
||||
// UpdateAttachments update attachments by UUIDs for the comment
|
||||
func (c *Comment) UpdateAttachments(uuids []string) error {
|
||||
sess := x.NewSession()
|
||||
sess := db.DefaultContext().NewSession()
|
||||
defer sess.Close()
|
||||
if err := sess.Begin(); err != nil {
|
||||
return err
|
||||
@@ -482,7 +487,7 @@ func (c *Comment) LoadAssigneeUserAndTeam() error {
|
||||
var err error
|
||||
|
||||
if c.AssigneeID > 0 && c.Assignee == nil {
|
||||
c.Assignee, err = getUserByID(x, c.AssigneeID)
|
||||
c.Assignee, err = getUserByID(db.DefaultContext().Engine(), c.AssigneeID)
|
||||
if err != nil {
|
||||
if !IsErrUserNotExist(err) {
|
||||
return err
|
||||
@@ -517,7 +522,7 @@ func (c *Comment) LoadResolveDoer() (err error) {
|
||||
if c.ResolveDoerID == 0 || c.Type != CommentTypeCode {
|
||||
return nil
|
||||
}
|
||||
c.ResolveDoer, err = getUserByID(x, c.ResolveDoerID)
|
||||
c.ResolveDoer, err = getUserByID(db.DefaultContext().Engine(), c.ResolveDoerID)
|
||||
if err != nil {
|
||||
if IsErrUserNotExist(err) {
|
||||
c.ResolveDoer = NewGhostUser()
|
||||
@@ -537,7 +542,7 @@ func (c *Comment) LoadDepIssueDetails() (err error) {
|
||||
if c.DependentIssueID <= 0 || c.DependentIssue != nil {
|
||||
return nil
|
||||
}
|
||||
c.DependentIssue, err = getIssueByID(x, c.DependentIssueID)
|
||||
c.DependentIssue, err = getIssueByID(db.DefaultContext().Engine(), c.DependentIssueID)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -551,7 +556,7 @@ func (c *Comment) LoadTime() error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Comment) loadReactions(e Engine, repo *Repository) (err error) {
|
||||
func (c *Comment) loadReactions(e db.Engine, repo *Repository) (err error) {
|
||||
if c.Reactions != nil {
|
||||
return nil
|
||||
}
|
||||
@@ -571,10 +576,10 @@ func (c *Comment) loadReactions(e Engine, repo *Repository) (err error) {
|
||||
|
||||
// LoadReactions loads comment reactions
|
||||
func (c *Comment) LoadReactions(repo *Repository) error {
|
||||
return c.loadReactions(x, repo)
|
||||
return c.loadReactions(db.DefaultContext().Engine(), repo)
|
||||
}
|
||||
|
||||
func (c *Comment) loadReview(e Engine) (err error) {
|
||||
func (c *Comment) loadReview(e db.Engine) (err error) {
|
||||
if c.Review == nil {
|
||||
if c.Review, err = getReviewByID(e, c.ReviewID); err != nil {
|
||||
return err
|
||||
@@ -586,7 +591,7 @@ func (c *Comment) loadReview(e Engine) (err error) {
|
||||
|
||||
// LoadReview loads the associated review
|
||||
func (c *Comment) LoadReview() error {
|
||||
return c.loadReview(x)
|
||||
return c.loadReview(db.DefaultContext().Engine())
|
||||
}
|
||||
|
||||
var notEnoughLines = regexp.MustCompile(`fatal: file .* has only \d+ lines?`)
|
||||
@@ -637,7 +642,7 @@ func (c *Comment) CodeCommentURL() string {
|
||||
log.Error("LoadIssue(%d): %v", c.IssueID, err)
|
||||
return ""
|
||||
}
|
||||
err = c.Issue.loadRepo(x)
|
||||
err = c.Issue.loadRepo(db.DefaultContext().Engine())
|
||||
if err != nil { // Silently dropping errors :unamused:
|
||||
log.Error("loadRepo(%d): %v", c.Issue.RepoID, err)
|
||||
return ""
|
||||
@@ -681,7 +686,7 @@ func (c *Comment) LoadPushCommits() (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err error) {
|
||||
func createComment(e db.Engine, opts *CreateCommentOptions) (_ *Comment, err error) {
|
||||
var LabelID int64
|
||||
if opts.Label != nil {
|
||||
LabelID = opts.Label.ID
|
||||
@@ -740,7 +745,7 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err
|
||||
return comment, nil
|
||||
}
|
||||
|
||||
func updateCommentInfos(e *xorm.Session, opts *CreateCommentOptions, comment *Comment) (err error) {
|
||||
func updateCommentInfos(e db.Engine, opts *CreateCommentOptions, comment *Comment) (err error) {
|
||||
// Check comment type.
|
||||
switch opts.Type {
|
||||
case CommentTypeCode:
|
||||
@@ -894,7 +899,7 @@ type CreateCommentOptions struct {
|
||||
|
||||
// CreateComment creates comment of issue or commit.
|
||||
func CreateComment(opts *CreateCommentOptions) (comment *Comment, err error) {
|
||||
sess := x.NewSession()
|
||||
sess := db.DefaultContext().NewSession()
|
||||
defer sess.Close()
|
||||
if err = sess.Begin(); err != nil {
|
||||
return nil, err
|
||||
@@ -919,7 +924,7 @@ func CreateRefComment(doer *User, repo *Repository, issue *Issue, content, commi
|
||||
}
|
||||
|
||||
// Check if same reference from same commit has already existed.
|
||||
has, err := x.Get(&Comment{
|
||||
has, err := db.DefaultContext().Engine().Get(&Comment{
|
||||
Type: CommentTypeCommitRef,
|
||||
IssueID: issue.ID,
|
||||
CommitSHA: commitSHA,
|
||||
@@ -943,10 +948,10 @@ func CreateRefComment(doer *User, repo *Repository, issue *Issue, content, commi
|
||||
|
||||
// GetCommentByID returns the comment by given ID.
|
||||
func GetCommentByID(id int64) (*Comment, error) {
|
||||
return getCommentByID(x, id)
|
||||
return getCommentByID(db.DefaultContext().Engine(), id)
|
||||
}
|
||||
|
||||
func getCommentByID(e Engine, id int64) (*Comment, error) {
|
||||
func getCommentByID(e db.Engine, id int64) (*Comment, error) {
|
||||
c := new(Comment)
|
||||
has, err := e.ID(id).Get(c)
|
||||
if err != nil {
|
||||
@@ -999,7 +1004,7 @@ func (opts *FindCommentsOptions) toConds() builder.Cond {
|
||||
return cond
|
||||
}
|
||||
|
||||
func findComments(e Engine, opts *FindCommentsOptions) ([]*Comment, error) {
|
||||
func findComments(e db.Engine, opts *FindCommentsOptions) ([]*Comment, error) {
|
||||
comments := make([]*Comment, 0, 10)
|
||||
sess := e.Where(opts.toConds())
|
||||
if opts.RepoID > 0 {
|
||||
@@ -1020,12 +1025,12 @@ func findComments(e Engine, opts *FindCommentsOptions) ([]*Comment, error) {
|
||||
|
||||
// FindComments returns all comments according options
|
||||
func FindComments(opts *FindCommentsOptions) ([]*Comment, error) {
|
||||
return findComments(x, opts)
|
||||
return findComments(db.DefaultContext().Engine(), opts)
|
||||
}
|
||||
|
||||
// CountComments count all comments according options by ignoring pagination
|
||||
func CountComments(opts *FindCommentsOptions) (int64, error) {
|
||||
sess := x.Where(opts.toConds())
|
||||
sess := db.DefaultContext().Engine().Where(opts.toConds())
|
||||
if opts.RepoID > 0 {
|
||||
sess.Join("INNER", "issue", "issue.id = comment.issue_id")
|
||||
}
|
||||
@@ -1034,7 +1039,7 @@ func CountComments(opts *FindCommentsOptions) (int64, error) {
|
||||
|
||||
// UpdateComment updates information of comment.
|
||||
func UpdateComment(c *Comment, doer *User) error {
|
||||
sess := x.NewSession()
|
||||
sess := db.DefaultContext().NewSession()
|
||||
defer sess.Close()
|
||||
if err := sess.Begin(); err != nil {
|
||||
return err
|
||||
@@ -1058,7 +1063,7 @@ func UpdateComment(c *Comment, doer *User) error {
|
||||
|
||||
// DeleteComment deletes the comment
|
||||
func DeleteComment(comment *Comment) error {
|
||||
sess := x.NewSession()
|
||||
sess := db.DefaultContext().NewSession()
|
||||
defer sess.Close()
|
||||
if err := sess.Begin(); err != nil {
|
||||
return err
|
||||
@@ -1071,7 +1076,7 @@ func DeleteComment(comment *Comment) error {
|
||||
return sess.Commit()
|
||||
}
|
||||
|
||||
func deleteComment(e Engine, comment *Comment) error {
|
||||
func deleteComment(e db.Engine, comment *Comment) error {
|
||||
if _, err := e.Delete(&Comment{
|
||||
ID: comment.ID,
|
||||
}); err != nil {
|
||||
@@ -1097,11 +1102,11 @@ func deleteComment(e Engine, comment *Comment) error {
|
||||
// CodeComments represents comments on code by using this structure: FILENAME -> LINE (+ == proposed; - == previous) -> COMMENTS
|
||||
type CodeComments map[string]map[int64][]*Comment
|
||||
|
||||
func fetchCodeComments(e Engine, issue *Issue, currentUser *User) (CodeComments, error) {
|
||||
func fetchCodeComments(e db.Engine, issue *Issue, currentUser *User) (CodeComments, error) {
|
||||
return fetchCodeCommentsByReview(e, issue, currentUser, nil)
|
||||
}
|
||||
|
||||
func fetchCodeCommentsByReview(e Engine, issue *Issue, currentUser *User, review *Review) (CodeComments, error) {
|
||||
func fetchCodeCommentsByReview(e db.Engine, issue *Issue, currentUser *User, review *Review) (CodeComments, error) {
|
||||
pathToLineToComment := make(CodeComments)
|
||||
if review == nil {
|
||||
review = &Review{ID: 0}
|
||||
@@ -1126,7 +1131,7 @@ func fetchCodeCommentsByReview(e Engine, issue *Issue, currentUser *User, review
|
||||
return pathToLineToComment, nil
|
||||
}
|
||||
|
||||
func findCodeComments(e Engine, opts FindCommentsOptions, issue *Issue, currentUser *User, review *Review) ([]*Comment, error) {
|
||||
func findCodeComments(e db.Engine, opts FindCommentsOptions, issue *Issue, currentUser *User, review *Review) ([]*Comment, error) {
|
||||
var comments []*Comment
|
||||
if review == nil {
|
||||
review = &Review{ID: 0}
|
||||
@@ -1202,17 +1207,17 @@ func FetchCodeCommentsByLine(issue *Issue, currentUser *User, treePath string, l
|
||||
TreePath: treePath,
|
||||
Line: line,
|
||||
}
|
||||
return findCodeComments(x, opts, issue, currentUser, nil)
|
||||
return findCodeComments(db.DefaultContext().Engine(), opts, issue, currentUser, nil)
|
||||
}
|
||||
|
||||
// FetchCodeComments will return a 2d-map: ["Path"]["Line"] = Comments at line
|
||||
func FetchCodeComments(issue *Issue, currentUser *User) (CodeComments, error) {
|
||||
return fetchCodeComments(x, issue, currentUser)
|
||||
return fetchCodeComments(db.DefaultContext().Engine(), issue, currentUser)
|
||||
}
|
||||
|
||||
// UpdateCommentsMigrationsByType updates comments' migrations information via given git service type and original id and poster id
|
||||
func UpdateCommentsMigrationsByType(tp structs.GitServiceType, originalAuthorID string, posterID int64) error {
|
||||
_, err := x.Table("comment").
|
||||
_, err := db.DefaultContext().Engine().Table("comment").
|
||||
Where(builder.In("issue_id",
|
||||
builder.Select("issue.id").
|
||||
From("issue").
|
||||
|
Reference in New Issue
Block a user