Skip to content

Commit be0abad

Browse files
committed
fix(types): make result a strong type
As a way to make it more predictable what the possible states the `result` in a given `Repository finished` log message can be, we can wrap these into a type. This requires a slight refactor where these are used to make sure that Typescript is happy with the value being returned. We can capture the common groups of errors (which are then present as a `RepositoryResult`) and expose that to be used.
1 parent bbd30ce commit be0abad

File tree

4 files changed

+100
-53
lines changed

4 files changed

+100
-53
lines changed

lib/constants/error-messages.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
export const SYSTEM_INSUFFICIENT_DISK_SPACE = 'disk-space';
33
export const SYSTEM_INSUFFICIENT_MEMORY = 'out-of-memory';
44

5+
export const SystemErrors = [
6+
SYSTEM_INSUFFICIENT_DISK_SPACE,
7+
SYSTEM_INSUFFICIENT_MEMORY,
8+
] as const;
9+
510
// Platform Error
611
export const PLATFORM_AUTHENTICATION_ERROR = 'authentication-error';
712
export const PLATFORM_BAD_CREDENTIALS = 'bad-credentials';
@@ -11,6 +16,17 @@ export const PLATFORM_NOT_FOUND = 'platform-not-found';
1116
export const PLATFORM_RATE_LIMIT_EXCEEDED = 'rate-limit-exceeded';
1217
export const PLATFORM_UNKNOWN_ERROR = 'platform-unknown-error';
1318

19+
/** cause repo to be considered as disabled */
20+
export const PlatformErrors = [
21+
PLATFORM_AUTHENTICATION_ERROR,
22+
PLATFORM_BAD_CREDENTIALS,
23+
PLATFORM_GPG_FAILED,
24+
PLATFORM_INTEGRATION_UNAUTHORIZED,
25+
PLATFORM_NOT_FOUND,
26+
PLATFORM_RATE_LIMIT_EXCEEDED,
27+
PLATFORM_UNKNOWN_ERROR,
28+
] as const;
29+
1430
// Config Error
1531
export const CONFIG_VALIDATION = 'config-validation';
1632
export const CONFIG_PRESETS_INVALID = 'config-presets-invalid';
@@ -21,6 +37,18 @@ export const CONFIG_GIT_URL_UNAVAILABLE = 'config-git-url-unavailable';
2137
export const CONFIG_INHERIT_NOT_FOUND = 'config-inherit-not-found';
2238
export const CONFIG_INHERIT_PARSE_ERROR = 'config-inherit-parse-error';
2339

40+
/** cause repo to be considered as disabled */
41+
export const ConfigErrors = [
42+
CONFIG_VALIDATION,
43+
CONFIG_PRESETS_INVALID,
44+
CONFIG_SECRETS_EXPOSED,
45+
CONFIG_SECRETS_INVALID,
46+
CONFIG_VARIABLES_INVALID,
47+
CONFIG_GIT_URL_UNAVAILABLE,
48+
CONFIG_INHERIT_NOT_FOUND,
49+
CONFIG_INHERIT_PARSE_ERROR,
50+
] as const;
51+
2452
// Repository Errors - causes repo to be considered as disabled
2553
export const REPOSITORY_ACCESS_FORBIDDEN = 'forbidden';
2654
export const REPOSITORY_ARCHIVED = 'archived';
@@ -40,11 +68,38 @@ export const REPOSITORY_NO_PACKAGE_FILES = 'no-package-files';
4068
export const REPOSITORY_RENAMED = 'renamed';
4169
export const REPOSITORY_UNINITIATED = 'uninitiated';
4270

71+
/** cause repo to be considered as disabled */
72+
export const RepositoryErrors = [
73+
REPOSITORY_ACCESS_FORBIDDEN,
74+
REPOSITORY_ARCHIVED,
75+
REPOSITORY_BLOCKED,
76+
REPOSITORY_CANNOT_FORK,
77+
REPOSITORY_DISABLED,
78+
REPOSITORY_CLOSED_ONBOARDING,
79+
REPOSITORY_DISABLED_BY_CONFIG,
80+
REPOSITORY_NO_CONFIG,
81+
REPOSITORY_EMPTY,
82+
REPOSITORY_FORK_MISSING,
83+
REPOSITORY_FORK_MODE_FORKED,
84+
REPOSITORY_FORKED,
85+
REPOSITORY_MIRRORED,
86+
REPOSITORY_NOT_FOUND,
87+
REPOSITORY_NO_PACKAGE_FILES,
88+
REPOSITORY_RENAMED,
89+
REPOSITORY_UNINITIATED,
90+
] as const;
91+
4392
// Temporary Error
4493
export const REPOSITORY_CHANGED = 'repository-changed';
4594
export const TEMPORARY_ERROR = 'temporary-error';
4695
export const NO_VULNERABILITY_ALERTS = 'no-vulnerability-alerts';
4796

