Skip to content

Commit 2f16919

Browse files
authored
Merge pull request #80 from w3c/no-octokat
Use @octokit/core instead of octokat
2 parents e73848e + d434d45 commit 2f16919

13 files changed

+210
-228
lines changed

lib/github.js

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,53 @@
22

33
"use strict";
44

5-
const config = require("../config.json");
5+
const octokit = require("./octokit.js");
66

7-
const graphql = require("./graphql.js");
8-
// github API v3 needed to check webhooks
9-
const Octokat = require("octokat");
10-
const octo = new Octokat({token: config.ghToken});
7+
const licenseQuery = `
8+
query {
9+
repository(owner: "w3c", name: "licenses") {
10+
contributing: object(expression: "HEAD:WG-CONTRIBUTING.md") {
11+
... on Blob {
12+
text
13+
}
14+
}
15+
contributingSw: object(expression: "HEAD:WG-CONTRIBUTING-SW.md") {
16+
... on Blob {
17+
text
18+
}
19+
}
20+
license: object(expression: "HEAD:WG-LICENSE.md") {
21+
... on Blob {
22+
text
23+
}
24+
}
25+
licenseSw: object(expression: "HEAD:WG-LICENSE-SW.md") {
26+
... on Blob {
27+
text
28+
}
29+
}
30+
}
31+
}
32+
`;
33+
34+
async function w3cLicenses() {
35+
let res = await octokit.graphql(licenseQuery);
36+
res = res.repository;
37+
38+
if (res.contributing) {
39+
res.contributing = res.contributing.text;
40+
}
41+
if (res.contributingSw) {
42+
res.contributingSw = res.contributingSw.text;
43+
}
44+
if (res.license) {
45+
res.license = res.license.text;
46+
}
47+
if (res.licenseSw) {
48+
res.licenseSw = res.licenseSw.text;
49+
}
50+
return res;
51+
}
1152

1253
const repoQuery = `
1354
query ($org: String!, $cursor: String) {
@@ -102,12 +143,12 @@ const labelQuery = `
102143

