Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix PR number extraction for pull_request_target events
- Add GITHUB_PR_NUMBER environment variable to workflow - Update main.py to use GITHUB_PR_NUMBER when available - Maintain backward compatibility with GITHUB_REF extraction - Format code with black Co-authored-by: shenxianpeng <3353385+shenxianpeng@users.noreply.github.com>
  • Loading branch information
Copilot and shenxianpeng committed Aug 19, 2025
commit 9df7725c1bb0c910c1c1ea8da0afca1d08934177
1 change: 1 addition & 0 deletions .github/workflows/commit-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
- uses: ./ # self test
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # use GITHUB_TOKEN because of use pr-comments
GITHUB_PR_NUMBER: ${{ github.event.pull_request.number }} # pass PR number for pull_request_target events
with:
message: true
branch: true
Expand Down
20 changes: 14 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
GITHUB_REPOSITORY = os.getenv("GITHUB_REPOSITORY")
GITHUB_REF = os.getenv("GITHUB_REF")
GITHUB_PR_NUMBER = os.getenv("GITHUB_PR_NUMBER")


def log_env_vars():
Expand Down Expand Up @@ -116,12 +117,19 @@ def add_pr_comments() -> int:
try:
token = os.getenv("GITHUB_TOKEN")
repo_name = os.getenv("GITHUB_REPOSITORY")
pr_number = os.getenv("GITHUB_REF")
if pr_number is not None:
pr_number = pr_number.split("/")[-2]
else:
# Handle the case where GITHUB_REF is not set
raise ValueError("GITHUB_REF environment variable is not set")

# Get PR number from GITHUB_PR_NUMBER (for pull_request_target) or extract from GITHUB_REF (for pull_request)
pr_number = os.getenv("GITHUB_PR_NUMBER")
if pr_number is None:
# Fallback to extracting from GITHUB_REF for backward compatibility
github_ref = os.getenv("GITHUB_REF")
if github_ref is not None:
pr_number = github_ref.split("/")[-2]
else:
# Handle the case where neither environment variable is set
raise ValueError(
"Neither GITHUB_PR_NUMBER nor GITHUB_REF environment variable is set"
)

# Initialize GitHub client
g = Github(token)
Expand Down