-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.js
82 lines (68 loc) · 2.2 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const axios = require('axios');
const { stripIndents } = require('common-tags');
const { Toolkit } = require('actions-toolkit');
const actionConfig = {
zeitToken: process.env.ZEIT_TOKEN,
teamId: process.env.ZEIT_TEAMID,
deployedCommit: process.env.GITHUB_SHA,
deployedBranch: process.env.GITHUB_REF,
};
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}` },
params: { teamId: actionConfig.teamId },
});
// Run your GitHub Action!
Toolkit.run(async tools => {
function fetchLastDeployment(params) {
return zeitAPIClient
.get('/v4/now/deployments', { params })
.then(({ data }) => data.deployments[0]);
}
const strategies = [
fetchLastDeployment({ 'meta-commit': actionConfig.deployedCommit }),
fetchLastDeployment({ 'meta-branch': actionConfig.deployedBranch }),
fetchLastDeployment({ limit: 1 }),
];
let deploymentUrl;
let deploymentCommit;
let deploymentProjectName;
for (const strategy of strategies) {
const deployment = await strategy();
if (deployment) {
deploymentProjectName = deployment.name;
deploymentUrl = deployment.url;
deploymentCommit = deployment.meta.commit;
break;
}
}
const { data: comments } = await tools.github.issues.listComments({
...tools.context.repo,
issue_number: tools.context.payload.pull_request.number,
});
const commentFirstSentence = `Deploy preview for _${deploymentProjectName}_ ready!`;
const zeitPreviewURLComment = comments.find(comment =>
comment.body.startsWith(commentFirstSentence)
);
const commentBody = stripIndents`
${commentFirstSentence}
Built with commit ${deploymentCommit}
https://${deploymentUrl}
`;
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,
});
}
});