Auto-Ready PRs #3
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
| --- | |
| # .github/workflows/auto-ready.yml | |
| name: "Auto-Ready PRs" | |
| on: | |
| workflow_run: | |
| workflows: | |
| - "PR → main: CI Tests and Linting" | |
| - "PR → main: Pre-commit Checks" | |
| - "Receive PR" | |
| types: | |
| - completed | |
| permissions: | |
| pull-requests: write | |
| contents: read | |
| jobs: | |
| ready-pr: | |
| if: > | |
| github.event.workflow_run.event == 'pull_request' && | |
| github.event.workflow_run.conclusion == 'success' | |
| runs-on: ubuntu-24.04 | |
| steps: | |
| - name: "Download artifact" | |
| uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 | |
| with: | |
| script: | | |
| var artifacts = await github.actions.listWorkflowRunArtifacts({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| run_id: ${{github.event.workflow_run.id }}, | |
| }); | |
| var matchArtifact = artifacts.data.artifacts.filter((artifact) => { | |
| return artifact.name == "pr"; | |
| })[0]; | |
| var download = await github.actions.downloadArtifact({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| artifact_id: matchArtifact.id, | |
| archive_format: 'zip', | |
| }); | |
| var fs = require('fs'); | |
| fs.writeFileSync('${{github.workspace}}/pr.zip', Buffer.from(download.data)); | |
| - run: | | |
| mkdir -p tmp | |
| unzip -d tmp/ pr.zip | |
| - name: "Update PR" | |
| uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: |- | |
| var fs = require('fs'); | |
| var issue_number = Number(fs.readFileSync('./tmp/NR')); | |
| // Verify that the file contains a numeric value | |
| const contains_numeric = /\d/.test(issue_number); | |
| if (!contains_numeric) { | |
| console.log(`Issue number ${issue_number} is not numeric, skipping...`); | |
| return; | |
| } | |
| try { | |
| const { data: pr } = await github.rest.pulls.get({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: issue_number, | |
| }); | |
| const hasLabel = pr.labels.some(l => l.name === 'auto-ready'); | |
| console.log(`PR #${issue_number}: draft=${pr.draft}, has auto-ready label=${hasLabel}`); | |
| if (!pr.draft && !hasLabel) { | |
| console.log(`PR #${issue_number} is either not a draft or does not have the auto-ready label.`); | |
| return; | |
| } | |
| console.log(`Processing PR #${issue_number}...`); | |
| await github.rest.pulls.update({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: issue_number, | |
| draft: false, | |
| }); | |
| console.log(`Successfully converted PR #${issue_number} to ready`); | |
| await github.rest.issues.removeLabel({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: issue_number, | |
| name: 'auto-ready', | |
| }); | |
| console.log(`Successfully removed auto-ready label from PR #${issue_number}`); | |
| } catch (error) { | |
| console.error(`Error processing PR #${number}:`, error); | |
| } |