diff --git a/.github/workflows/test-pr-risk.yml b/.github/workflows/test-pr-risk.yml index c70d0451..a313cf5c 100644 --- a/.github/workflows/test-pr-risk.yml +++ b/.github/workflows/test-pr-risk.yml @@ -48,7 +48,7 @@ jobs: persist-credentials: false - name: shellcheck - run: shellcheck -x grade-pr-risk.sh apply-risk-label.sh grade-targets.sh resolve-enabled.sh tests/test_grade_pr_risk.sh tests/test_apply_risk_label.sh tests/test_grade_targets.sh tests/test_resolve_enabled.sh tests/test_pin_contract.sh publish-risk-surfaces.sh tests/test_publish_risk_surfaces.sh + run: shellcheck -x grade-pr-risk.sh apply-risk-label.sh migrate-risk-labels.sh grade-targets.sh resolve-enabled.sh tests/test_grade_pr_risk.sh tests/test_apply_risk_label.sh tests/test_migrate_risk_labels.sh tests/test_grade_targets.sh tests/test_resolve_enabled.sh tests/test_pin_contract.sh publish-risk-surfaces.sh tests/test_publish_risk_surfaces.sh - name: default map + registry parse and validate # The shipped defaults must pass the grader's own structural validation: @@ -63,6 +63,9 @@ jobs: - name: label suite run: bash tests/test_apply_risk_label.sh + - name: label migration suite + run: bash tests/test_migrate_risk_labels.sh + - name: targets suite # The orchestration layer behind pr-risk.yml: base-ref resolution, the per-repo override # contract, the settle poll, per-target failure isolation. Hermetic — `gh` is stubbed and diff --git a/scripts/pr-risk/README.md b/scripts/pr-risk/README.md index c6aaf1d5..91faee4d 100644 --- a/scripts/pr-risk/README.md +++ b/scripts/pr-risk/README.md @@ -78,6 +78,19 @@ Anything unreadable grades `unknown` (labeled `risk:ungraded`), never a confident tier, and never "the axes that did resolve" — a PR whose file list we could not read is exactly the PR that might touch auth. +## Migrating legacy label assignments + +Preview the fixed R0–R3 to named-label migration, then apply it: + +```bash +bash scripts/pr-risk/migrate-risk-labels.sh OWNER/REPO +bash scripts/pr-risk/migrate-risk-labels.sh OWNER/REPO --apply +``` + +The CLI paginates all PRs, adds and verifies each named label before removing +its legacy label, preserves unrelated labels, and fails if any assignment could +not be migrated or remains afterward. + Two CI-specific mechanics worth knowing: - **The grading run excludes itself from the check rollup it reads** (its own diff --git a/scripts/pr-risk/migrate-risk-labels.sh b/scripts/pr-risk/migrate-risk-labels.sh new file mode 100755 index 00000000..64eb66ce --- /dev/null +++ b/scripts/pr-risk/migrate-risk-labels.sh @@ -0,0 +1,140 @@ +#!/usr/bin/env bash + +set -uo pipefail + +usage() { + cat <<'EOF' +Usage: migrate-risk-labels.sh OWNER/REPO [--apply] + +Migrates PR label assignments: + risk:R0 -> risk:low + risk:R1 -> risk:medium + risk:R2 -> risk:high + risk:R3 -> risk:xhigh + +Dry-run is the default. Pass --apply to add, verify, then remove each legacy label. +EOF +} + +die() { printf 'ERROR: %s\n' "$*" >&2; exit 2; } + +REPO="" +APPLY=0 +while [ "$#" -gt 0 ]; do + case "$1" in + --apply) APPLY=1 ;; + -h|--help) usage; exit 0 ;; + -*) die "unknown option '$1'" ;; + *) [ -z "$REPO" ] || die "only one repository may be specified" + REPO="$1" ;; + esac + shift +done + +[ -n "$REPO" ] || die "OWNER/REPO is required" +[[ "$REPO" =~ ^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$ ]] || die "bad repository '$REPO'" +command -v gh >/dev/null 2>&1 || die "gh not found on PATH" +command -v jq >/dev/null 2>&1 || die "jq not found on PATH" + +TMP="$(mktemp -d "${TMPDIR:-/tmp}/risk-label-migration.XXXXXX")" || die "mktemp failed" +trap 'rm -rf "$TMP"' EXIT +ERRF="$TMP/gh.err" + +ghq() { gh "$@" 2>"$ERRF"; } +gherr() { tr '\n' ' ' <"$ERRF" | sed 's/[[:space:]]*$//'; } +enc() { jq -rn --arg value "$1" '$value | @uri'; } + +list_prs() { + local source="$1" + ghq api --paginate \ + "repos/$REPO/issues?state=all&labels=$(enc "$source")&per_page=100" \ + --jq '.[] | select(.pull_request != null) | .number' | sort -n -u +} + +read_labels() { + local pr="$1" + ghq api --paginate "repos/$REPO/issues/$pr/labels?per_page=100" --jq '[.[].name]' \ + | jq -sc 'add // []' +} + +has_label() { + local labels="$1" label="$2" + jq -e --arg label "$label" \ + 'map(ascii_downcase) | index($label | ascii_downcase) != null' \ + >/dev/null <<<"$labels" +} + +migrate_pr() { + local pr="$1" source="$2" target="$3" labels final + + ghq api -X POST "repos/$REPO/issues/$pr/labels" -f "labels[]=$target" >/dev/null \ + || { printf 'FAIL #%s: could not add %s: %s\n' "$pr" "$target" "$(gherr)" >&2; return 1; } + + labels="$(read_labels "$pr")" \ + || { printf 'FAIL #%s: could not verify %s: %s\n' "$pr" "$target" "$(gherr)" >&2; return 1; } + has_label "$labels" "$target" \ + || { printf 'FAIL #%s: %s was not present after add; kept %s\n' "$pr" "$target" "$source" >&2; return 1; } + + if has_label "$labels" "$source"; then + ghq api -X DELETE "repos/$REPO/issues/$pr/labels/$(enc "$source")" >/dev/null \ + || { printf 'FAIL #%s: could not remove %s: %s\n' "$pr" "$source" "$(gherr)" >&2; return 1; } + fi + + final="$(read_labels "$pr")" \ + || { printf 'FAIL #%s: could not verify final labels: %s\n' "$pr" "$(gherr)" >&2; return 1; } + if ! has_label "$final" "$target" || has_label "$final" "$source"; then + printf 'FAIL #%s: final labels did not preserve %s and remove %s\n' \ + "$pr" "$target" "$source" >&2 + return 1 + fi +} + +MAPPINGS=( + 'risk:R0=risk:low' + 'risk:R1=risk:medium' + 'risk:R2=risk:high' + 'risk:R3=risk:xhigh' +) + +planned=0 +migrated=0 +failures=0 +residual=0 + +for mapping in "${MAPPINGS[@]}"; do + source="${mapping%%=*}" + target="${mapping#*=}" + list="$TMP/$(enc "$source").before" + list_prs "$source" >"$list" \ + || { printf 'FAIL: could not list %s assignments: %s\n' "$source" "$(gherr)" >&2; exit 1; } + count="$(wc -l <"$list" | tr -d ' ')" + planned=$((planned + count)) + printf '%s -> %s: %s PR(s)\n' "$source" "$target" "$count" + + [ "$APPLY" = 1 ] || continue + while IFS= read -r pr; do + [ -n "$pr" ] || continue + if migrate_pr "$pr" "$source" "$target"; then + migrated=$((migrated + 1)) + printf 'migrated #%s: %s -> %s\n' "$pr" "$source" "$target" >&2 + else + failures=$((failures + 1)) + fi + done <"$list" + + remaining="$TMP/$(enc "$source").after" + list_prs "$source" >"$remaining" \ + || { printf 'FAIL: could not check residual %s assignments: %s\n' "$source" "$(gherr)" >&2; exit 1; } + left="$(wc -l <"$remaining" | tr -d ' ')" + residual=$((residual + left)) + [ "$left" -eq 0 ] || printf 'FAIL: %s still exists on %s PR(s): %s\n' \ + "$source" "$left" "$(paste -sd, "$remaining")" >&2 +done + +if [ "$APPLY" != 1 ]; then + printf 'dry-run: %s assignment(s); rerun with --apply\n' "$planned" + exit 0 +fi + +printf 'complete: migrated=%s failures=%s residual=%s\n' "$migrated" "$failures" "$residual" +[ "$failures" -eq 0 ] && [ "$residual" -eq 0 ] diff --git a/scripts/pr-risk/tests/test_migrate_risk_labels.sh b/scripts/pr-risk/tests/test_migrate_risk_labels.sh new file mode 100755 index 00000000..f001d7e4 --- /dev/null +++ b/scripts/pr-risk/tests/test_migrate_risk_labels.sh @@ -0,0 +1,106 @@ +#!/usr/bin/env bash + +set -uo pipefail + +SELF_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +SCRIPT="$SELF_DIR/../migrate-risk-labels.sh" +SANDBOX="$(mktemp -d "${TMPDIR:-/tmp}/risk-label-migration-test.XXXXXX")" +trap 'rm -rf "$SANDBOX"' EXIT + +PASS=0 +FAIL=0 +ok() { PASS=$((PASS + 1)); printf 'ok %s\n' "$1"; } +bad() { FAIL=$((FAIL + 1)); printf 'FAIL %s\n' "$1"; } +eq() { if [ "$2" = "$3" ]; then ok "$1"; else bad "$1 (expected '$2', got '$3')"; fi; } + +mkdir -p "$SANDBOX/bin" "$SANDBOX/state" +export GH_LOG="$SANDBOX/gh.log" STATE_DIR="$SANDBOX/state" + +cat >"$SANDBOX/bin/gh" <<'STUB' +#!/usr/bin/env bash +set -uo pipefail + +printf '%s\n' "$*" >>"$GH_LOG" +url="" +for arg in "$@"; do [[ "$arg" == repos/* ]] && url="$arg"; done + +source_for_url() { + case "$1" in + *risk%3AR0*) echo 'risk:R0' ;; *risk%3AR1*) echo 'risk:R1' ;; + *risk%3AR2*) echo 'risk:R2' ;; *risk%3AR3*) echo 'risk:R3' ;; + esac +} + +if [[ "$url" == *'/issues?state=all&labels='* ]]; then + source="$(source_for_url "$url")" + for file in "$STATE_DIR"/*; do + [ -f "$file" ] || continue + grep -Fqx "$source" "$file" && basename "$file" + done + exit 0 +fi + +if [[ "$url" =~ /issues/([0-9]+)/labels\? ]]; then + pr="${BASH_REMATCH[1]}" + jq -Rsc 'split("\n") | map(select(length > 0))' "$STATE_DIR/$pr" + exit 0 +fi + +if [[ " $* " == *' -X POST '* && "$url" =~ /issues/([0-9]+)/labels$ ]]; then + pr="${BASH_REMATCH[1]}" + target="" + for arg in "$@"; do [[ "$arg" == labels\[\]=* ]] && target="${arg#*=}"; done + if [ "${FAIL_ADD:-0}" != 1 ] && ! grep -Fqx "$target" "$STATE_DIR/$pr"; then + printf '%s\n' "$target" >>"$STATE_DIR/$pr" + fi + exit 0 +fi + +if [[ " $* " == *' -X DELETE '* && "$url" =~ /issues/([0-9]+)/labels/ ]]; then + pr="${BASH_REMATCH[1]}" + source="$(source_for_url "$url")" + grep -Fvx "$source" "$STATE_DIR/$pr" >"$STATE_DIR/$pr.next" || true + mv "$STATE_DIR/$pr.next" "$STATE_DIR/$pr" + exit 0 +fi + +exit 1 +STUB +chmod +x "$SANDBOX/bin/gh" + +reset_state() { + printf 'risk:R0\nkeep\n' >"$STATE_DIR/1" + printf 'risk:R1\nrisk:medium\n' >"$STATE_DIR/2" + printf 'bug\n' >"$STATE_DIR/3" + : >"$GH_LOG" +} + +eq "help exits 0" 0 "$(PATH="$SANDBOX/bin:$PATH" bash "$SCRIPT" --help >/dev/null; echo $?)" +PATH="$SANDBOX/bin:$PATH" bash "$SCRIPT" bad-repo >/dev/null 2>&1 +eq "bad repository exits 2" 2 "$?" + +reset_state +dry="$(PATH="$SANDBOX/bin:$PATH" bash "$SCRIPT" test/repo)" +case "$dry" in *'dry-run: 2 assignment(s)'*) ok "dry-run reports the migration count" ;; *) bad "dry-run reports the migration count" ;; esac +if grep -Eq -- '-X (POST|DELETE)' "$GH_LOG"; then bad "dry-run writes nothing"; else ok "dry-run writes nothing"; fi + +reset_state +out="$(PATH="$SANDBOX/bin:$PATH" bash "$SCRIPT" test/repo --apply 2>/dev/null)" +case "$out" in *'complete: migrated=2 failures=0 residual=0'*) ok "apply completes with no residuals" ;; *) bad "apply completes with no residuals" ;; esac +eq "R0 becomes low without dropping unrelated labels" $'keep\nrisk:low' "$(cat "$STATE_DIR/1")" +eq "an existing target is kept while R1 is removed" 'risk:medium' "$(cat "$STATE_DIR/2")" +post_line="$(grep -n -- '-X POST repos/test/repo/issues/1/labels' "$GH_LOG" | cut -d: -f1)" +delete_line="$(grep -n -- '-X DELETE repos/test/repo/issues/1/labels/risk%3AR0' "$GH_LOG" | cut -d: -f1)" +if [ "$post_line" -lt "$delete_line" ]; then ok "target is added before source is removed"; else bad "target is added before source is removed"; fi + +reset_state +PATH="$SANDBOX/bin:$PATH" FAIL_ADD=1 bash "$SCRIPT" test/repo --apply >/dev/null 2>&1 +eq "an unverified add fails the run" 1 "$?" +if grep -q -- '-X DELETE repos/test/repo/issues/1/labels/risk%3AR0' "$GH_LOG"; then + bad "an unverified add never removes the source" +else + ok "an unverified add never removes the source" +fi + +printf '%s\n' "passed $PASS, failed $FAIL" +[ "$FAIL" -eq 0 ]