@@ -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"
@@ -141,36 +140,7 @@ func NewPullRequest(ctx context.Context, opts *NewPullRequestOptions) error {
141140 return err
142141 }
143142
144- compareInfo , err := GetCompareInfo (ctx , pr .BaseRepo , pr .BaseRepo , baseGitRepo ,
145- git .BranchPrefix + pr .BaseBranch , pr .GetGitHeadRefName (), false , false )
146- if err != nil {
147- return err
148- }
149- if len (compareInfo .Commits ) == 0 {
150- return nil
151- }
152-
153- data := issues_model.PushActionContent {IsForcePush : false }
154- data .CommitIDs = make ([]string , 0 , len (compareInfo .Commits ))
155- for i := len (compareInfo .Commits ) - 1 ; i >= 0 ; i -- {
156- data .CommitIDs = append (data .CommitIDs , compareInfo .Commits [i ].ID .String ())
157- }
158-
159- dataJSON , err := json .Marshal (data )
160- if err != nil {
161- return err
162- }
163-
164- ops := & issues_model.CreateCommentOptions {
165- Type : issues_model .CommentTypePullRequestPush ,
166- Doer : issue .Poster ,
167- Repo : repo ,
168- Issue : pr .Issue ,
169- IsForcePush : false ,
170- Content : string (dataJSON ),
171- }
172-
173- if _ , err = issues_model .CreateComment (ctx , ops ); err != nil {
143+ if _ , err := CreatePushPullComment (ctx , issue .Poster , pr , git .BranchPrefix + pr .BaseBranch , pr .GetGitHeadRefName (), false ); err != nil {
174144 return err
175145 }
176146
@@ -331,24 +301,42 @@ func ChangeTargetBranch(ctx context.Context, pr *issues_model.PullRequest, doer
331301 pr .CommitsAhead = divergence .Ahead
332302 pr .CommitsBehind = divergence .Behind
333303
334- if err := pr .UpdateColsIfNotMerged (ctx , "merge_base" , "status" , "conflicted_files" , "changed_protected_files" , "base_branch" , "commits_ahead" , "commits_behind" ); err != nil {
304+ // add first push codes comment
305+ baseGitRepo , err := gitrepo .OpenRepository (ctx , pr .BaseRepo )
306+ if err != nil {
335307 return err
336308 }
309+ defer baseGitRepo .Close ()
337310
338- // Create comment
339- options := & issues_model.CreateCommentOptions {
340- Type : issues_model .CommentTypeChangeTargetBranch ,
341- Doer : doer ,
342- Repo : pr .Issue .Repo ,
343- Issue : pr .Issue ,
344- OldRef : oldBranch ,
345- NewRef : targetBranch ,
346- }
347- if _ , err = issues_model .CreateComment (ctx , options ); err != nil {
348- return fmt .Errorf ("CreateChangeTargetBranchComment: %w" , err )
349- }
311+ return db .WithTx (ctx , func (ctx context.Context ) error {
312+ if err := pr .UpdateColsIfNotMerged (ctx , "merge_base" , "status" , "conflicted_files" , "changed_protected_files" , "base_branch" , "commits_ahead" , "commits_behind" ); err != nil {
313+ return err
314+ }
350315
351- return nil
316+ // Create comment
317+ options := & issues_model.CreateCommentOptions {
318+ Type : issues_model .CommentTypeChangeTargetBranch ,
319+ Doer : doer ,
320+ Repo : pr .Issue .Repo ,
321+ Issue : pr .Issue ,
322+ OldRef : oldBranch ,
323+ NewRef : targetBranch ,
324+ }
325+ if _ , err = issues_model .CreateComment (ctx , options ); err != nil {
326+ return fmt .Errorf ("CreateChangeTargetBranchComment: %w" , err )
327+ }
328+
329+ // Delete all old push comments and insert new push comments
330+ if _ , err := db .GetEngine (ctx ).Where ("issue_id = ?" , pr .IssueID ).
331+ And ("type = ?" , issues_model .CommentTypePullRequestPush ).
332+ NoAutoCondition ().
333+ Delete (new (issues_model.Comment )); err != nil {
334+ return err
335+ }
336+
337+ _ , err = CreatePushPullComment (ctx , doer , pr , git .BranchPrefix + pr .BaseBranch , pr .GetGitHeadRefName (), false )
338+ return err
339+ })
352340}
353341
354342func checkForInvalidation (ctx context.Context , requests issues_model.PullRequestList , repoID int64 , doer * user_model.User , branch string ) error {
@@ -409,7 +397,7 @@ func AddTestPullRequestTask(opts TestPullRequestOptions) {
409397 }
410398
411399 StartPullRequestCheckImmediately (ctx , pr )
412- comment , err := CreatePushPullComment (ctx , opts .Doer , pr , opts .OldCommitID , opts .NewCommitID )
400+ comment , err := CreatePushPullComment (ctx , opts .Doer , pr , opts .OldCommitID , opts .NewCommitID , opts . IsForcePush )
413401 if err == nil && comment != nil {
414402 notify_service .PullRequestPushCommits (ctx , opts .Doer , pr , comment )
415403 }
0 commit comments