forked from fossasia/badgemagic-app
-
Notifications
You must be signed in to change notification settings - Fork 0
284 lines (253 loc) · 12.1 KB
/
pull_request_comment.yml
File metadata and controls
284 lines (253 loc) · 12.1 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
name: Comment
on:
workflow_run:
workflows: [ Badge Magic PR CI ]
types:
- completed
jobs:
comment:
runs-on: ubuntu-latest
if: >
github.event.workflow_run.event == 'pull_request'
steps:
- name: Download artifacts
id: download-artifacts
uses: actions/github-script@v8
with:
script: |
var artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{github.event.workflow_run.id }},
});
var pr = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == "pr"
})[0];
var download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: pr.id,
archive_format: 'zip',
});
var fs = require('fs');
fs.writeFileSync('${{github.workspace}}/pr.zip', Buffer.from(download.data));
var androidScreenshots = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == "Android Screenshots"
})[0];
var downloadAndroidScreenshots = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: androidScreenshots.id,
archive_format: 'zip',
});
fs.writeFileSync('${{github.workspace}}/androidScreenshots.zip', Buffer.from(downloadAndroidScreenshots.data));
var iPadScreenshots = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == "iPad Screenshots"
})[0];
var downloadiPadScreenshots = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: iPadScreenshots.id,
archive_format: 'zip',
});
fs.writeFileSync('${{github.workspace}}/iPadScreenshots.zip', Buffer.from(downloadiPadScreenshots.data));
var iPhoneScreenshots = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == "iPhone Screenshots"
})[0];
var downloadiPhoneScreenshots = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: iPhoneScreenshots.id,
archive_format: 'zip',
});
fs.writeFileSync('${{github.workspace}}/iPhoneScreenshots.zip', Buffer.from(downloadiPhoneScreenshots.data));
- name: Unzip Artifacts
shell: bash
run: |
unzip pr.zip
mkdir screenshots
unzip androidScreenshots.zip -d screenshots/
unzip iPadScreenshots.zip -d screenshots/
unzip iPhoneScreenshots.zip -d screenshots/
- name: Fetch PR Number
id: fetch-pr-number
uses: actions/github-script@v8
with:
script: |
var fs = require('fs')
var issue_number = Number(fs.readFileSync('./NR'));
core.setOutput("pr_number", issue_number);
- name: Update PR Screenshots
shell: bash
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
git clone --branch=pr-screenshots --depth=1 https://${{ github.repository_owner }}:${{ github.token }}@github.com/${{ github.repository }} pr-screenshots
cd pr-screenshots
for file in ../screenshots/*; do
if [[ -f "$file" ]]; then
filename=$(basename "$file")
cp -f "$file" "./${{ steps.fetch-pr-number.outputs.pr_number }}_${filename}"
fi
done
# Force push to pr-screenshots branch
git checkout --orphan temporary
git add --all .
git commit -am "[Auto] Update screenshots for #${{ steps.fetch-pr-number.outputs.pr_number }} ($(date +%Y-%m-%d.%H:%M:%S))"
git branch -D pr-screenshots
git branch -m pr-screenshots
git push --force origin pr-screenshots
- name: Build success
if: ${{ github.event.workflow_run.conclusion == 'success' }}
uses: actions/github-script@v8
with:
script: |
var issue_number = ${{ steps.fetch-pr-number.outputs.pr_number }};
const owner = context.repo.owner;
const repo = context.repo.repo;
var artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner,
repo,
run_id: ${{github.event.workflow_run.id }},
});
var matchArtifact = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == "apk-files"
})[0];
const artifact_url = `https://github.com/${owner}/${repo}/actions/runs/${{ github.event.workflow_run.id }}/artifacts/${matchArtifact.id}`;
const comments = await github.rest.issues.listComments({
owner,
repo,
issue_number
});
let comment_id;
for (const comment of comments.data) {
if (comment.user.login === 'github-actions[bot]') {
comment_id = comment.id;
break;
}
}
var statusText = `Build successful. APKs to test: ${artifact_url}.`;
var androidScreenshots = `
<details>
<summary>Android Screenshots</summary>
<table>
<tr>
<td><img src="https://github.com/fossasia/badgemagic-app/blob/pr-screenshots/${issue_number}_Pixel_6-1_home_screen.png?raw=true" width="1080"/></td>
<td><img src="https://github.com/fossasia/badgemagic-app/blob/pr-screenshots/${issue_number}_Pixel_6-2_text_badge.png?raw=true" width="1080"/></td>
<td><img src="https://github.com/fossasia/badgemagic-app/blob/pr-screenshots/${issue_number}_Pixel_6-3_emoji_badge.png?raw=true" width="1080"/></td>
<td><img src="https://github.com/fossasia/badgemagic-app/blob/pr-screenshots/${issue_number}_Pixel_6-4_inverted_emoji_badge.png?raw=true" width="1080"/></td>
</tr>
<tr>
<td><img src="https://github.com/fossasia/badgemagic-app/blob/pr-screenshots/${issue_number}_Pixel_6-5_saved_badges.png?raw=true" width="1080"/></td>
<td><img src="https://github.com/fossasia/badgemagic-app/blob/pr-screenshots/${issue_number}_Pixel_6-6_saved_badges_clicked.png?raw=true" width="1080"/></td>
<td colspan="2">
<img src="https://github.com/fossasia/badgemagic-app/blob/pr-screenshots/${issue_number}_Pixel_6-7_draw_badge.png?raw=true" width="2146"/>
</td>
</tr>
</table>
</details>
`;
var iPhoneScreenshots = `
<details>
<summary>iPhone Screenshots</summary>
<table>
<tr>
<td><img src="https://github.com/fossasia/badgemagic-app/blob/pr-screenshots/${issue_number}_iPhone_16_Pro_Max-1_home_screen.png?raw=true" width="1080"/></td>
<td><img src="https://github.com/fossasia/badgemagic-app/blob/pr-screenshots/${issue_number}_iPhone_16_Pro_Max-2_text_badge.png?raw=true" width="1080"/></td>
<td><img src="https://github.com/fossasia/badgemagic-app/blob/pr-screenshots/${issue_number}_iPhone_16_Pro_Max-3_emoji_badge.png?raw=true" width="1080"/></td>
<td><img src="https://github.com/fossasia/badgemagic-app/blob/pr-screenshots/${issue_number}_iPhone_16_Pro_Max-4_inverted_emoji_badge.png?raw=true" width="1080"/></td>
</tr>
<tr>
<td><img src="https://github.com/fossasia/badgemagic-app/blob/pr-screenshots/${issue_number}_iPhone_16_Pro_Max-5_saved_badges.png?raw=true" width="1080"/></td>
<td><img src="https://github.com/fossasia/badgemagic-app/blob/pr-screenshots/${issue_number}_iPhone_16_Pro_Max-6_saved_badges_clicked.png?raw=true" width="1080"/></td>
<td colspan="2">
<img src="https://github.com/fossasia/badgemagic-app/blob/pr-screenshots/${issue_number}_iPhone_16_Pro_Max-7_draw_badge.png?raw=true" width="2146"/>
</td>
</tr>
</table>
</details>
`;
var iPadScreenshots = `
<details>
<summary>iPad Screenshots</summary>
<table>
<tr>
<td><img src="https://github.com/fossasia/badgemagic-app/blob/pr-screenshots/${issue_number}_iPad_Pro_13-inch_(M4)-1_home_screen.png?raw=true" width="1080"/></td>
<td><img src="https://github.com/fossasia/badgemagic-app/blob/pr-screenshots/${issue_number}_iPad_Pro_13-inch_(M4)-2_text_badge.png?raw=true" width="1080"/></td>
<td><img src="https://github.com/fossasia/badgemagic-app/blob/pr-screenshots/${issue_number}_iPad_Pro_13-inch_(M4)-3_emoji_badge.png?raw=true" width="1080"/></td>
<td><img src="https://github.com/fossasia/badgemagic-app/blob/pr-screenshots/${issue_number}_iPad_Pro_13-inch_(M4)-4_inverted_emoji_badge.png?raw=true" width="1080"/></td>
</tr>
<tr>
<td><img src="https://github.com/fossasia/badgemagic-app/blob/pr-screenshots/${issue_number}_iPad_Pro_13-inch_(M4)-5_saved_badges.png?raw=true" width="1080"/></td>
<td><img src="https://github.com/fossasia/badgemagic-app/blob/pr-screenshots/${issue_number}_iPad_Pro_13-inch_(M4)-6_saved_badges_clicked.png?raw=true" width="1080"/></td>
<td colspan="2">
<img src="https://github.com/fossasia/badgemagic-app/blob/pr-screenshots/${issue_number}_iPad_Pro_13-inch_(M4)-7_draw_badge.png?raw=true" width="2146"/>
</td>
</tr>
</table>
</details>
`;
const body = `
## Build Status
${statusText}
## Screenshots
${androidScreenshots}
${iPhoneScreenshots}
${iPadScreenshots}
`;
if (comment_id) {
await github.rest.issues.updateComment({
owner,
repo,
comment_id,
body
});
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number,
body
});
}
- name: Build failed
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
uses: actions/github-script@v8
with:
script: |
var issue_number = ${{ steps.fetch-pr-number.outputs.pr_number }};
const owner = context.repo.owner;
const repo = context.repo.repo;
const comments = await github.rest.issues.listComments({
owner,
repo,
issue_number
});
let comment_id;
for (const comment of comments.data) {
if (comment.user.login === 'github-actions[bot]') {
comment_id = comment.id;
break;
}
}
const body = `
## Build Status
_Build workflow failed. Please check the logs for more information._
## Screenshots
_Not able to fetch screenshots._
`;
if (comment_id) {
await github.rest.issues.updateComment({
owner,
repo,
comment_id,
body
});
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number,
body
});
}