-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
103 lines (93 loc) · 3.14 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
const github = require("@actions/github");
const core = require("@actions/core");
const parse = require("shell-quote/parse");
const { spawnSync } = require("node:child_process");
const path = require("node:path");
const sanitize = require("path-sanitizer");
// most @actions toolkit packages have async methods
async function run() {
const cmd = core.getInput("command");
const workingDirectory = core.getInput("working-dir");
const messageTemplate = core.getInput("template");
const updateText = core.getInput("update-text");
const githubToken = core.getInput("github-token");
const octokit = github.getOctokit(githubToken);
const context = github.context;
if (!context.payload.pull_request) {
core.error("This action only runs on Pull Requests");
return;
}
var comment = null;
var message = "";
// If we have text to check, we will search the PR for a comment with this text
if (updateText) {
try {
for await (const response of octokit.paginate.iterator(
octokit.rest.issues.listComments,
{
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
},
)) {
comment = response.data.find((comment) =>
comment.body.includes(updateText),
);
if (comment) break;
}
} catch (_) {
// Unable to find the comment, or an error occurred
// Continue and make a new comment
core.info("No comment found, will create a new one");
}
}
// Next, we execute the user's command
const splitCmd = parse(cmd);
const cwd =
workingDirectory === ""
? process.cwd()
: path.join(process.cwd(), sanitize(workingDirectory));
core.debug(
`Input working dir: ${workingDirectory}. Final working dir: ${cwd}`,
);
const proc = spawnSync(splitCmd[0], splitCmd.slice(1), { cwd });
const cmdOut = proc.stdout.toString();
const cmdErr = proc.stderr.toString();
if (proc.status === null || proc.status !== 0) {
core.error(`Command failed: ${cmd}\nstdout:${cmdOut}\nstderr:${cmdErr}`);
}
// If there's a template, we replace %command% with the output of the command
// Otherwise, we return a code block with the command output
if (messageTemplate) {
message = messageTemplate.replace("%command%", cmdOut);
message = message.replace("%stdout%", cmdOut);
message = message.replace("%stderr%", cmdErr);
} else {
message = `\n\`\`\`${cmdOut}\n\`\`\`<details>\n<summary>stderr</summary>\n\`\`\`${cmdErr}\`\`\`\n</details>`;
}
console.log(message);
// If we found a comment, we update it
// Otherwise we create a new comment
try {
if (comment) {
await octokit.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: comment.id,
body: message,
});
} else {
await octokit.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: message,
});
}
} catch (error) {
core.setFailed(
`Error with making comment - ${error.name}: ${error.message}`,
);
}
}
run();