Skip to content

Commit

Permalink
feat(platform/gitlab): Allow custom delay for pipeline status via exp…
Browse files Browse the repository at this point in the history
…erimental variable (#23239)
  • Loading branch information
mikkopiu committed Jul 7, 2023
1 parent 4e30552 commit 4ef1cd2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
8 changes: 8 additions & 0 deletions docs/usage/self-hosted-experimental.md
Expand Up @@ -124,6 +124,14 @@ If set, Renovate will rewrite GitHub Enterprise Server's pagination responses to
!!! note
For the GitHub Enterprise Server platform only.

## `RENOVATE_X_GITLAB_BRANCH_STATUS_DELAY`

Adjust default time (in milliseconds) given to GitLab to create pipelines for a commit pushed by Renovate.

Can be useful for slow-running, self-hosted GitLab instances that don't react fast enough for the default delay to help.

Default value: `1000` (milliseconds).

## `OTEL_EXPORTER_OTLP_ENDPOINT`

If set, Renovate will export OpenTelemetry data to the supplied endpoint.
Expand Down
55 changes: 55 additions & 0 deletions lib/modules/platform/gitlab/index.spec.ts
@@ -1,4 +1,5 @@
// TODO fix mocks
import type * as _timers from 'timers/promises';
import type { Platform, RepoParams } from '..';
import * as httpMock from '../../../../test/http-mock';
import {
Expand All @@ -22,6 +23,7 @@ describe('modules/platform/gitlab/index', () => {
let hostRules: jest.Mocked<typeof _hostRules>;
let git: jest.Mocked<typeof _git>;
let logger: jest.Mocked<typeof _logger>;
let timers: jest.Mocked<typeof _timers>;

beforeEach(async () => {
// reset module
Expand All @@ -32,6 +34,7 @@ describe('modules/platform/gitlab/index', () => {
logger = (await import('../../../logger')).logger as never;
jest.mock('../../../util/host-rules');
jest.mock('timers/promises');
timers = require('timers/promises');
hostRules = require('../../../util/host-rules');
jest.mock('../../../util/git');
git = require('../../../util/git');
Expand All @@ -44,6 +47,7 @@ describe('modules/platform/gitlab/index', () => {
token: '123test',
});
delete process.env.GITLAB_IGNORE_REPO_URL;
delete process.env.RENOVATE_X_GITLAB_BRANCH_STATUS_DELAY;
});

async function initFakePlatform(version: string) {
Expand Down Expand Up @@ -827,6 +831,57 @@ describe('modules/platform/gitlab/index', () => {
})
).toResolve();
});

it('waits for 1000ms by default', async () => {
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, []);

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

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

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

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, []);

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

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

describe('findIssue()', () => {
Expand Down
6 changes: 5 additions & 1 deletion lib/modules/platform/gitlab/index.ts
Expand Up @@ -866,7 +866,11 @@ export async function setBranchStatus({
}
try {
// give gitlab some time to create pipelines for the sha
await setTimeout(1000);
await setTimeout(
process.env.RENOVATE_X_GITLAB_BRANCH_STATUS_DELAY
? parseInt(process.env.RENOVATE_X_GITLAB_BRANCH_STATUS_DELAY, 10)
: 1000
);

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

Expand Down

0 comments on commit 4ef1cd2

Please sign in to comment.