Skip to content

Commit 9df7725

Browse files
Copilotshenxianpeng
andcommitted
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>
1 parent 1add093 commit 9df7725

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

.github/workflows/commit-check.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
- uses: ./ # self test
2222
env:
2323
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # use GITHUB_TOKEN because of use pr-comments
24+
GITHUB_PR_NUMBER: ${{ github.event.pull_request.number }} # pass PR number for pull_request_target events
2425
with:
2526
message: true
2627
branch: true

main.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
2626
GITHUB_REPOSITORY = os.getenv("GITHUB_REPOSITORY")
2727
GITHUB_REF = os.getenv("GITHUB_REF")
28+
GITHUB_PR_NUMBER = os.getenv("GITHUB_PR_NUMBER")
2829

2930

3031
def log_env_vars():
@@ -116,12 +117,19 @@ def add_pr_comments() -> int:
116117
try:
117118
token = os.getenv("GITHUB_TOKEN")
118119
repo_name = os.getenv("GITHUB_REPOSITORY")
119-
pr_number = os.getenv("GITHUB_REF")
120-
if pr_number is not None:
121-
pr_number = pr_number.split("/")[-2]
122-
else:
123-
# Handle the case where GITHUB_REF is not set
124-
raise ValueError("GITHUB_REF environment variable is not set")
120+
121+
# Get PR number from GITHUB_PR_NUMBER (for pull_request_target) or extract from GITHUB_REF (for pull_request)
122+
pr_number = os.getenv("GITHUB_PR_NUMBER")
123+
if pr_number is None:
124+
# Fallback to extracting from GITHUB_REF for backward compatibility
125+
github_ref = os.getenv("GITHUB_REF")
126+
if github_ref is not None:
127+
pr_number = github_ref.split("/")[-2]
128+
else:
129+
# Handle the case where neither environment variable is set
130+
raise ValueError(
131+
"Neither GITHUB_PR_NUMBER nor GITHUB_REF environment variable is set"
132+
)
125133

126134
# Initialize GitHub client
127135
g = Github(token)

0 commit comments

Comments
 (0)