Skip to content

Commit

Permalink
fix: fetch PR head ref in issue_comment workflow
Browse files Browse the repository at this point in the history
The issue_comment event payload doesn't include pull_request.head.ref, so we need
to explicitly fetch the PR information using the GitHub CLI. This fixes the
workflow by:

- Adding a step to fetch PR head branch using gh pr view
- Using the fetched ref for checkout, cache key, and git push
- Removing references to github.event.pull_request.head.ref

This ensures the workflow can properly checkout and push to PR branches when
triggered by comments.
  • Loading branch information
spachava753 committed Jan 20, 2025
1 parent d70aba7 commit 60b5bf2
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions .github/workflows/pr-comment-cpe.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: Execute CPE on Pull Request Comment

# See https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#issue_comment
on:
issue_comment:
types: [created]
Expand All @@ -13,9 +14,19 @@ jobs:
contents: write
pull-requests: write
steps:
- name: Get PR information
id: pr_info
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.issue.number }}
run: |
# Get PR data using GitHub CLI
pr_data=$(gh pr view $PR_NUMBER --json headRefName -q .headRefName)
echo "head_ref=$pr_data" >> $GITHUB_OUTPUT
- uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.ref }}
ref: ${{ steps.pr_info.outputs.head_ref }}

- name: Set up Go
uses: actions/setup-go@v4
Expand All @@ -37,9 +48,9 @@ jobs:
uses: actions/cache@v3
with:
path: .cpeconvo
key: cpe-cache-${{ github.event.pull_request.head.ref }}
key: cpe-cache-${{ steps.pr_info.outputs.head_ref }}
restore-keys: |
cpe-cache-${{ github.event.pull_request.head.ref }}
cpe-cache-${{ steps.pr_info.outputs.head_ref }}
- name: Run CPE
env:
Expand All @@ -64,7 +75,7 @@ jobs:
if [[ -n "$(git status --porcelain)" ]]; then
git add .
git commit -m "CPE: Process comment on PR #${{ github.event.issue.number }}"
git push origin HEAD:${{ github.event.pull_request.head.ref }}
git push origin HEAD:${{ steps.pr_info.outputs.head_ref }}
else
echo "No changes to commit"
fi

0 comments on commit 60b5bf2

Please sign in to comment.