-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathissues.js
104 lines (87 loc) · 2.73 KB
/
issues.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
/*
* This script assumes permissions a GitHub access token available as an environment variable
*/
const https = require('https');
const path = require('path');
const fs = require('fs');
const host = 'api.github.com';
const isProduction = process.env.NODE_ENV === 'production';
const isCI = process.env.NODE_ENV === 'ci';
const ACCESS_TOKEN = process.env.ACCESS_TOKEN;
const outputFile = 'issues.json';
const tmpDir = path.join(__dirname, '..', 'tmp');
const headers = {
'Accept': 'application/vnd.github.v3+json',
'Authorization': `token ${ACCESS_TOKEN}`,
'User-Agent': 'Contributary.community' // https://developer.github.com/v3/#user-agent-required
};
// expose handler for Lambda
exports.run = run;
if (!isProduction) {
const mockEvent = {
queryStringParameters: {
projectName: 'contributarycommunity',
repoName: 'www.contributary.community',
labelFilter: 'good+first+issue'
}
};
run(mockEvent);
}
function writeToFilesystem(response) {
const filePath = `${tmpDir}/${outputFile}`;
fs.writeFileSync(`${filePath}`, JSON.stringify(response, null, 2), (err) => {
if (err) {
return console.error(err); // eslint-disable-line no-console
}
console.log(`File ${filePath} was saved!`); // eslint-disable-line no-console
});
}
function handleIssuesResponse(response) {
console.log('handleIssuesResponse.isProduction', isProduction); // eslint-disable-line no-console
if (isProduction) {
return {
statusCode: 200,
headers: {},
body: JSON.stringify(response),
isBase64Encoded: false
};
} else if (isCI) {
return {};
} else {
writeToFilesystem(response);
}
}
// https://developer.github.com/v3/issues/
// application/vnd.github.symmetra-preview+json
function getIssues(projectName, repositoryName, labelFilter) {
const midFix = `${projectName}/${repositoryName}`;
const labelFix = labelFilter ? `?labels=${labelFilter}` : '';
const options = {
host,
path: `/repos/${midFix}/issues${labelFix}`,
headers
};
console.log(`GET issues for ${midFix}`); // eslint-disable-line no-console
console.log('GET options', options); // eslint-disable-line no-console
return new Promise((resolve, reject) => {
https.get(options, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
resolve(JSON.parse(data));
});
}).on('error', (err) => {
reject(err);
});
});
}
function run(event = {}) {
const { projectName, repoName, labelFilter } = event.queryStringParameters;
return getIssues(projectName, repoName, labelFilter)
.then(handleIssuesResponse)
.catch((error) => {
console.error(error); // eslint-disable-line no-console
});
}