forked from OctoLinker/OctoLinker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helper-github-api package (OctoLinker#542)
- Loading branch information
1 parent
9ba8c38
commit 3f07179
Showing
6 changed files
with
161 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import rateLimitNotification from '@octolinker/ratelimit-notification'; | ||
import { get } from '@octolinker/helper-settings'; | ||
import fetchTree from '../fetch-tree'; | ||
|
||
jest.mock('@octolinker/ratelimit-notification', () => jest.fn()); | ||
jest.mock('@octolinker/helper-settings', () => ({ | ||
get: jest.fn(), | ||
})); | ||
|
||
const response = JSON.stringify({ | ||
tree: [ | ||
{ type: 'blob', path: 'foo.js' }, | ||
{ type: 'tree', path: 'src' }, | ||
{ type: 'blob', path: 'src/bar.js' }, | ||
], | ||
}); | ||
|
||
const options = { | ||
user: 'octo', | ||
repo: 'cat', | ||
branch: 'tentacle', | ||
}; | ||
|
||
describe('helper-github-api tree', () => { | ||
beforeEach(() => { | ||
fetch.resetMocks(); | ||
}); | ||
|
||
it('calls the github tree api', async () => { | ||
fetch.mockResponseOnce(response); | ||
await fetchTree(options); | ||
|
||
expect(global.fetch).toBeCalledWith( | ||
'https://api.github.com/repos/octo/cat/git/trees/tentacle?recursive=1', | ||
{ | ||
method: 'GET', | ||
headers: { | ||
Accept: 'application/vnd.github.v3+json', | ||
}, | ||
}, | ||
); | ||
}); | ||
|
||
it('calls the github tree api with an api token', async () => { | ||
fetch.mockResponseOnce(response); | ||
get.mockReturnValue('fake-token'); | ||
await fetchTree(options); | ||
|
||
expect(global.fetch).toBeCalledWith( | ||
'https://api.github.com/repos/octo/cat/git/trees/tentacle?recursive=1', | ||
{ | ||
method: 'GET', | ||
headers: { | ||
Accept: 'application/vnd.github.v3+json', | ||
Authorization: 'token fake-token', | ||
}, | ||
}, | ||
); | ||
}); | ||
|
||
it('returns an empty array on failure', async () => { | ||
fetch.mockResponseOnce(() => new Promise((resolve, reject) => reject())); | ||
|
||
expect(await fetchTree(options)).toEqual([]); | ||
}); | ||
|
||
it('calls rateLimitNotification on success', async () => { | ||
const headers = new Headers(); | ||
headers.append('foo', 'bar'); | ||
|
||
fetch.mockResponseOnce(response, { | ||
status: 201, | ||
headers, | ||
}); | ||
await fetchTree(options); | ||
|
||
expect(rateLimitNotification).toBeCalledWith(headers, 201); | ||
}); | ||
|
||
it('returns an array of files', async () => { | ||
fetch.mockResponseOnce(response); | ||
|
||
expect(await fetchTree(options)).toEqual(['foo.js', 'src/bar.js']); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import * as storage from '@octolinker/helper-settings'; | ||
import rateLimitNotification from '@octolinker/ratelimit-notification'; | ||
|
||
export default async function({ user, repo, branch }) { | ||
const token = storage.get('githubToken'); | ||
|
||
const headers = { | ||
Accept: 'application/vnd.github.v3+json', | ||
}; | ||
|
||
if (token) { | ||
headers.Authorization = `token ${token}`; | ||
} | ||
|
||
let response; | ||
try { | ||
response = await fetch( | ||
`https://api.github.com/repos/${user}/${repo}/git/trees/${branch}?recursive=1`, | ||
{ | ||
method: 'GET', | ||
headers, | ||
}, | ||
); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
|
||
if (!response) return []; | ||
|
||
rateLimitNotification(response.headers, response.status); | ||
|
||
const json = await response.json(); | ||
|
||
if (!json.tree) { | ||
return []; | ||
} | ||
|
||
return json.tree | ||
.filter(({ type }) => type === 'blob') | ||
.map(({ path }) => path); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import fetchTree from './fetch-tree'; | ||
|
||
export { fetchTree }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "@octolinker/helper-github-api", | ||
"version": "1.0.0", | ||
"description": "", | ||
"repository": "https://github.com/octolinker/octolinker/tree/master/packages/helper-github-api", | ||
"license": "MIT", | ||
"main": "./index.js", | ||
"dependencies": { | ||
"@octolinker/helper-settings": "1.0.0", | ||
"@octolinker/ratelimit-notification": "1.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3427,6 +3427,14 @@ create-hmac@^1.1.0, create-hmac@^1.1.2, create-hmac@^1.1.4: | |
safe-buffer "^5.0.1" | ||
sha.js "^2.4.8" | ||
|
||
cross-fetch@^2.2.2: | ||
version "2.2.3" | ||
resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-2.2.3.tgz#e8a0b3c54598136e037f8650f8e823ccdfac198e" | ||
integrity sha512-PrWWNH3yL2NYIb/7WF/5vFG3DCQiXDOVf8k3ijatbrtnwNuhMWLC7YF7uqf53tbTFDzHIUD8oITw4Bxt8ST3Nw== | ||
dependencies: | ||
node-fetch "2.1.2" | ||
whatwg-fetch "2.0.4" | ||
|
||
cross-spawn@^5.0.1: | ||
version "5.1.0" | ||
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-5.1.0.tgz#e8bd0efee58fcff6f8f94510a0a554bbfa235449" | ||
|
@@ -6463,7 +6471,7 @@ is-retry-allowed@^1.0.0: | |
resolved "https://registry.yarnpkg.com/is-retry-allowed/-/is-retry-allowed-1.1.0.tgz#11a060568b67339444033d0125a61a20d564fb34" | ||
integrity sha1-EaBgVotnM5REAz0BJaYaINVk+zQ= | ||
|
||
is-stream@^1.0.0, is-stream@^1.0.1, is-stream@^1.1.0: | ||
is-stream@^1.0.0, is-stream@^1.1.0: | ||
version "1.1.0" | ||
resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-1.1.0.tgz#12d4a3dd4e68e0b79ceb8dbc84173ae80d91ca44" | ||
integrity sha1-EtSj3U5o4Lec6428hBc66A2RykQ= | ||
|
@@ -6547,14 +6555,6 @@ isobject@^3.0.0, isobject@^3.0.1: | |
resolved "https://registry.yarnpkg.com/isobject/-/isobject-3.0.1.tgz#4e431e92b11a9731636aa1f9c8d1ccbcfdab78df" | ||
integrity sha1-TkMekrEalzFjaqH5yNHMvP2reN8= | ||
|
||
isomorphic-fetch@^2.2.1: | ||
version "2.2.1" | ||
resolved "https://registry.yarnpkg.com/isomorphic-fetch/-/isomorphic-fetch-2.2.1.tgz#611ae1acf14f5e81f729507472819fe9733558a9" | ||
integrity sha1-YRrhrPFPXoH3KVB0coGf6XM1WKk= | ||
dependencies: | ||
node-fetch "^1.0.1" | ||
whatwg-fetch ">=0.10.0" | ||
|
||
isstream@~0.1.2: | ||
version "0.1.2" | ||
resolved "https://registry.yarnpkg.com/isstream/-/isstream-0.1.2.tgz#47e63f7af55afa6f92e1500e690eb8b8529c099a" | ||
|
@@ -6770,12 +6770,12 @@ jest-environment-puppeteer@^3.4.0: | |
jest-dev-server "^3.4.0" | ||
lodash "^4.17.11" | ||
|
||
jest-fetch-mock@^1.6.6: | ||
version "1.6.6" | ||
resolved "https://registry.yarnpkg.com/jest-fetch-mock/-/jest-fetch-mock-1.6.6.tgz#f07d80912689c21fb01199f313abd121beff5558" | ||
integrity sha512-qMorYR8iE0OyOHmroCJVVLuJlRHMcRdrrvYFo1PMY9ErWyCs9ZrL3RGHjkxVgGJSZwKzUlsWgqVFhOK3TMA3eg== | ||
jest-fetch-mock@^2.1.0: | ||
version "2.1.0" | ||
resolved "https://registry.yarnpkg.com/jest-fetch-mock/-/jest-fetch-mock-2.1.0.tgz#49c16451b82f311158ec897e467d704e0cb118f9" | ||
integrity sha512-jrTNlxDsZZCq6tMhdyH7gIbt4iDUHRr6C4Jp+kXItLaaaladOm9/wJjIwU3tCAEohbuW/7/naOSfg2A8H6/35g== | ||
dependencies: | ||
isomorphic-fetch "^2.2.1" | ||
cross-fetch "^2.2.2" | ||
promise-polyfill "^7.1.1" | ||
|
||
jest-get-type@^22.1.0: | ||
|
@@ -8241,13 +8241,10 @@ node-fetch-npm@^2.0.2: | |
json-parse-better-errors "^1.0.0" | ||
safe-buffer "^5.1.1" | ||
|
||
node-fetch@^1.0.1: | ||
version "1.7.3" | ||
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-1.7.3.tgz#980f6f72d85211a5347c6b2bc18c5b84c3eb47ef" | ||
integrity sha512-NhZ4CsKx7cYm2vSrBAr2PvFOe6sWDf0UYLRqA6svUYg7+/TSfVAu49jYC4BvQ4Sms9SZgdqGBgroqfDhJdTyKQ== | ||
dependencies: | ||
encoding "^0.1.11" | ||
is-stream "^1.0.1" | ||
[email protected]: | ||
version "2.1.2" | ||
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.1.2.tgz#ab884e8e7e57e38a944753cec706f788d1768bb5" | ||
integrity sha1-q4hOjn5X44qUR1POxwb3iNF2i7U= | ||
|
||
[email protected]: | ||
version "1.2.0" | ||
|
@@ -12194,7 +12191,7 @@ whatwg-encoding@^1.0.1, whatwg-encoding@^1.0.3: | |
dependencies: | ||
iconv-lite "0.4.23" | ||
|
||
whatwg-fetch@>=0.10.0: | ||
whatwg-fetch@2.0.4: | ||
version "2.0.4" | ||
resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-2.0.4.tgz#dde6a5df315f9d39991aa17621853d720b85566f" | ||
integrity sha512-dcQ1GWpOD/eEQ97k66aiEVpNnapVj90/+R+SXTPYGHpYBBypfKJEQjLrvMZ7YXbKm21gXd4NcuxUTjiv1YtLng== | ||
|