Skip to content

Commit 616d092

Browse files
lunnywxiaoguang
andcommitted
Creating push comments before invoke pull request checking (#35647)
This PR moved the creation of pushing comments before pull request mergeable checking. So that when the pull request status changed, the comments should have been created. --------- Co-authored-by: wxiaoguang <[email protected]>
1 parent 602af14 commit 616d092

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

services/issue/comments.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
user_model "code.gitea.io/gitea/models/user"
1616
"code.gitea.io/gitea/modules/gitrepo"
1717
"code.gitea.io/gitea/modules/json"
18+
"code.gitea.io/gitea/modules/log"
1819
"code.gitea.io/gitea/modules/timeutil"
1920
git_service "code.gitea.io/gitea/services/git"
2021
notify_service "code.gitea.io/gitea/services/notify"
@@ -151,15 +152,15 @@ func DeleteComment(ctx context.Context, doer *user_model.User, comment *issues_m
151152
}
152153

153154
// LoadCommentPushCommits Load push commits
154-
func LoadCommentPushCommits(ctx context.Context, c *issues_model.Comment) (err error) {
155+
func LoadCommentPushCommits(ctx context.Context, c *issues_model.Comment) error {
155156
if c.Content == "" || c.Commits != nil || c.Type != issues_model.CommentTypePullRequestPush {
156157
return nil
157158
}
158159

159160
var data issues_model.PushActionContent
160-
err = json.Unmarshal([]byte(c.Content), &data)
161-
if err != nil {
162-
return err
161+
if err := json.Unmarshal([]byte(c.Content), &data); err != nil {
162+
log.Debug("Unmarshal: %v", err) // no need to show 500 error to end user when the JSON is broken
163+
return nil
163164
}
164165

165166
c.IsForcePush = data.IsForcePush
@@ -168,9 +169,15 @@ func LoadCommentPushCommits(ctx context.Context, c *issues_model.Comment) (err e
168169
if len(data.CommitIDs) != 2 {
169170
return nil
170171
}
171-
c.OldCommit = data.CommitIDs[0]
172-
c.NewCommit = data.CommitIDs[1]
172+
c.OldCommit, c.NewCommit = data.CommitIDs[0], data.CommitIDs[1]
173173
} else {
174+
if err := c.LoadIssue(ctx); err != nil {
175+
return err
176+
}
177+
if err := c.Issue.LoadRepo(ctx); err != nil {
178+
return err
179+
}
180+
174181
gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, c.Issue.Repo)
175182
if err != nil {
176183
return err
@@ -179,10 +186,11 @@ func LoadCommentPushCommits(ctx context.Context, c *issues_model.Comment) (err e
179186

180187
c.Commits, err = git_service.ConvertFromGitCommit(ctx, gitRepo.GetCommitsFromIDs(data.CommitIDs), c.Issue.Repo)
181188
if err != nil {
182-
return err
189+
log.Debug("ConvertFromGitCommit: %v", err) // no need to show 500 error to end user when the commit does not exist
190+
} else {
191+
c.CommitsNum = int64(len(c.Commits))
183192
}
184-
c.CommitsNum = int64(len(c.Commits))
185193
}
186194

187-
return err
195+
return nil
188196
}

services/pull/merge.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ func Merge(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.U
248248
}
249249
defer releaser()
250250
defer func() {
251+
// This is a duplicated call to AddTestPullRequestTask (it will also be called by the post-receive hook, via a push queue).
252+
// This call will do some operations (push to base repo, sync commit divergence, add PR conflict check queue task, etc)
253+
// immediately instead of waiting for the "push queue"'s task. The code is from https://github.com/go-gitea/gitea/pull/7082.
254+
// But it's really questionable whether it's worth to do it ahead without waiting for the "push queue" task to run.
255+
// TODO: DUPLICATE-PR-TASK: maybe can try to remove this in 1.26 to see if there is any issue.
251256
go AddTestPullRequestTask(TestPullRequestOptions{
252257
RepoID: pr.BaseRepo.ID,
253258
Doer: doer,

services/pull/pull.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,8 @@ type TestPullRequestOptions struct {
379379
func AddTestPullRequestTask(opts TestPullRequestOptions) {
380380
log.Trace("AddTestPullRequestTask [head_repo_id: %d, head_branch: %s]: finding pull requests", opts.RepoID, opts.Branch)
381381
graceful.GetManager().RunWithShutdownContext(func(ctx context.Context) {
382-
// There is no sensible way to shut this down ":-("
383-
// If you don't let it run all the way then you will lose data
384-
// TODO: graceful: AddTestPullRequestTask needs to become a queue!
385-
382+
// this function does a lot of operations to various models, if the process gets killed in the middle,
383+
// there is no way to recover at the moment. The best workaround is to let end user push again.
386384
// GetUnmergedPullRequestsByHeadInfo() only return open and unmerged PR.
387385
prs, err := issues_model.GetUnmergedPullRequestsByHeadInfo(ctx, opts.RepoID, opts.Branch)
388386
if err != nil {
@@ -401,11 +399,15 @@ func AddTestPullRequestTask(opts TestPullRequestOptions) {
401399
continue
402400
}
403401

404-
StartPullRequestCheckImmediately(ctx, pr)
402+
// create push comment before check pull request status,
403+
// then when the status is mergeable, the comment is already in database, to make testing easy and stable
405404
comment, err := CreatePushPullComment(ctx, opts.Doer, pr, opts.OldCommitID, opts.NewCommitID, opts.IsForcePush)
406405
if err == nil && comment != nil {
407406
notify_service.PullRequestPushCommits(ctx, opts.Doer, pr, comment)
408407
}
408+
// The caller can be in a goroutine or a "push queue", "conflict check" can be time-consuming,
409+
// and the concurrency should be limited, so the conflict check will be done in another queue
410+
StartPullRequestCheckImmediately(ctx, pr)
409411
}
410412

411413
if opts.IsSync {

services/pull/update.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ func Update(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.
5959
}
6060

6161
defer func() {
62+
// The code is from https://github.com/go-gitea/gitea/pull/9784,
63+
// it seems a simple copy-paste from https://github.com/go-gitea/gitea/pull/7082 without a real reason.
64+
// TODO: DUPLICATE-PR-TASK: search and see another TODO comment for more details
6265
go AddTestPullRequestTask(TestPullRequestOptions{
6366
RepoID: pr.BaseRepo.ID,
6467
Doer: doer,

0 commit comments

Comments
 (0)