Skip to content

Commit

Permalink
feat(presets): github preset and subpreset names
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Apr 28, 2020
1 parent 4154c9f commit 61065ea
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 15 deletions.
27 changes: 27 additions & 0 deletions lib/config/presets/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,33 @@ Object {
}
`;

exports[`config/presets parsePreset parses github subfiles 1`] = `
Object {
"packageName": "some/repo",
"params": undefined,
"presetName": "somefile",
"presetSource": "github",
}
`;

exports[`config/presets parsePreset parses github subfiles with preset and sub-preset name 1`] = `
Object {
"packageName": "some/repo",
"params": undefined,
"presetName": "somefile/somepreset/somesubpreset",
"presetSource": "github",
}
`;

exports[`config/presets parsePreset parses github subfiles with preset name 1`] = `
Object {
"packageName": "some/repo",
"params": undefined,
"presetName": "somefile/somepreset",
"presetSource": "github",
}
`;

exports[`config/presets parsePreset parses gitlab 1`] = `
Object {
"packageName": "some/repo",
Expand Down
30 changes: 30 additions & 0 deletions lib/config/presets/github.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,36 @@ describe('config/presets/github', () => {
const content = await github.getPreset('some/repo');
expect(content).toEqual({ foo: 'bar' });
});
it('should query preset within the file', async () => {
hostRules.find.mockReturnValueOnce({ token: 'abc' });
got.mockImplementationOnce(() => ({
body: {
content: Buffer.from('{"somename":{"foo":"bar"}}').toString('base64'),
},
}));
const content = await github.getPreset('some/repo', 'somefile/somename');
expect(content).toEqual({ foo: 'bar' });
});
it('should query subpreset', async () => {
hostRules.find.mockReturnValueOnce({ token: 'abc' });
got.mockImplementation(() => ({
body: {
content: Buffer.from(
'{"somename":{"somesubname":{"foo":"bar"}}}'
).toString('base64'),
},
}));
let content = await github.getPreset(
'some/repo',
'somefile/somename/somesubname'
);
expect(content).toEqual({ foo: 'bar' });
content = await github.getPreset(
'some/repo',
'somefile/wrongname/somesubname'
);
expect(content).toBeUndefined();
});
it('should return custom.json', async () => {
hostRules.find.mockReturnValueOnce({ token: 'abc' });
got.mockImplementationOnce(() => ({
Expand Down
34 changes: 19 additions & 15 deletions lib/config/presets/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,35 @@ async function fetchJSONFile(

export async function getPresetFromEndpoint(
pkgName: string,
presetName: string,
filePreset: string,
endpoint = 'https://api.github.com/'
): Promise<Preset> {
// eslint-disable-next-line no-param-reassign
endpoint = ensureTrailingSlash(endpoint);
if (presetName === 'default') {
const [fileName, presetName, subPresetName] = filePreset.split('/');
let jsonContent;
if (fileName === 'default') {
try {
const defaultJson = await fetchJSONFile(
pkgName,
'default.json',
endpoint
);
return defaultJson;
jsonContent = await fetchJSONFile(pkgName, 'default.json', endpoint);
} catch (err) {
if (err.message === PLATFORM_FAILURE) {
if (err.message !== 'dep not found') {
throw err;
}
if (err.message === 'dep not found') {
logger.debug('default.json preset not found - trying renovate.json');
return fetchJSONFile(pkgName, 'renovate.json', endpoint);
}
throw err;
logger.debug('default.json preset not found - trying renovate.json');
jsonContent = await fetchJSONFile(pkgName, 'renovate.json', endpoint);
}
} else {
jsonContent = await fetchJSONFile(pkgName, `${filePreset}.json`, endpoint);
}
if (presetName) {
if (subPresetName) {
return jsonContent[presetName]
? jsonContent[presetName][subPresetName]
: undefined;
}
return jsonContent[presetName];
}
return fetchJSONFile(pkgName, `${presetName}.json`, endpoint);
return jsonContent;
}

export async function getPreset(
Expand Down
17 changes: 17 additions & 0 deletions lib/config/presets/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,23 @@ describe('config/presets', () => {
it('parses github', () => {
expect(presets.parsePreset('github>some/repo')).toMatchSnapshot();
});
it('parses github subfiles', () => {
expect(
presets.parsePreset('github>some/repo:somefile')
).toMatchSnapshot();
});
it('parses github subfiles with preset name', () => {
expect(
presets.parsePreset('github>some/repo:somefile/somepreset')
).toMatchSnapshot();
});
it('parses github subfiles with preset and sub-preset name', () => {
expect(
presets.parsePreset(
'github>some/repo:somefile/somepreset/somesubpreset'
)
).toMatchSnapshot();
});
it('parses gitlab', () => {
expect(presets.parsePreset('gitlab>some/repo')).toMatchSnapshot();
});
Expand Down

0 comments on commit 61065ea

Please sign in to comment.