|
| 1 | +--- |
| 2 | +name: Post PR comment and assign labels |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | +jobs: |
| 6 | + run: |
| 7 | + runs-on: ubuntu-latest |
| 8 | + steps: |
| 9 | + - name: Checkout workflows repo ↪️ |
| 10 | + # Check out .github repo to gain access to scripts |
| 11 | + uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 |
| 12 | + with: |
| 13 | + repository: VoronDesign/.github |
| 14 | + persist-credentials: false |
| 15 | + - name: Setup python 3.11 🐍 |
| 16 | + id: python311 |
| 17 | + uses: actions/setup-python@bd6b4b6205c4dbad673328db7b31b7fab9e241c0 |
| 18 | + with: |
| 19 | + python-version: '3.11' |
| 20 | + - name: Install python packages 🛠️ |
| 21 | + # Install required packages |
| 22 | + id: pip-install-packages |
| 23 | + run: | |
| 24 | + pip install requests |
| 25 | + - name: Download artifact |
| 26 | + # Download the artifact that was stored during the PR CI process |
| 27 | + # This file contains the action_run_id and the pull_request number |
| 28 | + # which are often not accessible from contexts |
| 29 | + uses: actions/github-script@v6 |
| 30 | + with: |
| 31 | + script: | |
| 32 | + var artifacts = await github.rest.actions.listWorkflowRunArtifacts({ |
| 33 | + owner: context.repo.owner, |
| 34 | + repo: context.repo.repo, |
| 35 | + run_id: ${{github.event.workflow_run.id }}, |
| 36 | + }); |
| 37 | + var matchArtifact = artifacts.data.artifacts.filter((artifact) => { |
| 38 | + return artifact.name == "pr" |
| 39 | + })[0]; |
| 40 | + var download = await github.rest.actions.downloadArtifact({ |
| 41 | + owner: context.repo.owner, |
| 42 | + repo: context.repo.repo, |
| 43 | + artifact_id: matchArtifact.id, |
| 44 | + archive_format: 'zip', |
| 45 | + }); |
| 46 | + var fs = require('fs'); |
| 47 | + fs.writeFileSync('${{github.workspace}}/pr.zip', Buffer.from(download.data)); |
| 48 | + - run: unzip pr.zip |
| 49 | + - name: Get action_run_id and pr_number |
| 50 | + # The PR file contains two files, one with the PR number and one with the action run ID |
| 51 | + # Store them in outputs so they're easily accessible to other steps in this workflow |
| 52 | + id: get_artifact_values |
| 53 | + uses: actions/github-script@v6 |
| 54 | + with: |
| 55 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 56 | + script: | |
| 57 | + var fs = require('fs'); |
| 58 | + var pr_number = String(fs.readFileSync('${{github.workspace}}/pr_number')); |
| 59 | + var action_run_id = String(fs.readFileSync('${{github.workspace}}/action_run_id')); |
| 60 | + core.setOutput('pr_number', pr_number); |
| 61 | + core.setOutput('action_run_id', action_run_id); |
| 62 | + - name: Generate PR Comment 📃 |
| 63 | + id: generate_pr_comment |
| 64 | + # Generate the contents of the PR comment, this also stores the labels to be set on the PR |
| 65 | + # in the output variable "LABELS_TO_SET" |
| 66 | + run: | |
| 67 | + python3 ${{ github.workspace }}/scripts/generate_pr_comment.py --repository=${{ github.repository }} --action_id=${{ steps.get_artifact_values.outputs.action_run_id }} --out_file=${{ github.workspace }}/pr/pr-comment.md |
| 68 | + - name: Post/Update PR Comment 📣 |
| 69 | + # Actually post/update the comment in the PR |
| 70 | + uses: thollander/actions-comment-pull-request@8c77f42bbcc27c832a3a5962c8f9a60e34b594f3 |
| 71 | + with: |
| 72 | + filePath: ${{ github.workspace }}/pr/pr-comment.md |
| 73 | + pr_number: ${{ steps.get_artifact_values.outputs.pr_number }} |
| 74 | + comment_tag: voron_users_ci |
| 75 | + - name: Set labels on PR |
| 76 | + # The python script determined which labels should be set. By using PUT, this workflow will |
| 77 | + # also remove all labels that are not explicitly set by the python script. |
| 78 | + env: |
| 79 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 80 | + run: |- |
| 81 | + curl --request PUT \ |
| 82 | + --header "Accept: application/vnd.github+json" \ |
| 83 | + --header "Authorization: Bearer $GH_TOKEN" \ |
| 84 | + --header "X-GitHub-Api-Version: 2022-11-28" \ |
| 85 | + --url https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.get_artifact_values.outputs.pr_number }}/labels \ |
| 86 | + -d '{"labels":[${{ steps.generate_pr_comment.outputs.LABELS_TO_SET}}]}' |
0 commit comments