Skip to content

Commit

Permalink
refactor: github presets use util/http/github (#6658)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Jul 3, 2020
1 parent 4506497 commit ae0fb69
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 36 deletions.
4 changes: 2 additions & 2 deletions lib/config/presets/github/__snapshots__/index.spec.ts.snap
Expand Up @@ -58,7 +58,7 @@ exports[`config/presets/github/index getPreset() should return custom.json 1`] =
Array [
Object {
"headers": Object {
"accept": "application/vnd.github.machine-man-preview+json",
"accept": "application/vnd.github.v3+json",
"accept-encoding": "gzip, deflate",
"authorization": "token abc",
"host": "api.github.com",
Expand Down Expand Up @@ -90,7 +90,7 @@ exports[`config/presets/github/index getPreset() should throws not-found 1`] = `
Array [
Object {
"headers": Object {
"accept": "application/vnd.github.machine-man-preview+json",
"accept": "application/vnd.github.v3+json",
"accept-encoding": "gzip, deflate",
"authorization": "token abc",
"host": "api.github.com",
Expand Down
34 changes: 11 additions & 23 deletions lib/config/presets/github/index.spec.ts
Expand Up @@ -132,17 +132,11 @@ describe(getName(__filename), () => {
.reply(200, {
content: Buffer.from('{"foo":"bar"}').toString('base64'),
});

try {
global.appMode = true;
const content = await github.getPreset({
packageName: 'some/repo',
presetName: 'custom',
});
expect(content).toEqual({ foo: 'bar' });
} finally {
delete global.appMode;
}
const content = await github.getPreset({
packageName: 'some/repo',
presetName: 'custom',
});
expect(content).toEqual({ foo: 'bar' });
expect(httpMock.getTrace()).toMatchSnapshot();
});

Expand All @@ -153,18 +147,12 @@ describe(getName(__filename), () => {
.reply(200, {
content: Buffer.from('{}').toString('base64'),
});

try {
global.appMode = true;
await expect(
github.getPreset({
packageName: 'some/repo',
presetName: 'somefile/somename/somesubname',
})
).rejects.toThrow(PRESET_NOT_FOUND);
} finally {
delete global.appMode;
}
await expect(
github.getPreset({
packageName: 'some/repo',
presetName: 'somefile/somename/somesubname',
})
).rejects.toThrow(PRESET_NOT_FOUND);
expect(httpMock.getTrace()).toMatchSnapshot();
});
});
Expand Down
14 changes: 3 additions & 11 deletions lib/config/presets/github/index.ts
@@ -1,30 +1,22 @@
import { PLATFORM_TYPE_GITHUB } from '../../../constants/platforms';
import { logger } from '../../../logger';
import { ExternalHostError } from '../../../types/errors/external-host-error';
import { Http, HttpOptions } from '../../../util/http';
import { GithubHttp } from '../../../util/http/github';
import { Preset, PresetConfig } from '../common';
import { PRESET_DEP_NOT_FOUND, fetchPreset } from '../util';

export const Endpoint = 'https://api.github.com/';

const http = new Http(PLATFORM_TYPE_GITHUB);
const http = new GithubHttp();

export async function fetchJSONFile(
repo: string,
fileName: string,
endpoint: string
): Promise<Preset> {
const url = `${endpoint}repos/${repo}/contents/${fileName}`;
const opts: HttpOptions = {
headers: {
accept: global.appMode
? 'application/vnd.github.machine-man-preview+json'
: 'application/vnd.github.v3+json',
},
};
let res: { body: { content: string } };
try {
res = await http.getJson(url, opts);
res = await http.getJson(url);
} catch (err) {
// istanbul ignore if: not testable with nock
if (err instanceof ExternalHostError) {
Expand Down

0 comments on commit ae0fb69

Please sign in to comment.