|
| 1 | +# This workflow adds the 'ci-passed' label to a pull request once the 'CI Check' workflow completes successfully. |
| 2 | +# Resets the 'ci-passed' label status when a pull request is synchronized or reopened, |
| 3 | +# indicating that changes have been pushed and CI needs to rerun. |
| 4 | +name: Add CI Passed Label |
| 5 | + |
| 6 | +on: |
| 7 | + workflow_run: |
| 8 | + workflows: ["CI Check"] |
| 9 | + types: |
| 10 | + - completed |
| 11 | + |
| 12 | +permissions: |
| 13 | + pull-requests: write |
| 14 | + checks: read |
| 15 | + actions: read |
| 16 | + |
| 17 | +jobs: |
| 18 | + fetch_data: |
| 19 | + name: Fetch workflow payload |
| 20 | + runs-on: ubuntu-latest |
| 21 | + if: > |
| 22 | + github.event.workflow_run.event == 'pull_request' && |
| 23 | + github.event.workflow_run.conclusion == 'success' |
| 24 | + outputs: |
| 25 | + pr_number: ${{ steps.extract.outputs.pr_number }} |
| 26 | + event_action: ${{ steps.extract.outputs.event_action }} |
| 27 | + steps: |
| 28 | + - name: 'Download artifact' |
| 29 | + uses: actions/github-script@v3.1.0 |
| 30 | + with: |
| 31 | + script: | |
| 32 | + var artifacts = await github.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.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 | + |
| 49 | + - name: Unzip artifact |
| 50 | + run: unzip pr.zip |
| 51 | + |
| 52 | + - name: Extract PR information |
| 53 | + id: extract |
| 54 | + run: | |
| 55 | + pr_number=$(cat ./pr_number) |
| 56 | + event_action=$(cat ./event_action) |
| 57 | + echo "pr_number=${pr_number}" >> $GITHUB_OUTPUT |
| 58 | + echo "event_action=${event_action}" >> $GITHUB_OUTPUT |
| 59 | +
|
| 60 | + add_ci_passed_label: |
| 61 | + name: Add 'ci-passed' label |
| 62 | + runs-on: ubuntu-latest |
| 63 | + needs: fetch_data |
| 64 | + steps: |
| 65 | + - name: Add 'ci-passed' label |
| 66 | + run: | |
| 67 | + echo "Adding 'ci-passed' label to PR #${{ needs.fetch_data.outputs.pr_number }}" |
| 68 | + gh pr edit ${{ needs.fetch_data.outputs.pr_number }} --add-label "ci-passed" --repo $GITHUB_REPOSITORY |
| 69 | + env: |
| 70 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 71 | + |
| 72 | + reset_ci_passed_label: |
| 73 | + name: Reset 'ci-passed' label on PR Synchronization |
| 74 | + runs-on: ubuntu-latest |
| 75 | + needs: fetch_data |
| 76 | + steps: |
| 77 | + - name: Check and reset label |
| 78 | + run: | |
| 79 | + if [[ "${{ needs.fetch_data.outputs.event_action }}" == "synchronize" || "${{ needs.fetch_data.outputs.event_action }}" == "reopened" ]]; then |
| 80 | + echo "Resetting 'ci-passed' label as changes were pushed (event: ${{ needs.fetch_data.outputs.event_action }})." |
| 81 | + gh pr edit ${{ needs.fetch_data.outputs.pr_number }} --remove-label "ci-passed" --repo $GITHUB_REPOSITORY || echo "Label not present" |
| 82 | + fi |
| 83 | + env: |
| 84 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments