-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
62 lines (56 loc) · 1.55 KB
/
index.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
const nodeFetch = require('node-fetch');
const stripAnsi = require('strip-ansi');
const getTestReport = require('./src/getTestReport');
const getEnvVariables = require('./src/getEnvVariables');
function comment(filepath) {
const envVars = getEnvVariables();
const { username, repoName, prNumber, apiKey } = envVars;
if (!prNumber) {
console.log('test report not uploaded to github; PR was not detected');
return;
}
if (!username || !repoName || !apiKey) {
const undefinedVars = [];
if (!username) {
undefinedVars.push('Username');
}
if (!repoName) {
undefinedVars.push('Repo Name');
}
if (!apiKey) {
undefinedVars.push('GitHub API Key');
}
throw `${undefinedVars.join(', ')} env variables must not be undefined`;
}
const body = stripAnsi(getTestReport(filepath));
const request = {
method: 'POST',
body: JSON.stringify({ body }),
headers: {
'Content-Type': 'application/json',
Accept: 'application/vnd.github.v3+json',
Authorization: `token ${apiKey}`,
'User-Agent': repoName
}
};
console.log(
`https://api.github.com/repos/${username}/${repoName}/issues/${prNumber}/comments`
);
return nodeFetch(
`https://api.github.com/repos/${username}/${repoName}/issues/${prNumber}/comments`,
request
).then(res => {
if (res.status === 201) {
return body;
} else {
throw 'Error posting GitHub request';
}
});
}
function getReport(filepath) {
return stripAnsi(getTestReport(filepath));
}
module.exports = {
comment,
getReport
};