Skip to content

Commit 024bf28

Browse files
authored
Merge pull request #4497 from github/copilot/fix-fork-invalid-pr-close-20260814
2 parents 4ab8707 + efb1c23 commit 024bf28

1 file changed

Lines changed: 55 additions & 7 deletions

File tree

.github/workflows/close-invalid-pr-writer.yml

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,67 @@ jobs:
4444
exit 0
4545
fi
4646
47-
if [ "$(jq '.pull_requests | length' <<<"$workflow_run")" -ne 1 ]; then
48-
echo "Workflow run is not associated with exactly one PR; skipping."
47+
run_head_sha="$(jq -r .head_sha <<<"$workflow_run")"
48+
run_head_repo="$(jq -r '.head_repository.full_name // empty' <<<"$workflow_run")"
49+
run_head_branch="$(jq -r '.head_branch // empty' <<<"$workflow_run")"
50+
51+
if [ -z "$run_head_repo" ] ||
52+
[ -z "$run_head_branch" ] ||
53+
[[ ! "$run_head_sha" =~ ^[0-9a-f]{40}$ ]] ||
54+
[ "${run_head_repo#*/}" = "$run_head_repo" ] ||
55+
[ -z "${run_head_repo%%/*}" ] ||
56+
[ -z "${run_head_repo#*/}" ]; then
57+
echo "Workflow run is missing valid head repository, branch, or SHA metadata; skipping."
58+
exit 0
59+
fi
60+
61+
pull_request_count="$(jq '.pull_requests | length' <<<"$workflow_run")"
62+
if [ "$pull_request_count" -eq 1 ]; then
63+
pr_number="$(jq -r .pull_requests[0].number <<<"$workflow_run")"
64+
elif [ "$pull_request_count" -eq 0 ]; then
65+
run_head_owner="${run_head_repo%%/*}"
66+
matching_prs="$(
67+
gh api --method GET --paginate "repos/$GH_REPO/pulls" \
68+
-f state=open \
69+
-f head="$run_head_owner:$run_head_branch" \
70+
-f per_page=100 |
71+
jq -cs \
72+
--arg repo "$GH_REPO" \
73+
--arg head_repo "$run_head_repo" \
74+
--arg head_branch "$run_head_branch" \
75+
--arg head_sha "$run_head_sha" \
76+
'add | [
77+
.[] |
78+
select(
79+
.state == "open" and
80+
.base.repo.full_name == $repo and
81+
.head.repo.full_name == $head_repo and
82+
.head.ref == $head_branch and
83+
.head.sha == $head_sha
84+
)
85+
]'
86+
)"
87+
88+
if [ "$(jq 'length' <<<"$matching_prs")" -ne 1 ]; then
89+
echo "Workflow run could not be uniquely associated with an open PR; skipping."
90+
exit 0
91+
fi
92+
pr_number="$(jq -r '.[0].number' <<<"$matching_prs")"
93+
else
94+
echo "Workflow run is associated with multiple PRs; skipping."
95+
exit 0
96+
fi
97+
98+
if [[ ! "$pr_number" =~ ^[1-9][0-9]*$ ]]; then
99+
echo "Workflow run produced an invalid PR number; skipping."
49100
exit 0
50101
fi
51102
52-
pr_number="$(jq -r .pull_requests[0].number <<<"$workflow_run")"
53-
run_head_sha="$(jq -r .head_sha <<<"$workflow_run")"
54-
run_head_repo="$(jq -r '.head_repository.full_name // empty' <<<"$workflow_run")"
55103
pr="$(gh api "repos/$GH_REPO/pulls/$pr_number")"
56104
57-
if [ -z "$run_head_repo" ] ||
58-
[ "$(jq -r .base.repo.full_name <<<"$pr")" != "$GH_REPO" ] ||
105+
if [ "$(jq -r .base.repo.full_name <<<"$pr")" != "$GH_REPO" ] ||
59106
[ "$(jq -r '.head.repo.full_name // empty' <<<"$pr")" != "$run_head_repo" ] ||
107+
[ "$(jq -r .head.ref <<<"$pr")" != "$run_head_branch" ] ||
60108
[ "$(jq -r .head.sha <<<"$pr")" != "$run_head_sha" ]; then
61109
echo "PR #$pr_number no longer matches the workflow run head; skipping."
62110
exit 0

0 commit comments

Comments
 (0)