Skip to content

Commit 6c2cf89

Browse files
committed
fix: replace raw API calls with gh pr commands in staging auto-PR workflow
Switches from gh api GET/POST/PATCH to gh pr list/create/edit to avoid a 422 error caused by gh api exiting non-zero when jq produces empty output via `// empty`, which killed the script before reaching the if/else branch.
1 parent f3a9f54 commit 6c2cf89

1 file changed

Lines changed: 25 additions & 26 deletions

File tree

.github/workflows/staging-auto-pr.yaml

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ jobs:
3030
compare_url="$(echo "${compare_json}" | jq -r '.html_url')"
3131
ahead_by="$(echo "${compare_json}" | jq -r '.ahead_by')"
3232
33+
if [ "${ahead_by}" -eq 0 ]; then
34+
echo "staging is not ahead of production (ahead_by=0). Nothing to do."
35+
exit 0
36+
fi
37+
3338
prs_tsv="$(mktemp)"
3439
while IFS= read -r sha; do
3540
[ -n "${sha}" ] || continue
@@ -58,38 +63,32 @@ jobs:
5863
echo "- Compare: ${compare_url}"
5964
} > "${pr_body_file}"
6065
61-
pr_number="$(gh api "repos/${REPO}/pulls" \
62-
-f state="open" \
63-
-f base="${DESTINATION_BRANCH}" \
64-
-f head="${OWNER}:${SOURCE_BRANCH}" \
66+
pr_number="$(gh pr list \
67+
--repo "${REPO}" \
68+
--base "${DESTINATION_BRANCH}" \
69+
--head "${SOURCE_BRANCH}" \
70+
--state open \
71+
--json number \
6572
--jq '.[0].number // empty')"
6673
6774
if [ -z "${pr_number}" ]; then
6875
echo "No open PR found for ${SOURCE_BRANCH} -> ${DESTINATION_BRANCH}. Creating one."
69-
create_payload="$(jq -n \
70-
--arg title "${PR_TITLE}" \
71-
--arg head "${SOURCE_BRANCH}" \
72-
--arg base "${DESTINATION_BRANCH}" \
73-
--rawfile body "${pr_body_file}" \
74-
'{title: $title, head: $head, base: $base, body: $body}')"
75-
76-
pr_number="$(gh api \
77-
-X POST "repos/${REPO}/pulls" \
78-
--input - \
79-
--jq '.number' <<<"${create_payload}")"
76+
pr_url="$(gh pr create \
77+
--repo "${REPO}" \
78+
--base "${DESTINATION_BRANCH}" \
79+
--head "${SOURCE_BRANCH}" \
80+
--title "${PR_TITLE}" \
81+
--body-file "${pr_body_file}")"
82+
pr_number="${pr_url##*/}"
8083
else
8184
echo "Updating existing PR #${pr_number}."
82-
update_payload="$(jq -n \
83-
--arg title "${PR_TITLE}" \
84-
--rawfile body "${pr_body_file}" \
85-
'{title: $title, body: $body}')"
86-
87-
gh api \
88-
-X PATCH "repos/${REPO}/pulls/${pr_number}" \
89-
--input - <<<"${update_payload}" >/dev/null
85+
gh pr edit "${pr_number}" \
86+
--repo "${REPO}" \
87+
--title "${PR_TITLE}" \
88+
--body-file "${pr_body_file}"
9089
fi
9190
92-
gh api \
93-
-X POST "repos/${REPO}/pulls/${pr_number}/requested_reviewers" \
94-
-f reviewers[]="${REVIEWER}" >/dev/null || \
91+
gh pr edit "${pr_number}" \
92+
--repo "${REPO}" \
93+
--add-reviewer "${REVIEWER}" || \
9594
echo "Reviewer assignment skipped for ${REVIEWER}."

0 commit comments

Comments
 (0)