Skip to content

Commit 6870453

Browse files
authored
Rename almost all Ctx functions (#22071)
1 parent 097d4e3 commit 6870453

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+562
-611
lines changed

cmd/admin.go

+1
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,7 @@ func runRepoSyncReleases(_ *cli.Context) error {
778778

779779
func getReleaseCount(id int64) (int64, error) {
780780
return repo_model.GetReleaseCountByRepoID(
781+
db.DefaultContext,
781782
id,
782783
repo_model.FindReleasesOptions{
783784
IncludeTags: true,

models/issues/assignees.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func toggleIssueAssignee(ctx context.Context, issue *Issue, doer *user_model.Use
102102
AssigneeID: assigneeID,
103103
}
104104
// Comment
105-
comment, err = CreateCommentCtx(ctx, opts)
105+
comment, err = CreateComment(ctx, opts)
106106
if err != nil {
107107
return false, nil, fmt.Errorf("createComment: %w", err)
108108
}

models/issues/comment.go

+6-202
Original file line numberDiff line numberDiff line change
@@ -779,8 +779,8 @@ func (c *Comment) LoadPushCommits(ctx context.Context) (err error) {
779779
return err
780780
}
781781

782-
// CreateCommentCtx creates comment with context
783-
func CreateCommentCtx(ctx context.Context, opts *CreateCommentOptions) (_ *Comment, err error) {
782+
// CreateComment creates comment with context
783+
func CreateComment(ctx context.Context, opts *CreateCommentOptions) (_ *Comment, err error) {
784784
e := db.GetEngine(ctx)
785785
var LabelID int64
786786
if opts.Label != nil {
@@ -915,7 +915,7 @@ func createDeadlineComment(ctx context.Context, doer *user_model.User, issue *Is
915915
Issue: issue,
916916
Content: content,
917917
}
918-
comment, err := CreateCommentCtx(ctx, opts)
918+
comment, err := CreateComment(ctx, opts)
919919
if err != nil {
920920
return nil, err
921921
}
@@ -940,7 +940,7 @@ func createIssueDependencyComment(ctx context.Context, doer *user_model.User, is
940940
Issue: issue,
941941
DependentIssueID: dependentIssue.ID,
942942
}
943-
if _, err = CreateCommentCtx(ctx, opts); err != nil {
943+
if _, err = CreateComment(ctx, opts); err != nil {
944944
return
945945
}
946946

@@ -951,7 +951,7 @@ func createIssueDependencyComment(ctx context.Context, doer *user_model.User, is
951951
Issue: dependentIssue,
952952
DependentIssueID: issue.ID,
953953
}
954-
_, err = CreateCommentCtx(ctx, opts)
954+
_, err = CreateComment(ctx, opts)
955955
return err
956956
}
957957

@@ -993,55 +993,6 @@ type CreateCommentOptions struct {
993993
Invalidated bool
994994
}
995995

996-
// CreateComment creates comment of issue or commit.
997-
func CreateComment(opts *CreateCommentOptions) (comment *Comment, err error) {
998-
ctx, committer, err := db.TxContext(db.DefaultContext)
999-
if err != nil {
1000-
return nil, err
1001-
}
1002-
defer committer.Close()
1003-
1004-
comment, err = CreateCommentCtx(ctx, opts)
1005-
if err != nil {
1006-
return nil, err
1007-
}
1008-
1009-
if err = committer.Commit(); err != nil {
1010-
return nil, err
1011-
}
1012-
1013-
return comment, nil
1014-
}
1015-
1016-
// CreateRefComment creates a commit reference comment to issue.
1017-
func CreateRefComment(doer *user_model.User, repo *repo_model.Repository, issue *Issue, content, commitSHA string) error {
1018-
if len(commitSHA) == 0 {
1019-
return fmt.Errorf("cannot create reference with empty commit SHA")
1020-
}
1021-
1022-
// Check if same reference from same commit has already existed.
1023-
has, err := db.GetEngine(db.DefaultContext).Get(&Comment{
1024-
Type: CommentTypeCommitRef,
1025-
IssueID: issue.ID,
1026-
CommitSHA: commitSHA,
1027-
})
1028-
if err != nil {
1029-
return fmt.Errorf("check reference comment: %w", err)
1030-
} else if has {
1031-
return nil
1032-
}
1033-
1034-
_, err = CreateComment(&CreateCommentOptions{
1035-
Type: CommentTypeCommitRef,
1036-
Doer: doer,
1037-
Repo: repo,
1038-
Issue: issue,
1039-
CommitSHA: commitSHA,
1040-
Content: content,
1041-
})
1042-
return err
1043-
}
1044-
1045996
// GetCommentByID returns the comment by given ID.
1046997
func GetCommentByID(ctx context.Context, id int64) (*Comment, error) {
1047998
c := new(Comment)
@@ -1317,39 +1268,6 @@ func UpdateCommentsMigrationsByType(tp structs.GitServiceType, originalAuthorID
13171268
return err
13181269
}
13191270

1320-
// CreatePushPullComment create push code to pull base comment
1321-
func CreatePushPullComment(ctx context.Context, pusher *user_model.User, pr *PullRequest, oldCommitID, newCommitID string) (comment *Comment, err error) {
1322-
if pr.HasMerged || oldCommitID == "" || newCommitID == "" {
1323-
return nil, nil
1324-
}
1325-
1326-
ops := &CreateCommentOptions{
1327-
Type: CommentTypePullRequestPush,
1328-
Doer: pusher,
1329-
Repo: pr.BaseRepo,
1330-
}
1331-
1332-
var data PushActionContent
1333-
1334-
data.CommitIDs, data.IsForcePush, err = getCommitIDsFromRepo(ctx, pr.BaseRepo, oldCommitID, newCommitID, pr.BaseBranch)
1335-
if err != nil {
1336-
return nil, err
1337-
}
1338-
1339-
ops.Issue = pr.Issue
1340-
1341-
dataJSON, err := json.Marshal(data)
1342-
if err != nil {
1343-
return nil, err
1344-
}
1345-
1346-
ops.Content = string(dataJSON)
1347-
1348-
comment, err = CreateComment(ops)
1349-
1350-
return comment, err
1351-
}
1352-
13531271
// CreateAutoMergeComment is a internal function, only use it for CommentTypePRScheduledToAutoMerge and CommentTypePRUnScheduledToAutoMerge CommentTypes
13541272
func CreateAutoMergeComment(ctx context.Context, typ CommentType, pr *PullRequest, doer *user_model.User) (comment *Comment, err error) {
13551273
if typ != CommentTypePRScheduledToAutoMerge && typ != CommentTypePRUnScheduledToAutoMerge {
@@ -1363,7 +1281,7 @@ func CreateAutoMergeComment(ctx context.Context, typ CommentType, pr *PullReques
13631281
return
13641282
}
13651283

1366-
comment, err = CreateCommentCtx(ctx, &CreateCommentOptions{
1284+
comment, err = CreateComment(ctx, &CreateCommentOptions{
13671285
Type: typ,
13681286
Doer: doer,
13691287
Repo: pr.BaseRepo,
@@ -1372,120 +1290,6 @@ func CreateAutoMergeComment(ctx context.Context, typ CommentType, pr *PullReques
13721290
return comment, err
13731291
}
13741292

1375-
// getCommitsFromRepo get commit IDs from repo in between oldCommitID and newCommitID
1376-
// isForcePush will be true if oldCommit isn't on the branch
1377-
// Commit on baseBranch will skip
1378-
func getCommitIDsFromRepo(ctx context.Context, repo *repo_model.Repository, oldCommitID, newCommitID, baseBranch string) (commitIDs []string, isForcePush bool, err error) {
1379-
repoPath := repo.RepoPath()
1380-
gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repoPath)
1381-
if err != nil {
1382-
return nil, false, err
1383-
}
1384-
defer closer.Close()
1385-
1386-
oldCommit, err := gitRepo.GetCommit(oldCommitID)
1387-
if err != nil {
1388-
return nil, false, err
1389-
}
1390-
1391-
if err = oldCommit.LoadBranchName(); err != nil {
1392-
return nil, false, err
1393-
}
1394-
1395-
if len(oldCommit.Branch) == 0 {
1396-
commitIDs = make([]string, 2)
1397-
commitIDs[0] = oldCommitID
1398-
commitIDs[1] = newCommitID
1399-
1400-
return commitIDs, true, err
1401-
}
1402-
1403-
newCommit, err := gitRepo.GetCommit(newCommitID)
1404-
if err != nil {
1405-
return nil, false, err
1406-
}
1407-
1408-
commits, err := newCommit.CommitsBeforeUntil(oldCommitID)
1409-
if err != nil {
1410-
return nil, false, err
1411-
}
1412-
1413-
commitIDs = make([]string, 0, len(commits))
1414-
commitChecks := make(map[string]*commitBranchCheckItem)
1415-
1416-
for _, commit := range commits {
1417-
commitChecks[commit.ID.String()] = &commitBranchCheckItem{
1418-
Commit: commit,
1419-
Checked: false,
1420-
}
1421-
}
1422-
1423-
if err = commitBranchCheck(gitRepo, newCommit, oldCommitID, baseBranch, commitChecks); err != nil {
1424-
return
1425-
}
1426-
1427-
for i := len(commits) - 1; i >= 0; i-- {
1428-
commitID := commits[i].ID.String()
1429-
if item, ok := commitChecks[commitID]; ok && item.Checked {
1430-
commitIDs = append(commitIDs, commitID)
1431-
}
1432-
}
1433-
1434-
return commitIDs, isForcePush, err
1435-
}
1436-
1437-
type commitBranchCheckItem struct {
1438-
Commit *git.Commit
1439-
Checked bool
1440-
}
1441-
1442-
func commitBranchCheck(gitRepo *git.Repository, startCommit *git.Commit, endCommitID, baseBranch string, commitList map[string]*commitBranchCheckItem) error {
1443-
if startCommit.ID.String() == endCommitID {
1444-
return nil
1445-
}
1446-
1447-
checkStack := make([]string, 0, 10)
1448-
checkStack = append(checkStack, startCommit.ID.String())
1449-
1450-
for len(checkStack) > 0 {
1451-
commitID := checkStack[0]
1452-
checkStack = checkStack[1:]
1453-
1454-
item, ok := commitList[commitID]
1455-
if !ok {
1456-
continue
1457-
}
1458-
1459-
if item.Commit.ID.String() == endCommitID {
1460-
continue
1461-
}
1462-
1463-
if err := item.Commit.LoadBranchName(); err != nil {
1464-
return err
1465-
}
1466-
1467-
if item.Commit.Branch == baseBranch {
1468-
continue
1469-
}
1470-
1471-
if item.Checked {
1472-
continue
1473-
}
1474-
1475-
item.Checked = true
1476-
1477-
parentNum := item.Commit.ParentCount()
1478-
for i := 0; i < parentNum; i++ {
1479-
parentCommit, err := item.Commit.Parent(i)
1480-
if err != nil {
1481-
return err
1482-
}
1483-
checkStack = append(checkStack, parentCommit.ID.String())
1484-
}
1485-
}
1486-
return nil
1487-
}
1488-
14891293
// RemapExternalUser ExternalUserRemappable interface
14901294
func (c *Comment) RemapExternalUser(externalName string, externalID, userID int64) error {
14911295
c.OriginalAuthor = externalName

models/issues/comment_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestCreateComment(t *testing.T) {
2424
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
2525

2626
now := time.Now().Unix()
27-
comment, err := issues_model.CreateComment(&issues_model.CreateCommentOptions{
27+
comment, err := issues_model.CreateComment(db.DefaultContext, &issues_model.CreateCommentOptions{
2828
Type: issues_model.CommentTypeComment,
2929
Doer: doer,
3030
Repo: repo,

models/issues/issue.go

+10-14
Original file line numberDiff line numberDiff line change
@@ -202,16 +202,12 @@ func (issue *Issue) LoadRepo(ctx context.Context) (err error) {
202202
}
203203

204204
// IsTimetrackerEnabled returns true if the repo enables timetracking
205-
func (issue *Issue) IsTimetrackerEnabled() bool {
206-
return issue.isTimetrackerEnabled(db.DefaultContext)
207-
}
208-
209-
func (issue *Issue) isTimetrackerEnabled(ctx context.Context) bool {
205+
func (issue *Issue) IsTimetrackerEnabled(ctx context.Context) bool {
210206
if err := issue.LoadRepo(ctx); err != nil {
211207
log.Error(fmt.Sprintf("loadRepo: %v", err))
212208
return false
213209
}
214-
return issue.Repo.IsTimetrackerEnabledCtx(ctx)
210+
return issue.Repo.IsTimetrackerEnabled(ctx)
215211
}
216212

217213
// GetPullRequest returns the issue pull request
@@ -404,7 +400,7 @@ func (issue *Issue) LoadAttributes(ctx context.Context) (err error) {
404400
if err = CommentList(issue.Comments).loadAttributes(ctx); err != nil {
405401
return err
406402
}
407-
if issue.isTimetrackerEnabled(ctx) {
403+
if issue.IsTimetrackerEnabled(ctx) {
408404
if err = issue.LoadTotalTimes(ctx); err != nil {
409405
return err
410406
}
@@ -673,7 +669,7 @@ func changeIssueStatus(ctx context.Context, issue *Issue, doer *user_model.User,
673669

674670
func doChangeIssueStatus(ctx context.Context, issue *Issue, doer *user_model.User, isMergePull bool) (*Comment, error) {
675671
// Check for open dependencies
676-
if issue.IsClosed && issue.Repo.IsDependenciesEnabledCtx(ctx) {
672+
if issue.IsClosed && issue.Repo.IsDependenciesEnabled(ctx) {
677673
// 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
678674
noDeps, err := IssueNoDependenciesLeft(ctx, issue)
679675
if err != nil {
@@ -725,7 +721,7 @@ func doChangeIssueStatus(ctx context.Context, issue *Issue, doer *user_model.Use
725721
cmtType = CommentTypeMergePull
726722
}
727723

728-
return CreateCommentCtx(ctx, &CreateCommentOptions{
724+
return CreateComment(ctx, &CreateCommentOptions{
729725
Type: cmtType,
730726
Doer: doer,
731727
Repo: issue.Repo,
@@ -769,7 +765,7 @@ func ChangeIssueTitle(issue *Issue, doer *user_model.User, oldTitle string) (err
769765
OldTitle: oldTitle,
770766
NewTitle: issue.Title,
771767
}
772-
if _, err = CreateCommentCtx(ctx, opts); err != nil {
768+
if _, err = CreateComment(ctx, opts); err != nil {
773769
return fmt.Errorf("createComment: %w", err)
774770
}
775771
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
805801
OldRef: oldRefFriendly,
806802
NewRef: newRefFriendly,
807803
}
808-
if _, err = CreateCommentCtx(ctx, opts); err != nil {
804+
if _, err = CreateComment(ctx, opts); err != nil {
809805
return fmt.Errorf("createComment: %w", err)
810806
}
811807

@@ -825,7 +821,7 @@ func AddDeletePRBranchComment(ctx context.Context, doer *user_model.User, repo *
825821
Issue: issue,
826822
OldRef: branchName,
827823
}
828-
_, err = CreateCommentCtx(ctx, opts)
824+
_, err = CreateComment(ctx, opts)
829825
return err
830826
}
831827

@@ -992,7 +988,7 @@ func NewIssueWithIndex(ctx context.Context, doer *user_model.User, opts NewIssue
992988
OldMilestoneID: 0,
993989
MilestoneID: opts.Issue.MilestoneID,
994990
}
995-
if _, err = CreateCommentCtx(ctx, opts); err != nil {
991+
if _, err = CreateComment(ctx, opts); err != nil {
996992
return err
997993
}
998994
}
@@ -2000,7 +1996,7 @@ func UpdateIssueByAPI(issue *Issue, doer *user_model.User) (statusChangeComment
20001996
OldTitle: currentIssue.Title,
20011997
NewTitle: issue.Title,
20021998
}
2003-
_, err := CreateCommentCtx(ctx, opts)
1999+
_, err := CreateComment(ctx, opts)
20042000
if err != nil {
20052001
return nil, false, fmt.Errorf("createComment: %w", err)
20062002
}

models/issues/issue_list.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ func (issues IssueList) loadTotalTrackedTimes(ctx context.Context) (err error) {
461461

462462
ids := make([]int64, 0, len(issues))
463463
for _, issue := range issues {
464-
if issue.Repo.IsTimetrackerEnabled() {
464+
if issue.Repo.IsTimetrackerEnabled(ctx) {
465465
ids = append(ids, issue.ID)
466466
}
467467
}

models/issues/issue_lock.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func updateIssueLock(opts *IssueLockOptions, lock bool) error {
5656
Type: commentType,
5757
Content: opts.Reason,
5858
}
59-
if _, err := CreateCommentCtx(ctx, opt); err != nil {
59+
if _, err := CreateComment(ctx, opt); err != nil {
6060
return err
6161
}
6262

0 commit comments

Comments
 (0)