/
post-comment.js
executable file
·61 lines (49 loc) · 1.67 KB
/
post-comment.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
#!/usr/bin/env node
const path = require('path');
const fetch = require('node-fetch');
const authToken = process.env.GH_AUTH_TOKEN;
if (!authToken) {
console.log('Could not find auth token.');
process.exit(0);
}
const prNumber = path.basename(
process.env.CIRCLE_PULL_REQUEST || process.env.CI_PULL_REQUEST || ''
);
if (!prNumber) {
console.log('No pull request number found');
process.exit(0);
}
const headline = '### Thank you for your contribution! ❤️';
postComment();
async function postComment() {
const existingId = await findExistingComment();
console.log(existingId ? `Update comment ${existingId}` : 'Create new comment.');
const installPath = await getInstallPath();
const path = existingId ? `issues/comments/${existingId}` : `issues/${prNumber}/comments`;
const method = existingId ? 'PATCH' : 'POST';
await fetch(getApiUrl(path), {
method,
body: JSON.stringify({
body: `${headline}
You can try out this pull request locally by installing Rollup via
\`\`\`
npm install ${installPath}
\`\`\`
or load it into the REPL:
https://rollupjs.org/repl/?circleci=${process.env.CIRCLE_BUILD_NUM}
`
})
});
}
async function findExistingComment() {
const comments = await (await fetch(getApiUrl(`issues/${prNumber}/comments`), {})).json();
const existingComment = comments.find(comment => comment.body.startsWith(headline));
return existingComment && existingComment.id;
}
async function getInstallPath() {
const prInfo = await (await fetch(getApiUrl(`pulls/${prNumber}`), {})).json();
return `${prInfo.head.repo.full_name}#${prInfo.head.ref}`;
}
function getApiUrl(path) {
return `https://${authToken}:x-oauth-basic@api.github.com/repos/rollup/rollup/${path}`;
}