@@ -28,7 +28,6 @@ import (
2828 "code.gitea.io/gitea/modules/gitrepo"
2929 "code.gitea.io/gitea/modules/globallock"
3030 "code.gitea.io/gitea/modules/graceful"
31- "code.gitea.io/gitea/modules/json"
3231 "code.gitea.io/gitea/modules/log"
3332 repo_module "code.gitea.io/gitea/modules/repository"
3433 "code.gitea.io/gitea/modules/setting"
@@ -142,37 +141,9 @@ func NewPullRequest(ctx context.Context, opts *NewPullRequestOptions) error {
142141 return err
143142 }
144143
145- compareInfo , err := baseGitRepo .GetCompareInfo (pr .BaseRepo .RepoPath (),
146- git .BranchPrefix + pr .BaseBranch , pr .GetGitRefName (), false , false )
147- if err != nil {
144+ if _ , err := CreatePushPullComment (ctx , issue .Poster , pr , git .BranchPrefix + pr .BaseBranch , pr .GetGitRefName (), false ); err != nil {
148145 return err
149146 }
150- // It maybe an empty pull request. Only non-empty pull request need to create push comment
151- if len (compareInfo .Commits ) > 0 {
152- data := issues_model.PushActionContent {IsForcePush : false }
153- data .CommitIDs = make ([]string , 0 , len (compareInfo .Commits ))
154- for i := len (compareInfo .Commits ) - 1 ; i >= 0 ; i -- {
155- data .CommitIDs = append (data .CommitIDs , compareInfo .Commits [i ].ID .String ())
156- }
157-
158- dataJSON , err := json .Marshal (data )
159- if err != nil {
160- return err
161- }
162-
163- ops := & issues_model.CreateCommentOptions {
164- Type : issues_model .CommentTypePullRequestPush ,
165- Doer : issue .Poster ,
166- Repo : repo ,
167- Issue : pr .Issue ,
168- IsForcePush : false ,
169- Content : string (dataJSON ),
170- }
171-
172- if _ , err = issues_model .CreateComment (ctx , ops ); err != nil {
173- return err
174- }
175- }
176147
177148 if ! pr .IsWorkInProgress (ctx ) {
178149 reviewNotifiers , err = issue_service .PullRequestCodeOwnersReview (ctx , pr )
@@ -335,24 +306,42 @@ func ChangeTargetBranch(ctx context.Context, pr *issues_model.PullRequest, doer
335306 pr .CommitsAhead = divergence .Ahead
336307 pr .CommitsBehind = divergence .Behind
337308
338- if err := pr .UpdateColsIfNotMerged (ctx , "merge_base" , "status" , "conflicted_files" , "changed_protected_files" , "base_branch" , "commits_ahead" , "commits_behind" ); err != nil {
309+ // add first push codes comment
310+ baseGitRepo , err := gitrepo .OpenRepository (ctx , pr .BaseRepo )
311+ if err != nil {
339312 return err
340313 }
314+ defer baseGitRepo .Close ()
341315
342- // Create comment
343- options := & issues_model.CreateCommentOptions {
344- Type : issues_model .CommentTypeChangeTargetBranch ,
345- Doer : doer ,
346- Repo : pr .Issue .Repo ,
347- Issue : pr .Issue ,
348- OldRef : oldBranch ,
349- NewRef : targetBranch ,
350- }
351- if _ , err = issues_model .CreateComment (ctx , options ); err != nil {
352- return fmt .Errorf ("CreateChangeTargetBranchComment: %w" , err )
353- }
316+ return db .WithTx (ctx , func (ctx context.Context ) error {
317+ if err := pr .UpdateColsIfNotMerged (ctx , "merge_base" , "status" , "conflicted_files" , "changed_protected_files" , "base_branch" , "commits_ahead" , "commits_behind" ); err != nil {
318+ return err
319+ }
354320
355- return nil
321+ // Create comment
322+ options := & issues_model.CreateCommentOptions {
323+ Type : issues_model .CommentTypeChangeTargetBranch ,
324+ Doer : doer ,
325+ Repo : pr .Issue .Repo ,
326+ Issue : pr .Issue ,
327+ OldRef : oldBranch ,
328+ NewRef : targetBranch ,
329+ }
330+ if _ , err = issues_model .CreateComment (ctx , options ); err != nil {
331+ return fmt .Errorf ("CreateChangeTargetBranchComment: %w" , err )
332+ }
333+
334+ // Delete all old push comments and insert new push comments
335+ if _ , err := db .GetEngine (ctx ).Where ("issue_id = ?" , pr .IssueID ).
336+ And ("type = ?" , issues_model .CommentTypePullRequestPush ).
337+ NoAutoCondition ().
338+ Delete (new (issues_model.Comment )); err != nil {
339+ return err
340+ }
341+
342+ _ , err = CreatePushPullComment (ctx , doer , pr , git .BranchPrefix + pr .BaseBranch , pr .GetGitRefName (), false )
343+ return err
344+ })
356345}
357346
358347func checkForInvalidation (ctx context.Context , requests issues_model.PullRequestList , repoID int64 , doer * user_model.User , branch string ) error {
@@ -413,7 +402,7 @@ func AddTestPullRequestTask(opts TestPullRequestOptions) {
413402 }
414403
415404 StartPullRequestCheckImmediately (ctx , pr )
416- comment , err := CreatePushPullComment (ctx , opts .Doer , pr , opts .OldCommitID , opts .NewCommitID )
405+ comment , err := CreatePushPullComment (ctx , opts .Doer , pr , opts .OldCommitID , opts .NewCommitID , opts . IsForcePush )
417406 if err == nil && comment != nil {
418407 notify_service .PullRequestPushCommits (ctx , opts .Doer , pr , comment )
419408 }
0 commit comments