forked from renovatebot/renovate
/
check.ts
150 lines (140 loc) · 4.92 KB
/
check.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { configFileNames } from '../../../../config/app-strings';
import type { RenovateConfig } from '../../../../config/types';
import {
REPOSITORY_CLOSED_ONBOARDING,
REPOSITORY_NO_CONFIG,
} from '../../../../constants/error-messages';
import { logger } from '../../../../logger';
import { Pr, platform } from '../../../../modules/platform';
import { ensureComment } from '../../../../modules/platform/comment';
import { getCache } from '../../../../util/cache/repository';
import { readLocalFile } from '../../../../util/fs';
import { getBranchCommit, getFileList } from '../../../../util/git';
async function findFile(fileName: string): Promise<boolean> {
logger.debug(`findFile(${fileName})`);
const fileList = await getFileList();
return fileList.includes(fileName);
}
async function configFileExists(): Promise<boolean> {
for (const fileName of configFileNames) {
if (fileName !== 'package.json' && (await findFile(fileName))) {
logger.debug(`Config file exists, fileName: ${fileName}`);
return true;
}
}
return false;
}
async function packageJsonConfigExists(): Promise<boolean> {
try {
// TODO #7154
const pJson = JSON.parse((await readLocalFile('package.json', 'utf8'))!);
if (pJson.renovate) {
return true;
}
} catch (err) {
// Do nothing
}
return false;
}
function closedPrExists(config: RenovateConfig): Promise<Pr | null> {
return platform.findPr({
branchName: config.onboardingBranch!,
prTitle: config.onboardingPrTitle,
state: '!open',
});
}
export async function isOnboarded(config: RenovateConfig): Promise<boolean> {
logger.debug('isOnboarded()');
const title = `Action required: Add a Renovate config`;
// Repo is onboarded if global config is bypassing onboarding and does not require a
// configuration file.
if (config.requireConfig === 'optional' && config.onboarding === false) {
// Return early and avoid checking for config files
return true;
}
if (config.requireConfig === 'ignored') {
logger.debug('Config file will be ignored');
return true;
}
const pr = await closedPrExists(config);
const cache = getCache();
const onboardingBranchCache = cache?.onboardingBranchCache;
// if onboarding cache is present and base branch has not been updated branch is not onboarded
// if closed pr exists then presence of onboarding cache doesn't matter as we need to skip onboarding
if (
config.onboarding &&
!pr &&
onboardingBranchCache &&
onboardingBranchCache.defaultBranchSha ===
getBranchCommit(config.defaultBranch!) &&
onboardingBranchCache.onboardingBranchSha ===
getBranchCommit(config.onboardingBranch!)
) {
logger.debug('Onboarding cache is valid. Repo is not onboarded');
return false;
}
if (cache.configFileName) {
logger.debug('Checking cached config file name');
try {
const configFileContent = await platform.getJsonFile(
cache.configFileName
);
if (configFileContent) {
if (
cache.configFileName !== 'package.json' ||
configFileContent.renovate
) {
logger.debug('Existing config file confirmed');
logger.debug(
{ fileName: cache.configFileName, config: configFileContent },
'Repository config'
);
return true;
}
}
} catch (err) {
// probably file doesn't exist
}
logger.debug('Existing config file no longer exists');
delete cache.configFileName;
}
if (await configFileExists()) {
await platform.ensureIssueClosing(title);
return true;
}
logger.debug('config file not found');
if (await packageJsonConfigExists()) {
logger.debug('package.json contains config');
await platform.ensureIssueClosing(title);
return true;
}
// If onboarding has been disabled and config files are required then the
// repository has not been onboarded yet
if (config.requireConfig === 'required' && config.onboarding === false) {
throw new Error(REPOSITORY_NO_CONFIG);
}
if (!pr) {
logger.debug('Found no closed onboarding PR');
return false;
}
logger.debug('Found closed onboarding PR');
if (config.requireConfig === 'optional') {
logger.debug('Config not mandatory so repo is considered onboarded');
return true;
}
logger.debug('Repo is not onboarded and no merged PRs exist');
if (!config.suppressNotifications!.includes('onboardingClose')) {
// ensure PR comment
await ensureComment({
number: pr.number,
topic: `Renovate is disabled`,
content: `Renovate is disabled due to lack of config. If you wish to reenable it, you can either (a) commit a config file to your base branch, or (b) rename this closed PR to trigger a replacement onboarding PR.`,
});
}
throw new Error(REPOSITORY_CLOSED_ONBOARDING);
}
export async function getOnboardingPr(
config: RenovateConfig
): Promise<Pr | null> {
return await platform.getBranchPr(config.onboardingBranch!);
}