This repository has been archived by the owner on May 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
github.js
101 lines (86 loc) · 2.55 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const express = require('express');
const fetch = require('node-fetch');
const octokit = require('@octokit/rest')();
const graphql = require('@octokit/graphql');
const router = express.Router();
const getLabsRepos = () => {
const apiCall = 'https://api.github.com/search/repositories?q=org:nycplanning%20topic:labs&per_page=100';
return fetch(apiCall).then(d => d.json());
};
router.get('/repo-activity', async (req, res) => {
octokit.authenticate({
type: 'oauth',
token: process.env.GITHUB_PERSONAL_ACCESS_TOKEN,
});
const { items: repos } = await getLabsRepos();
const names = repos.map(d => d.name);
const promises = [];
names.forEach((repoName) => {
promises.push(
octokit.repos.getCommitActivityStats({
owner: 'nycplanning',
repo: repoName,
}),
);
});
Promise.all(promises)
.then((response) => {
const simplified = names.map((name, i) => {
const weeklyData = response[i].data.map(({ week, total }) => ({
week,
total,
}));
return {
name,
data: weeklyData,
};
});
res.json(simplified);
});
});
router.get('/issues', async (req, res) => {
const { items: repos } = await getLabsRepos();
const names = repos.map(d => d.name);
const promises = [];
names.forEach((repoName) => {
promises.push(
graphql(`{
repository(owner:"nycplanning", name:"${repoName}") {
openIssues: issues(states:OPEN) {
totalCount
}
openBugs: issues(states:OPEN, labels:bug) {
totalCount
}
openEnhancements: issues(states:OPEN, labels:enhancement) {
totalCount
}
}
}`, {
headers: {
authorization: `token ${process.env.GITHUB_PERSONAL_ACCESS_TOKEN}`,
},
}),
);
});
Promise.all(promises)
.then((response) => {
const transformed = response.map((d, i) => {
const name = names[i];
const openIssues = d.data.repository.openIssues.totalCount;
const openBugs = d.data.repository.openBugs.totalCount;
const openEnhancements = d.data.repository.openEnhancements.totalCount;
return {
name,
openIssues,
openBugs,
openEnhancements,
};
});
res.json(transformed);
})
.catch((err) => {
res.json(err);
});
});
module.exports = router;