From d12d6449805c2700e900266e3b2609947a270741 Mon Sep 17 00:00:00 2001 From: Jean-Paul van Ravensberg <14926452+DevSecNinja@users.noreply.github.com> Date: Wed, 11 Nov 2026 16:48:12 +0100 Subject: [PATCH 1/2] fix(open-pr): create missing labels instead of failing the PR gh pr create --label fails outright when the repo lacks a label, so one missing label (apm-sync in ai-toolkit) blocked every APM Materialize PR. Create missing labels first and drop one that can't be created with a warning. Also add the labels when an existing PR is reused. Fixes #483 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .mise.toml | 1 + actions/open-pr/action.yml | 35 ++++++++++-- tests/open-pr-labels.sh | 113 +++++++++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+), 6 deletions(-) create mode 100755 tests/open-pr-labels.sh diff --git a/.mise.toml b/.mise.toml index 06a514c3..cab2b511 100644 --- a/.mise.toml +++ b/.mise.toml @@ -28,5 +28,6 @@ run = [ "bash ./tests/automation-app-contract.sh", "bash ./tests/lint-job-modes-contract.sh", "bash ./tests/open-pr-push.sh", + "bash ./tests/open-pr-labels.sh", "python3 ./tests/workflow-job-conditions.py", ] diff --git a/actions/open-pr/action.yml b/actions/open-pr/action.yml index 9f4f4cf7..9b36d166 100644 --- a/actions/open-pr/action.yml +++ b/actions/open-pr/action.yml @@ -60,9 +60,9 @@ inputs: default: "." labels: description: | - Newline-delimited list of labels to apply on PR creation. Labels - that don't yet exist on the repo are skipped by `gh` with a - warning. Empty by default. + Newline-delimited list of labels to apply when opening the PR, and to + add when an existing PR is reused. Missing labels are created first; a + label that can't be created is skipped with a warning. Empty by default. required: false default: "" draft: @@ -224,14 +224,33 @@ runs: run: |- set -euo pipefail - # Build label flags from newline-delimited input. - label_args=() + # Collect labels from newline-delimited input. + labels=() while IFS= read -r line; do trimmed="${line#"${line%%[![:space:]]*}"}" trimmed="${trimmed%"${trimmed##*[![:space:]]}"}" - [ -n "${trimmed}" ] && label_args+=(--label "${trimmed}") + [ -n "${trimmed}" ] && labels+=("${trimmed}") done <<< "${LABELS}" + # `gh pr create --label` fails outright on a label the repo doesn't + # have. Create missing labels first (the Pull requests permission is + # enough); drop one that can't be created with a warning rather than + # failing the whole PR. + label_args=() + add_label_args=() + if [ "${#labels[@]}" -gt 0 ]; then + repo_labels="$(gh label list --limit 1000 --json name --jq '.[].name' 2>/dev/null)" || repo_labels="" + for label in "${labels[@]}"; do + if ! grep -Fxiq -- "${label}" <<< "${repo_labels}" && + ! gh label create "${label}" >/dev/null 2>&1; then + echo "::warning::Label '${label}' does not exist and could not be created; continuing without it." + continue + fi + label_args+=(--label "${label}") + add_label_args+=(--add-label "${label}") + done + fi + draft_args=() if [ "${DRAFT}" = "true" ]; then draft_args+=(--draft) @@ -270,6 +289,10 @@ runs: fi number=$(printf '%s' "${existing}" | jq -r '.number') url=$(printf '%s' "${existing}" | jq -r '.url') + if [ "${#add_label_args[@]}" -gt 0 ] && + ! gh pr edit "${number}" "${add_label_args[@]}" >/dev/null; then + echo "::warning::Could not add labels to existing PR #${number}." + fi { echo "created=false" echo "pr-url=${url}" diff --git a/tests/open-pr-labels.sh b/tests/open-pr-labels.sh new file mode 100755 index 00000000..16b21060 --- /dev/null +++ b/tests/open-pr-labels.sh @@ -0,0 +1,113 @@ +#!/usr/bin/env bash +# +# Functional test for the label handling of actions/open-pr. +# +# Runs the action's "Open or reuse pull request" script against a fake `gh` +# that, like the real one, rejects `gh pr create --label` for a label the repo +# doesn't have. Before the fix, one missing label (e.g. `apm-sync` in a repo +# that never synced it) made the whole step fail, and a reused PR never got +# its labels. + +set -euo pipefail + +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +action="${repo_root}/actions/open-pr/action.yml" +tmp_dir="$(mktemp -d)" +trap 'rm -rf "${tmp_dir}"' EXIT +cd "${tmp_dir}" + +fail() { + echo "error: $*" >&2 + exit 1 +} + +script="${tmp_dir}/open-pr.sh" +awk ' + $0 == " - name: Open or reuse pull request" { in_step = 1; next } + in_step && $0 ~ /^ run: [|]/ { in_run = 1; next } + in_run { + if ($0 ~ /^ - name:/ || $0 ~ /^ [a-z-]+:/) exit + sub(/^ /, "") + print + } +' "${action}" >"${script}" +[ -s "${script}" ] || fail "could not extract the PR script from ${action}" + +# Fake gh: repo labels live in $STATE/labels, every call is logged. +mkdir -p "${tmp_dir}/bin" +cat >"${tmp_dir}/bin/gh" <<'EOF' +#!/usr/bin/env bash +set -euo pipefail +echo "$*" >>"${STATE}/calls" +has_label() { grep -Fxiq -- "$1" "${STATE}/labels"; } +case "$1 $2" in + "label list") cat "${STATE}/labels" ;; + "label create") + if grep -Fxq -- "$3" "${STATE}/uncreatable" || has_label "$3"; then exit 1; fi + echo "$3" >>"${STATE}/labels" + ;; + "pr create") + shift 2 + while [ "$#" -gt 0 ]; do + if [ "$1" = "--label" ] && ! has_label "$2"; then + echo "could not add label: '$2' not found" >&2 + exit 1 + fi + shift + done + [ -f "${STATE}/pr-exists" ] && { echo "a pull request already exists" >&2; exit 1; } + echo "https://github.com/o/r/pull/7" + ;; + "pr view") echo 7 ;; + "pr list") if [ -f "${STATE}/pr-exists" ]; then echo '{"number":7,"url":"https://github.com/o/r/pull/7"}'; fi ;; + "pr edit") ;; + *) echo "unexpected gh call: $*" >&2; exit 1 ;; +esac +EOF +chmod +x "${tmp_dir}/bin/gh" + +# run_case NAME LABELS: fresh state seeded with `apm-sync` missing. +run_case() { + export STATE="${tmp_dir}/$1" + mkdir -p "${STATE}" + printf 'bug\nMerge: Manual\n' >"${STATE}/labels" + : >"${STATE}/uncreatable" + : >"${STATE}/calls" + [ "${3:-}" = "pr-exists" ] && touch "${STATE}/pr-exists" + [ -n "${4:-}" ] && echo "$4" >"${STATE}/uncreatable" + PATH="${tmp_dir}/bin:${PATH}" \ + GITHUB_OUTPUT="${STATE}/output" \ + GH_TOKEN="fake" \ + BRANCH="chore/apm-materialize" \ + BASE="main" \ + TITLE="chore: materialize" \ + BODY="" \ + LABELS="$2" \ + DRAFT="false" \ + bash "${script}" >"${STATE}/log" 2>&1 +} + +# 1. A missing label is created, an existing one (any case) is not. +run_case create $'apm-sync\n merge: manual\n' || fail "PR creation failed with a missing label: $(cat "${STATE}/log")" +grep -Fxq "created=true" "${STATE}/output" || fail "expected a new PR" +grep -Fxq "label create apm-sync" "${STATE}/calls" || fail "missing label was not created" +grep -Fq "label create merge" "${STATE}/calls" && fail "existing label must not be re-created" +grep -Fq -- "--label apm-sync --label merge: manual" "${STATE}/calls" || fail "PR was not opened with both labels" + +# 2. A label that can't be created is dropped with a warning; the PR still opens. +run_case uncreatable $'apm-sync\nbug' "" apm-sync || fail "PR creation failed when a label could not be created" +grep -Fxq "created=true" "${STATE}/output" || fail "expected a new PR without the uncreatable label" +grep -Fq "::warning::Label 'apm-sync'" "${STATE}/log" || fail "expected a warning for the dropped label" +grep -F "pr create" "${STATE}/calls" | grep -Fq -- "--label apm-sync" && fail "uncreatable label must not be passed to gh" + +# 3. A reused PR gets the labels added. +run_case reuse $'apm-sync\nbug' pr-exists || fail "reusing the existing PR failed: $(cat "${STATE}/log")" +grep -Fxq "created=false" "${STATE}/output" || fail "expected the existing PR to be reused" +grep -Fxq "pr edit 7 --add-label apm-sync --add-label bug" "${STATE}/calls" || fail "labels were not added to the reused PR" + +# 4. No labels: no label API calls. +run_case none "" || fail "PR creation without labels failed" +grep -Fq "label " "${STATE}/calls" && fail "no label calls expected without labels" +grep -Fq "pr edit" "${STATE}/calls" && fail "no pr edit expected without labels" + +echo "✓ open-pr: missing, uncreatable, reused and absent labels all handled." From 473b05df4558ba6bf57f34d5e18fabad7dd4609c Mon Sep 17 00:00:00 2001 From: Jean-Paul van Ravensberg <14926452+DevSecNinja@users.noreply.github.com> Date: Wed, 11 Nov 2026 16:57:09 +0100 Subject: [PATCH 2/2] fix(open-pr): check each label directly before creating it A paged `gh label list` lookup could miss an existing label, and a failed create then dropped it. Look labels up one by one via the API and re-check after a failed create. Refs #483 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- actions/open-pr/action.yml | 29 ++++++++++++++++------------- tests/open-pr-labels.sh | 30 ++++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/actions/open-pr/action.yml b/actions/open-pr/action.yml index 9b36d166..e88a8fc2 100644 --- a/actions/open-pr/action.yml +++ b/actions/open-pr/action.yml @@ -235,21 +235,24 @@ runs: # `gh pr create --label` fails outright on a label the repo doesn't # have. Create missing labels first (the Pull requests permission is # enough); drop one that can't be created with a warning rather than - # failing the whole PR. + # failing the whole PR. Check each label directly: a listing is paged + # and can miss labels. Re-check after a failed create, which also + # covers another run creating the label first. + label_exists() { + gh api "repos/{owner}/{repo}/labels/$(jq -rn --arg l "$1" '$l | @uri')" --silent >/dev/null 2>&1 + } label_args=() add_label_args=() - if [ "${#labels[@]}" -gt 0 ]; then - repo_labels="$(gh label list --limit 1000 --json name --jq '.[].name' 2>/dev/null)" || repo_labels="" - for label in "${labels[@]}"; do - if ! grep -Fxiq -- "${label}" <<< "${repo_labels}" && - ! gh label create "${label}" >/dev/null 2>&1; then - echo "::warning::Label '${label}' does not exist and could not be created; continuing without it." - continue - fi - label_args+=(--label "${label}") - add_label_args+=(--add-label "${label}") - done - fi + for label in ${labels[@]+"${labels[@]}"}; do + if ! label_exists "${label}" && + ! gh label create "${label}" >/dev/null 2>&1 && + ! label_exists "${label}"; then + echo "::warning::Label '${label}' does not exist and could not be created; continuing without it." + continue + fi + label_args+=(--label "${label}") + add_label_args+=(--add-label "${label}") + done draft_args=() if [ "${DRAFT}" = "true" ]; then diff --git a/tests/open-pr-labels.sh b/tests/open-pr-labels.sh index 16b21060..c8f92477 100755 --- a/tests/open-pr-labels.sh +++ b/tests/open-pr-labels.sh @@ -40,8 +40,22 @@ cat >"${tmp_dir}/bin/gh" <<'EOF' set -euo pipefail echo "$*" >>"${STATE}/calls" has_label() { grep -Fxiq -- "$1" "${STATE}/labels"; } +encode() { jq -rn --arg l "$1" '$l | @uri | ascii_downcase'; } +if [ "$1" = "api" ]; then + # GET repos/{owner}/{repo}/labels/; a `flaky` label fails once. + name="$(tr '[:upper:]' '[:lower:]' <<<"${2##*/}")" + while IFS= read -r label; do + [ "$(encode "${label}")" = "${name}" ] || continue + if grep -Fxq -- "${label}" "${STATE}/flaky"; then + grep -Fxv -- "${label}" "${STATE}/flaky" >"${STATE}/flaky.new" || true + mv "${STATE}/flaky.new" "${STATE}/flaky" + exit 1 + fi + exit 0 + done <"${STATE}/labels" + exit 1 +fi case "$1 $2" in - "label list") cat "${STATE}/labels" ;; "label create") if grep -Fxq -- "$3" "${STATE}/uncreatable" || has_label "$3"; then exit 1; fi echo "$3" >>"${STATE}/labels" @@ -66,13 +80,15 @@ esac EOF chmod +x "${tmp_dir}/bin/gh" -# run_case NAME LABELS: fresh state seeded with `apm-sync` missing. +# run_case NAME LABELS [pr-exists] [UNCREATABLE] [FLAKY]: fresh state seeded +# with `apm-sync` missing. run_case() { export STATE="${tmp_dir}/$1" mkdir -p "${STATE}" printf 'bug\nMerge: Manual\n' >"${STATE}/labels" : >"${STATE}/uncreatable" : >"${STATE}/calls" + echo "${5:-}" >"${STATE}/flaky" [ "${3:-}" = "pr-exists" ] && touch "${STATE}/pr-exists" [ -n "${4:-}" ] && echo "$4" >"${STATE}/uncreatable" PATH="${tmp_dir}/bin:${PATH}" \ @@ -107,7 +123,13 @@ grep -Fxq "pr edit 7 --add-label apm-sync --add-label bug" "${STATE}/calls" || f # 4. No labels: no label API calls. run_case none "" || fail "PR creation without labels failed" -grep -Fq "label " "${STATE}/calls" && fail "no label calls expected without labels" +grep -Eq "^(api|label) " "${STATE}/calls" && fail "no label calls expected without labels" grep -Fq "pr edit" "${STATE}/calls" && fail "no pr edit expected without labels" -echo "✓ open-pr: missing, uncreatable, reused and absent labels all handled." +# 5. An existing label whose first lookup fails (create then reports it exists) +# is kept, not dropped. +run_case recheck $'bug' "" "" bug || fail "PR creation failed when a label lookup failed once" +grep -Fq "::warning::" "${STATE}/log" && fail "an existing label must not be dropped after a failed lookup" +grep -F "pr create" "${STATE}/calls" | grep -Fq -- "--label bug" || fail "existing label was dropped after a failed lookup" + +echo "✓ open-pr: missing, uncreatable, reused, absent and re-checked labels all handled."