-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgithub.js
72 lines (67 loc) · 1.88 KB
/
github.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const _ = require("lodash");
const got = require("got");
const { Octokit } = require("@octokit/rest");
const Promise = require("bluebird");
const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN,
// Set GitHub Auth Token in environment variable
});
class GitHubClient {
constructor(token) {
this.token = token;
this.owner = "sparkpost";
}
_getReposQuery(searchQuery) {
return `query {
search(
query: "${searchQuery}",
type: REPOSITORY, last: 50
) {
repositoryCount
edges {
node {
... on Repository {
name
nameWithOwner
}
}
}
}
}`;
}
async getRepos(searchQuery) {
const results = await octokit.graphql(this._getReposQuery(searchQuery));
const repos = _.map(results.search.edges, "node");
return repos.map((repo) => {
const [org, name] = repo.nameWithOwner.split("/");
return { org, name };
});
}
async hasAlertsEnabled(repos) {
const enabled = [];
const disabled = [];
await Promise.each(repos, async (repo) => {
const repoUrl = `https://api.github.com/repos/${repo.org}/${repo.name}`;
try {
await got(`${repoUrl}/vulnerability-alerts`, {
headers: {
Accept: "application/vnd.github.dorian-preview+json",
"User-Agent": "node-script",
Authorization: `token ${this.token}`,
},
});
enabled.push(repo);
} catch (err) {
if (err.response.statusCode === 404) {
disabled.push(`<https://github.com/${repo.org}/${repo.name}>`);
} else {
throw new Error(
`Could not retrieve vulnerability alerts - status code ${err.response.statusCode}`
);
}
}
});
return { enabled, disabled };
}
}
module.exports = GitHubClient;