|
| 1 | +name: Write build artifact comments |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: ["Build"] |
| 6 | + types: |
| 7 | + - completed |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + |
| 12 | +jobs: |
| 13 | + on-success: |
| 14 | + |
| 15 | + permissions: |
| 16 | + pull-requests: write |
| 17 | + |
| 18 | + runs-on: ubuntu-latest |
| 19 | + steps: |
| 20 | + - name: Get source code |
| 21 | + uses: actions/checkout@v4 |
| 22 | + with: |
| 23 | + # To fetch tags |
| 24 | + fetch-depth: 0 |
| 25 | + |
| 26 | + - name: 'Set plugin version environment variables' |
| 27 | + run: | |
| 28 | + TAG=$(git describe --tags $(git rev-list --tags --max-count=1)) |
| 29 | + echo "VERSION=$(echo ${TAG} | awk -F. -v OFS=. 'NF==1{print ++$NF}; NF>1{if(length($NF+1)>length($NF))$(NF-1)++; $NF=sprintf("%0*d", length($NF), ($NF+1)%(10^length($NF))); print}')-alpha" >> $GITHUB_ENV |
| 30 | + echo "SHA_SHORT=$(git rev-parse --short HEAD)" >> $GITHUB_ENV |
| 31 | + - name: 'Download artifact' |
| 32 | + id: download_artifact |
| 33 | + uses: actions/github-script@v7 |
| 34 | + with: |
| 35 | + script: | |
| 36 | + let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({ |
| 37 | + owner: context.repo.owner, |
| 38 | + repo: context.repo.repo, |
| 39 | + run_id: context.payload.workflow_run.id, |
| 40 | + }); |
| 41 | + let matchArtifacts = allArtifacts.data.artifacts.filter((artifact) => { |
| 42 | + return artifact.name == 'redistrict_plugin.${{ env.VERSION }}' |
| 43 | + }); |
| 44 | + matchArtifacts.forEach((artifact) => { |
| 45 | + }); |
| 46 | + if (matchArtifacts.length>0) |
| 47 | + { |
| 48 | + let download = await github.rest.actions.downloadArtifact({ |
| 49 | + owner: context.repo.owner, |
| 50 | + repo: context.repo.repo, |
| 51 | + artifact_id: matchArtifacts[0].id, |
| 52 | + archive_format: 'zip', |
| 53 | + }); |
| 54 | + let fs = require('fs'); |
| 55 | + fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/redistrict_plugin.${{ env.VERSION }}.zip`, Buffer.from(download.data)); |
| 56 | + core.setOutput('artifact_id', matchArtifacts[0].id); |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + core.setOutput('artifact_id', 0); |
| 61 | + } |
| 62 | +
|
| 63 | + - name: 'Unzip artifact' |
| 64 | + if: fromJSON(steps.download_artifact.outputs.artifact_id) > 0 |
| 65 | + run: | |
| 66 | + unzip -n redistrict_plugin.${{ env.VERSION }} |
| 67 | +
|
| 68 | + - name: 'Post artifact download link as comment on PR' |
| 69 | + if: fromJSON(steps.download_artifact.outputs.artifact_id) > 0 |
| 70 | + uses: actions/github-script@v7 |
| 71 | + with: |
| 72 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 73 | + script: | |
| 74 | + let fs = require('fs'); |
| 75 | + let issue_number = Number(fs.readFileSync('./pr_number')); |
| 76 | + let git_sha = String(fs.readFileSync('./git_commit')).trim(); |
| 77 | + const prComments = await github.rest.issues.listComments({ |
| 78 | + owner: context.repo.owner, |
| 79 | + repo: context.repo.repo, |
| 80 | + issue_number: issue_number, |
| 81 | + }); |
| 82 | + const PREFIX = "## Plugin ready!"; |
| 83 | + let body = PREFIX + "\n\n" + |
| 84 | + "A test version of this PR is available for testing [here](https://github.com/" + context.repo.owner + "/" + context.repo.repo + "/suites/" + context.payload.workflow_run.check_suite_id + "/artifacts/${{steps.download_artifact.outputs.artifact_id}})."; |
| 85 | + body += "\n\n*(Built from commit " + git_sha + ")*"; |
| 86 | +
|
| 87 | + const winBuildComment = prComments.data?.find(c => c.body.startsWith(PREFIX)); |
| 88 | + if (!!winBuildComment) { |
| 89 | + // update the existing comment |
| 90 | + await github.rest.issues.updateComment({ |
| 91 | + owner: context.repo.owner, |
| 92 | + repo: context.repo.repo, |
| 93 | + comment_id: winBuildComment.id, |
| 94 | + body: body |
| 95 | + }); |
| 96 | + } else { |
| 97 | + // submit a new comment |
| 98 | + await github.rest.issues.createComment({ |
| 99 | + owner: context.repo.owner, |
| 100 | + repo: context.repo.repo, |
| 101 | + issue_number: issue_number, |
| 102 | + body: body |
| 103 | + }); |
| 104 | + } |
| 105 | +
|
0 commit comments