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

feat(gitlab): add branch status check attemps #32692

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
304f2e3
feat(gitlab): add branch status check attemps
philippe-granet Nov 23, 2024
2db9c2a
feat(gitlab): reduce RENOVATE_X_GITLAB_BRANCH_STATUS_CHECK_ATTEMPS f…
philippe-granet Nov 23, 2024
825b3bb
Fix unnecessary type assertion
philippe-granet Nov 23, 2024
200551a
Fix tests
philippe-granet Nov 23, 2024
96427de
Fix variable syntax
philippe-granet Nov 24, 2024
46f82f2
Merge branch 'main' into fix-gitlab-external-job
philippe-granet Nov 24, 2024
cbb80d9
Merge branch 'main' into fix-gitlab-external-job
rarkins Nov 25, 2024
c417329
Apply suggestions from code review
philippe-granet Nov 25, 2024
d32d16c
Merge branch 'main' into fix-gitlab-external-job
philippe-granet Nov 25, 2024
429b0e1
Merge branch 'main' into fix-gitlab-external-job
philippe-granet Nov 25, 2024
2bce62d
Merge branch 'main' into fix-gitlab-external-job
philippe-granet Nov 26, 2024
86c7cd6
Merge branch 'main' into fix-gitlab-external-job
rarkins Nov 26, 2024
8b07574
Fix coverage threshold
philippe-granet Nov 26, 2024
f38de6e
Merge branch 'main' into fix-gitlab-external-job
philippe-granet Nov 26, 2024
dff4417
Merge branch 'main' into fix-gitlab-external-job
rarkins Nov 27, 2024
a390ded
catch errors on polling
philippe-granet Dec 11, 2024
bf86af5
Don't use cache when retrieve commit pipeline
philippe-granet Dec 11, 2024
661779b
Fix tests
philippe-granet Dec 11, 2024
78c7228
Merge branch 'main' into fix-gitlab-external-job
philippe-granet Dec 11, 2024
5a3f6d3
Add caution in doc for projects that do not have defined pipelines
philippe-granet Dec 11, 2024
8b818bd
Merge branch 'main' into fix-gitlab-external-job
philippe-granet Dec 12, 2024
9bc2099
Merge branch 'main' into fix-gitlab-external-job
philippe-granet Dec 12, 2024
70babcd
Merge branch 'main' into fix-gitlab-external-job
philippe-granet Jan 18, 2025
c9679a2
Don't skip the for loop if the value is unset
philippe-granet Jan 18, 2025
6d71bd4
Add failed to retrieve commit pipeline test
philippe-granet Jan 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/usage/self-hosted-experimental.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ The formula for the delay between attempts is `RENOVATE_X_GITLAB_MERGE_REQUEST_D

Default value: `5` (attempts results in max. 13.75 seconds timeout).

## `RENOVATE_X_GITLAB_BRANCH_STATUS_CHECK_ATTEMPTS`

If set to an positive integer, Renovate will use this as the number of attempts to check branch status before trying to add status check.
philippe-granet marked this conversation as resolved.
Show resolved Hide resolved
The delay between attempts is `RENOVATE_X_GITLAB_BRANCH_STATUS_DELAY` milliseconds.

Default value: `2` (attempts results in max. 2 seconds timeout).
philippe-granet marked this conversation as resolved.
Show resolved Hide resolved

## `RENOVATE_X_GITLAB_BRANCH_STATUS_DELAY`

Adjust default time (in milliseconds) given to GitLab to create pipelines for a commit pushed by Renovate.
Expand Down
66 changes: 64 additions & 2 deletions lib/modules/platform/gitlab/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,10 @@ describe('modules/platform/gitlab/index', () => {
.get(
'/api/v4/projects/some%2Frepo/repository/commits/0d9c7726c3d628b7e28af234595cfd20febdbf8e',
)
.reply(200, [])
.get(
'/api/v4/projects/some%2Frepo/repository/commits/0d9c7726c3d628b7e28af234595cfd20febdbf8e',
)
.reply(200, []);

await expect(
Expand Down Expand Up @@ -1098,6 +1102,10 @@ describe('modules/platform/gitlab/index', () => {
.get(
'/api/v4/projects/some%2Frepo/repository/commits/0d9c7726c3d628b7e28af234595cfd20febdbf8e',
)
.reply(200, [])
.get(
'/api/v4/projects/some%2Frepo/repository/commits/0d9c7726c3d628b7e28af234595cfd20febdbf8e',
)
.reply(200, []);

await gitlab.setBranchStatus({
Expand All @@ -1108,7 +1116,7 @@ describe('modules/platform/gitlab/index', () => {
url: 'some-url',
});

expect(timers.setTimeout.mock.calls).toHaveLength(1);
expect(timers.setTimeout.mock.calls).toHaveLength(2);
expect(timers.setTimeout.mock.calls[0][0]).toBe(1000);
});

Expand Down Expand Up @@ -1145,6 +1153,7 @@ describe('modules/platform/gitlab/index', () => {

it('waits for RENOVATE_X_GITLAB_BRANCH_STATUS_DELAY ms when set', async () => {
const delay = 5000;
const retry = 2;
process.env.RENOVATE_X_GITLAB_BRANCH_STATUS_DELAY = String(delay);

const scope = await initRepo();
Expand All @@ -1160,6 +1169,59 @@ describe('modules/platform/gitlab/index', () => {
.get(
'/api/v4/projects/some%2Frepo/repository/commits/0d9c7726c3d628b7e28af234595cfd20febdbf8e',
)
.reply(200, [])
.get(
'/api/v4/projects/some%2Frepo/repository/commits/0d9c7726c3d628b7e28af234595cfd20febdbf8e',
)
.reply(200, []);

await gitlab.setBranchStatus({
branchName: 'some-branch',
context: 'some-context',
description: 'some-description',
state: 'green',
url: 'some-url',
});

expect(timers.setTimeout.mock.calls).toHaveLength(retry);
expect(timers.setTimeout.mock.calls[0][0]).toBe(delay);
});

it('do RENOVATE_X_GITLAB_BRANCH_STATUS_CHECK_ATTEMPTS attemps when set', async () => {
const delay = 1000;
const retry = 5;
process.env.RENOVATE_X_GITLAB_BRANCH_STATUS_CHECK_ATTEMPTS =
String(retry);
philippe-granet marked this conversation as resolved.
Show resolved Hide resolved

const scope = await initRepo();
scope
.post(
'/api/v4/projects/some%2Frepo/statuses/0d9c7726c3d628b7e28af234595cfd20febdbf8e',
)
.reply(200, {})
.get(
'/api/v4/projects/some%2Frepo/repository/commits/0d9c7726c3d628b7e28af234595cfd20febdbf8e/statuses',
)
.reply(200, [])
.get(
'/api/v4/projects/some%2Frepo/repository/commits/0d9c7726c3d628b7e28af234595cfd20febdbf8e',
)
.reply(200, [])
.get(
'/api/v4/projects/some%2Frepo/repository/commits/0d9c7726c3d628b7e28af234595cfd20febdbf8e',
)
.reply(200, [])
.get(
'/api/v4/projects/some%2Frepo/repository/commits/0d9c7726c3d628b7e28af234595cfd20febdbf8e',
)
.reply(200, [])
.get(
'/api/v4/projects/some%2Frepo/repository/commits/0d9c7726c3d628b7e28af234595cfd20febdbf8e',
)
.reply(200, [])
.get(
'/api/v4/projects/some%2Frepo/repository/commits/0d9c7726c3d628b7e28af234595cfd20febdbf8e',
)
.reply(200, []);

await gitlab.setBranchStatus({
Expand All @@ -1170,7 +1232,7 @@ describe('modules/platform/gitlab/index', () => {
url: 'some-url',
});

expect(timers.setTimeout.mock.calls).toHaveLength(1);
expect(timers.setTimeout.mock.calls).toHaveLength(retry);
expect(timers.setTimeout.mock.calls[0][0]).toBe(delay);
});
});
Expand Down
22 changes: 17 additions & 5 deletions lib/modules/platform/gitlab/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -966,9 +966,13 @@ export async function setBranchStatus({
}: BranchStatusConfig): Promise<void> {
// First, get the branch commit SHA
const branchSha = git.getBranchCommit(branchName);
if (!branchSha) {
logger.warn('Failed to get the branch commit SHA');
return;
}
// Now, check the statuses for that commit
// TODO: types (#22198)
philippe-granet marked this conversation as resolved.
Show resolved Hide resolved
const url = `projects/${config.repository}/statuses/${branchSha!}`;
const url = `projects/${config.repository}/statuses/${branchSha}`;
let state = 'success';
if (renovateState === 'yellow') {
state = 'pending';
Expand All @@ -985,21 +989,29 @@ export async function setBranchStatus({
options.target_url = targetUrl;
}

if (branchSha) {
const retryTimes = parseInteger(
process.env.RENOVATE_X_GITLAB_BRANCH_STATUS_CHECK_ATTEMPTS,
2,
);

for (let attempt = 1; attempt <= retryTimes; attempt += 1) {
philippe-granet marked this conversation as resolved.
Show resolved Hide resolved
const commitUrl = `projects/${config.repository}/repository/commits/${branchSha}`;
await gitlabApi
.getJsonSafe(commitUrl, LastPipelineId)
.onValue((pipelineId) => {
options.pipeline_id = pipelineId;
});
}

try {
if (options.pipeline_id !== undefined) {
break;
}
logger.debug(`Pipeline not yet created. Retrying ${attempt}`);
// give gitlab some time to create pipelines for the sha
await setTimeout(
parseInteger(process.env.RENOVATE_X_GITLAB_BRANCH_STATUS_DELAY, 1000),
);
}

try {
await gitlabApi.postJson(url, { body: options });

// update status cache
Expand Down