Skip to content

Commit 862b71a

Browse files
mrecachinasCopilot
andcommitted
Make labeler workflow_run PR lookup robust
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 95962c9 commit 862b71a

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

.github/workflows/labeler-apply.yml

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@ jobs:
4141
pr_number=$(jq -r 'if (.pull_requests | length) == 1 then .pull_requests[0].number else empty end' <<<"$run_json")
4242
4343
if [ -z "$pr_number" ]; then
44-
prs_json=$(gh api -H 'Accept: application/vnd.github+json' "repos/$REPO/commits/$head_sha/pulls")
45-
pr_number=$(jq -r 'map(select(.state == "open")) | if length == 1 then .[0].number else empty end' <<<"$prs_json")
44+
prs_json=$(gh api -H 'Accept: application/vnd.github+json' "repos/$REPO/commits/$head_sha/pulls" || echo '[]')
45+
pr_number=$(jq -r 'map(select(.state == "open")) | if length == 1 then .[0].number else empty end' <<<"$prs_json" 2>/dev/null || true)
4646
fi
4747
4848
if [ -z "$pr_number" ]; then
4949
head_owner=$(jq -r '.head_repository.owner.login // empty' <<<"$run_json")
5050
head_branch=$(jq -r '.head_branch // empty' <<<"$run_json")
5151
if [ -n "$head_owner" ] && [ -n "$head_branch" ]; then
52-
prs_json=$(gh api --method GET "repos/$REPO/pulls" -f state=open -f head="$head_owner:$head_branch")
53-
pr_number=$(jq -r 'if length == 1 then .[0].number else empty end' <<<"$prs_json")
52+
prs_json=$(gh api --method GET "repos/$REPO/pulls" -f state=open -f head="$head_owner:$head_branch" || echo '[]')
53+
pr_number=$(jq -r 'if length == 1 then .[0].number else empty end' <<<"$prs_json" 2>/dev/null || true)
5454
fi
5555
fi
5656
@@ -60,11 +60,23 @@ jobs:
6060
exit 0
6161
fi
6262
63-
pr_json=$(gh api "repos/$REPO/pulls/$pr_number")
64-
state=$(jq -r '.state' <<<"$pr_json")
65-
base_repo=$(jq -r '.base.repo.full_name' <<<"$pr_json")
66-
pr_head_sha=$(jq -r '.head.sha' <<<"$pr_json")
67-
pr_head_repo=$(jq -r '.head.repo.full_name // empty' <<<"$pr_json")
63+
pr_json=$(gh api "repos/$REPO/pulls/$pr_number" || true)
64+
if [ -z "$pr_json" ]; then
65+
echo "Pull request #$pr_number could not be fetched; skipping."
66+
echo "apply=false" >> "$GITHUB_OUTPUT"
67+
exit 0
68+
fi
69+
70+
state=$(jq -r '.state // empty' <<<"$pr_json" 2>/dev/null || true)
71+
base_repo=$(jq -r '.base.repo.full_name // empty' <<<"$pr_json" 2>/dev/null || true)
72+
pr_head_sha=$(jq -r '.head.sha // empty' <<<"$pr_json" 2>/dev/null || true)
73+
pr_head_repo=$(jq -r '.head.repo.full_name // empty' <<<"$pr_json" 2>/dev/null || true)
74+
75+
if [ -z "$state" ]; then
76+
echo "Pull request #$pr_number could not be parsed; skipping."
77+
echo "apply=false" >> "$GITHUB_OUTPUT"
78+
exit 0
79+
fi
6880
6981
if [ "$state" != "open" ] || [ "$base_repo" != "$REPO" ]; then
7082
echo "Pull request #$pr_number is no longer an open pull request against $REPO; skipping."

0 commit comments

Comments
 (0)