Skip to content

Commit

Permalink
refactor(repo/init): return additional raw config from detectRepoFile…
Browse files Browse the repository at this point in the history
…Config (#17021)

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
Co-authored-by: Rhys Arkins <rhys@arkins.net>
  • Loading branch information
3 people committed Aug 18, 2022
1 parent 6be533b commit 1b8fd39
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 30 deletions.
33 changes: 20 additions & 13 deletions lib/workers/repository/init/merge.spec.ts
Expand Up @@ -54,14 +54,15 @@ describe('workers/repository/init/merge', () => {
},
});
fs.readLocalFile.mockResolvedValue(pJson);
platform.getJsonFile.mockResolvedValueOnce(pJson);
platform.getRawFile.mockResolvedValueOnce(pJson);
expect(await detectRepoFileConfig()).toEqual({
configFileName: 'package.json',
configFileParsed: { prHourlyLimit: 10 },
});
// get from repoCache
expect(await detectRepoFileConfig()).toEqual({
configFileName: 'package.json',
configFileParsed: undefined,
configFileParsed: { prHourlyLimit: 10 },
});
});

Expand All @@ -72,7 +73,7 @@ describe('workers/repository/init/merge', () => {
renovate: 'github>renovatebot/renovate',
});
fs.readLocalFile.mockResolvedValue(pJson);
platform.getJsonFile.mockResolvedValueOnce(pJson);
platform.getRawFile.mockResolvedValueOnce(pJson);
expect(await detectRepoFileConfig()).toEqual({
configFileName: 'package.json',
configFileParsed: { extends: ['github>renovatebot/renovate'] },
Expand Down Expand Up @@ -107,13 +108,15 @@ describe('workers/repository/init/merge', () => {
});

it('finds and parse renovate.json5', async () => {
git.getFileList.mockResolvedValue(['package.json', 'renovate.json5']);
fs.readLocalFile.mockResolvedValue(`{
const configFileRaw = `{
// this is json5 format
}`);
}`;
git.getFileList.mockResolvedValue(['package.json', 'renovate.json5']);
fs.readLocalFile.mockResolvedValue(configFileRaw);
expect(await detectRepoFileConfig()).toEqual({
configFileName: 'renovate.json5',
configFileParsed: {},
configFileRaw,
});
});

Expand All @@ -126,6 +129,7 @@ describe('workers/repository/init/merge', () => {
expect(await detectRepoFileConfig()).toEqual({
configFileName: '.github/renovate.json',
configFileParsed: {},
configFileRaw: '{}',
});
});

Expand All @@ -138,23 +142,26 @@ describe('workers/repository/init/merge', () => {
expect(await detectRepoFileConfig()).toEqual({
configFileName: '.gitlab/renovate.json',
configFileParsed: {},
configFileRaw: '{}',
});
});

it('finds .renovaterc.json', async () => {
git.getFileList.mockResolvedValue(['package.json', '.renovaterc.json']);
fs.readLocalFile.mockResolvedValue('{}');
platform.getJsonFile.mockResolvedValueOnce('{"something":"new"}');
platform.getRawFile.mockResolvedValueOnce('{"something":"new"}');
expect(await detectRepoFileConfig()).toEqual({
configFileName: '.renovaterc.json',
configFileParsed: {},
configFileRaw: '{}',
});
expect(await detectRepoFileConfig()).toEqual({
configFileName: '.renovaterc.json',
configFileParsed: {
something: 'new',
},
configFileRaw: '{"something":"new"}',
});
expect(await detectRepoFileConfig()).toMatchInlineSnapshot(`
{
"configFileName": ".renovaterc.json",
"configFileParsed": "{"something":"new"}",
}
`);
});
});

Expand Down
38 changes: 21 additions & 17 deletions lib/workers/repository/init/merge.ts
Expand Up @@ -49,14 +49,17 @@ export async function detectRepoFileConfig(): Promise<RepoFileConfig> {
const cache = getCache();
let { configFileName } = cache;
if (configFileName) {
let configFileParsed = (await platform.getJsonFile(configFileName))!;
if (configFileParsed) {
if (configFileName === 'package.json') {
configFileParsed = configFileParsed.renovate;
const configFileRaw = await platform.getRawFile(configFileName);
if (configFileRaw) {
let configFileParsed = JSON5.parse(configFileRaw);
if (configFileName !== 'package.json') {
return { configFileName, configFileRaw, configFileParsed };
}
return { configFileName, configFileParsed };
configFileParsed = configFileParsed.renovate;
return { configFileName, configFileParsed }; // don't return raw 'package.json'
} else {
logger.debug('Existing config file no longer exists');
}
logger.debug('Existing config file no longer exists');
}
configFileName = (await detectConfigFile()) ?? undefined;
if (!configFileName) {
Expand All @@ -67,6 +70,7 @@ export async function detectRepoFileConfig(): Promise<RepoFileConfig> {
logger.debug(`Found ${configFileName} config file`);
// TODO #7154
let configFileParsed: any;
let configFileRaw: string | undefined | null;
if (configFileName === 'package.json') {
// We already know it parses
configFileParsed = JSON.parse(
Expand All @@ -79,25 +83,25 @@ export async function detectRepoFileConfig(): Promise<RepoFileConfig> {
}
logger.debug({ config: configFileParsed }, 'package.json>renovate config');
} else {
let rawFileContents = await readLocalFile(configFileName, 'utf8');
configFileRaw = await readLocalFile(configFileName, 'utf8');
// istanbul ignore if
if (!is.string(rawFileContents)) {
if (!is.string(configFileRaw)) {
logger.warn({ configFileName }, 'Null contents when reading config file');
throw new Error(REPOSITORY_CHANGED);
}
// istanbul ignore if
if (!rawFileContents.length) {
rawFileContents = '{}';
if (!configFileRaw.length) {
configFileRaw = '{}';
}

const fileType = upath.extname(configFileName);

if (fileType === '.json5') {
try {
configFileParsed = JSON5.parse(rawFileContents);
configFileParsed = JSON5.parse(configFileRaw);
} catch (err) /* istanbul ignore next */ {
logger.debug(
{ renovateConfig: rawFileContents },
{ renovateConfig: configFileRaw },
'Error parsing renovate config renovate.json5'
);
const validationError = 'Invalid JSON5 (parsing failed)';
Expand All @@ -110,7 +114,7 @@ export async function detectRepoFileConfig(): Promise<RepoFileConfig> {
} else {
let allowDuplicateKeys = true;
let jsonValidationError = jsonValidator.validate(
rawFileContents,
configFileRaw,
allowDuplicateKeys
);
if (jsonValidationError) {
Expand All @@ -123,7 +127,7 @@ export async function detectRepoFileConfig(): Promise<RepoFileConfig> {
}
allowDuplicateKeys = false;
jsonValidationError = jsonValidator.validate(
rawFileContents,
configFileRaw,
allowDuplicateKeys
);
if (jsonValidationError) {
Expand All @@ -135,10 +139,10 @@ export async function detectRepoFileConfig(): Promise<RepoFileConfig> {
};
}
try {
configFileParsed = JSON5.parse(rawFileContents);
configFileParsed = JSON5.parse(configFileRaw);
} catch (err) /* istanbul ignore next */ {
logger.debug(
{ renovateConfig: rawFileContents },
{ renovateConfig: configFileRaw },
'Error parsing renovate config'
);
const validationError = 'Invalid JSON (parsing failed)';
Expand All @@ -154,7 +158,7 @@ export async function detectRepoFileConfig(): Promise<RepoFileConfig> {
'Repository config'
);
}
return { configFileName, configFileParsed };
return { configFileName, configFileRaw, configFileParsed };
}

export function checkForRepoConfigError(repoConfig: RepoFileConfig): void {
Expand Down
1 change: 1 addition & 0 deletions lib/workers/repository/init/types.ts
Expand Up @@ -5,6 +5,7 @@ export interface RepoConfigError {

export interface RepoFileConfig {
configFileName?: string;
configFileRaw?: string | null;
configFileParsed?: any;
configFileParseError?: RepoConfigError;
}
Expand Down

0 comments on commit 1b8fd39

Please sign in to comment.