summaryrefslogtreecommitdiffstats
path: root/models/issues
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2022-12-10 10:46:31 +0800
committerGitHub <noreply@github.com>2022-12-10 10:46:31 +0800
commit68704532c28cf09db96c988291b2f82c5e615984 (patch)
treec6537092dc11054f96b202fdb957755ed116cd99 /models/issues
parent097d4e30b180eef30600beef2c08095e2571319c (diff)
downloadgitea-68704532c28cf09db96c988291b2f82c5e615984.tar.gz
gitea-68704532c28cf09db96c988291b2f82c5e615984.zip
Rename almost all Ctx functions (#22071)
Diffstat (limited to 'models/issues')
-rw-r--r--models/issues/assignees.go2
-rw-r--r--models/issues/comment.go208
-rw-r--r--models/issues/comment_test.go2
-rw-r--r--models/issues/issue.go24
-rw-r--r--models/issues/issue_list.go2
-rw-r--r--models/issues/issue_lock.go2
-rw-r--r--models/issues/issue_project.go2
-rw-r--r--models/issues/issue_xref.go2
-rw-r--r--models/issues/label.go4
-rw-r--r--models/issues/review.go16
-rw-r--r--models/issues/stopwatch.go6
-rw-r--r--models/issues/tracked_time.go6
12 files changed, 38 insertions, 238 deletions
diff --git a/models/issues/assignees.go b/models/issues/assignees.go
index d3a1f5ffe8..159086bd01 100644
--- a/models/issues/assignees.go
+++ b/models/issues/assignees.go
@@ -102,7 +102,7 @@ func toggleIssueAssignee(ctx context.Context, issue *Issue, doer *user_model.Use
AssigneeID: assigneeID,
}
// Comment
- comment, err = CreateCommentCtx(ctx, opts)
+ comment, err = CreateComment(ctx, opts)
if err != nil {
return false, nil, fmt.Errorf("createComment: %w", err)
}
diff --git a/models/issues/comment.go b/models/issues/comment.go
index 2abe692a6f..612f17aa5a 100644
--- a/models/issues/comment.go
+++ b/models/issues/comment.go
@@ -779,8 +779,8 @@ func (c *Comment) LoadPushCommits(ctx context.Context) (err error) {
return err
}
-// CreateCommentCtx creates comment with context
-func CreateCommentCtx(ctx context.Context, opts *CreateCommentOptions) (_ *Comment, err error) {
+// CreateComment creates comment with context
+func CreateComment(ctx context.Context, opts *CreateCommentOptions) (_ *Comment, err error) {
e := db.GetEngine(ctx)
var LabelID int64
if opts.Label != nil {
@@ -915,7 +915,7 @@ func createDeadlineComment(ctx context.Context, doer *user_model.User, issue *Is
Issue: issue,
Content: content,
}
- comment, err := CreateCommentCtx(ctx, opts)
+ comment, err := CreateComment(ctx, opts)
if err != nil {
return nil, err
}
@@ -940,7 +940,7 @@ func createIssueDependencyComment(ctx context.Context, doer *user_model.User, is
Issue: issue,
DependentIssueID: dependentIssue.ID,
}
- if _, err = CreateCommentCtx(ctx, opts); err != nil {
+ if _, err = CreateComment(ctx, opts); err != nil {
return
}
@@ -951,7 +951,7 @@ func createIssueDependencyComment(ctx context.Context, doer *user_model.User, is
Issue: dependentIssue,
DependentIssueID: issue.ID,
}
- _, err = CreateCommentCtx(ctx, opts)
+ _, err = CreateComment(ctx, opts)
return err
}
@@ -993,55 +993,6 @@ type CreateCommentOptions struct {
Invalidated bool
}
-// CreateComment creates comment of issue or commit.
-func CreateComment(opts *CreateCommentOptions) (comment *Comment, err error) {
- ctx, committer, err := db.TxContext(db.DefaultContext)
- if err != nil {
- return nil, err
- }
- defer committer.Close()
-
- comment, err = CreateCommentCtx(ctx, opts)
- if err != nil {
- return nil, err
- }
-
- if err = committer.Commit(); err != nil {
- return nil, err
- }
-
- return comment, nil
-}
-
-// CreateRefComment creates a commit reference comment to issue.
-func CreateRefComment(doer *user_model.User, repo *repo_model.Repository, issue *Issue, content, commitSHA string) error {
- if len(commitSHA) == 0 {
- return fmt.Errorf("cannot create reference with empty commit SHA")
- }
-
- // Check if same reference from same commit has already existed.
- has, err := db.GetEngine(db.DefaultContext).Get(&Comment{
- Type: CommentTypeCommitRef,
- IssueID: issue.ID,
- CommitSHA: commitSHA,
- })
- if err != nil {
- return fmt.Errorf("check reference comment: %w", err)
- } else if has {
- return nil
- }
-
- _, err = CreateComment(&CreateCommentOptions{
- Type: CommentTypeCommitRef,
- Doer: doer,
- Repo: repo,
- Issue: issue,
- CommitSHA: commitSHA,
- Content: content,
- })
- return err
-}
-
// GetCommentByID returns the comment by given ID.
func GetCommentByID(ctx context.Context, id int64) (*Comment, error) {
c := new(Comment)
@@ -1317,39 +1268,6 @@ func UpdateCommentsMigrationsByType(tp structs.GitServiceType, originalAuthorID
return err
}
-// CreatePushPullComment create push code to pull base comment
-func CreatePushPullComment(ctx context.Context, pusher *user_model.User, pr *PullRequest, oldCommitID, newCommitID string) (comment *Comment, err error) {
- if pr.HasMerged || oldCommitID == "" || newCommitID == "" {
- return nil, nil
- }
-
- ops := &CreateCommentOptions{
- Type: CommentTypePullRequestPush,
- Doer: pusher,
- Repo: pr.BaseRepo,
- }
-
- var data PushActionContent
-
- data.CommitIDs, data.IsForcePush, err = getCommitIDsFromRepo(ctx, pr.BaseRepo, oldCommitID, newCommitID, pr.BaseBranch)
- if err != nil {
- return nil, err
- }
-
- ops.Issue = pr.Issue
-
- dataJSON, err := json.Marshal(data)
- if err != nil {
- return nil, err
- }
-
- ops.Content = string(dataJSON)
-
- comment, err = CreateComment(ops)
-
- return comment, err
-}
-
// CreateAutoMergeComment is a internal function, only use it for CommentTypePRScheduledToAutoMerge and CommentTypePRUnScheduledToAutoMerge CommentTypes
func CreateAutoMergeComment(ctx context.Context, typ CommentType, pr *PullRequest, doer *user_model.User) (comment *Comment, err error) {
if typ != CommentTypePRScheduledToAutoMerge && typ != CommentTypePRUnScheduledToAutoMerge {
@@ -1363,7 +1281,7 @@ func CreateAutoMergeComment(ctx context.Context, typ CommentType, pr *PullReques
return
}
- comment, err = CreateCommentCtx(ctx, &CreateCommentOptions{
+ comment, err = CreateComment(ctx, &CreateCommentOptions{
Type: typ,
Doer: doer,
Repo: pr.BaseRepo,
@@ -1372,120 +1290,6 @@ func CreateAutoMergeComment(ctx context.Context, typ CommentType, pr *PullReques
return comment, err
}
-// getCommitsFromRepo get commit IDs from repo in between oldCommitID and newCommitID
-// isForcePush will be true if oldCommit isn't on the branch
-// Commit on baseBranch will skip
-func getCommitIDsFromRepo(ctx context.Context, repo *repo_model.Repository, oldCommitID, newCommitID, baseBranch string) (commitIDs []string, isForcePush bool, err error) {
- repoPath := repo.RepoPath()
- gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repoPath)
- if err != nil {
- return nil, false, err
- }
- defer closer.Close()
-
- oldCommit, err := gitRepo.GetCommit(oldCommitID)
- if err != nil {
- return nil, false, err
- }
-
- if err = oldCommit.LoadBranchName(); err != nil {
- return nil, false, err
- }
-
- if len(oldCommit.Branch) == 0 {
- commitIDs = make([]string, 2)
- commitIDs[0] = oldCommitID
- commitIDs[1] = newCommitID
-
- return commitIDs, true, err
- }
-
- newCommit, err := gitRepo.GetCommit(newCommitID)
- if err != nil {
- return nil, false, err
- }
-
- commits, err := newCommit.CommitsBeforeUntil(oldCommitID)
- if err != nil {
- return nil, false, err
- }
-
- commitIDs = make([]string, 0, len(commits))
- commitChecks := make(map[string]*commitBranchCheckItem)
-
- for _, commit := range commits {
- commitChecks[commit.ID.String()] = &commitBranchCheckItem{
- Commit: commit,
- Checked: false,
- }
- }
-
- if err = commitBranchCheck(gitRepo, newCommit, oldCommitID, baseBranch, commitChecks); err != nil {
- return
- }
-
- for i := len(commits) - 1; i >= 0; i-- {
- commitID := commits[i].ID.String()
- if item, ok := commitChecks[commitID]; ok && item.Checked {
- commitIDs = append(commitIDs, commitID)
- }
- }
-
- return commitIDs, isForcePush, err
-}
-
-type commitBranchCheckItem struct {
- Commit *git.Commit
- Checked bool
-}
-
-func commitBranchCheck(gitRepo *git.Repository, startCommit *git.Commit, endCommitID, baseBranch string, commitList map[string]*commitBranchCheckItem) error {
- if startCommit.ID.String() == endCommitID {
- return nil
- }
-
- checkStack := make([]string, 0, 10)
- checkStack = append(checkStack, startCommit.ID.String())
-
- for len(checkStack) > 0 {
- commitID := checkStack[0]
- checkStack = checkStack[1:]
-
- item, ok := commitList[commitID]
- if !ok {
- continue
- }
-
- if item.Commit.ID.String() == endCommitID {
- continue
- }
-
- if err := item.Commit.LoadBranchName(); err != nil {
- return err
- }
-
- if item.Commit.Branch == baseBranch {
- continue
- }
-
- if item.Checked {
- continue
- }
-
- item.Checked = true
-
- parentNum := item.Commit.ParentCount()
- for i := 0; i < parentNum; i++ {
- parentCommit, err := item.Commit.Parent(i)
- if err != nil {
- return err
- }
- checkStack = append(checkStack, parentCommit.ID.String())
- }
- }
- return nil
-}
-
// RemapExternalUser ExternalUserRemappable interface
func (c *Comment) RemapExternalUser(externalName string, externalID, userID int64) error {
c.OriginalAuthor = externalName
diff --git a/models/issues/comment_test.go b/models/issues/comment_test.go
index 6a647474dc..0d0570ae34 100644
--- a/models/issues/comment_test.go
+++ b/models/issues/comment_test.go
@@ -24,7 +24,7 @@ func TestCreateComment(t *testing.T) {
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
now := time.Now().Unix()
- comment, err := issues_model.CreateComment(&issues_model.CreateCommentOptions{
+ comment, err := issues_model.CreateComment(db.DefaultContext, &issues_model.CreateCommentOptions{
Type: issues_model.CommentTypeComment,
Doer: doer,
Repo: repo,
diff --git a/models/issues/issue.go b/models/issues/issue.go
index 3ab6d204ba..fc93fcf454 100644
--- a/models/issues/issue.go
+++ b/models/issues/issue.go
@@ -202,16 +202,12 @@ func (issue *Issue) LoadRepo(ctx context.Context) (err error) {
}
// IsTimetrackerEnabled returns true if the repo enables timetracking
-func (issue *Issue) IsTimetrackerEnabled() bool {
- return issue.isTimetrackerEnabled(db.DefaultContext)
-}
-
-func (issue *Issue) isTimetrackerEnabled(ctx context.Context) bool {
+func (issue *Issue) IsTimetrackerEnabled(ctx context.Context) bool {
if err := issue.LoadRepo(ctx); err != nil {
log.Error(fmt.Sprintf("loadRepo: %v", err))
return false
}
- return issue.Repo.IsTimetrackerEnabledCtx(ctx)
+ return issue.Repo.IsTimetrackerEnabled(ctx)
}
// GetPullRequest returns the issue pull request
@@ -404,7 +400,7 @@ func (issue *Issue) LoadAttributes(ctx context.Context) (err error) {
if err = CommentList(issue.Comments).loadAttributes(ctx); err != nil {
return err
}
- if issue.isTimetrackerEnabled(ctx) {
+ if issue.IsTimetrackerEnabled(ctx) {
if err = issue.LoadTotalTimes(ctx); err != nil {
return err
}
@@ -673,7 +669,7 @@ func changeIssueStatus(ctx context.Context, issue *Issue, doer *user_model.User,
func doChangeIssueStatus(ctx context.Context, issue *Issue, doer *user_model.User, isMergePull bool) (*Comment, error) {
// Check for open dependencies
- if issue.IsClosed && issue.Repo.IsDependenciesEnabledCtx(ctx) {
+ if issue.IsClosed && issue.Repo.IsDependenciesEnabled(ctx) {
// only check if dependencies are enabled and we're about to close an issue, otherwise reopening an issue would fail when there are unsatisfied dependencies
noDeps, err := IssueNoDependenciesLeft(ctx, issue)
if err != nil {
@@ -725,7 +721,7 @@ func doChangeIssueStatus(ctx context.Context, issue *Issue, doer *user_model.Use
cmtType = CommentTypeMergePull
}
- return CreateCommentCtx(ctx, &CreateCommentOptions{
+ return CreateComment(ctx, &CreateCommentOptions{
Type: cmtType,
Doer: doer,
Repo: issue.Repo,
@@ -769,7 +765,7 @@ func ChangeIssueTitle(issue *Issue, doer *user_model.User, oldTitle string) (err
OldTitle: oldTitle,
NewTitle: issue.Title,
}
- if _, err = CreateCommentCtx(ctx, opts); err != nil {
+ if _, err = CreateComment(ctx, opts); err != nil {
return fmt.Errorf("createComment: %w", err)
}
if err = issue.AddCrossReferences(ctx, doer, true); err != nil {
@@ -805,7 +801,7 @@ func ChangeIssueRef(issue *Issue, doer *user_model.User, oldRef string) (err err
OldRef: oldRefFriendly,
NewRef: newRefFriendly,
}
- if _, err = CreateCommentCtx(ctx, opts); err != nil {
+ if _, err = CreateComment(ctx, opts); err != nil {
return fmt.Errorf("createComment: %w", err)
}
@@ -825,7 +821,7 @@ func AddDeletePRBranchComment(ctx context.Context, doer *user_model.User, repo *
Issue: issue,
OldRef: branchName,
}
- _, err = CreateCommentCtx(ctx, opts)
+ _, err = CreateComment(ctx, opts)
return err
}
@@ -992,7 +988,7 @@ func NewIssueWithIndex(ctx context.Context, doer *user_model.User, opts NewIssue
OldMilestoneID: 0,
MilestoneID: opts.Issue.MilestoneID,
}
- if _, err = CreateCommentCtx(ctx, opts); err != nil {
+ if _, err = CreateComment(ctx, opts); err != nil {
return err
}
}
@@ -2000,7 +1996,7 @@ func UpdateIssueByAPI(issue *Issue, doer *user_model.User) (statusChangeComment
OldTitle: currentIssue.Title,
NewTitle: issue.Title,
}
- _, err := CreateCommentCtx(ctx, opts)
+ _, err := CreateComment(ctx, opts)
if err != nil {
return nil, false, fmt.Errorf("createComment: %w", err)
}
diff --git a/models/issues/issue_list.go b/models/issues/issue_list.go
index 35a0c1f0e3..e22e48c0bb 100644
--- a/models/issues/issue_list.go
+++ b/models/issues/issue_list.go
@@ -461,7 +461,7 @@ func (issues IssueList) loadTotalTrackedTimes(ctx context.Context) (err error) {
ids := make([]int64, 0, len(issues))
for _, issue := range issues {
- if issue.Repo.IsTimetrackerEnabled() {
+ if issue.Repo.IsTimetrackerEnabled(ctx) {
ids = append(ids, issue.ID)
}
}
diff --git a/models/issues/issue_lock.go b/models/issues/issue_lock.go
index 1376ffcada..19cd6d3167 100644
--- a/models/issues/issue_lock.go
+++ b/models/issues/issue_lock.go
@@ -56,7 +56,7 @@ func updateIssueLock(opts *IssueLockOptions, lock bool) error {
Type: commentType,
Content: opts.Reason,
}
- if _, err := CreateCommentCtx(ctx, opt); err != nil {
+ if _, err := CreateComment(ctx, opt); err != nil {
return err
}
diff --git a/models/issues/issue_project.go b/models/issues/issue_project.go
index a366a92fad..8e559f13c9 100644
--- a/models/issues/issue_project.go
+++ b/models/issues/issue_project.go
@@ -145,7 +145,7 @@ func addUpdateIssueProject(ctx context.Context, issue *Issue, doer *user_model.U
}
if oldProjectID > 0 || newProjectID > 0 {
- if _, err := CreateCommentCtx(ctx, &CreateCommentOptions{
+ if _, err := CreateComment(ctx, &CreateCommentOptions{
Type: CommentTypeProject,
Doer: doer,
Repo: issue.Repo,
diff --git a/models/issues/issue_xref.go b/models/issues/issue_xref.go
index 267ccc73a0..21ee24210f 100644
--- a/models/issues/issue_xref.go
+++ b/models/issues/issue_xref.go
@@ -121,7 +121,7 @@ func (issue *Issue) createCrossReferences(stdCtx context.Context, ctx *crossRefe
RefAction: xref.Action,
RefIsPull: ctx.OrigIssue.IsPull,
}
- _, err := CreateCommentCtx(stdCtx, opts)
+ _, err := CreateComment(stdCtx, opts)
if err != nil {
return err
}
diff --git a/models/issues/label.go b/models/issues/label.go
index e5583ff00f..dbb7a139ef 100644
--- a/models/issues/label.go
+++ b/models/issues/label.go
@@ -613,7 +613,7 @@ func newIssueLabel(ctx context.Context, issue *Issue, label *Label, doer *user_m
Label: label,
Content: "1",
}
- if _, err = CreateCommentCtx(ctx, opts); err != nil {
+ if _, err = CreateComment(ctx, opts); err != nil {
return err
}
@@ -714,7 +714,7 @@ func deleteIssueLabel(ctx context.Context, issue *Issue, label *Label, doer *use
Issue: issue,
Label: label,
}
- if _, err = CreateCommentCtx(ctx, opts); err != nil {
+ if _, err = CreateComment(ctx, opts); err != nil {
return err
}
diff --git a/models/issues/review.go b/models/issues/review.go
index db2d2686cb..7dee28fe97 100644
--- a/models/issues/review.go
+++ b/models/issues/review.go
@@ -294,7 +294,7 @@ func IsOfficialReviewerTeam(ctx context.Context, issue *Issue, team *organizatio
}
if !pr.ProtectedBranch.EnableApprovalsWhitelist {
- return team.UnitAccessModeCtx(ctx, unit.TypeCode) >= perm.AccessModeWrite, nil
+ return team.UnitAccessMode(ctx, unit.TypeCode) >= perm.AccessModeWrite, nil
}
return base.Int64sContains(pr.ProtectedBranch.ApprovalsWhitelistTeamIDs, team.ID), nil
@@ -436,7 +436,7 @@ func SubmitReview(doer *user_model.User, issue *Issue, reviewType ReviewType, co
}
}
- comm, err := CreateCommentCtx(ctx, &CreateCommentOptions{
+ comm, err := CreateComment(ctx, &CreateCommentOptions{
Type: CommentTypeReview,
Doer: doer,
Content: review.Content,
@@ -692,7 +692,7 @@ func AddReviewRequest(issue *Issue, reviewer, doer *user_model.User) (*Comment,
return nil, err
}
- comment, err := CreateCommentCtx(ctx, &CreateCommentOptions{
+ comment, err := CreateComment(ctx, &CreateCommentOptions{
Type: CommentTypeReviewRequest,
Doer: doer,
Repo: issue.Repo,
@@ -746,7 +746,7 @@ func RemoveReviewRequest(issue *Issue, reviewer, doer *user_model.User) (*Commen
}
}
- comment, err := CreateCommentCtx(ctx, &CreateCommentOptions{
+ comment, err := CreateComment(ctx, &CreateCommentOptions{
Type: CommentTypeReviewRequest,
Doer: doer,
Repo: issue.Repo,
@@ -804,7 +804,7 @@ func AddTeamReviewRequest(issue *Issue, reviewer *organization.Team, doer *user_
}
}
- comment, err := CreateCommentCtx(ctx, &CreateCommentOptions{
+ comment, err := CreateComment(ctx, &CreateCommentOptions{
Type: CommentTypeReviewRequest,
Doer: doer,
Repo: issue.Repo,
@@ -814,7 +814,7 @@ func AddTeamReviewRequest(issue *Issue, reviewer *organization.Team, doer *user_
ReviewID: review.ID,
})
if err != nil {
- return nil, fmt.Errorf("CreateCommentCtx(): %w", err)
+ return nil, fmt.Errorf("CreateComment(): %w", err)
}
return comment, committer.Commit()
@@ -864,7 +864,7 @@ func RemoveTeamReviewRequest(issue *Issue, reviewer *organization.Team, doer *us
return nil, committer.Commit()
}
- comment, err := CreateCommentCtx(ctx, &CreateCommentOptions{
+ comment, err := CreateComment(ctx, &CreateCommentOptions{
Type: CommentTypeReviewRequest,
Doer: doer,
Repo: issue.Repo,
@@ -873,7 +873,7 @@ func RemoveTeamReviewRequest(issue *Issue, reviewer *organization.Team, doer *us
AssigneeTeamID: reviewer.ID, // Use AssigneeTeamID as reviewer team ID
})
if err != nil {
- return nil, fmt.Errorf("CreateCommentCtx(): %w", err)
+ return nil, fmt.Errorf("CreateComment(): %w", err)
}
return comment, committer.Commit()
diff --git a/models/issues/stopwatch.go b/models/issues/stopwatch.go
index 8a8fdca339..6bf936c5d4 100644
--- a/models/issues/stopwatch.go
+++ b/models/issues/stopwatch.go
@@ -196,7 +196,7 @@ func FinishIssueStopwatch(ctx context.Context, user *user_model.User, issue *Iss
return err
}
- if _, err := CreateCommentCtx(ctx, &CreateCommentOptions{
+ if _, err := CreateComment(ctx, &CreateCommentOptions{
Doer: user,
Issue: issue,
Repo: issue.Repo,
@@ -246,7 +246,7 @@ func CreateIssueStopwatch(ctx context.Context, user *user_model.User, issue *Iss
return err
}
- if _, err := CreateCommentCtx(ctx, &CreateCommentOptions{
+ if _, err := CreateComment(ctx, &CreateCommentOptions{
Doer: user,
Issue: issue,
Repo: issue.Repo,
@@ -287,7 +287,7 @@ func cancelStopwatch(ctx context.Context, user *user_model.User, issue *Issue) e
return err
}
- if _, err := CreateCommentCtx(ctx, &CreateCommentOptions{
+ if _, err := CreateComment(ctx, &CreateCommentOptions{
Doer: user,
Issue: issue,
Repo: issue.Repo,
diff --git a/models/issues/tracked_time.go b/models/issues/tracked_time.go
index 554b01bd40..ac65d654f2 100644
--- a/models/issues/tracked_time.go
+++ b/models/issues/tracked_time.go
@@ -172,7 +172,7 @@ func AddTime(user *user_model.User, issue *Issue, amount int64, created time.Tim
return nil, err
}
- if _, err := CreateCommentCtx(ctx, &CreateCommentOptions{
+ if _, err := CreateComment(ctx, &CreateCommentOptions{
Issue: issue,
Repo: issue.Repo,
Doer: user,
@@ -250,7 +250,7 @@ func DeleteIssueUserTimes(issue *Issue, user *user_model.User) error {
if err := issue.LoadRepo(ctx); err != nil {
return err
}
- if _, err := CreateCommentCtx(ctx, &CreateCommentOptions{
+ if _, err := CreateComment(ctx, &CreateCommentOptions{
Issue: issue,
Repo: issue.Repo,
Doer: user,
@@ -279,7 +279,7 @@ func DeleteTime(t *TrackedTime) error {
return err
}
- if _, err := CreateCommentCtx(ctx, &CreateCommentOptions{
+ if _, err := CreateComment(ctx, &CreateCommentOptions{
Issue: t.Issue,
Repo: t.Issue.Repo,
Doer: t.User,