@@ -94,7 +94,7 @@ export const filterBotLabels = (labels: string[]) => {
9494} ;
9595
9696export const acceptMergeRequest = async ( gitlabApi : GitlabApi , mergeRequest : MergeRequest , user : User , options : AcceptMergeRequestOptions ) : Promise < AcceptMergeRequestResult > => {
97- let mergeRequestInfo ;
97+ let mergeRequestInfo : MergeRequestInfo ;
9898 let numberOfPipelineValidationRetries = defaultPipelineValidationRetries ;
9999
100100 while ( true ) {
@@ -162,26 +162,35 @@ export const acceptMergeRequest = async (gitlabApi: GitlabApi, mergeRequest: Mer
162162 continue ;
163163 }
164164
165- if ( mergeRequestInfo . pipeline === null || mergeRequestInfo . pipeline . sha !== mergeRequestInfo . sha ) {
166- const message = mergeRequestInfo . pipeline === null
167- ? `[MR] Merge request can't be merged. Pipeline does not exist`
168- : `[MR] Merge request can't be merged. The latest pipeline is not executed on the latest commit` ;
169- console . log ( message ) ;
170-
171- if ( numberOfPipelineValidationRetries > 0 ) {
172- numberOfPipelineValidationRetries -- ;
173- await Promise . all ( tasks ) ;
174- continue ;
165+ let currentPipeline : MergeRequestPipeline | null = mergeRequestInfo . pipeline ;
166+
167+ if ( currentPipeline === null || currentPipeline . sha !== mergeRequestInfo . sha ) {
168+ const pipelines = await gitlabApi . getMergeRequestPipelines ( mergeRequest . project_id , mergeRequest . iid ) ;
169+ const currentPipelineCandidate = pipelines . find ( ( pipeline ) => pipeline . sha === mergeRequestInfo . sha ) ;
170+
171+ if ( currentPipelineCandidate === undefined ) {
172+ const message = mergeRequestInfo . pipeline === null
173+ ? `[MR] Merge request can't be merged. Pipeline does not exist`
174+ : `[MR] Merge request can't be merged. The latest pipeline is not executed on the latest commit` ;
175+ console . log ( message ) ;
176+
177+ if ( numberOfPipelineValidationRetries > 0 ) {
178+ numberOfPipelineValidationRetries -- ;
179+ await Promise . all ( tasks ) ;
180+ continue ;
181+ }
182+
183+ return {
184+ kind : AcceptMergeRequestResultKind . InvalidPipeline ,
185+ mergeRequestInfo,
186+ pipeline : mergeRequestInfo . pipeline ,
187+ } ;
175188 }
176189
177- return {
178- kind : AcceptMergeRequestResultKind . InvalidPipeline ,
179- mergeRequestInfo,
180- pipeline : mergeRequestInfo . pipeline ,
181- } ;
190+ currentPipeline = currentPipelineCandidate ;
182191 }
183192
184- if ( mergeRequestInfo . pipeline . status === PipelineStatus . Running || mergeRequestInfo . pipeline . status === PipelineStatus . Pending ) {
193+ if ( currentPipeline . status === PipelineStatus . Running || currentPipeline . status === PipelineStatus . Pending ) {
185194 if ( ! containsLabel ( mergeRequestInfo . labels , BotLabels . WaitingForPipeline ) ) {
186195 tasks . push (
187196 gitlabApi . updateMergeRequest ( mergeRequest . project_id , mergeRequest . iid , {
@@ -190,28 +199,28 @@ export const acceptMergeRequest = async (gitlabApi: GitlabApi, mergeRequest: Mer
190199 ) ;
191200 }
192201
193- console . log ( `[MR] Waiting for CI. Current status: ${ mergeRequestInfo . pipeline . status } ` ) ;
202+ console . log ( `[MR] Waiting for CI. Current status: ${ currentPipeline . status } ` ) ;
194203 await Promise . all ( tasks ) ;
195204 continue ;
196205 }
197206
198- if ( mergeRequestInfo . pipeline . status === PipelineStatus . Canceled ) {
207+ if ( currentPipeline . status === PipelineStatus . Canceled ) {
199208 console . log ( `[MR] pipeline is canceled calling retry` ) ;
200- await gitlabApi . retryPipeline ( mergeRequest . project_id , mergeRequestInfo . pipeline . id ) ;
209+ await gitlabApi . retryPipeline ( mergeRequest . project_id , currentPipeline . id ) ;
201210 numberOfPipelineValidationRetries = defaultPipelineValidationRetries ;
202211 continue ;
203212 }
204213
205- if ( mergeRequestInfo . pipeline . status === PipelineStatus . Failed ) {
214+ if ( currentPipeline . status === PipelineStatus . Failed ) {
206215 return {
207216 kind : AcceptMergeRequestResultKind . FailedPipeline ,
208217 mergeRequestInfo,
209- pipeline : mergeRequestInfo . pipeline ,
218+ pipeline : currentPipeline ,
210219 } ;
211220 }
212221
213- if ( mergeRequestInfo . pipeline . status !== PipelineStatus . Success ) {
214- throw new Error ( `Unexpected pipeline status: ${ mergeRequestInfo . pipeline . status } ` ) ;
222+ if ( currentPipeline . status !== PipelineStatus . Success ) {
223+ throw new Error ( `Unexpected pipeline status: ${ currentPipeline . status } ` ) ;
215224 }
216225
217226 console . log ( '[MR] Calling merge request' ) ;
0 commit comments