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 7 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.
Jump to
Jump to file
Failed to load files.
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
20 changes: 12 additions & 8 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 rawFileContents;
Gabriel-Ladzaretti marked this conversation as resolved.
Show resolved Hide resolved
if (configFileName === 'package.json') {
// We already know it parses
configFileParsed = JSON.parse(
Expand All @@ -79,7 +83,7 @@ export async function detectRepoFileConfig(): Promise<RepoFileConfig> {
}
logger.debug({ config: configFileParsed }, 'package.json>renovate config');
} else {
let rawFileContents = await readLocalFile(configFileName, 'utf8');
rawFileContents = await readLocalFile(configFileName, 'utf8');
Gabriel-Ladzaretti marked this conversation as resolved.
Show resolved Hide resolved
// istanbul ignore if
if (!is.string(rawFileContents)) {
logger.warn({ configFileName }, 'Null contents when reading config file');
Expand Down Expand Up @@ -154,7 +158,7 @@ export async function detectRepoFileConfig(): Promise<RepoFileConfig> {
'Repository config'
);
}
return { configFileName, configFileParsed };
return { configFileName, configFileRaw: rawFileContents, configFileParsed };
Gabriel-Ladzaretti marked this conversation as resolved.
Show resolved Hide resolved
}

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;
Gabriel-Ladzaretti marked this conversation as resolved.
Show resolved Hide resolved
configFileParsed?: any;
configFileParseError?: RepoConfigError;
}
Expand Down