Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gitlab): verify if MR is mergeable before setting automerge #6250

Merged
merged 5 commits into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/platform/gitlab/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('platform/gitlab', () => {
gitlab = await import('.');
api = require('./gl-got-wrapper').api;
jest.mock('../../util/host-rules');
jest.mock('delay', () => Promise.resolve());
hostRules = require('../../util/host-rules');
jest.mock('../git/storage');
GitStorage = require('../git/storage').Storage;
Expand Down Expand Up @@ -952,6 +953,22 @@ describe('platform/gitlab', () => {
});
});
describe('createPr(branchName, title, body)', () => {
beforeEach(() => {
api.get.mockResolvedValueOnce(
viceice marked this conversation as resolved.
Show resolved Hide resolved
partial<GotResponse>({
body: {
merge_status: 'can_be_merged',
pipeline: {
id: 29626725,
sha: '2be7ddb704c7b6b83732fdd5b9f09d5a397b5f8f',
ref: 'patch-28',
status: 'success',
},
},
})
);
});

it('returns the PR', async () => {
api.post.mockResolvedValueOnce(
partial<GotResponse>({
Expand Down
16 changes: 16 additions & 0 deletions lib/platform/gitlab/index.ts
Original file line number Diff line number Diff line change
@@ -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