Skip to content

Commit

Permalink
fix(gitlab): verify if MR is mergeable before setting automerge (#6250)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
tmeijn and viceice committed May 28, 2020
1 parent 0ee84ed commit fa3e017
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/platform/gitlab/__snapshots__/index.spec.ts.snap
Expand Up @@ -220,6 +220,28 @@ Array [
"method": "POST",
"url": "https://gitlab.com/api/v4/projects/undefined/merge_requests",
},
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"host": "gitlab.com",
"private-token": "abc123",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://gitlab.com/api/v4/projects/undefined/merge_requests/12345",
},
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"host": "gitlab.com",
"private-token": "abc123",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://gitlab.com/api/v4/projects/undefined/merge_requests/12345",
},
Object {
"body": "{\\"should_remove_source_branch\\":true,\\"merge_when_pipeline_succeeds\\":true}",
"headers": Object {
Expand Down
13 changes: 13 additions & 0 deletions lib/platform/gitlab/index.spec.ts
Expand Up @@ -28,6 +28,7 @@ describe('platform/gitlab', () => {
jest.resetAllMocks();
gitlab = await import('.');
jest.mock('../../util/host-rules');
jest.mock('delay');
hostRules = require('../../util/host-rules');
jest.mock('../git/storage');
GitStorage = require('../git/storage').Storage;
Expand Down Expand Up @@ -1101,6 +1102,18 @@ describe('platform/gitlab', () => {
id: 1,
iid: 12345,
})
.get('/api/v4/projects/undefined/merge_requests/12345')
.reply(200)
.get('/api/v4/projects/undefined/merge_requests/12345')
.reply(200, {
merge_status: 'can_be_merged',
pipeline: {
id: 29626725,
sha: '2be7ddb704c7b6b83732fdd5b9f09d5a397b5f8f',
ref: 'patch-28',
status: 'success',
},
})
.put('/api/v4/projects/undefined/merge_requests/12345/merge')
.reply(200);
await gitlab.createPr({
Expand Down
16 changes: 16 additions & 0 deletions lib/platform/gitlab/index.ts
@@ -1,6 +1,7 @@
import URL, { URLSearchParams } from 'url';
import is from '@sindresorhus/is';

import delay from 'delay';
import { configFileNames } from '../../config/app-strings';
import { RenovateConfig } from '../../config/common';
import {
Expand Down Expand Up @@ -410,6 +411,21 @@ export async function createPr({
}
if (platformOptions && platformOptions.gitLabAutomerge) {
try {
const desiredStatus = 'can_be_merged';
const retryTimes = 5;

// Check for correct merge request status before setting `merge_when_pipeline_succeeds` to `true`.
for (let attempt = 1; attempt <= retryTimes; attempt += 1) {
const { body } = await api.get(
`projects/${config.repository}/merge_requests/${pr.iid}`
);
// Only continue if the merge request can be merged and has a pipeline.
if (body.merge_status === desiredStatus && body.pipeline !== null) {
break;
}
await delay(500 * attempt);
}

await api.put(
`projects/${config.repository}/merge_requests/${pr.iid}/merge`,
{
Expand Down

0 comments on commit fa3e017

Please sign in to comment.