Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename almost all Ctx functions #22071

Merged
merged 9 commits into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,7 @@ func runRepoSyncReleases(_ *cli.Context) error {

func getReleaseCount(id int64) (int64, error) {
return repo_model.GetReleaseCountByRepoID(
db.DefaultContext,
id,
repo_model.FindReleasesOptions{
IncludeTags: true,
Expand Down
2 changes: 1 addition & 1 deletion models/issues/assignees.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
206 changes: 5 additions & 201 deletions models/issues/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ func (c *Comment) LoadPushCommits(ctx context.Context) (err error) {
}

// CreateCommentCtx creates comment with context
lunny marked this conversation as resolved.
Show resolved Hide resolved
func CreateCommentCtx(ctx context.Context, opts *CreateCommentOptions) (_ *Comment, err error) {
func CreateComment(ctx context.Context, opts *CreateCommentOptions) (_ *Comment, err error) {
e := db.GetEngine(ctx)
var LabelID int64
if opts.Label != nil {
Expand Down Expand Up @@ -913,7 +913,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
}
Expand All @@ -938,7 +938,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
}

Expand All @@ -949,7 +949,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
}

Expand Down Expand Up @@ -991,55 +991,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)
Expand Down Expand Up @@ -1315,39 +1266,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 {
Expand All @@ -1361,7 +1279,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,
Expand All @@ -1370,120 +1288,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
Expand Down
2 changes: 1 addition & 1 deletion models/issues/comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 10 additions & 14 deletions models/issues/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion models/issues/issue_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion models/issues/issue_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Loading