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(config): custom status checks #26047

Merged
merged 21 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 20 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 docs/usage/configuration-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -3564,6 +3564,23 @@ Configure this to `true` if you wish to get one PR for every separate major vers
e.g. if you are on webpack@v1 currently then default behavior is a PR for upgrading to webpack@v3 and not for webpack@v2.
If this setting is true then you would get one PR for webpack@v2 and one for webpack@v3.

## statusCheckNames

You can customize the context of the status check that Renovate adds to its update branches.
rarkins marked this conversation as resolved.
Show resolved Hide resolved

You can modify existing status checks, but adding new status checks is _not_ supported.
rarkins marked this conversation as resolved.
Show resolved Hide resolved
Setting the value to `null` or an empty string, effectively disables or skips that status check.
This option is mergeable, which means you only have to specify the status checks that you want to modify.

```json title="Example of overriding status check strings"
{
"statusCheckNames": {
"minimumReleaseAge": "custom/stability-days",
"mergeConfidence": "custom/merge-confidence-level"
}
}
```

## stopUpdatingLabel

This feature only works on supported platforms, check the table above.
Expand Down
13 changes: 13 additions & 0 deletions lib/config/options/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,19 @@ const options: RenovateOptions[] = [
type: 'string',
},
},
{
name: 'statusCheckNames',
description: 'Custom strings to use as status check names.',
type: 'object',
mergeable: true,
rarkins marked this conversation as resolved.
Show resolved Hide resolved
advancedUse: true,
default: {
minimumReleaseAge: 'renovate/stability-days',
mergeConfidence: 'renovate/merge-confidence',
configValidation: 'renovate/config-validation',
artifactError: 'renovate/artifacts',
rarkins marked this conversation as resolved.
Show resolved Hide resolved
},
},
{
name: 'extends',
description: 'Configuration presets to use or extend.',
Expand Down
10 changes: 10 additions & 0 deletions lib/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ export type RenovateRepository =
export type UseBaseBranchConfigType = 'merge' | 'none';
export type ConstraintsFilter = 'strict' | 'none';

export const allowedStatusCheckStrings = [
'minimumReleaseAge',
'mergeConfidence',
'configValidation',
'artifactError',
] as const;
export type StatusCheckKey = (typeof allowedStatusCheckStrings)[number];

// TODO: Proper typings
export interface RenovateConfig
extends LegacyAdminConfig,
Expand Down Expand Up @@ -260,6 +268,8 @@ export interface RenovateConfig

checkedBranches?: string[];
customizeDashboard?: Record<string, string>;

statusCheckNames?: Record<StatusCheckKey, string | null>;
}

export interface CustomDatasourceConfig {
Expand Down
24 changes: 24 additions & 0 deletions lib/config/validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,30 @@ describe('config/validation', () => {
]);
});

it('validates invalid statusCheckNames', async () => {
const config = {
statusCheckNames: {
randomKey: '',
mergeConfidence: 10,
configValidation: '',
artifactError: null,
},
};
// @ts-expect-error invalid options
const { errors } = await configValidation.validateConfig(config);
expect(errors).toMatchObject([
{
message:
'Invalid `statusCheckNames.mergeConfidence` configuration: status check is not a string.',
},
{
message:
'Invalid `statusCheckNames.statusCheckNames.randomKey` configuration: key is not allowed.',
},
]);
expect(errors).toHaveLength(2);
});

