Auto-Ready PRs #342
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: | |
actions: read | |
contents: read | |
pull-requests: write | |
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: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const { owner, repo } = context.repo; | |
const run_id = ${{ github.event.workflow_run.id }}; | |
const { data } = await github.rest.actions.listWorkflowRunArtifacts({ | |
owner, repo, run_id | |
}); | |
const prArtifact = data.artifacts.find(a => a.name === 'pr'); | |
if (!prArtifact) { | |
core.setFailed('artifact "pr" not found'); | |
return; | |
} | |
const download = await github.rest.actions.downloadArtifact({ | |
owner, repo, | |
artifact_id: prArtifact.id, | |
archive_format: 'zip', | |
}); | |
const fs = require('fs'); | |
fs.writeFileSync(`${process.env.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: |- | |
const fs = require('fs'); | |
const raw = fs.readFileSync('./tmp/NR', 'utf8').trim(); | |
const issue_number = Number(raw); | |
if (!Number.isFinite(issue_number)) { | |
core.info(`nr not numeric: "${raw}", skipping`); | |
return; | |
} | |
try { | |
const { owner, repo } = context.repo; | |
const { data: pr } = await github.rest.pulls.get({ | |
owner, repo, pull_number: issue_number, | |
}); | |
const hasLabel = pr.labels?.some(l => l.name === 'autoready') ?? false; | |
core.info(`pr #${issue_number}: draft=${pr.draft}, has autoready=${hasLabel}`); | |
// only proceed when it IS a draft AND has the label | |
if (!(pr.draft && hasLabel)) { | |
core.info(`skipping pr #${issue_number} (must be draft + labeled autoready)`); | |
return; | |
} | |
await github.rest.pulls.update({ | |
owner, repo, pull_number: issue_number, draft: false, | |
}); | |
core.info(`converted pr #${issue_number} to ready`); | |
await github.rest.issues.removeLabel({ | |
owner, repo, issue_number, name: 'autoready', | |
}); | |
core.info(`removed autoready label from pr #${issue_number}`); | |
} catch (error) { | |
const msg = error && typeof error.message === 'string' ? error.message : String(error); | |
core.setFailed(`error processing pr #${issue_number}: ${msg}`); | |
} |