This repository was archived by the owner on Apr 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
212 lines (189 loc) · 7.34 KB
/
app.js
File metadata and controls
212 lines (189 loc) · 7.34 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
const express = require('express');
const app = express();
const path = require('path');
const GitHub = require('github-api');
const Promise = require('bluebird');
const JSONStream = require('JSONStream');
const moment = require('moment');
const requestLog = require('./request-log');
const logger = require('./logger');
const retry = require('bluebird-retry');
const _ = require('lodash');
const MarkdownIt = require('markdown-it');
const readFile = Promise.promisify(require("fs").readFile);
const RETRY_OPTS = {
backoff: 5,
interval: 1000,
max_tries: 5
};
const getErrorCode = (err) => err.code || err.statusCode || err.status || 500;
const getMessage = (err) => {
let message = err.message || err.statusText || 'Ops... Something terrible happens';
if (getErrorCode(err) === 401) {
message = `Provide your valid GitHub token to query as 'token=[your github token]' or use authorization header 'Authorization:token [your github token]'. You can retrieve personal GitHub token at https://github.com/settings/tokens`
}
return message;
};
const catchGitHub401or404 = (err) => {
logger.info(`GITHUB ERROR: ${err.message}`);
const errorCode = getErrorCode(err.response || {});
if (errorCode === 401 || errorCode === 404) {
err.status = errorCode;
throw new retry.StopError(err);
}
throw err;
};
const processGitHubResponse = (r) => {
logger.info(`${r.config.method} ${r.status} ${r.config.url} | ${r.statusText}`);
if (r.status === 204) {
return [];
}
if (r.status !== 200) {
const error = new Error(r.statusText);
error.code = r.status;
error.status = r.status;
throw error;
}
return r.data;
};
const initGitHub = async (req) => {
const authToken = getAuthToken(req);
if (_.isEmpty(authToken)) {
const error = new Error('Unauthorized');
error.status = 401;
throw error;
}
return new GitHub({token: authToken});
};
const execWithRetry = (fn) => retry(() => fn().then(processGitHubResponse).catch(catchGitHub401or404), RETRY_OPTS);
const getRepositoriesForLoggedUser = (github) => execWithRetry(() => github.getUser().listRepos({'type': 'collaborator'}));
const getRepositoriesForOrganization = (github, organization) => execWithRetry(() => github.getOrganization(organization).getRepos());
const getRepository = (github, owner, repository) => execWithRetry(() => github.getRepo(owner, repository).getDetails());
const getTeamRepositories = (github, team) => execWithRetry(() => github.getTeam(team.id).listRepos());
const getTeams = (github, organization) => execWithRetry(() => github.getOrganization(organization).getTeams());
const getRepoStats = (github, repo) => execWithRetry(() => github.getRepo(repo.owner.login, repo.name).getContributorStats());
const writeContributorStatsToStream = (entry, repo, contributorName, stream) => {
entry.weeks.forEach(week => {
if (week.a + week.d + week.c === 0) {
return;
}
const v = {
'Repository Owner': repo.owner.login,
'Repository': repo.name,
'Repository Created On': moment(repo.created_at).format('DD-MMM-YYYY'),
'Repository Language': repo.language || 'N/A',
'Repository Size': repo.size,
'Is Private Repository': repo.private,
'Contributor': contributorName,
'Date': moment.unix(week.w).format('DD-MMM-YYYY'),
'Code Additions': week.a,
'Code Deletions': week.d,
'Code Commits': week.c
};
stream.write(v);
});
};
const writeRepoStatsToStream = async (github, repo, stream) => {
const stats = await getRepoStats(github, repo);
await Promise.each(stats, async (entry) => {
const login = entry.author.login;
const contributorName = login || 'Unknown Member';
writeContributorStatsToStream(entry, repo, contributorName, stream);
});
};
const writeStatsToStream = async (repos, github, stream) => {
await Promise.each(repos, (repo) => writeRepoStatsToStream(github, repo, stream))
.catch(err => {
logger.info(`ERROR STREAMING: ${err.message}`);
return stream.write({
__streamError: {
message: getMessage(err),
code: getErrorCode(err)
}
});
});
};
const getAuthToken = (req) => {
const accessToken = (req.headers['authorization'] || '').replace('token ', '');
if (accessToken) {
return accessToken;
}
return req.query.token;
};
const streamStats = async (github, repos, out) => {
logger.info(`Count of repos to process is ${repos.length}`);
out.type('json');
const stream = JSONStream.stringify();
stream.pipe(out);
await writeStatsToStream(repos, github, stream);
stream.end();
};
app.use(requestLog());
app.get('/favicon.ico', (req, res) => res.sendFile(path.resolve('./favicon.ico')));
app.get('/markdown.css', (req, res) => res.sendFile(path.resolve('./markdown.css')));
app.get('/', async (req, res, next) => {
try {
const indexPage = await readFile(path.resolve('./index.html'));
const readme = await readFile(path.resolve('./README.md'));
const rendered = indexPage.toString().replace('#body#', new MarkdownIt().render(readme.toString()));
res.type('html');
res.end(rendered);
} catch (e) {
logger.error(e);
next(e);
}
});
app.get('/vizydrop-status-ping', (req, res) => {
res.json({ok:true});
});
app.get('/me', async (req, res, next) => {
try {
const github = await initGitHub(req);
const repos = await getRepositoriesForLoggedUser(github);
await streamStats(github, repos, res);
} catch (err) {
next(err);
}
});
app.get('/:organization/team/:team', async (req, res, next) => {
try {
const github = await initGitHub(req);
const teams = await getTeams(github, req.params.organization);
const teamToFind = req.params.team.toLowerCase();
const team = _.find(teams, (t) => t.name.toLowerCase() === teamToFind || t.slug.toLowerCase() === teamToFind);
if (!team) {
const error = new Error(`Team '${req.params.team}' is not found in ${req.params.organization}`);
error.status = 404;
throw error;
}
const repos = await getTeamRepositories(github, team);
await streamStats(github, repos, res);
} catch (err) {
next(err);
}
});
app.get('/:organization', async (req, res, next) => {
try {
const github = await initGitHub(req);
const repos = await getRepositoriesForOrganization(github, req.params.organization);
await streamStats(github, repos, res);
} catch (err) {
next(err);
}
});
app.get('/:owner/:repository', async (req, res, next) => {
try {
const github = await initGitHub(req);
const repo = await getRepository(github, req.params.owner, req.params.repository);
await streamStats(github, [repo], res);
} catch (err) {
next(err);
}
});
app.use((err, req, res, next) => res.status(getErrorCode(err)).send({
message: getMessage(err),
code: getErrorCode(err)
}));
const port = process.env.PORT || 7770;
const server = app.listen(port, () => logger.info(`github-data-link is listening on port ${port}!`));
module.exports = () => server;