Skip to content

Commit

Permalink
fix(gitlab): ensure getPrList() runtime integrity (renovatebot#32291)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kriese <[email protected]>
  • Loading branch information
2 people authored and ssams committed Dec 2, 2024
1 parent b425b08 commit 78627a9
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 20 deletions.
110 changes: 94 additions & 16 deletions lib/modules/platform/gitlab/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type * as _git from '../../../util/git';
import type { LongCommitSha } from '../../../util/git/types';
import type * as _hostRules from '../../../util/host-rules';
import { toBase64 } from '../../../util/string';
import { getPrBodyStruct } from '../pr-body';

jest.mock('../../../util/host-rules', () => mockDeep());
jest.mock('../../../util/git');
Expand Down Expand Up @@ -3004,11 +3005,17 @@ describe('modules/platform/gitlab/index', () => {
source_branch: 'branch-a',
title: 'branch a pr',
description: 'a merge request',
state: 'open',
state: 'opened',
},
])
.put('/api/v4/projects/undefined/merge_requests/1')
.reply(200);
.reply(200, {
iid: 1,
source_branch: 'branch-a',
title: 'title',
description: 'body',
state: 'opened',
});
await expect(
gitlab.updatePr({ number: 1, prTitle: 'title', prBody: 'body' }),
).toResolve();
Expand All @@ -3027,11 +3034,17 @@ describe('modules/platform/gitlab/index', () => {
source_branch: 'branch-a',
title: 'Draft: foo',
description: 'a merge request',
state: 'open',
state: 'opened',
},
])
.put('/api/v4/projects/undefined/merge_requests/1')
.reply(200);
.reply(200, {
iid: 1,
source_branch: 'branch-a',
title: 'Draft: title',
description: 'body',
state: 'opened',
});
await expect(
gitlab.updatePr({ number: 1, prTitle: 'title', prBody: 'body' }),
).toResolve();
Expand All @@ -3050,11 +3063,17 @@ describe('modules/platform/gitlab/index', () => {
source_branch: 'branch-a',
title: 'WIP: foo',
description: 'a merge request',
state: 'open',
state: 'opened',
},
])
.put('/api/v4/projects/undefined/merge_requests/1')
.reply(200);
.reply(200, {
iid: 1,
source_branch: 'branch-a',
title: 'WIP: title',
description: 'body',
state: 'opened',
});
await expect(
gitlab.updatePr({ number: 1, prTitle: 'title', prBody: 'body' }),
).toResolve();
Expand All @@ -3073,19 +3092,25 @@ describe('modules/platform/gitlab/index', () => {
source_branch: 'branch-a',
title: 'branch a pr',
description: 'a merge request',
state: 'open',
state: 'opened',
target_branch: 'branch-b',
},
])
.put('/api/v4/projects/undefined/merge_requests/1')
.reply(200);
.reply(200, {
iid: 1,
source_branch: 'branch-a',
title: 'branch a pr',
description: 'body',
state: 'opened',
target_branch: 'branch-new',
});
await expect(
gitlab.updatePr({
number: 1,
prTitle: 'title',
prBody: 'body',
state: 'closed',
targetBranch: 'branch-b',
targetBranch: 'branch-new',
}),
).toResolve();
});
Expand All @@ -3103,11 +3128,17 @@ describe('modules/platform/gitlab/index', () => {
source_branch: 'branch-a',
title: 'branch a pr',
description: 'a merge request',
state: 'open',
state: 'opened',
},
])
.put('/api/v4/projects/undefined/merge_requests/1')
.reply(200)
.reply(200, {
iid: 1,
source_branch: 'branch-a',
title: 'title',
description: 'body',
state: 'opened',
})
.post('/api/v4/projects/undefined/merge_requests/1/approve')
.reply(200);
await expect(
Expand Down Expand Up @@ -3135,11 +3166,17 @@ describe('modules/platform/gitlab/index', () => {
source_branch: 'branch-a',
title: 'branch a pr',
description: 'a merge request',
state: 'open',
state: 'opened',
},
])
.put('/api/v4/projects/undefined/merge_requests/1')
.reply(200);
.reply(200, {
iid: 1,
source_branch: 'branch-a',
title: 'title',
description: 'a merge requbody',
state: 'closed',
});
await expect(
gitlab.updatePr({
number: 1,
Expand All @@ -3163,11 +3200,17 @@ describe('modules/platform/gitlab/index', () => {
source_branch: 'branch-a',
title: 'branch a pr',
description: 'a merge request',
state: 'open',
state: 'opened',
},
])
.put('/api/v4/projects/undefined/merge_requests/1')
.reply(200);
.reply(200, {
iid: 1,
source_branch: 'branch-a',
title: 'title',
description: 'body',
state: 'opened',
});
await expect(
gitlab.updatePr({
number: 1,
Expand All @@ -3179,6 +3222,41 @@ describe('modules/platform/gitlab/index', () => {
}),
).toResolve();
});

it('updates runtime pr list when pr is updated', async () => {
await initPlatform('13.3.6-ee');
httpMock
.scope(gitlabApiHost)
.get(
'/api/v4/projects/undefined/merge_requests?per_page=100&scope=created_by_me',
)
.reply(200, [
{
iid: 1,
source_branch: 'branch-a',
title: 'branch a pr',
description: 'a merge request',
state: 'opened',
},
])
.put('/api/v4/projects/undefined/merge_requests/1')
.reply(200, {
iid: 1,
source_branch: 'branch-a',
title: 'new_title',
description: 'body',
state: 'opened',
});
await gitlab.updatePr({
number: 1,
prTitle: 'new_title',
prBody: 'body',
});
const prList = await gitlab.getPrList();
const updatedPr = prList.find((pr) => pr.number === 1);
expect(updatedPr?.title).toBe('new_title');
expect(updatedPr?.bodyStruct).toMatchObject(getPrBodyStruct('body'));
});
});

describe('reattemptPlatformAutomerge(number, platformPrOptions)', () => {
Expand Down
28 changes: 24 additions & 4 deletions lib/modules/platform/gitlab/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -811,10 +811,30 @@ export async function updatePr({
body.remove_labels = removeLabels;
}

await gitlabApi.putJson(
`projects/${config.repository}/merge_requests/${iid}`,
{ body },
);
const updatedPrInfo = (
await gitlabApi.putJson<GitLabMergeRequest>(
`projects/${config.repository}/merge_requests/${iid}`,
{ body },
)
).body;

const updatedPr = prInfo(updatedPrInfo);

if (config.prList) {
const existingIndex = config.prList.findIndex(
(pr) => pr.number === updatedPr.number,
);
// istanbul ignore if: should not happen
if (existingIndex === -1) {
logger.warn(
{ pr: updatedPr },
'Possible error: Updated PR was not found in the PRs that were returned from getPrList().',
);
config.prList.push(updatedPr);
} else {
config.prList[existingIndex] = updatedPr;
}
}

if (platformPrOptions?.autoApprove) {
await approvePr(iid);
Expand Down

0 comments on commit 78627a9

Please sign in to comment.