Skip to content

Commit c7ea5b0

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.
1 parent 3189198 commit c7ea5b0

File tree

3 files changed

+76
-14
lines changed

3 files changed

+76
-14
lines changed

lib/workers/repository/error.ts

Lines changed: 10 additions & 10 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,13 +56,12 @@ 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)) {
59+
if (
60+
err.message === REPOSITORY_CLOSED_ONBOARDING ||
61+
err.message === REPOSITORY_DISABLED ||
62+
err.message === REPOSITORY_DISABLED_BY_CONFIG ||
63+
err.message === REPOSITORY_NO_CONFIG
64+
) {
6565
logger.info('Repository is disabled - skipping');
6666
return err.message;
6767
}
@@ -159,15 +159,15 @@ export default async function handleError(
159159
);
160160
logger.info('External host error causing abort - skipping');
161161
delete config.branchList;
162-
return err.message;
162+
return EXTERNAL_HOST_ERROR;
163163
}
164164
if (
165165
err.message.includes('No space left on device') ||
166166
err.message === SYSTEM_INSUFFICIENT_DISK_SPACE
167167
) {
168168
logger.error('Disk space error - skipping');
169169
delete config.branchList;
170-
return err.message;
170+
return SYSTEM_INSUFFICIENT_DISK_SPACE;
171171
}
172172
if (err.message === PLATFORM_RATE_LIMIT_EXCEEDED) {
173173
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: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
import type { RenovateConfig } from '../../config/types';
22

3+
import type {
4+
EXTERNAL_HOST_ERROR,
5+
MANAGER_LOCKFILE_ERROR,
6+
NO_VULNERABILITY_ALERTS,
7+
PLATFORM_AUTHENTICATION_ERROR,
8+
PLATFORM_BAD_CREDENTIALS,
9+
PLATFORM_INTEGRATION_UNAUTHORIZED,
10+
PLATFORM_RATE_LIMIT_EXCEEDED,
11+
REPOSITORY_CANNOT_FORK,
12+
REPOSITORY_CHANGED,
13+
REPOSITORY_FORK_MISSING,
14+
SYSTEM_INSUFFICIENT_DISK_SPACE,
15+
SYSTEM_INSUFFICIENT_MEMORY,
16+
TEMPORARY_ERROR,
17+
UNKNOWN_ERROR,
18+
} from '../../constants/error-messages';
319
import {
420
CONFIG_SECRETS_EXPOSED,
521
CONFIG_VALIDATION,
@@ -30,15 +46,61 @@ export type ProcessStatus =
3046
| 'unknown';
3147

3248
export interface ProcessResult {
33-
res: string;
49+
res: RepositoryResult;
3450
status: ProcessStatus;
3551
enabled: boolean | undefined;
3652
onboarded: boolean | undefined;
3753
}
3854

55+
/** a strong type for any repository result status that Renovate may report */
56+
export type RepositoryResult =
57+
// repository was processed successfully
58+
| 'done'
59+
// Renovate performed branch-based automerge on one branch during its run
60+
| 'automerged'
61+
// Repository Errors - causes repo to be considered as disabled
62+
| typeof REPOSITORY_UNINITIATED
63+
| typeof REPOSITORY_EMPTY
64+
| typeof REPOSITORY_CLOSED_ONBOARDING
65+
| typeof REPOSITORY_DISABLED
66+
| typeof REPOSITORY_DISABLED_BY_CONFIG
67+
| typeof REPOSITORY_NO_CONFIG
68+
| typeof REPOSITORY_ARCHIVED
69+
| typeof REPOSITORY_MIRRORED
70+
| typeof REPOSITORY_RENAMED
71+
| typeof REPOSITORY_BLOCKED
72+
| typeof REPOSITORY_ACCESS_FORBIDDEN
73+
| typeof REPOSITORY_NOT_FOUND
74+
| typeof REPOSITORY_FORK_MODE_FORKED
75+
| typeof REPOSITORY_FORKED
76+
| typeof REPOSITORY_CANNOT_FORK
77+
| typeof REPOSITORY_FORK_MISSING
78+
| typeof REPOSITORY_NO_PACKAGE_FILES
79+
// temporary errors
80+
| typeof NO_VULNERABILITY_ALERTS
81+
| typeof REPOSITORY_CHANGED
82+
| typeof TEMPORARY_ERROR
83+
// Config Error
84+
| typeof CONFIG_VALIDATION
85+
| typeof MISSING_API_CREDENTIALS
86+
| typeof CONFIG_SECRETS_EXPOSED
87+
// system errors
88+
| typeof SYSTEM_INSUFFICIENT_DISK_SPACE
89+
| typeof SYSTEM_INSUFFICIENT_MEMORY
90+
// host errors
91+
| typeof EXTERNAL_HOST_ERROR
92+
// platform errors
93+
| typeof PLATFORM_RATE_LIMIT_EXCEEDED
94+
| typeof PLATFORM_BAD_CREDENTIALS
95+
| typeof PLATFORM_INTEGRATION_UNAUTHORIZED
96+
| typeof PLATFORM_AUTHENTICATION_ERROR
97+
// other errors
98+
| typeof MANAGER_LOCKFILE_ERROR
99+
| typeof UNKNOWN_ERROR;
100+
39101
export function processResult(
40102
config: RenovateConfig,
41-
res: string,
103+
res: RepositoryResult,
42104
): ProcessResult {
43105
const disabledStatuses = [
44106
REPOSITORY_ACCESS_FORBIDDEN,

0 commit comments

Comments
 (0)