Skip to content

PR build artifact comment #48

PR build artifact comment

PR build artifact comment #48

Workflow file for this run

name: PR build artifact comment
on:
workflow_run:
workflows: ["PR handler"]
types: [completed]
permissions:
pull-requests: write
jobs:
comment:
name: Comment on PR build result
if: github.event.workflow_run.event == 'pull_request'
runs-on: ubuntu-latest
steps:
- name: Add or update artifact links
uses: actions/github-script@v9
with:
script: |
const run = context.payload.workflow_run;
let pullNumber = run.pull_requests[0]?.number;
if (!pullNumber) {
const head = `${run.head_repository.owner.login}:${run.head_branch}`;
const candidates = await github.paginate(github.rest.pulls.list, {
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
head,
per_page: 100,
});
pullNumber = candidates.find((pull) => pull.head.sha === run.head_sha)?.number;
}
if (!pullNumber) {
core.setFailed(`Could not find the pull request for workflow run ${run.id}`);
return;
}
const { data: pull } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pullNumber,
});
if (pull.head.sha !== run.head_sha) {
core.notice(`Skipping superseded workflow run ${run.id}`);
return;
}
const marker = '<!-- chameleon-ultra-pr-build-artifacts -->';
const lines = [marker, `# Build result for commit ${run.head_sha}`, ''];
if (run.conclusion === 'success') {
const baseUrl = `https://nightly.link/${context.repo.owner}/${context.repo.repo}/actions/runs/${run.id}`;
lines.push(
'## Firmware',
'',
`- [Ultra APP DFU Package](${baseUrl}/ultra-dfu-app.zip)`,
`- [Ultra binaries](${baseUrl}/ultra-firmware.zip)`,
`- [Lite APP DFU Package](${baseUrl}/lite-dfu-app.zip)`,
`- [Lite binaries](${baseUrl}/lite-firmware.zip)`,
'',
'## Client',
'',
`- [Linux](${baseUrl}/client-linux.zip)`,
`- [macOS](${baseUrl}/client-macos.zip)`,
`- [Windows](${baseUrl}/client-windows.zip)`,
);
} else {
lines.push(
`**Status:** ${run.conclusion ?? 'unknown'}`,
'',
`[View workflow run](${run.html_url})`,
);
}
const body = lines.join('\n');
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pullNumber,
per_page: 100,
});
const existing = comments.find((comment) =>
comment.user?.login === 'github-actions[bot]' &&
(comment.body?.includes(marker) || comment.body?.includes('# Built artifacts for commit '))
);
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pullNumber,
body,
});
}