This repository has been archived by the owner on Dec 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
maintainers.js
124 lines (101 loc) · 3.46 KB
/
maintainers.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const express = require('express');
const GitHubApi = require('@octokit/rest');
const winston = require('winston');
const logger = winston.createLogger({
level: 'debug',
transports: [
new winston.transports.File({ filename: 'combined.log' })
]
});
// if no datafile parameter was supplied, bail immediately
function uploadPreconditionsCheck(req, res, next) {
if (!process.env.GITHUB_ACCESS_TOKEN) {
res.status(500).type('application/json').send({
error: {
code: 500,
message: 'GITHUB_ACCESS_TOKEN not defined in process environment'
}
});
} else {
return next();
}
}
// retrieve sources (files or directories) on a path
function getCommits(req, res, next) {
const github = new GitHubApi();
// github rate-limits unauthenticated requests, so authenticate since
// this functionality can make many requests
github.authenticate({
type: 'oauth',
token: process.env.GITHUB_ACCESS_TOKEN
});
// get all commits for a source
github.repos.getCommits({
owner: 'openaddresses',
repo: 'openaddresses',
path: req.baseUrl.replace('/maintainers', '')
}, (err, response) => {
if (err) {
logger.error(`Error getting contents: ${err}`);
res.status(400).type('application/json').send({
error: {
code: 400,
message: `Error getting commits: ${err}`
}
});
} else {
const expectedNumberOfCommits = response.data.length;
const commits = [];
if (response.data.length === 0) {
res.status(200).type('application/json').send([]);
return;
}
// call getContent on each commit
response.data.forEach(commit => {
github.repos.getContent({
owner: 'openaddresses',
repo: 'openaddresses',
path: req.baseUrl.replace('/maintainers', ''),
ref: commit.sha
}, (err, content) => {
if (err) {
// an error occurred for a getContent call so return an error
logger.error(`Error getting contents: ${err}`);
res.status(400).type('application/json').send({
error: {
code: 400,
message: `Error getting contents: ${err}`
}
});
} else {
// parse the file, fixing bad regex escaping
const parsed = JSON.parse(
new Buffer(content.data.content, 'base64').
toString('utf8').
// this fixes an odd number of backslashes to an even number
// eg: this file contains single backslashes:
// https://github.com/openaddresses/openaddresses/blob/038592684059f70382575032c5591337313c2b90/sources/us/va/james_city.json
replace(/\\(\\\\)*/g, '\\\\$1'));
// record appropriate metadata
commits.push({
sha: commit.sha,
email: parsed.email,
lastModified: content.meta['last-modified']
});
// if all commits have been retrieved, sort by date and return them
if (commits.length === expectedNumberOfCommits) {
// sort because async means they're unordered
commits.sort((a, b) => new Date(b.lastModified) < new Date(a.lastModified));
res.status(200).type('application/json').send(commits);
}
}
});
});
}
});
}
module.exports = express.Router()
.get('/',
uploadPreconditionsCheck,
getCommits
);