it('catches invalid customDatasources record type', async () => {
const config = {
customDatasources: {
Expand Down
36 changes: 31 additions & 5 deletions lib/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import {
import { migrateConfig } from './migration';
import { getOptions } from './options';
import { resolveConfigPresets } from './presets';
import type {
RenovateConfig,
RenovateOptions,
ValidationMessage,
ValidationResult,
import {
type RenovateConfig,
type RenovateOptions,
type StatusCheckKey,
type ValidationMessage,
type ValidationResult,
allowedStatusCheckStrings,
} from './types';
import * as managerValidator from './validation-helpers/managers';

Expand Down Expand Up @@ -562,6 +564,30 @@ export async function validateConfig(
message: `Invalid \`${currentPath}.${key}.${res}\` configuration: value is not a string`,
});
}
} else if (key === 'statusCheckNames') {
for (const [statusCheckKey, statusCheckValue] of Object.entries(
val,
)) {
if (
!allowedStatusCheckStrings.includes(
statusCheckKey as StatusCheckKey,
)
) {
errors.push({
topic: 'Configuration Error',
message: `Invalid \`${currentPath}.${key}.${statusCheckKey}\` configuration: key is not allowed.`,
});
}
if (
!(is.string(statusCheckValue) || is.null_(statusCheckValue))
) {
errors.push({
topic: 'Configuration Error',
message: `Invalid \`${currentPath}.${statusCheckKey}\` configuration: status check is not a string.`,
});
continue;
}
}
} else if (key === 'customDatasources') {
const allowedKeys = [
'description',
Expand Down
46 changes: 45 additions & 1 deletion lib/workers/repository/reconfigure/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
fs,
git,
mocked,
partial,
platform,
scm,
} from '../../../../test/util';
Expand All @@ -26,6 +27,9 @@ describe('workers/repository/reconfigure/index', () => {
const config: RenovateConfig = {
branchPrefix: 'prefix/',
baseBranch: 'base',
statusCheckNames: partial<RenovateConfig['statusCheckNames']>({
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
configValidation: 'renovate/config-validation',
}),
};

beforeEach(() => {
Expand Down Expand Up @@ -151,6 +155,46 @@ describe('workers/repository/reconfigure/index', () => {
});
});

it('skips adding status check if statusCheckNames.configValidation is null', async () => {
cache.getCache.mockReturnValueOnce({
reconfigureBranchCache: {
reconfigureBranchSha: 'new-sha',
isConfigValid: false,
},
});

await validateReconfigureBranch({
...config,
statusCheckNames: partial<RenovateConfig['statusCheckNames']>({
configValidation: null,
}),
});
expect(logger.debug).toHaveBeenCalledWith(
'Status check is null or an empty string, skipping status check addition.',
);
expect(platform.setBranchStatus).not.toHaveBeenCalled();
});

it('skips adding status check if statusCheckNames.configValidation is empty string', async () => {
cache.getCache.mockReturnValueOnce({
reconfigureBranchCache: {
reconfigureBranchSha: 'new-sha',
isConfigValid: false,
},
});

await validateReconfigureBranch({
...config,
statusCheckNames: partial<RenovateConfig['statusCheckNames']>({
configValidation: '',
}),
});
expect(logger.debug).toHaveBeenCalledWith(
'Status check is null or an empty string, skipping status check addition.',
);
expect(platform.setBranchStatus).not.toHaveBeenCalled();
});

it('skips validation if cache is valid', async () => {
cache.getCache.mockReturnValueOnce({
reconfigureBranchCache: {
Expand All @@ -174,7 +218,7 @@ describe('workers/repository/reconfigure/index', () => {
platform.getBranchStatusCheck.mockResolvedValueOnce('green');
await validateReconfigureBranch(config);
expect(logger.debug).toHaveBeenCalledWith(
'Skipping validation check as status check already exists',
'Skipping validation check because status check already exists.',
);
});

Expand Down
85 changes: 52 additions & 33 deletions lib/workers/repository/reconfigure/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { logger } from '../../../logger';
import { platform } from '../../../modules/platform';
import { ensureComment } from '../../../modules/platform/comment';
import { scm } from '../../../modules/platform/scm';
import type { BranchStatus } from '../../../types';
import { getCache } from '../../../util/cache/repository';
import { readLocalFile } from '../../../util/fs';
import { getBranchCommit } from '../../../util/git';
Expand All @@ -16,14 +17,33 @@ import {
setReconfigureBranchCache,
} from './reconfigure-cache';

async function setBranchStatus(
branchName: string,
description: string,
state: BranchStatus,
context?: string | null,
): Promise<void> {
if (!is.nonEmptyString(context)) {
// already logged this case when validating the status check
return;
}

await platform.setBranchStatus({
branchName,
context,
description,
state,
});
}

export function getReconfigureBranchName(prefix: string): string {
return `${prefix}reconfigure`;
}
export async function validateReconfigureBranch(
config: RenovateConfig,
): Promise<void> {
logger.debug('validateReconfigureBranch()');
const context = `renovate/config-validation`;
const context = config.statusCheckNames?.configValidation;

const branchName = getReconfigureBranchName(config.branchPrefix!);
const branchExists = await scm.branchExists(branchName);
Expand All @@ -48,14 +68,23 @@ export async function validateReconfigureBranch(
return;
}

const validationStatus = await platform.getBranchStatusCheck(
branchName,
'renovate/config-validation',
);
// if old status check is present skip validation
if (is.nonEmptyString(validationStatus)) {
logger.debug('Skipping validation check as status check already exists');
return;
if (context) {
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
const validationStatus = await platform.getBranchStatusCheck(
branchName,
context,
);

// if old status check is present skip validation
if (is.nonEmptyString(validationStatus)) {
logger.debug(
'Skipping validation check because status check already exists.',
);
return;
}
} else {
logger.debug(
'Status check is null or an empty string, skipping status check addition.',
);
}

try {
Expand All @@ -70,12 +99,12 @@ export async function validateReconfigureBranch(

if (!is.nonEmptyString(configFileName)) {
logger.warn('No config file found in reconfigure branch');
await platform.setBranchStatus({
await setBranchStatus(
branchName,
'Validation Failed - No config file found',
'red',
context,
description: 'Validation Failed - No config file found',
state: 'red',
});
);
setReconfigureBranchCache(branchSha, false);
await scm.checkoutBranch(config.defaultBranch!);
return;
Expand All @@ -90,12 +119,12 @@ export async function validateReconfigureBranch(

if (!is.nonEmptyString(configFileRaw)) {
logger.warn('Empty or invalid config file');
await platform.setBranchStatus({
await setBranchStatus(
branchName,
'Validation Failed - Empty/Invalid config file',
'red',
context,
description: 'Validation Failed - Empty/Invalid config file',
state: 'red',
});
);
setReconfigureBranchCache(branchSha, false);
await scm.checkoutBranch(config.baseBranch!);
return;
Expand All @@ -110,12 +139,12 @@ export async function validateReconfigureBranch(
}
} catch (err) {
logger.error({ err }, 'Error while parsing config file');
await platform.setBranchStatus({
await setBranchStatus(
branchName,
'Validation Failed - Unparsable config file',
'red',
context,
description: 'Validation Failed - Unparsable config file',
state: 'red',
});
);
setReconfigureBranchCache(branchSha, false);
await scm.checkoutBranch(config.baseBranch!);
return;
Expand Down Expand Up @@ -150,24 +179,14 @@ export async function validateReconfigureBranch(
content: body,
});
}
await platform.setBranchStatus({
branchName,
context,
description: 'Validation Failed',
state: 'red',
});
await setBranchStatus(branchName, 'Validation Failed', 'red', context);
setReconfigureBranchCache(branchSha, false);
await scm.checkoutBranch(config.baseBranch!);
return;
}

// passing check
await platform.setBranchStatus({
branchName,
context,
description: 'Validation Successful',
state: 'green',
});
await setBranchStatus(branchName, 'Validation Successful', 'green', context);

setReconfigureBranchCache(branchSha, true);
await scm.checkoutBranch(config.baseBranch!);
Expand Down