@@ -21,6 +21,84 @@ import (
2121 pull_service "code.gitea.io/gitea/services/pull"
2222)
2323
24+ type updateExistPullOption struct {
25+ ctx context.Context
26+ pr * issues_model.PullRequest
27+ gitRepo * git.Repository
28+ repo * repo_model.Repository
29+ forcePush bool
30+ pusher * user_model.User
31+
32+ RefFullName git.RefName
33+ OldCommitID string
34+ NewCommitID string
35+ }
36+
37+ func updateExistPull (opts * updateExistPullOption ) (* private.HookProcReceiveRefResult , error ) {
38+ // update exist pull request
39+ if err := opts .pr .LoadBaseRepo (opts .ctx ); err != nil {
40+ return nil , fmt .Errorf ("unable to load base repository for PR[%d] Error: %w" , opts .pr .ID , err )
41+ }
42+
43+ oldCommitID , err := opts .gitRepo .GetRefCommitID (opts .pr .GetGitRefName ())
44+ if err != nil {
45+ return nil , fmt .Errorf ("unable to get ref commit id in base repository for PR[%d] Error: %w" , opts .pr .ID , err )
46+ }
47+
48+ if oldCommitID == opts .NewCommitID {
49+ return & private.HookProcReceiveRefResult {
50+ OriginalRef : opts .RefFullName ,
51+ OldOID : opts .OldCommitID ,
52+ NewOID : opts .NewCommitID ,
53+ Err : "new commit is same with old commit" ,
54+ }, nil
55+ }
56+
57+ if ! opts .forcePush {
58+ output , _ , err := git .NewCommand (opts .ctx , "rev-list" , "--max-count=1" ).
59+ AddDynamicArguments (oldCommitID , "^" + opts .NewCommitID ).
60+ RunStdString (& git.RunOpts {Dir : opts .repo .RepoPath (), Env : os .Environ ()})
61+ if err != nil {
62+ return nil , fmt .Errorf ("failed to detect force push: %w" , err )
63+ } else if len (output ) > 0 {
64+ return & private.HookProcReceiveRefResult {
65+ OriginalRef : opts .RefFullName ,
66+ OldOID : opts .OldCommitID ,
67+ NewOID : opts .NewCommitID ,
68+ Err : "request `force-push` push option" ,
69+ }, nil
70+ }
71+ }
72+
73+ opts .pr .HeadCommitID = opts .NewCommitID
74+ if err = pull_service .UpdateRef (opts .ctx , opts .pr ); err != nil {
75+ return nil , fmt .Errorf ("failed to update pull ref. Error: %w" , err )
76+ }
77+
78+ pull_service .AddToTaskQueue (opts .ctx , opts .pr )
79+ err = opts .pr .LoadIssue (opts .ctx )
80+ if err != nil {
81+ return nil , fmt .Errorf ("failed to load pull issue. Error: %w" , err )
82+ }
83+ comment , err := pull_service .CreatePushPullComment (opts .ctx , opts .pusher , opts .pr , oldCommitID , opts .NewCommitID )
84+ if err == nil && comment != nil {
85+ notify_service .PullRequestPushCommits (opts .ctx , opts .pusher , opts .pr , comment )
86+ }
87+ notify_service .PullRequestSynchronized (opts .ctx , opts .pusher , opts .pr )
88+ isForcePush := comment != nil && comment .IsForcePush
89+
90+ return & private.HookProcReceiveRefResult {
91+ OldOID : oldCommitID ,
92+ NewOID : opts .NewCommitID ,
93+ Ref : opts .pr .GetGitRefName (),
94+ OriginalRef : opts .RefFullName ,
95+ IsForcePush : isForcePush ,
96+ IsCreatePR : false ,
97+ URL : fmt .Sprintf ("%s/pulls/%d" , opts .repo .HTMLURL (), opts .pr .Index ),
98+ ShouldShowMessage : setting .Git .PullRequestPushMessage && opts .repo .AllowsPulls (opts .ctx ),
99+ }, nil
100+ }
101+
24102// ProcReceive handle proc receive work
25103func ProcReceive (ctx context.Context , repo * repo_model.Repository , gitRepo * git.Repository , opts * private.HookOptions ) ([]private.HookProcReceiveRefResult , error ) {
26104 results := make ([]private.HookProcReceiveRefResult , 0 , len (opts .OldCommitIDs ))
@@ -36,75 +114,6 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
36114 return nil , fmt .Errorf ("failed to get user. Error: %w" , err )
37115 }
38116
39- updateExistPull := func (pr * issues_model.PullRequest , i int ) error {
40- // update exist pull request
41- if err := pr .LoadBaseRepo (ctx ); err != nil {
42- return fmt .Errorf ("unable to load base repository for PR[%d] Error: %w" , pr .ID , err )
43- }
44-
45- oldCommitID , err := gitRepo .GetRefCommitID (pr .GetGitRefName ())
46- if err != nil {
47- return fmt .Errorf ("unable to get ref commit id in base repository for PR[%d] Error: %w" , pr .ID , err )
48- }
49-
50- if oldCommitID == opts .NewCommitIDs [i ] {
51- results = append (results , private.HookProcReceiveRefResult {
52- OriginalRef : opts .RefFullNames [i ],
53- OldOID : opts .OldCommitIDs [i ],
54- NewOID : opts .NewCommitIDs [i ],
55- Err : "new commit is same with old commit" ,
56- })
57- return nil
58- }
59-
60- if ! forcePush {
61- output , _ , err := git .NewCommand (ctx , "rev-list" , "--max-count=1" ).
62- AddDynamicArguments (oldCommitID , "^" + opts .NewCommitIDs [i ]).
63- RunStdString (& git.RunOpts {Dir : repo .RepoPath (), Env : os .Environ ()})
64- if err != nil {
65- return fmt .Errorf ("failed to detect force push: %w" , err )
66- } else if len (output ) > 0 {
67- results = append (results , private.HookProcReceiveRefResult {
68- OriginalRef : opts .RefFullNames [i ],
69- OldOID : opts .OldCommitIDs [i ],
70- NewOID : opts .NewCommitIDs [i ],
71- Err : "request `force-push` push option" ,
72- })
73- return nil
74- }
75- }
76-
77- pr .HeadCommitID = opts .NewCommitIDs [i ]
78- if err = pull_service .UpdateRef (ctx , pr ); err != nil {
79- return fmt .Errorf ("failed to update pull ref. Error: %w" , err )
80- }
81-
82- pull_service .AddToTaskQueue (ctx , pr )
83- err = pr .LoadIssue (ctx )
84- if err != nil {
85- return fmt .Errorf ("failed to load pull issue. Error: %w" , err )
86- }
87- comment , err := pull_service .CreatePushPullComment (ctx , pusher , pr , oldCommitID , opts .NewCommitIDs [i ])
88- if err == nil && comment != nil {
89- notify_service .PullRequestPushCommits (ctx , pusher , pr , comment )
90- }
91- notify_service .PullRequestSynchronized (ctx , pusher , pr )
92- isForcePush := comment != nil && comment .IsForcePush
93-
94- results = append (results , private.HookProcReceiveRefResult {
95- OldOID : oldCommitID ,
96- NewOID : opts .NewCommitIDs [i ],
97- Ref : pr .GetGitRefName (),
98- OriginalRef : opts .RefFullNames [i ],
99- IsForcePush : isForcePush ,
100- IsCreatePR : false ,
101- URL : fmt .Sprintf ("%s/pulls/%d" , repo .HTMLURL (), pr .Index ),
102- ShouldShowMessage : setting .Git .PullRequestPushMessage && repo .AllowsPulls (ctx ),
103- })
104-
105- return nil
106- }
107-
108117 for i := range opts .OldCommitIDs {
109118 if opts .NewCommitIDs [i ] == objectFormat .EmptyObjectID ().String () {
110119 results = append (results , private.HookProcReceiveRefResult {
@@ -139,10 +148,21 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
139148 continue
140149 }
141150
142- err = updateExistPull (pull , i )
151+ result , err := updateExistPull (& updateExistPullOption {
152+ ctx : ctx ,
153+ pr : pull ,
154+ gitRepo : gitRepo ,
155+ repo : repo ,
156+ forcePush : forcePush ,
157+ pusher : pusher ,
158+ RefFullName : opts .RefFullNames [i ],
159+ OldCommitID : opts .OldCommitIDs [i ],
160+ NewCommitID : opts .NewCommitIDs [i ],
161+ })
143162 if err != nil {
144163 return nil , err
145164 }
165+ results = append (results , * result )
146166
147167 continue
148168 }
@@ -258,10 +278,21 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
258278 continue
259279 }
260280
261- err = updateExistPull (pr , i )
281+ result , err := updateExistPull (& updateExistPullOption {
282+ ctx : ctx ,
283+ pr : pr ,
284+ gitRepo : gitRepo ,
285+ repo : repo ,
286+ forcePush : forcePush ,
287+ pusher : pusher ,
288+ RefFullName : opts .RefFullNames [i ],
289+ OldCommitID : opts .OldCommitIDs [i ],
290+ NewCommitID : opts .NewCommitIDs [i ],
291+ })
262292 if err != nil {
263293 return nil , err
264294 }
295+ results = append (results , * result )
265296 }
266297
267298 return results , nil
0 commit comments