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

refactor(repo/init): return additional raw config from detectRepoFileConfig #17021

Merged
merged 16 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions lib/workers/repository/init/merge.spec.ts
Original file line number Diff line number Diff line change
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(`
Object {
"configFileName": ".renovaterc.json",
"configFileParsed": "{\\"something\\":\\"new\\"}",
}
`);
});
});

Expand All @@ -174,6 +181,7 @@ describe('workers/repository/init/merge', () => {

describe('mergeRenovateConfig()', () => {
beforeEach(() => {
platform.getRawFile.mockResolvedValueOnce(null);
Gabriel-Ladzaretti marked this conversation as resolved.
Show resolved Hide resolved
migrate.migrateConfig.mockReturnValue({
isMigrated: false,
migratedConfig: {},
Expand Down
38 changes: 21 additions & 17 deletions lib/workers/repository/init/merge.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface RepoConfigError {

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