forked from iam4x/now-deploy-preview-comment
-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
272 lines (241 loc) · 6.85 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
const { stripIndents } = require("common-tags");
const core = require("@actions/core");
const github = require("@actions/github");
const { execSync } = require("child_process");
const exec = require("@actions/exec");
const context = github.context;
const zeitToken = core.getInput("zeit-token");
const nowArgs = core.getInput("now-args");
const githubToken = core.getInput("github-token");
const githubComment = core.getInput("github-comment") === "true";
const workingDirectory = core.getInput("working-directory");
const nowOrgId = core.getInput("now-org-id");
const nowProjectId = core.getInput("now-project-id");
let octokit;
if (githubToken) {
octokit = new github.GitHub(githubToken);
}
async function run() {
core.debug(`action : ${context.action}`);
core.debug(`ref : ${context.ref}`);
core.debug(`eventName : ${context.eventName}`);
core.debug(`actor : ${context.actor}`);
core.debug(`sha : ${context.sha}`);
core.debug(`workflow : ${context.workflow}`);
let ref = context.ref;
let sha = context.sha;
await setEnv();
let commit = execSync("git log -1 --pretty=format:%B")
.toString()
.trim();
if (github.context.eventName === 'push') {
const pushPayload = github.context.payload;
core.debug(`The head commit is: ${pushPayload.head_commit}`);
} else if ( github.context.eventName === 'pull_request') {
const pullRequestPayload = github.context.payload;
core.debug(`head : ${pullRequestPayload.pull_request.head}`);
ref = pullRequestPayload.pull_request.head.ref;
sha = pullRequestPayload.pull_request.head.sha;
core.debug(`The head ref is: ${pullRequestPayload.pull_request.head.ref}`);
core.debug(`The head sha is: ${pullRequestPayload.pull_request.head.sha}`);
if ( octokit ) {
const { data: commitData } = await octokit.git.getCommit({
...context.repo, commit_sha: sha
});
commit = commitData.message;
core.debug(`The head commit is: ${commit}`);
}
}
const deploymentUrl = await nowDeploy(ref, commit);
if (deploymentUrl) {
core.info("set preview-url output");
core.setOutput("preview-url", deploymentUrl);
} else {
core.warning("get preview-url error");
}
const deploymentName = await nowInspect(deploymentUrl);
if (deploymentName) {
core.info("set preview-name output");
core.setOutput("preview-name", deploymentName);
} else {
core.warning("get preview-name error");
}
if (githubComment && githubToken) {
if (context.issue.number) {
core.info("this is related issue or pull_request ");
await createCommentOnPullRequest(sha, deploymentUrl, deploymentName);
} else if (context.eventName === "push") {
core.info("this is push event");
await createCommentOnCommit(sha, deploymentUrl, deploymentName);
}
} else {
core.info("comment : disabled");
}
}
async function setEnv() {
core.info("set environment for now cli 17+");
if (nowOrgId) {
core.info("set env variable : NOW_ORG_ID");
core.exportVariable("NOW_ORG_ID", nowOrgId);
}
if (nowProjectId) {
core.info("set env variable : NOW_PROJECT_ID");
core.exportVariable("NOW_PROJECT_ID", nowProjectId);
}
}
async function nowDeploy(ref, commit) {
let myOutput = "";
let myError = "";
const options = {};
options.listeners = {
stdout: data => {
myOutput += data.toString();
core.info(data.toString());
},
stderr: data => {
myError += data.toString();
core.info(data.toString());
}
};
if (workingDirectory) {
options.cwd = workingDirectory;
}
await exec.exec(
"npx",
[
"now",
...nowArgs.split(/ +/),
"-t",
zeitToken,
"-m",
`githubCommitSha=${context.sha}`,
"-m",
`githubCommitAuthorName=${context.actor}`,
"-m",
`githubCommitAuthorLogin=${context.actor}`,
"-m",
"githubDeployment=1",
"-m",
`githubOrg=${context.repo.owner}`,
"-m",
`githubRepo=${context.repo.repo}`,
"-m",
`githubCommitOrg=${context.repo.owner}`,
"-m",
`githubCommitRepo=${context.repo.repo}`,
"-m",
`githubCommitMessage=${commit}`,
"-m",
`githubCommitRef=${ref}`
],
options
);
return myOutput;
}
async function nowInspect(deploymentUrl) {
let myOutput = "";
let myError = "";
const options = {};
options.listeners = {
stdout: data => {
myOutput += data.toString();
core.info(data.toString());
},
stderr: data => {
myError += data.toString();
core.info(data.toString());
}
};
if (workingDirectory) {
options.cwd = workingDirectory;
}
await exec.exec(
"npx",
[
"now",
"inspect",
deploymentUrl,
"-t",
zeitToken,
],
options
);
const match = myError.match(/^\s+name\s+(.+)$/m);
return match && match.length ? match[1] : null;
}
async function findPreviousComment(text) {
if (!octokit) {
return null;
}
core.info("find comment");
const { data: comments } = await octokit.repos.listCommentsForCommit({
...context.repo,
commit_sha: context.sha
});
const zeitPreviewURLComment = comments.find(comment =>
comment.body.startsWith(text)
);
if (zeitPreviewURLComment) {
core.info("previous comment found");
return zeitPreviewURLComment.id;
} else {
core.info("previous comment not found");
return null;
}
}
async function createCommentOnCommit(deploymentCommit, deploymentUrl, deploymentName) {
if (!octokit) {
return;
}
const commentId = await findPreviousComment(
`Deploy preview for _${deploymentName}_ ready!`
);
const commentBody = stripIndents`
Deploy preview for _${deploymentName}_ ready!
Built with commit ${deploymentCommit}
https://${deploymentUrl}
`;
if (commentId) {
await octokit.repos.updateCommitComment({
...context.repo,
comment_id: commentId,
body: commentBody
});
} else {
await octokit.repos.createCommitComment({
...context.repo,
commit_sha: context.sha,
body: commentBody
});
}
}
async function createCommentOnPullRequest(deploymentCommit, deploymentUrl, deploymentName) {
if (!octokit) {
return;
}
const commentId = await findPreviousComment(
`Deploy preview for _${deploymentName}_ ready!`
);
const commentBody = stripIndents`
Deploy preview for _${deploymentName}_ ready!
Built with commit ${deploymentCommit}
✅ Preview: ${deploymentUrl}
This pull request is being automatically deployed with [now-deployment](https://github.com/amondnet/now-deployment)
`;
if (commentId) {
await octokit.issues.updateComment({
...context.repo,
comment_id: commentId,
body: commentBody
});
} else {
await octokit.issues.createComment({
...context.repo,
issue_number: context.issue.number,
body: commentBody
});
}
}
run().catch(error => {
core.setFailed(error.message);
});