|
| 1 | +name: Automated tests - publish results |
| 2 | +on: |
| 3 | + workflow_run: |
| 4 | + workflows: |
| 5 | + - Automated tests |
| 6 | + types: |
| 7 | + - completed |
| 8 | + |
| 9 | +jobs: |
| 10 | + get-pr-data: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + if: ${{ github.event.workflow_run.event == 'pull_request' }} |
| 13 | + outputs: |
| 14 | + pr-number: ${{ steps.set-env.outputs.pr-number }} |
| 15 | + workflow-id: ${{ steps.set-env.outputs.workflow-id }} |
| 16 | + steps: |
| 17 | + # https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#using-data-from-the-triggering-workflow |
| 18 | + - name: Download artifact |
| 19 | + uses: actions/github-script@v6 |
| 20 | + with: |
| 21 | + script: | |
| 22 | + let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({ |
| 23 | + owner: context.repo.owner, |
| 24 | + repo: context.repo.repo, |
| 25 | + run_id: context.payload.workflow_run.id, |
| 26 | + }); |
| 27 | +
|
| 28 | + let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => { |
| 29 | + return artifact.name == "pr-comment-data" |
| 30 | + })[0]; |
| 31 | + let download = await github.rest.actions.downloadArtifact({ |
| 32 | + owner: context.repo.owner, |
| 33 | + repo: context.repo.repo, |
| 34 | + artifact_id: matchArtifact.id, |
| 35 | + archive_format: 'zip', |
| 36 | + }); |
| 37 | + let fs = require('fs'); |
| 38 | + fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/pr-comment-data.zip`, Buffer.from(download.data)); |
| 39 | + - name: Unzip artifact |
| 40 | + run: unzip pr-comment-data.zip |
| 41 | + - name: Set env variables |
| 42 | + id: set-env |
| 43 | + run: | |
| 44 | + echo "pr-number=$(cat ./pr_number)" >> $GITHUB_OUTPUT |
| 45 | + echo "workflow-id=$(cat ./workflow_id)" >> $GITHUB_OUTPUT |
| 46 | + comment-pr: |
| 47 | + runs-on: ubuntu-latest |
| 48 | + permissions: |
| 49 | + pull-requests: write |
| 50 | + needs: get-pr-data |
| 51 | + steps: |
| 52 | + - name: Find Comment |
| 53 | + uses: peter-evans/find-comment@v2 |
| 54 | + id: fc |
| 55 | + with: |
| 56 | + issue-number: ${{ needs.get-pr-data.outputs.pr-number }} |
| 57 | + comment-author: "github-actions[bot]" |
| 58 | + body-includes: Automated tests Summary |
| 59 | + - name: Remove previous comment |
| 60 | + if: steps.fc.outputs.comment-id != '' |
| 61 | + uses: actions/github-script@v6 |
| 62 | + with: |
| 63 | + script: | |
| 64 | + github.rest.issues.deleteComment({ |
| 65 | + owner: context.repo.owner, |
| 66 | + repo: context.repo.repo, |
| 67 | + comment_id: ${{ steps.fc.outputs.comment-id }} |
| 68 | + }) |
| 69 | + - name: Passing tests comment |
| 70 | + if: ${{ github.event.workflow_run.conclusion == 'success' }} |
| 71 | + uses: peter-evans/create-or-update-comment@v2 |
| 72 | + with: |
| 73 | + issue-number: ${{ needs.get-pr-data.outputs.pr-number }} |
| 74 | + body: | |
| 75 | + <h1>Automated tests Summary</h1> |
| 76 | + <h3><strong>:white_check_mark:</strong> All the CI tests have passed!</h3> |
| 77 | + - name: Failing tests comment |
| 78 | + if: ${{ github.event.workflow_run.conclusion == 'failure' }} |
| 79 | + uses: peter-evans/create-or-update-comment@v2 |
| 80 | + with: |
| 81 | + issue-number: ${{ needs.get-pr-data.outputs.pr-number }} |
| 82 | + body: | |
| 83 | + <h1> Automated tests Summary</h1> |
| 84 | + <h3><strong>:rotating_light:</strong> Test workflow has failed</h3> |
| 85 | +
|
| 86 | + ___ |
| 87 | +
|
| 88 | + [Click here](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ needs.get-pr-data.outputs.workflow-id }}) to check the action test reports |
0 commit comments