-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
action.yml
237 lines (205 loc) · 7.75 KB
/
action.yml
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
# based on:
# https://securitylab.github.com/research/github-actions-preventing-pwn-requests/
# https://github.com/thollander/actions-comment-pull-request
name: "Post PR Comment from Artifact"
description: "Post PR Comment from Artifact"
branding:
icon: edit
color: white
inputs:
GITHUB_TOKEN:
description: "Github token of the repository (automatically created by Github)"
default: ${{ github.token }}
required: true
artifact:
description: "Name of the artifact"
required: true
default: pr
outputs:
id:
description: "ID of the comment"
value: ${{ steps.comment.outputs.id }}
body:
description: "Body of the comment"
value: ${{ steps.comment.outputs.body }}
html_url:
description: "HTML URL of the comment"
value: ${{ steps.comment.outputs.html_url }}
runs:
using: "composite"
steps:
- name: "Download artifact"
uses: actions/github-script@v7
with:
script: |
var artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id,
});
var matchArtifact = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == "${{inputs.artifact}}";
})[0];
var download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip',
});
var fs = require('fs');
fs.writeFileSync('${{github.workspace}}/pr.zip', Buffer.from(download.data));
- name: "Extract artifact"
run: |
mkdir -p pr
unzip pr.zip -d pr
shell: bash
- name: "Comment on PR"
id: comment
uses: actions/github-script@v7
with:
github-token: ${{ inputs.GITHUB_TOKEN }}
script: |
const fs = require("fs");
const removeExtension = (path) => path.split(".").slice(0, -1).join(".");
const fileName = fs.readdirSync("./pr")?.[0];
if (!fileName) {
core.setFailed("No artifact found");
return;
}
const issue_number = removeExtension(fileName);
const output = fs.readFileSync(`./pr/${fileName}`, "utf8");
async function run(issue_number, message) {
const REACTIONS = ['+1', '-1', 'laugh', 'confused', 'heart', 'hooray', 'rocket', 'eyes'];
try {
const comment_tag = `<!-- livecodes/thollander/actions-comment-pull-request -->`;
const reactions = "";
const mode = "upsert";
const create_if_not_exists = true;
if (!message) {
core.setFailed('"message" should be provided as input');
return;
}
let content = message;
if (!issue_number) {
core.setFailed(
"No issue/pull request in input neither in current context.",
);
return;
}
async function addReactions(comment_id, reactions) {
const validReactions = reactions
.replace(/\s/g, "")
.split(",")
.filter((reaction) => REACTIONS.includes(reaction));
await Promise.allSettled(
validReactions.map(async (content) => {
await github.rest.reactions.createForIssueComment({
...context.repo,
comment_id,
content,
});
}),
);
}
async function createComment({ owner, repo, issue_number, body }) {
const { data: comment } = await github.rest.issues.createComment({
owner,
repo,
issue_number,
body,
});
core.setOutput("id", comment.id);
core.setOutput("body", comment.body);
core.setOutput("html_url", comment.html_url);
await addReactions(comment.id, reactions);
return comment;
}
async function updateComment({ owner, repo, comment_id, body }) {
const { data: comment } = await github.rest.issues.updateComment({
owner,
repo,
comment_id,
body,
});
core.setOutput("id", comment.id);
core.setOutput("body", comment.body);
core.setOutput("html_url", comment.html_url);
await addReactions(comment.id, reactions);
return comment;
}
async function deleteComment({ owner, repo, comment_id }) {
const { data: comment } = await github.rest.issues.deleteComment({
owner,
repo,
comment_id,
});
return comment;
}
const comment_tag_pattern = comment_tag;
const body = comment_tag_pattern
? `${content}\n${comment_tag_pattern}`
: content;
if (comment_tag_pattern) {
let comment;
for await (const { data: comments } of github.paginate.iterator(
github.rest.issues.listComments,
{
...context.repo,
issue_number,
},
)) {
comment = comments.find((comment) =>
comment?.body?.includes(comment_tag_pattern),
);
if (comment) break;
}
if (comment) {
if (mode === "upsert") {
await updateComment({
...context.repo,
comment_id: comment.id,
body,
});
return;
} else if (mode === "recreate") {
await deleteComment({
...context.repo,
comment_id: comment.id,
});
await createComment({
...context.repo,
issue_number,
body,
});
return;
} else if (mode === "delete") {
core.debug("Registering this comment to be deleted.");
} else {
core.setFailed(
`Mode ${mode} is unknown. Please use 'upsert', 'recreate' or 'delete'.`,
);
return;
}
} else if (create_if_not_exists) {
core.info(
"No comment has been found with asked pattern. Creating a new comment.",
);
} else {
core.info(
"Not creating comment as the pattern has not been found. Use `create_if_not_exists: true` to create a new comment anyway.",
);
return;
}
}
await createComment({
...context.repo,
issue_number,
body,
});
} catch (error) {
if (error instanceof Error) {
core.setFailed(error.message);
}
}
}
run(issue_number, output);