-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
108 lines (85 loc) · 2.62 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
const core = require("@actions/core");
const github = require("@actions/github");
const { Octokit } = require("@octokit/action");
const exec = require("@actions/exec");
const { spawnSync } = require("child_process");
(async () => {
try {
const prettierCommand = core.getInput("prettier_command");
const pullRequestNumber = github.context.payload.issue.number;
const commentBody = github.context.payload.comment.body;
// /p とか /pre とか /prettier とか /pretier (つづりミス) などにマッチ
const commandRegexp = new RegExp("^/p(ret?t?i?e?r?)?$");
if (!commentBody || !commandRegexp.test(commentBody.trim())) {
console.log("skip.");
return;
}
const octokit = new Octokit();
const owner = github.context.repo.owner;
const repo = github.context.repo.repo;
octokit.reactions.createForIssueComment({
owner,
repo,
comment_id: github.context.payload.comment.id,
content: "+1",
});
const { data } = await octokit.pulls.get({
owner,
repo,
pull_number: pullRequestNumber,
});
const branch = data.head.ref;
await exec.exec(`git fetch origin ${branch} --depth 1`);
await exec.exec(`git switch ${branch}`);
// todo: paginate
const { data: fileList } = await octokit.pulls.listFiles({
owner,
repo,
pull_number: pullRequestNumber,
});
const files = fileList.map((s) => s.filename);
console.log("target files:", files);
// todo: inputs.glob
const command = `${prettierCommand} ${files.join(" ")}`;
console.log("run command:", command);
// actions/exec のほうが良いかも
const { status, error, stdout, stderr } = spawnSync(command, {
shell: true,
});
// 処理できなかったファイルとかあると止まる
// if (status !== 0 || status !== 1) {
// if (error) {
// throw error
// }
// }
if (error) {
console.error(error);
}
const cmdOutput = stdout.toString();
console.log(cmdOutput);
// create PR comment
const body = `prettier executed. (exit with status ${status})\n\n${cmdOutput}`;
await octokit.pulls.createReview({
owner,
repo,
pull_number: pullRequestNumber,
body: body,
event: "COMMENT",
});
if (stderr) {
console.log("stderr", stderr);
}
if (error) {
const body = `error: ${error}`;
await octokit.pulls.createReview({
owner,
repo,
pull_number: pullRequestNumber,
body: body,
event: "COMMENT",
});
}
} catch (error) {
core.setFailed(error.message);
}
})();