97+
export const TemporaryErrors = [
98+
REPOSITORY_CHANGED,
99+
TEMPORARY_ERROR,
100+
NO_VULNERABILITY_ALERTS,
101+
] as const;
102+
48103
// Manager Error
49104
export const MANAGER_LOCKFILE_ERROR = 'lockfile-error';
50105
export const FILE_ACCESS_VIOLATION_ERROR = 'file-access-violation-error';

lib/workers/repository/error.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ import {
4040
raiseConfigWarningIssue,
4141
raiseCredentialsWarningIssue,
4242
} from './error-config';
43+
import type { RepositoryResult } from './result';
4344

4445
export default async function handleError(
4546
config: RenovateConfig,
4647
err: Error,
47-
): Promise<string> {
48+
): Promise<RepositoryResult> {
4849
if (err.message === REPOSITORY_UNINITIATED) {
4950
logger.info('Repository is uninitiated - skipping');
5051
delete config.branchList;
@@ -55,15 +56,13 @@ export default async function handleError(
5556
delete config.branchList;
5657
return err.message;
5758
}
58-
const disabledMessages = [
59-
REPOSITORY_CLOSED_ONBOARDING,
60-
REPOSITORY_DISABLED,
61-
REPOSITORY_DISABLED_BY_CONFIG,
62-
REPOSITORY_NO_CONFIG,
63-
];
64-
if (disabledMessages.includes(err.message)) {
65-
logger.info('Repository is disabled - skipping');
66-
return err.message;
59+
switch (err.message) {
60+
case REPOSITORY_CLOSED_ONBOARDING:
61+
case REPOSITORY_DISABLED:
62+
case REPOSITORY_DISABLED_BY_CONFIG:
63+
case REPOSITORY_NO_CONFIG:
64+
logger.info('Repository is disabled - skipping');
65+
return err.message;
6766
}
6867
if (err.message === REPOSITORY_ARCHIVED) {
6968
logger.info('Repository is archived - skipping');
@@ -159,15 +158,15 @@ export default async function handleError(
159158
);
160159
logger.info('External host error causing abort - skipping');
161160
delete config.branchList;
162-
return err.message;
161+
return EXTERNAL_HOST_ERROR;
163162
}
164163
if (
165164
err.message.includes('No space left on device') ||
166165
err.message === SYSTEM_INSUFFICIENT_DISK_SPACE
167166
) {
168167
logger.error('Disk space error - skipping');
169168
delete config.branchList;
170-
return err.message;
169+
return SYSTEM_INSUFFICIENT_DISK_SPACE;
171170
}
172171
if (err.message === PLATFORM_RATE_LIMIT_EXCEEDED) {
173172
logger.warn('Rate limit exceeded - aborting');

lib/workers/repository/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import { OnboardingState } from './onboarding/common';
4242
import { ensureOnboardingPr } from './onboarding/pr';
4343
import { extractDependencies, updateRepo } from './process';
4444
import type { ExtractResult } from './process/extract-update';
45-
import type { ProcessResult } from './result';
45+
import type { ProcessResult, RepositoryResult } from './result';
4646
import { processResult } from './result';
4747

4848
// istanbul ignore next
@@ -60,7 +60,7 @@ export async function renovateRepository(
6060
localDir: string;
6161
errorRes?: string;
6262
}> => {
63-
let errorRes: string | undefined;
63+
let errorRes: RepositoryResult | undefined;
6464
let config = GlobalConfig.set(
6565
applySecretsAndVariablesToConfig({
6666
config: repoConfig,

lib/workers/repository/result.ts

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
import type { RenovateConfig } from '../../config/types';
22

3+
import type {
4+
ConfigErrors,
5+
EXTERNAL_HOST_ERROR,
6+
MANAGER_LOCKFILE_ERROR,
7+
PlatformErrors,
8+
SystemErrors,
9+
TemporaryErrors,
10+
UNKNOWN_ERROR,
11+
} from '../../constants/error-messages';
312
import {
413
CONFIG_SECRETS_EXPOSED,
514
CONFIG_VALIDATION,
615
MISSING_API_CREDENTIALS,
7-
REPOSITORY_ACCESS_FORBIDDEN,
8-
REPOSITORY_ARCHIVED,
9-
REPOSITORY_BLOCKED,
10-
REPOSITORY_CANNOT_FORK,
11-
REPOSITORY_CLOSED_ONBOARDING,
12-
REPOSITORY_DISABLED,
13-
REPOSITORY_DISABLED_BY_CONFIG,
14-
REPOSITORY_EMPTY,
15-
REPOSITORY_FORKED,
16-
REPOSITORY_FORK_MISSING,
17-
REPOSITORY_FORK_MODE_FORKED,
18-
REPOSITORY_MIRRORED,
19-
REPOSITORY_NOT_FOUND,
20-
REPOSITORY_NO_CONFIG,
21-
REPOSITORY_NO_PACKAGE_FILES,
22-
REPOSITORY_RENAMED,
23-
REPOSITORY_UNINITIATED,
16+
RepositoryErrors,
2417
} from '../../constants/error-messages';
18+
2519
import { logger } from '../../logger';
2620

2721
export type ProcessStatus =
@@ -32,35 +26,34 @@ export type ProcessStatus =
3226
| 'unknown';
3327

3428
export interface ProcessResult {
35-
res: string;
29+
res: RepositoryResult;
3630
status: ProcessStatus;
3731
enabled: boolean | undefined;
3832
onboarded: boolean | undefined;
3933
}
4034

35+
/** a strong type for any repository result status that Renovate may report */
36+
export type RepositoryResult =
37+
/** repository was processed successfully */
38+
| 'done'
39+
/** Renovate performed branch-based automerge on one branch during its run */
40+
| 'automerged'
41+
// common set of errors
42+
| (typeof SystemErrors)[number]
43+
| (typeof RepositoryErrors)[number]
44+
| (typeof TemporaryErrors)[number]
45+
| (typeof ConfigErrors)[number]
46+
| (typeof PlatformErrors)[number]
47+
// other errors
48+
| typeof EXTERNAL_HOST_ERROR
49+
| typeof MISSING_API_CREDENTIALS
50+
| typeof MANAGER_LOCKFILE_ERROR
51+
| typeof UNKNOWN_ERROR;
52+
4153
export function processResult(
4254
config: RenovateConfig,
43-
res: string,
55+
res: RepositoryResult,
4456
): ProcessResult {
45-
const disabledStatuses = [
46-
REPOSITORY_ACCESS_FORBIDDEN,
47-
REPOSITORY_ARCHIVED,
48-
REPOSITORY_BLOCKED,
49-
REPOSITORY_CANNOT_FORK,
50-
REPOSITORY_CLOSED_ONBOARDING,
51-
REPOSITORY_DISABLED,
52-
REPOSITORY_DISABLED_BY_CONFIG,
53-
REPOSITORY_EMPTY,
54-
REPOSITORY_FORK_MISSING,
55-
REPOSITORY_FORK_MODE_FORKED,
56-
REPOSITORY_FORKED,
57-
REPOSITORY_MIRRORED,
58-
REPOSITORY_NOT_FOUND,
59-
REPOSITORY_NO_CONFIG,
60-
REPOSITORY_NO_PACKAGE_FILES,
61-
REPOSITORY_RENAMED,
62-
REPOSITORY_UNINITIATED,
63-
];
6457
const enabledStatuses = [
6558
CONFIG_SECRETS_EXPOSED,
6659
CONFIG_VALIDATION,
@@ -70,7 +63,7 @@ export function processResult(
7063
let enabled: boolean | undefined;
7164
let onboarded: boolean | undefined;
7265
// istanbul ignore next
73-
if (disabledStatuses.includes(res)) {
66+
if (RepositoryErrors.includes(res as (typeof RepositoryErrors)[number])) {
7467
status = 'disabled';
7568
enabled = false;
7669
} else if (config.repoIsActivated) {

0 commit comments

Comments
 (0)