Skip to content

Commit

Permalink
fix(gitlab): correctly detect merged results pipelines (#31423)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
sichapman and viceice authored Sep 17, 2024
1 parent 639c53a commit aad49f1
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 4 deletions.
8 changes: 8 additions & 0 deletions lib/modules/platform/gitlab/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ exports[`modules/platform/gitlab/index getBranchPr(branchName) should return the
"hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
},
"hasAssignees": false,
"headPipelineSha": undefined,
"headPipelineStatus": undefined,
"labels": undefined,
"number": 91,
Expand All @@ -86,6 +87,7 @@ exports[`modules/platform/gitlab/index getBranchPr(branchName) should strip depr
"hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
},
"hasAssignees": false,
"headPipelineSha": undefined,
"headPipelineStatus": undefined,
"isDraft": true,
"labels": undefined,
Expand All @@ -105,6 +107,7 @@ exports[`modules/platform/gitlab/index getBranchPr(branchName) should strip draf
"hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
},
"hasAssignees": false,
"headPipelineSha": undefined,
"headPipelineStatus": undefined,
"isDraft": true,
"labels": undefined,
Expand All @@ -124,6 +127,7 @@ exports[`modules/platform/gitlab/index getPr(prNo) removes deprecated draft pref
"hash": "23f41dbec0785a6c77457dd6ebf99ae5970c5fffc9f7a8ad7f66c1b8eeba5b90",
},
"hasAssignees": false,
"headPipelineSha": undefined,
"headPipelineStatus": undefined,
"isDraft": true,
"labels": undefined,
Expand All @@ -143,6 +147,7 @@ exports[`modules/platform/gitlab/index getPr(prNo) removes draft prefix from ret
"hash": "23f41dbec0785a6c77457dd6ebf99ae5970c5fffc9f7a8ad7f66c1b8eeba5b90",
},
"hasAssignees": false,
"headPipelineSha": undefined,
"headPipelineStatus": undefined,
"isDraft": true,
"labels": undefined,
Expand All @@ -162,6 +167,7 @@ exports[`modules/platform/gitlab/index getPr(prNo) returns the PR 1`] = `
"hash": "23f41dbec0785a6c77457dd6ebf99ae5970c5fffc9f7a8ad7f66c1b8eeba5b90",
},
"hasAssignees": false,
"headPipelineSha": undefined,
"headPipelineStatus": undefined,
"labels": undefined,
"number": 12345,
Expand All @@ -180,6 +186,7 @@ exports[`modules/platform/gitlab/index getPr(prNo) returns the PR with nonexisti
"hash": "23f41dbec0785a6c77457dd6ebf99ae5970c5fffc9f7a8ad7f66c1b8eeba5b90",
},
"hasAssignees": true,
"headPipelineSha": undefined,
"headPipelineStatus": undefined,
"labels": undefined,
"number": 12345,
Expand All @@ -198,6 +205,7 @@ exports[`modules/platform/gitlab/index getPr(prNo) returns the mergeable PR 1`]
"hash": "23f41dbec0785a6c77457dd6ebf99ae5970c5fffc9f7a8ad7f66c1b8eeba5b90",
},
"hasAssignees": true,
"headPipelineSha": undefined,
"headPipelineStatus": undefined,
"labels": undefined,
"number": 12345,
Expand Down
139 changes: 138 additions & 1 deletion lib/modules/platform/gitlab/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ describe('modules/platform/gitlab/index', () => {
expect(res).toBe('yellow');
});

it('returns success if no results but head pipeline success', async () => {
it('returns success if no results with merged results pipeline success', async () => {
const scope = await initRepo();
scope
.get(
Expand Down Expand Up @@ -688,7 +688,9 @@ describe('modules/platform/gitlab/index', () => {
},
head_pipeline: {
status: 'success',
sha: 'abcd',
},
sha: 'defg',
});
const res = await gitlab.getBranchStatus('some-branch', true);
expect(res).toBe('green');
Expand Down Expand Up @@ -730,6 +732,140 @@ describe('modules/platform/gitlab/index', () => {
expect(res).toBe('yellow');
});

it('returns pending if merge request with no pipelines', async () => {
const scope = await initRepo();
scope
.get(
'/api/v4/projects/some%2Frepo/repository/commits/0d9c7726c3d628b7e28af234595cfd20febdbf8e/statuses',
)
.reply(200, [])
.get(
'/api/v4/projects/some%2Frepo/merge_requests?per_page=100&scope=created_by_me',
)
.reply(200, [
{
iid: 91,
title: 'some change',
source_branch: 'some-branch',
target_branch: 'master',
state: 'opened',
},
])
.get(
'/api/v4/projects/some%2Frepo/merge_requests/91?include_diverged_commits_count=1',
)
.reply(200, {
iid: 91,
title: 'some change',
state: 'opened',
additions: 1,
deletions: 1,
commits: 1,
source_branch: 'some-branch',
target_branch: 'master',
base: {
sha: '1234',
},
sha: 'abcd',
});
const res = await gitlab.getBranchStatus('some-branch', false);
expect(res).toBe('yellow');
});

it('returns pending if all are internal success with no merged results pipeline', async () => {
const scope = await initRepo();
scope
.get(
'/api/v4/projects/some%2Frepo/repository/commits/0d9c7726c3d628b7e28af234595cfd20febdbf8e/statuses',
)
.reply(200, [
{ name: 'renovate/stability-days', status: 'success' },
{ name: 'renovate/other', status: 'success' },
])
.get(
'/api/v4/projects/some%2Frepo/merge_requests?per_page=100&scope=created_by_me',
)
.reply(200, [
{
iid: 91,
title: 'some change',
source_branch: 'some-branch',
target_branch: 'master',
state: 'opened',
},
])
.get(
'/api/v4/projects/some%2Frepo/merge_requests/91?include_diverged_commits_count=1',
)
.reply(200, {
iid: 91,
title: 'some change',
state: 'opened',
additions: 1,
deletions: 1,
commits: 1,
source_branch: 'some-branch',
target_branch: 'master',
base: {
sha: '1234',
},
head_pipeline: {
status: 'success',
sha: 'abcd',
},
sha: 'abcd',
});
const res = await gitlab.getBranchStatus('some-branch', false);
expect(res).toBe('yellow');
});

it('returns success if all are internal success with merged results pipeline success', async () => {
const scope = await initRepo();
scope
.get(
'/api/v4/projects/some%2Frepo/repository/commits/0d9c7726c3d628b7e28af234595cfd20febdbf8e/statuses',
)
.reply(200, [
{ name: 'renovate/stability-days', status: 'success' },
{ name: 'renovate/other', status: 'success' },
])
.get(
'/api/v4/projects/some%2Frepo/merge_requests?per_page=100&scope=created_by_me',
)
.reply(200, [
{
iid: 91,
title: 'some change',
source_branch: 'some-branch',
target_branch: 'master',
state: 'opened',
},
])
.get(
'/api/v4/projects/some%2Frepo/merge_requests/91?include_diverged_commits_count=1',
)
.reply(200, {
iid: 91,
title: 'some change',
state: 'opened',
additions: 1,
deletions: 1,
commits: 1,
source_branch: 'some-branch',
target_branch: 'master',
base: {
sha: '1234',
},
head_pipeline: {
status: 'success',
sha: 'defg',
},
sha: 'abcd',
});
const res = await gitlab.getBranchStatus('some-branch', false);
expect(res).toBe('green');
});

it('returns success if optional jobs fail', async () => {
const scope = await initRepo();
scope
Expand Down Expand Up @@ -2785,6 +2921,7 @@ describe('modules/platform/gitlab/index', () => {
},
hasAssignees: false,
headPipelineStatus: undefined,
headPipelineSha: undefined,
labels: undefined,
number: 12345,
reviewers: ['foo', 'bar'],
Expand Down
10 changes: 7 additions & 3 deletions lib/modules/platform/gitlab/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,10 +482,13 @@ export async function getBranchStatus(
}
logger.debug(`Got res with ${branchStatuses.length} results`);

const mrStatus = (await getBranchPr(branchName))?.headPipelineStatus;
if (!is.undefined(mrStatus)) {
const mr = await getBranchPr(branchName);
if (mr && mr.sha !== mr.headPipelineSha && mr.headPipelineStatus) {
logger.debug(
'Merge request head pipeline has different sha to commit, assuming merged results pipeline',
);
branchStatuses.push({
status: mrStatus as BranchState,
status: mr.headPipelineStatus as BranchState,
name: 'head_pipeline',
});
}
Expand Down Expand Up @@ -804,6 +807,7 @@ export async function getPr(iid: number): Promise<GitlabPr> {
bodyStruct: getPrBodyStruct(mr.description),
state: mr.state === 'opened' ? 'open' : mr.state,
headPipelineStatus: mr.head_pipeline?.status,
headPipelineSha: mr.head_pipeline?.sha,
hasAssignees: !!(mr.assignee?.id ?? mr.assignees?.[0]?.id),
reviewers: mr.reviewers?.map(({ username }) => username),
title: mr.title,
Expand Down
2 changes: 2 additions & 0 deletions lib/modules/platform/gitlab/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ export interface GitLabMergeRequest {
sha: LongCommitSha;
head_pipeline?: {
status: string;
sha: string;
};
}

export interface GitlabPr extends Pr {
headPipelineStatus?: string;
headPipelineSha?: string;
}

export interface UpdateMergeRequest {
Expand Down

0 comments on commit aad49f1

Please # to comment.