Skip to content
Snippets Groups Projects
Unverified Commit 537e911e authored by Rhys Arkins's avatar Rhys Arkins Committed by GitHub
Browse files

fix: fetch internal presets over www not api (#6078)

parent b806e98c
No related branches found
No related tags found
No related merge requests found
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`config/presets/github fetchJSONFile() returns JSON 1`] = `
Object {
"from": "api",
}
`;
exports[`config/presets/github fetchJSONFile() returns renovate internal 1`] = `
Object {
"from": "www",
}
`;
...@@ -18,6 +18,54 @@ describe('config/presets/github', () => { ...@@ -18,6 +18,54 @@ describe('config/presets/github', () => {
got.mockReset(); got.mockReset();
return global.renovateCache.rmAll(); return global.renovateCache.rmAll();
}); });
describe('setInternalPreset()', () => {
it('allows override', () => {
github.setInternalPreset({ body: {} });
});
});
describe('fetchJSONFile()', () => {
beforeEach(() => {
delete global.repoCache.internalPresets;
});
it('returns JSON', async () => {
hostRules.find.mockReturnValueOnce({ token: 'abc' });
got.mockImplementationOnce(() => ({
body: {
content: Buffer.from('{"from":"api"}').toString('base64'),
},
}));
const res = await github.fetchJSONFile(
'some/repo',
'some-filename',
'https://api.github.com'
);
expect(res).toMatchSnapshot();
});
it('returns renovate internal', async () => {
hostRules.find.mockReturnValueOnce({ token: 'abc' });
got.mockImplementationOnce(() => ({
body: { from: 'www' },
}));
const res = await github.fetchJSONFile(
'renovatebot/presets',
'presets.json',
'https://api.github.com/'
);
expect(res).toMatchSnapshot();
});
it('throws platform error', async () => {
got.mockImplementationOnce(() => {
throw new Error();
});
await expect(
github.fetchJSONFile(
'renovatebot/presets',
'presets.json',
'https://api.github.com/'
)
).rejects.toThrow(PLATFORM_FAILURE);
});
});
describe('getPreset()', () => { describe('getPreset()', () => {
it('passes up platform-failure', async () => { it('passes up platform-failure', async () => {
got.mockImplementationOnce(() => { got.mockImplementationOnce(() => {
......
...@@ -7,12 +7,40 @@ import { PLATFORM_TYPE_GITHUB } from '../../constants/platforms'; ...@@ -7,12 +7,40 @@ import { PLATFORM_TYPE_GITHUB } from '../../constants/platforms';
const http = new Http(PLATFORM_TYPE_GITHUB); const http = new Http(PLATFORM_TYPE_GITHUB);
async function fetchJSONFile( export function setInternalPreset(content: { body: Preset }): void {
global.repoCache.internalPresets = Promise.resolve(content);
}
async function fetchInternalPreset(): Promise<Preset> {
const res = await http.getJson<Preset>(
'https://raw.githubusercontent.com/renovatebot/presets/master/presets.json'
);
return res.body;
}
function getInternalPreset(): Promise<Preset> {
global.repoCache.internalPresets =
global.repoCache.internalPresets || fetchInternalPreset();
return global.repoCache.internalPresets;
}
export async function fetchJSONFile(
repo: string, repo: string,
fileName: string, fileName: string,
endpoint: string endpoint: string
): Promise<Preset> { ): Promise<Preset> {
const url = `${endpoint}repos/${repo}/contents/${fileName}`; const url = `${endpoint}repos/${repo}/contents/${fileName}`;
if (
url ===
'https://api.github.com/repos/renovatebot/presets/contents/presets.json'
) {
try {
const res = await getInternalPreset();
return res;
} catch (err) {
throw new Error(PLATFORM_FAILURE);
}
}
const opts: HttpOptions = { const opts: HttpOptions = {
headers: { headers: {
accept: global.appMode accept: global.appMode
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment