@@ -36,6 +36,75 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
3636 return nil , fmt .Errorf ("failed to get user. Error: %w" , err )
3737 }
3838
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+
39108 for i := range opts .OldCommitIDs {
40109 if opts .NewCommitIDs [i ] == objectFormat .EmptyObjectID ().String () {
41110 results = append (results , private.HookProcReceiveRefResult {
@@ -47,6 +116,37 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
47116 continue
48117 }
49118
119+ if opts .RefFullNames [i ].IsForReview () {
120+ // try match refs/for-review/<pull index>
121+ pullIndex , err := strconv .ParseInt (strings .TrimPrefix (string (opts .RefFullNames [i ]), git .ForReviewPrefix ), 10 , 64 )
122+ if err != nil {
123+ results = append (results , private.HookProcReceiveRefResult {
124+ OriginalRef : opts .RefFullNames [i ],
125+ OldOID : opts .OldCommitIDs [i ],
126+ NewOID : opts .NewCommitIDs [i ],
127+ Err : "Unknow pull request index" ,
128+ })
129+ continue
130+ }
131+ pull , err := issues_model .GetPullRequestByIndex (ctx , repo .ID , pullIndex )
132+ if err != nil {
133+ results = append (results , private.HookProcReceiveRefResult {
134+ OriginalRef : opts .RefFullNames [i ],
135+ OldOID : opts .OldCommitIDs [i ],
136+ NewOID : opts .NewCommitIDs [i ],
137+ Err : "Unknow pull request index" ,
138+ })
139+ continue
140+ }
141+
142+ err = updateExistPull (pull , i )
143+ if err != nil {
144+ return nil , err
145+ }
146+
147+ continue
148+ }
149+
50150 if ! opts .RefFullNames [i ].IsFor () {
51151 results = append (results , private.HookProcReceiveRefResult {
52152 IsNotMatched : true ,
@@ -158,70 +258,10 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
158258 continue
159259 }
160260
161- // update exist pull request
162- if err := pr .LoadBaseRepo (ctx ); err != nil {
163- return nil , fmt .Errorf ("unable to load base repository for PR[%d] Error: %w" , pr .ID , err )
164- }
165-
166- oldCommitID , err := gitRepo .GetRefCommitID (pr .GetGitRefName ())
167- if err != nil {
168- return nil , fmt .Errorf ("unable to get ref commit id in base repository for PR[%d] Error: %w" , pr .ID , err )
169- }
170-
171- if oldCommitID == opts .NewCommitIDs [i ] {
172- results = append (results , private.HookProcReceiveRefResult {
173- OriginalRef : opts .RefFullNames [i ],
174- OldOID : opts .OldCommitIDs [i ],
175- NewOID : opts .NewCommitIDs [i ],
176- Err : "new commit is same with old commit" ,
177- })
178- continue
179- }
180-
181- if ! forcePush {
182- output , _ , err := git .NewCommand (ctx , "rev-list" , "--max-count=1" ).
183- AddDynamicArguments (oldCommitID , "^" + opts .NewCommitIDs [i ]).
184- RunStdString (& git.RunOpts {Dir : repo .RepoPath (), Env : os .Environ ()})
185- if err != nil {
186- return nil , fmt .Errorf ("failed to detect force push: %w" , err )
187- } else if len (output ) > 0 {
188- results = append (results , private.HookProcReceiveRefResult {
189- OriginalRef : opts .RefFullNames [i ],
190- OldOID : opts .OldCommitIDs [i ],
191- NewOID : opts .NewCommitIDs [i ],
192- Err : "request `force-push` push option" ,
193- })
194- continue
195- }
196- }
197-
198- pr .HeadCommitID = opts .NewCommitIDs [i ]
199- if err = pull_service .UpdateRef (ctx , pr ); err != nil {
200- return nil , fmt .Errorf ("failed to update pull ref. Error: %w" , err )
201- }
202-
203- pull_service .AddToTaskQueue (ctx , pr )
204- err = pr .LoadIssue (ctx )
261+ err = updateExistPull (pr , i )
205262 if err != nil {
206- return nil , fmt . Errorf ( "failed to load pull issue. Error: %w" , err )
263+ return nil , err
207264 }
208- comment , err := pull_service .CreatePushPullComment (ctx , pusher , pr , oldCommitID , opts .NewCommitIDs [i ])
209- if err == nil && comment != nil {
210- notify_service .PullRequestPushCommits (ctx , pusher , pr , comment )
211- }
212- notify_service .PullRequestSynchronized (ctx , pusher , pr )
213- isForcePush := comment != nil && comment .IsForcePush
214-
215- results = append (results , private.HookProcReceiveRefResult {
216- OldOID : oldCommitID ,
217- NewOID : opts .NewCommitIDs [i ],
218- Ref : pr .GetGitRefName (),
219- OriginalRef : opts .RefFullNames [i ],
220- IsForcePush : isForcePush ,
221- IsCreatePR : false ,
222- URL : fmt .Sprintf ("%s/pulls/%d" , repo .HTMLURL (), pr .Index ),
223- ShouldShowMessage : setting .Git .PullRequestPushMessage && repo .AllowsPulls (ctx ),
224- })
225265 }
226266
227267 return results , nil
0 commit comments