Skip to content

Commit cdd2eec

Browse files
fix: address bot feedback on prerelease workflow
- Add explicit permissions block (contents: read, pull-requests: write, issues: write) - Fix --ref to use PR head branch instead of github.ref (main) - Add PR info fetching step to get head branch and repo - Add guard to prevent usage on forks (only works for branches in this repo) - Add guard to prevent usage on issues (PR-only) - Improve race condition handling by filtering workflow runs by branch - Add fallback URL if workflow run not found - Document that hardcoded defaults mirror publish.yml defaults Co-Authored-By: AJ Steers <[email protected]>
1 parent 563b6e8 commit cdd2eec

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed

.github/workflows/prerelease-command.yml

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
name: On-Demand Prerelease
22

3+
# Minimal permissions for security (addresses GitHub Advanced Security feedback)
4+
permissions:
5+
contents: read
6+
pull-requests: write
7+
issues: write
8+
39
on:
410
workflow_dispatch:
511
inputs:
@@ -30,6 +36,32 @@ jobs:
3036
id: vars
3137
run: echo "run-url=https://github.com/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" >> $GITHUB_OUTPUT
3238

39+
- name: Check that PR number is provided
40+
if: github.event.inputs.pr == ''
41+
run: |
42+
echo "Error: /prerelease command must be invoked on a pull request, not an issue."
43+
exit 1
44+
45+
- name: Get PR info
46+
id: pr-info
47+
run: |
48+
PR_JSON=$(gh api repos/${{ github.repository }}/pulls/${{ github.event.inputs.pr }})
49+
HEAD_REF=$(echo "$PR_JSON" | jq -r .head.ref)
50+
HEAD_REPO=$(echo "$PR_JSON" | jq -r .head.repo.full_name)
51+
echo "head-ref=${HEAD_REF}" >> $GITHUB_OUTPUT
52+
echo "head-repo=${HEAD_REPO}" >> $GITHUB_OUTPUT
53+
echo "PR branch: ${HEAD_REF} from ${HEAD_REPO}"
54+
env:
55+
GH_TOKEN: ${{ steps.get-app-token.outputs.token }}
56+
57+
- name: Check that PR is from this repository (not a fork)
58+
if: steps.pr-info.outputs.head-repo != github.repository
59+
run: |
60+
echo "Error: /prerelease only works for branches in this repository, not forks."
61+
echo "PR is from: ${{ steps.pr-info.outputs.head-repo }}"
62+
echo "Expected: ${{ github.repository }}"
63+
exit 1
64+
3365
- name: Append comment with job run link
3466
if: github.event.inputs.comment-id
3567
id: first-comment-action
@@ -47,9 +79,13 @@ jobs:
4779
[1]: ${{ steps.vars.outputs.run-url }}
4880
4981
- name: Trigger publish workflow
82+
id: trigger-publish
5083
run: |
84+
# Trigger the publish workflow on the PR's head branch
85+
# Note: These defaults mirror the defaults in publish.yml and can be extended
86+
# to accept optional overrides via slash command arguments if needed in the future.
5187
gh workflow run publish.yml \
52-
--ref ${{ github.ref }} \
88+
--ref "${{ steps.pr-info.outputs.head-ref }}" \
5389
-f version="" \
5490
-f publish_to_pypi=true \
5591
-f publish_to_dockerhub=true \
@@ -63,8 +99,20 @@ jobs:
6399
run: |
64100
# Wait a moment for the workflow to be created
65101
sleep 5
66-
# Get the most recent workflow run for publish.yml
67-
WORKFLOW_RUN_URL=$(gh run list --workflow=publish.yml --limit=1 --json url --jq '.[0].url')
102+
# Query for the most recent publish workflow run on the PR branch
103+
# Filter by branch to avoid race conditions with concurrent runs
104+
WORKFLOW_RUN_URL=$(gh run list \
105+
--workflow=publish.yml \
106+
--branch "${{ steps.pr-info.outputs.head-ref }}" \
107+
--limit=1 \
108+
--json url \
109+
--jq '.[0].url')
110+
111+
if [ -z "$WORKFLOW_RUN_URL" ] || [ "$WORKFLOW_RUN_URL" = "null" ]; then
112+
echo "Warning: Could not find workflow run URL. Using fallback."
113+
WORKFLOW_RUN_URL="https://github.com/${{ github.repository }}/actions/workflows/publish.yml"
114+
fi
115+
68116
echo "workflow-run-url=${WORKFLOW_RUN_URL}" >> $GITHUB_OUTPUT
69117
env:
70118
GH_TOKEN: ${{ steps.get-app-token.outputs.token }}

0 commit comments

Comments
 (0)