Skip to content

Comment

Comment #113

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@v7
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 iosScreenshots = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == "iOS Screenshots"
})[0];
var downloadIosScreenshots = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: iosScreenshots.id,
archive_format: 'zip',
});
fs.writeFileSync('${{github.workspace}}/iosScreenshots.zip', Buffer.from(downloadIosScreenshots.data));
- name: Unzip Artifacts
shell: bash
run: |
unzip pr.zip
mkdir screenshots
unzip androidScreenshots.zip -d screenshots/
unzip iosScreenshots.zip -d screenshots/
- name: Fetch PR Number
id: fetch-pr-number
uses: actions/github-script@v7
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@v7
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 = `
<table>
<tr>
<td><img src="https://github.com/fossasia/badgemagic-app/blob/pr-screenshots/${issue_number}_Pixel_6-1_home_screen.png" width="1080"/></td>
<td><img src="https://github.com/fossasia/badgemagic-app/blob/pr-screenshots/${issue_number}_Pixel_6-2_text_badge.png" width="1080"/></td>
<td><img src="https://github.com/fossasia/badgemagic-app/blob/pr-screenshots/${issue_number}_Pixel_6-3_emoji_badge.png" width="1080"/></td>
<td><img src="https://github.com/fossasia/badgemagic-app/blob/pr-screenshots/${issue_number}_Pixel_6-4_inverted_emoji_badge.png" 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" width="1080"/></td>
<td><img src="https://github.com/fossasia/badgemagic-app/blob/pr-screenshots/${issue_number}_Pixel_6-6_saved_badges_clicked.png" 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" width="2146"/>
</td>
</tr>
</table>
`;
var iPhoneScreenshots = `
<table>
<tr>
<td><img src="https://github.com/fossasia/badgemagic-app/blob/pr-screenshots/${issue_number}_iPhone_16_Pro_Max-1_home_screen.png" 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" 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" 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" 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" 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" 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" width="2146"/>
</td>
</tr>
</table>
`;
var iPadScreenshots = `
<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" 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" 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" 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" 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" 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" 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" width="2146"/>
</td>
</tr>
</table>
`;
const body = `
## Build Status
${statusText}
## Screenshots (Android)
${androidScreenshots}
## Screenshots (iPhone)
${iPhoneScreenshots}
## Screenshots (iPad)
${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@v7
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 (Android)
_Not able to fetch screenshots._
## Screenshots (iPhone)
_Not able to fetch screenshots._
## Screenshots (iPad)
_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
});
}