forked from ssskram/now-deploy-preview-comment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (54 loc) · 1.73 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
63
64
65
const axios = require('axios');
const { stripIndents } = require('common-tags');
const { Toolkit } = require('actions-toolkit');
// Run your GitHub Action!
Toolkit.run(async tools => {
const actionConfig = {
zeitToken: process.env.ZEIT_TOKEN,
teamId: process.env.ZEIT_TEAMID,
projectName: process.env.PROJECT_NAME,
};
if (!actionConfig.zeitToken) {
throw new Error(`ZEIT_TOKEN environment variable is not set`);
}
const zeitAPIClient = axios.create({
baseURL: 'https://api.zeit.co',
headers: { Authorization: `Bearer ${actionConfig.zeitToken}` },
});
function fetchLastDeployment() {
return zeitAPIClient
.get(`/v5/now/deployments?teamId=${actionConfig.teamId}`)
.then(({ data }) => {
return data.deployments.filter(
d => d.name === actionConfig.projectName
)[0];
});
}
const deployment = await fetchLastDeployment();
const { data: comments } = await tools.github.issues.listComments({
...tools.context.repo,
issue_number: tools.context.payload.pull_request.number,
});
const commentFirstSentence = `**${deployment.name}** ready!`;
const zeitPreviewURLComment = comments.find(comment =>
comment.body.startsWith(commentFirstSentence)
);
const commentBody = stripIndents`
${commentFirstSentence}
commit ${deployment.meta.commit}
https://${deployment.url}
`;
if (zeitPreviewURLComment) {
await tools.github.issues.updateComment({
...tools.context.repo,
comment_id: zeitPreviewURLComment.id,
body: commentBody,
});
} else {
await tools.github.issues.createComment({
...tools.context.repo,
issue_number: tools.context.payload.pull_request.number,
body: commentBody,
});
}
});