pull request build result comment #111
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Comment on a pull request with the status of the build and links to builds. | |
name: pull request build result comment | |
on: | |
workflow_run: | |
workflows: [build] | |
types: [completed] | |
env: | |
runUrl: https://github.com/jcsteh/osara/actions/runs/${{ github.event.workflow_run.id }} | |
jobs: | |
success: | |
if: ${{ github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' }} | |
runs-on: ubuntu-latest | |
steps: | |
# The pull request number isn't readily available, so get it using the | |
# branch. | |
# https://github.com/orgs/community/discussions/25220#discussioncomment-11300118 | |
- id: getPr | |
name: get PR number | |
env: | |
GH_TOKEN: ${{ github.token }} | |
# For a PR submitted from a fork, this will be someUser:someBranch. | |
# This doesn't work for a PR from the main repo, so this will be just | |
# someBranch in that case. | |
prBranch: |- | |
${{ | |
(github.event.workflow_run.head_repository.owner.login != github.event.workflow_run.repository.owner.login) | |
&& format('{0}:{1}', github.event.workflow_run.head_repository.owner.login, github.event.workflow_run.head_branch) | |
|| github.event.workflow_run.head_branch | |
}} | |
run: gh pr view --repo ${{ github.repository }} $prBranch --json 'number' --jq '"number=\(.number)"' >> "${GITHUB_OUTPUT}" | |
# Get the ids for the Windows and mac artifacts. The download URLs here | |
# are for the API and aren't useful in a browser, so we construct the | |
# browser URL ourselves using the id. | |
- id: getArtifactIds | |
name: get artifact ids | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const result = {}; | |
const resp = await github.rest.actions.listWorkflowRunArtifacts({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
run_id: context.payload.workflow_run.id, | |
}); | |
for (const artifact of resp.data.artifacts) { | |
if (artifact.name.startsWith("osara_windows")) { | |
result.windows = artifact.id; | |
} else if (artifact.name.startsWith("osara_mac")) { | |
result.mac = artifact.id; | |
} | |
} | |
return result; | |
- name: comment | |
uses: thollander/actions-comment-pull-request@v3 | |
with: | |
pr-number: ${{ steps.getPr.outputs.number }} | |
message: | | |
[Build ${{ github.event.workflow_run.run_number }} succeeded!](${{ env.runUrl }}) | |
- [Download for Windows](${{ env.runUrl }}/artifacts/${{ fromJSON(steps.getArtifactIds.outputs.result).windows }}) | |
- [Download for Mac](${{ env.runUrl }}/artifacts/${{ fromJSON(steps.getArtifactIds.outputs.result).mac }}) | |
failure: | |
if: ${{ github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'failure' }} | |
runs-on: ubuntu-latest | |
steps: | |
- id: getPr | |
name: get PR number | |
env: | |
GH_TOKEN: ${{ github.token }} | |
prBranch: |- | |
${{ | |
(github.event.workflow_run.head_repository.owner.login != github.event.workflow_run.repository.owner.login) | |
&& format('{0}:{1}', github.event.workflow_run.head_repository.owner.login, github.event.workflow_run.head_branch) | |
|| github.event.workflow_run.head_branch | |
}} | |
run: gh pr view --repo ${{ github.repository }} $prBranch --json 'number' --jq '"number=\(.number)"' >> "${GITHUB_OUTPUT}" | |
- name: comment | |
uses: thollander/actions-comment-pull-request@v3 | |
with: | |
pr-number: ${{ steps.getPr.outputs.number }} | |
message: | | |
[Build ${{ github.event.workflow_run.run_number }} failed!](${{ env.runUrl }}) |