1
0
Fork 1
mirror of https://github.com/NixOS/nixpkgs.git synced 2025-06-10 18:12:34 +09:00

workflows: replace API calls from bash with github-script (#412659)

This commit is contained in:
Wolfgang Walther 2025-06-01 10:05:31 +00:00 committed by GitHub
commit b429336d37
Signed by: github
GPG key ID: B5690EEEBB952194
4 changed files with 125 additions and 101 deletions

View file

@ -11,6 +11,7 @@ on:
permissions: permissions:
contents: read contents: read
issues: write
pull-requests: write pull-requests: write
jobs: jobs:
@ -49,13 +50,13 @@ jobs:
- name: "Add 'has: port to stable' label" - name: "Add 'has: port to stable' label"
if: steps.backport.outputs.created_pull_numbers != '' if: steps.backport.outputs.created_pull_numbers != ''
env: uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
# Not the app on purpose to avoid triggering another workflow run after adding this label with:
GH_TOKEN: ${{ github.token }} # Not using the app on purpose to avoid triggering another workflow run after adding this label.
REPOSITORY: ${{ github.repository }} script: |
NUMBER: ${{ github.event.number }} await github.rest.issues.addLabels({
run: | owner: context.repo.owner,
gh api \ repo: context.repo.repo,
--method POST \ issue_number: context.payload.pull_request.number,
/repos/"$REPOSITORY"/issues/"$NUMBER"/labels \ labels: [ '8.has: port to stable' ]
-f "labels[]=8.has: port to stable" })

View file

@ -34,16 +34,17 @@ jobs:
private-key: ${{ secrets.NIXPKGS_CI_APP_PRIVATE_KEY }} private-key: ${{ secrets.NIXPKGS_CI_APP_PRIVATE_KEY }}
permission-pull-requests: write permission-pull-requests: write
- env: - uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
GH_TOKEN: ${{ steps.app-token.outputs.token }} with:
REPOSITORY: ${{ github.repository }} github-token: ${{ steps.app-token.outputs.token }}
NUMBER: ${{ github.event.number }} script: |
run: | function changeState(state) {
gh api \ return github.rest.pulls.update({
--method PATCH \ owner: context.repo.owner,
/repos/"$REPOSITORY"/pulls/"$NUMBER" \ repo: context.repo.repo,
-f "state=closed" pull_number: context.payload.pull_request.number,
gh api \ state
--method PATCH \ })
/repos/"$REPOSITORY"/pulls/"$NUMBER" \ }
-f "state=open" await changeState('closed')
await changeState('open')

View file

@ -87,44 +87,43 @@ jobs:
- name: Get target run id - name: Get target run id
if: needs.prepare.outputs.targetSha if: needs.prepare.outputs.targetSha
id: targetRunId id: targetRunId
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
env: env:
GH_TOKEN: ${{ github.token }}
MATRIX_SYSTEM: ${{ matrix.system }} MATRIX_SYSTEM: ${{ matrix.system }}
REPOSITORY: ${{ github.repository }}
TARGET_SHA: ${{ needs.prepare.outputs.targetSha }} TARGET_SHA: ${{ needs.prepare.outputs.targetSha }}
run: | with:
# Get the latest eval.yml workflow run for the PR's target commit script: |
if ! run=$(gh api --method GET /repos/"$REPOSITORY"/actions/workflows/eval.yml/runs \ const system = process.env.MATRIX_SYSTEM
-f head_sha="$TARGET_SHA" -f event=push \ const targetSha = process.env.TARGET_SHA
--jq '.workflow_runs | sort_by(.run_started_at) | .[-1]') \
|| [[ -z "$run" ]]; then
echo "Could not find an eval.yml workflow run for $TARGET_SHA, cannot make comparison"
exit 1
fi
echo "Comparing against $(jq .html_url <<< "$run")"
runId=$(jq .id <<< "$run")
if ! job=$(gh api --method GET /repos/"$REPOSITORY"/actions/runs/"$runId"/jobs \ let run_id
--jq ".jobs[] | select (.name == \"Outpaths ($MATRIX_SYSTEM)\")") \ try {
|| [[ -z "$job" ]]; then run_id = (await github.rest.actions.listWorkflowRuns({
echo "Could not find the Outpaths ($MATRIX_SYSTEM) job for workflow run $runId, cannot make comparison" owner: context.repo.owner,
exit 1 repo: context.repo.repo,
fi workflow_id: 'eval.yml',
jobId=$(jq .id <<< "$job") event: 'push',
conclusion=$(jq -r .conclusion <<< "$job") head_sha: targetSha
})).data.workflow_runs[0].id
} catch {
throw new Error(`Could not find an eval.yml workflow run for ${targetSha}.`)
}
while [[ "$conclusion" == null || "$conclusion" == "" ]]; do core.setOutput('targetRunId', run_id)
echo "Job not done, waiting 10 seconds before checking again"
sleep 10
conclusion=$(gh api /repos/"$REPOSITORY"/actions/jobs/"$jobId" --jq '.conclusion')
done
if [[ "$conclusion" != "success" ]]; then // Waiting 120 * 5 sec = 10 min. max.
echo "Job was not successful (conclusion: $conclusion), cannot make comparison" // Eval takes max 5-6 minutes, normally.
exit 1 for (let i = 0; i < 120; i++) {
fi const result = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
echo "targetRunId=$runId" >> "$GITHUB_OUTPUT" repo: context.repo.repo,
run_id,
name: `merged-${system}`
})
if (result.data.total_count > 0) return
await new Promise(resolve => setTimeout(resolve, 5000))
}
throw new Error(`No merged-${system} artifact found.`)
- uses: actions/download-artifact@v4 - uses: actions/download-artifact@v4
if: steps.targetRunId.outputs.targetRunId if: steps.targetRunId.outputs.targetRunId
@ -212,57 +211,75 @@ jobs:
- name: Labelling pull request - name: Labelling pull request
if: ${{ github.event_name == 'pull_request_target' }} if: ${{ github.event_name == 'pull_request_target' }}
env: uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
GH_TOKEN: ${{ github.token }} with:
REPOSITORY: ${{ github.repository }} script: |
NUMBER: ${{ github.event.number }} const { readFile } = require('node:fs/promises')
run: |
# Get all currently set labels that we manage
gh api \
/repos/"$REPOSITORY"/issues/"$NUMBER"/labels \
--jq '.[].name | select(startswith("10.rebuild") or . == "11.by: package-maintainer")' \
| sort > before
# And the labels that should be there const pr = {
jq -r '.labels[]' comparison/changed-paths.json \ owner: context.repo.owner,
| sort > after repo: context.repo.repo,
issue_number: context.payload.pull_request.number
}
# Remove the ones not needed anymore // Get all currently set labels that we manage
while read -r toRemove; do const before =
echo "Removing label $toRemove" (await github.paginate(github.rest.issues.listLabelsOnIssue, pr))
gh api \ .map(({ name }) => name)
--method DELETE \ .filter(name => name.startsWith('10.rebuild') || name == '11.by: package-maintainer')
/repos/"$REPOSITORY"/issues/"$NUMBER"/labels/"$toRemove"
done < <(comm -23 before after)
# And add the ones that aren't set already // And the labels that should be there
while read -r toAdd; do const after = JSON.parse(await readFile('comparison/changed-paths.json', 'utf-8')).labels
echo "Adding label $toAdd"
gh api \ // Remove the ones not needed anymore
--method POST \ await Promise.all(
/repos/"$REPOSITORY"/issues/"$NUMBER"/labels \ before.filter(name => !after.includes(name))
-f "labels[]=$toAdd" .map(name => github.rest.issues.removeLabel({
done < <(comm -13 before after) ...pr,
name
}))
)
// And add the ones that aren't set already
const added = after.filter(name => !before.includes(name))
if (added.length > 0) {
await github.rest.issues.addLabels({
...pr,
labels: added
})
}
- name: Add eval summary to commit statuses - name: Add eval summary to commit statuses
if: ${{ github.event_name == 'pull_request_target' }} if: ${{ github.event_name == 'pull_request_target' }}
env: uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
GH_TOKEN: ${{ github.token }} with:
PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }} script: |
NUMBER: ${{ github.event.number }} const { readFile } = require('node:fs/promises')
run: | const changed = JSON.parse(await readFile('comparison/changed-paths.json', 'utf-8'))
description=$(jq -r ' const description =
"Package: added " + (.attrdiff.added | length | tostring) + 'Package: ' + [
", removed " + (.attrdiff.removed | length | tostring) + `added ${changed.attrdiff.added.length}`,
", changed " + (.attrdiff.changed | length | tostring) + `removed ${changed.attrdiff.removed.length}`,
", Rebuild: linux " + (.rebuildCountByKernel.linux | tostring) + `changed ${changed.attrdiff.changed.length}`
", darwin " + (.rebuildCountByKernel.darwin | tostring) ].join(', ') +
' <comparison/changed-paths.json) ' — Rebuild: ' + [
target_url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID?pr=$NUMBER" `linux ${changed.rebuildCountByKernel.linux}`,
gh api --method POST \ `darwin ${changed.rebuildCountByKernel.darwin}`
-H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" \ ].join(', ')
"/repos/$GITHUB_REPOSITORY/statuses/$PR_HEAD_SHA" \
-f "context=Eval / Summary" -f "state=success" -f "description=$description" -f "target_url=$target_url" const { serverUrl, repo, runId, payload } = context
const target_url =
`${serverUrl}/${repo.owner}/${repo.repo}/actions/runs/${runId}?pr=${payload.pull_request.number}`
await github.rest.repos.createCommitStatus({
owner: repo.owner,
repo: repo.repo,
sha: payload.pull_request.head.sha,
context: 'Eval / Summary',
state: 'success',
description,
target_url
})
reviewers: reviewers:
name: Reviewers name: Reviewers

View file

@ -173,7 +173,12 @@ runCommand "compare"
} >> $out/step-summary.md } >> $out/step-summary.md
fi fi
jq -r -f ${./generate-step-summary.jq} < ${changed-paths} >> $out/step-summary.md {
echo
echo "# Packages"
echo
jq -r -f ${./generate-step-summary.jq} < ${changed-paths}
} >> $out/step-summary.md
cp "$maintainersPath" "$out/maintainers.json" cp "$maintainersPath" "$out/maintainers.json"
'' ''