103144
async function *listRepos(org) {
104145
for (let cursor = null; ;) {
105-
const res = await graphql(repoQuery, {org, cursor});
146+
const res = await octokit.graphql(repoQuery, {org, cursor});
106147
for (const repo of res.organization.repositories.nodes) {
107148
const labels = repo.labels.nodes;
108149
// Fetch more labels if they are paginated
109150
for (let pageInfo = repo.labels.pageInfo; pageInfo.hasNextPage;) {
110-
const res = await graphql(labelQuery, {
151+
const res = await octokit.graphql(labelQuery, {
111152
org,
112153
repo: repo.name,
113154
cursor: pageInfo.endCursor
@@ -126,9 +167,23 @@ async function *listRepos(org) {
126167
}
127168
}
128169

129-
async function listRepoHooks(org, repo) {
130-
const hooks = await octo.repos(`${org}/${repo}`).hooks.fetch();
131-
return hooks.items;
170+
async function listRepoContributors(owner, repo) {
171+
const res = await octokit.request('GET /repos/:owner/:repo/contributors', {
172+
owner, repo,
173+
});
174+
return res.data;
175+
}
176+
177+
async function listRepoHooks(owner, repo) {
178+
const res = await octokit.request('GET /repos/:owner/:repo/hooks', {
179+
owner, repo,
180+
});
181+
return res.data;
132182
}
133183

134-
module.exports = {listRepos, listRepoHooks};
184+
module.exports = {
185+
w3cLicenses,
186+
listRepos,
187+
listRepoContributors,
188+
listRepoHooks,
189+
};

lib/graphql.js renamed to lib/octokit.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ const octokit = new Octokit({
2323
},
2424
onAbuseLimit: (retryAfter, options) => {
2525
if (options.request.retryCount < MAX_RETRIES) {
26-
console.warn(`Abuse detected triggered, retrying after ${retryAfter} seconds`)
26+
console.warn(`Abuse detection triggered, retrying after ${retryAfter} seconds`)
2727
return true;
2828
} else {
29-
console.error(`Abuse detected triggered, giving up after ${MAX_RETRIES} retries`);
29+
console.error(`Abuse detection triggered, giving up after ${MAX_RETRIES} retries`);
3030
return false;
3131
}
3232
}
3333
}
3434
});
3535

36-
module.exports = octokit.graphql;
36+
module.exports = octokit;

lib/validator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ function validateAshHooks(hooks) {
190190
// pushed instead of nulls. This is simply to match the original structure
191191
// of report.json. TODO: change this to null, or add error details.
192192
const errors = [];
193-
const ashHooks = hooks.filter(h => ashnazgHookUrls.includes(h.config.url) && h.config.contentType === "json" && h.config.insecureSsl === "0" && h.config.secret !== "");
193+
const ashHooks = hooks.filter(h => ashnazgHookUrls.includes(h.config.url) && h.config.content_type === "json" && h.config.insecure_ssl === "0" && h.config.secret !== "");
194194
if (ashHooks.length === 0) {
195195
errors.push(['missingashnazghook', {}]);
196196
}

lib/w3cLicenses.js

Lines changed: 0 additions & 53 deletions
This file was deleted.

list-unconnected-contributors.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
/* eslint-env node */
22

3-
const Octokat = require("octokat");
43
const config = require("./config.json");
4+
const github = require("./lib/github.js");
55
const w3c = require('node-w3capi');
66

77
// based on a downloaded a local copy from https://labs.w3.org/hatchery/repo-manager/api/users when logged in there, filtered to leave only list of ghID
88
const ashnazgusers = require('./ashnazg-users.json');
99

1010
w3c.apiKey = config.w3capikey;
11-
const octo = new Octokat({token: config.ghToken});
1211

1312
if (!process.argv[2] || process.argv[2].indexOf('/') === -1) {
1413
console.error("Required: name of repo to check, e.g. w3c/webrtc-pc");
1514
process.exit(2);
1615
}
1716

18-
const selectedrepo = process.argv[2];
17+
const [owner, repo] = process.argv[2].split('/');
1918

20-
octo.repos(selectedrepo).contributors.fetch().then(contributors => {
21-
Promise.all(contributors.items.map(contributor => {
19+
github.listRepoContributors(owner, repo).then(contributors => {
20+
Promise.all(contributors.map(contributor => {
2221
return new Promise(function(res, rej) {
2322
w3c.user({type: 'github', id: contributor.id}).fetch(function(err, /* w3cuser */) {
2423
if (err) {

package-lock.json

Lines changed: 0 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
"@octokit/core": "2.1.2",
1414
"@octokit/plugin-throttling": "2.7.0",
1515
"node-fetch": "^1.6.3",
16-
"node-w3capi": "^1.4.0",
17-
"octokat": "^0.7.0"
16+
"node-w3capi": "^1.4.0"
1817
},
1918
"devDependencies": {
2019
"eslint": "^6.6.0",

test/github.js

Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,59 @@ const proxyquire = require('proxyquire');
77
const sinon = require('sinon');
88

99
describe('github', () => {
10+
describe('w3cLicenses', () => {
11+
it('happy path', async () => {
12+
const graphql = sinon.fake.resolves({
13+
repository: {
14+
contributing: {
15+
text: 'mock WG-CONTRIBUTING.md content'
16+
},
17+
contributingSw: {
18+
text: 'mock WG-CONTRIBUTING-SW.md content'
19+
},
20+
license: {
21+
text: 'mock WG-LICENSE.md content'
22+
},
23+
licenseSw: {
24+
text: 'mock WG-LICENSE-SW.md content'
25+
},
26+
}
27+
});
28+
const github = proxyquire('../lib/github.js', {
29+
'./octokit.js': {graphql}
30+
});
31+
const lic = await github.w3cLicenses();
32+
assert.deepStrictEqual(lic, {
33+
contributing: 'mock WG-CONTRIBUTING.md content',
34+
contributingSw: 'mock WG-CONTRIBUTING-SW.md content',
35+
license: 'mock WG-LICENSE.md content',
36+
licenseSw: 'mock WG-LICENSE-SW.md content',
37+
});
38+
assert(graphql.calledOnce);
39+
});
40+
41+
it('no files found', async () => {
42+
const graphql = sinon.fake.resolves({repository: {}});
43+
const github = proxyquire('../lib/github.js', {
44+
'./octokit.js': {graphql}
45+
});
46+
const lic = await github.w3cLicenses();
47+
assert.deepStrictEqual(lic, {});
48+
assert(graphql.calledOnce);
49+
});
50+
51+
it('graphql error', async () => {
52+
const graphql = sinon.fake.rejects(new Error('mock error'));
53+
const github = proxyquire('../lib/github.js', {
54+
'./octokit.js': {graphql}
55+
});
56+
await assert.rejects(github.w3cLicenses(), {
57+
message: 'mock error'
58+
});
59+
assert(graphql.calledOnce);
60+
});
61+
});
62+
1063
describe('listRepos', () => {
1164
async function toArray(iterator) {
1265
const array = [];
@@ -41,7 +94,7 @@ describe('github', () => {
4194
}
4295
});
4396
const github = proxyquire('../lib/github.js', {
44-
'./graphql.js': graphql
97+
'./octokit.js': {graphql}
4598
});
4699
const res = await toArray(github.listRepos('WICG'));
47100
assert(graphql.calledOnce);
@@ -99,7 +152,7 @@ describe('github', () => {
99152
}
100153
});
101154
const github = proxyquire('../lib/github.js', {
102-
'./graphql.js': graphql
155+
'./octokit.js': {graphql}
103156
});
104157
const res = await toArray(github.listRepos('WICG'));
105158
assert(graphql.calledTwice);
@@ -165,7 +218,7 @@ describe('github', () => {
165218
}
166219
});
167220
const github = proxyquire('../lib/github.js', {
168-
'./graphql.js': graphql
221+
'./octokit.js': {graphql}
169222
});
170223
const res = await toArray(github.listRepos('WICG'));
171224
assert(graphql.calledThrice);
@@ -181,22 +234,27 @@ describe('github', () => {
181234
});
182235
});
183236

184-
it('fetchRepoHooks', async () => {
185-
class Stubkat {
186-
repos() {
187-
return {
188-
hooks: {
189-
async fetch() {
190-
return {
191-
items: ['mock-hook'],
192-
}
193-
}
194-
}
195-
}
196-
}
197-
}
198-
const github = proxyquire('../lib/github.js', {'octokat': Stubkat});
237+
it('listRepoContributors', async () => {
238+
const request = sinon.stub();
239+
request.resolves({data: ['mock-contributor']});
240+
const github = proxyquire('../lib/github.js', {'./octokit.js': {request}});
241+
const contributors = await github.listRepoContributors('foo', 'bar');
242+
assert(request.calledOnceWith('GET /repos/:owner/:repo/contributors', {
243+
owner: 'foo',
244+
repo: 'bar',
245+
}));
246+
assert.deepEqual(contributors, ['mock-contributor']);
247+
});
248+
249+
it('listRepoHooks', async () => {
250+
const request = sinon.stub();
251+
request.resolves({data: ['mock-hook']});
252+
const github = proxyquire('../lib/github.js', {'./octokit.js': {request}});
199253
const hooks = await github.listRepoHooks('foo', 'bar');
254+
assert(request.calledOnceWith('GET /repos/:owner/:repo/hooks', {
255+
owner: 'foo',
256+
repo: 'bar',
257+
}));
200258
assert.deepEqual(hooks, ['mock-hook']);
201259
});
202260

0 commit comments

Comments
 (0)