Skip to content

Commit b89bb02

Browse files
authored
Handle unexpected pipeline sha vs latest commit sha (#12)
1 parent 8fa454f commit b89bb02

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

src/MergeRequestAcceptor.ts

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export enum AcceptMergeRequestResultKind {
1818
CanNotBeMerged,
1919
RebaseFailed,
2020
FailedPipeline,
21+
InvalidPipeline,
2122
Unauthorized,
2223
}
2324

@@ -47,6 +48,12 @@ interface FailedPipelineResponse {
4748
pipeline: MergeRequestPipeline;
4849
}
4950

51+
interface InvalidPipelineResponse {
52+
kind: AcceptMergeRequestResultKind.InvalidPipeline;
53+
mergeRequestInfo: MergeRequestInfo;
54+
pipeline: MergeRequestPipeline | null;
55+
}
56+
5057
interface UnauthorizedResponse {
5158
kind: AcceptMergeRequestResultKind.Unauthorized;
5259
mergeRequestInfo: MergeRequestInfo;
@@ -63,6 +70,7 @@ type AcceptMergeRequestResult = SuccessResponse
6370
| CanNotBeMergedResponse
6471
| RebaseFailedResponse
6572
| FailedPipelineResponse
73+
| InvalidPipelineResponse
6674
| UnauthorizedResponse;
6775

6876
interface AcceptMergeRequestOptions {
@@ -152,31 +160,19 @@ export const acceptMergeRequest = async (gitlabApi: GitlabApi, mergeRequest: Mer
152160
}
153161

154162
if (mergeRequestInfo.pipeline === null) {
155-
if (!containsLabel(mergeRequestInfo.labels, BotLabels.WaitingForPipeline)) {
156-
tasks.push(
157-
gitlabApi.updateMergeRequest(mergeRequest.project_id, mergeRequest.iid, {
158-
labels: [...filterBotLabels(mergeRequestInfo.labels), BotLabels.WaitingForPipeline].join(','),
159-
}),
160-
);
161-
}
162-
163-
console.log(`[MR] Pipeline doesn't exist, retrying`);
164-
await Promise.all(tasks);
165-
continue;
163+
return {
164+
kind: AcceptMergeRequestResultKind.InvalidPipeline,
165+
mergeRequestInfo,
166+
pipeline: mergeRequestInfo.pipeline,
167+
};
166168
}
167169

168170
if (mergeRequestInfo.pipeline.sha !== mergeRequestInfo.sha) {
169-
if (!containsLabel(mergeRequestInfo.labels, BotLabels.WaitingForPipeline)) {
170-
tasks.push(
171-
gitlabApi.updateMergeRequest(mergeRequest.project_id, mergeRequest.iid, {
172-
labels: [...filterBotLabels(mergeRequestInfo.labels), BotLabels.WaitingForPipeline].join(','),
173-
}),
174-
);
175-
}
176-
177-
console.log(`[MR] Unexpected pipeline sha, retrying`);
178-
await Promise.all(tasks);
179-
continue;
171+
return {
172+
kind: AcceptMergeRequestResultKind.InvalidPipeline,
173+
mergeRequestInfo,
174+
pipeline: mergeRequestInfo.pipeline,
175+
};
180176
}
181177

182178
if (mergeRequestInfo.pipeline.status === PipelineStatus.Running || mergeRequestInfo.pipeline.status === PipelineStatus.Pending) {

src/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,24 @@ const runMergeRequestCheckerLoop = async (user: User) => {
154154
return;
155155
}
156156

157+
if (result.kind === AcceptMergeRequestResultKind.InvalidPipeline) {
158+
if (result.pipeline === null) {
159+
console.log(`[MR] Pipeline doesn't exist`);
160+
await Promise.all([
161+
assignToAuthorAndResetLabels(gitlabApi, result.mergeRequestInfo),
162+
sendNote(gitlabApi, mergeRequest, `Merge request can't be merged. Pipeline does not exist`),
163+
]);
164+
} else {
165+
console.log(`[MR] Unexpected pipeline sha ${result.pipeline.sha} vs commit ${result.mergeRequestInfo.sha}`);
166+
await Promise.all([
167+
assignToAuthorAndResetLabels(gitlabApi, result.mergeRequestInfo),
168+
sendNote(gitlabApi, mergeRequest, `Merge request can't be merged. The latest pipeline is not executed on the latest commit`),
169+
]);
170+
}
171+
172+
return;
173+
}
174+
157175
if (result.kind === AcceptMergeRequestResultKind.Unauthorized) {
158176
console.log(`[MR] You don't have permissions to accept this merge request, assigning back`);
159177

0 commit comments

Comments
 (0)