Skip to content

Commit a8d911b

Browse files
author
Stefan Buck
committed
Add helper-github-api package
1 parent 9ba8c38 commit a8d911b

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import rateLimitNotification from '@octolinker/ratelimit-notification';
2+
import { get } from '@octolinker/helper-settings';
3+
import fetchTree from '../fetch-tree';
4+
5+
jest.mock('@octolinker/ratelimit-notification', () => jest.fn());
6+
jest.mock('@octolinker/helper-settings', () => ({
7+
get: jest.fn(),
8+
}));
9+
10+
const response = JSON.stringify({
11+
tree: [
12+
{ type: 'blob', path: 'foo.js' },
13+
{ type: 'tree', path: 'src' },
14+
{ type: 'blob', path: 'src/bar.js' },
15+
],
16+
});
17+
18+
const options = {
19+
user: 'octo',
20+
repo: 'cat',
21+
branch: 'tentacle',
22+
};
23+
24+
describe('helper-github-api tree', () => {
25+
beforeEach(() => {
26+
fetch.resetMocks();
27+
});
28+
29+
it('calls the github tree api', async () => {
30+
fetch.mockResponseOnce(response);
31+
await fetchTree(options);
32+
33+
expect(global.fetch).toBeCalledWith(
34+
'https://api.github.com/repos/octo/cat/git/trees/tentacle?recursive=1',
35+
{
36+
method: 'GET',
37+
headers: {
38+
Accept: 'application/vnd.github.v3+json',
39+
},
40+
},
41+
);
42+
});
43+
44+
it('calls the github tree api with an api token', async () => {
45+
fetch.mockResponseOnce(response);
46+
get.mockReturnValue('fake-token');
47+
await fetchTree(options);
48+
49+
expect(global.fetch).toBeCalledWith(
50+
'https://api.github.com/repos/octo/cat/git/trees/tentacle?recursive=1',
51+
{
52+
method: 'GET',
53+
headers: {
54+
Accept: 'application/vnd.github.v3+json',
55+
Authorization: 'token fake-token',
56+
},
57+
},
58+
);
59+
});
60+
61+
it('returns an empty array on failure', async () => {
62+
fetch.mockResponseOnce(() => new Promise((resolve, reject) => reject()));
63+
64+
expect(await fetchTree(options)).toEqual([]);
65+
});
66+
67+
it('calls rateLimitNotification on success', async () => {
68+
const headers = new Headers();
69+
headers.append('foo', 'bar');
70+
71+
fetch.mockResponseOnce(response, {
72+
status: 201,
73+
headers,
74+
});
75+
await fetchTree(options);
76+
77+
expect(rateLimitNotification).toBeCalledWith(headers, 201);
78+
});
79+
80+
it('returns an array of files', async () => {
81+
fetch.mockResponseOnce(response);
82+
83+
expect(await fetchTree(options)).toEqual(['foo.js', 'src/bar.js']);
84+
});
85+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import * as storage from '@octolinker/helper-settings';
2+
import rateLimitNotification from '@octolinker/ratelimit-notification';
3+
4+
export default async function({ user, repo, branch }) {
5+
const token = storage.get('githubToken');
6+
7+
const headers = {
8+
Accept: 'application/vnd.github.v3+json',
9+
};
10+
11+
if (token) {
12+
headers.Authorization = `token ${token}`;
13+
}
14+
15+
let response;
16+
try {
17+
response = await fetch(
18+
`https://api.github.com/repos/${user}/${repo}/git/trees/${branch}?recursive=1`,
19+
{
20+
method: 'GET',
21+
headers,
22+
},
23+
);
24+
} catch (error) {
25+
console.error(error);
26+
}
27+
28+
if (!response) return [];
29+
30+
rateLimitNotification(response.headers, response.status);
31+
32+
const json = await response.json();
33+
34+
if (!json.tree) {
35+
return [];
36+
}
37+
38+
return json.tree
39+
.filter(({ type }) => type === 'blob')
40+
.map(({ path }) => path);
41+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import fetchTree from './fetch-tree';
2+
3+
export { fetchTree };
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "@octolinker/helper-github-api",
3+
"version": "1.0.0",
4+
"description": "",
5+
"repository": "https://github.com/octolinker/octolinker/tree/master/packages/helper-github-api",
6+
"license": "MIT",
7+
"main": "./index.js",
8+
"dependencies": {
9+
"@octolinker/helper-settings": "1.0.0",
10+
"@octolinker/ratelimit-notification": "1.0.0"
11+
}
12+
}

0 commit comments

Comments
 (0)