diff --git a/.changes/source-control/fix-fork-target-repo-5b7e2c94.md b/.changes/source-control/fix-fork-target-repo-5b7e2c94.md new file mode 100644 index 00000000000..51ffe4c2935 --- /dev/null +++ b/.changes/source-control/fix-fork-target-repo-5b7e2c94.md @@ -0,0 +1,7 @@ +--- +bump: patch +--- + +### Changed + +- `skills/pull-request/scripts/target-repo.sh` is now a generated copy of the shared `lib/target-repo.sh`, which the work-items GitHub adapter also carries; its resolution order is unchanged. diff --git a/.changes/work-items/fix-fork-target-repo-5b7e2c94.md b/.changes/work-items/fix-fork-target-repo-5b7e2c94.md new file mode 100644 index 00000000000..6f6fcd10667 --- /dev/null +++ b/.changes/work-items/fix-fork-target-repo-5b7e2c94.md @@ -0,0 +1,7 @@ +--- +bump: patch +--- + +### Fixed + +- In a fork checkout, the GitHub tracker adapter no longer follows `gh`'s default repository, which a stock `gh repo clone` of a fork records as upstream. With no `--repo`, it resolves the repository with the same `target-repo.sh` that `/source-control:pull-request` uses (canonical `lib/target-repo.sh`): the pushable fork wins, and remotes naming two or more GitHub repositories with no fork and no `gh repo set-default` exit `3` with that fix, instead of creating or listing items on whichever repository `gh` picks by remote name. Each resolution prints `target repository: /` on stderr. diff --git a/docs/upstream/aihero-before-we-start.md b/docs/upstream/aihero-before-we-start.md index 28a4f743fca..369c73b5cca 100644 --- a/docs/upstream/aihero-before-we-start.md +++ b/docs/upstream/aihero-before-we-start.md @@ -106,8 +106,8 @@ record decisions kept outside this repository. |---|---|---|---|---|---| | A fresh practice repository, separate from the crash course one | Starts the cohort on a clean copy | Inference: leftover work confuses later lessons | Same | **already have**: the follow-along experiment | none | | Clone over HTTPS or SSH | Picks a clone protocol | Inference: learners stall on setup | Either works | **no change** | none | -| A command-line fork: one fresh commit, a new public repository, scripts that still reach the course repository | Copies the course without a platform fork | Inference: learners need their own remote for issues and pull requests | Same | **already have** in the experiment | none | -| Your own issues and pull requests are the ticket tracker | Keeps tickets beside the code | Inference: one place for work | A fork checkout with an `upstream` remote could send our tracker and pull-request writes to the course's public repository | **adapt**: write paths pin the target repository in fork checkouts | [#15375](https://github.com/melodic-software/claude-code-plugins/pull/15375) | +| A command-line fork: a new public repository holding the course's `main` with its history (one fresh commit before ai-hero-cli 0.13.0), scripts that still reach the course repository | Copies the course without a platform fork | Inference: learners need their own remote for issues and pull requests | Same | **already have** in the experiment, forked before 0.13.0 with one commit ([ai-hero-cli changelog](https://github.com/mattpocock/ai-hero-cli/blob/main/CHANGELOG.md)) | none | +| Your own issues and pull requests are the ticket tracker | Keeps tickets beside the code | Inference: one place for work | A fork checkout with an `upstream` remote could send our tracker and pull-request writes to the course's public repository | **adapt**: write paths pin the target repository in fork checkouts, and the work-item tracker resolves it with the pull-request skill's resolver, so a fork checkout targets the fork | [#15375](https://github.com/melodic-software/claude-code-plugins/pull/15375) | | Public, so hosted CI runs free | Avoids CI charges | Inference: private repositories run out of included minutes | Larger runners bill even on public repositories | **already have**: the pull-request pipeline's cost hold covers larger runners ([pr-pipeline](../conventions/pr-pipeline/README.md)) | none | | The same sample app as the crash course | Builds new skills on a known codebase | Inference: a new app adds noise | Same | **already have** | none | | Install, seed, then start the app; ignore install warnings | Gets the app running | Inference: warnings scare learners | Same | **already have** in the experiment; ignoring audit warnings fits a throwaway repository only | none | @@ -122,7 +122,7 @@ record decisions kept outside this repository. | Keep your code at his checkpoint so your agent's results match his | Aligns starting states | Inference: different code gives different results | One run from the same commit does not show a difference is ours | **adapt** in the experiment: the same commit is necessary, not sufficient; compare several runs from one checkpoint | none | | A reset command replaces your work with a checkpoint | Jumps to a lesson's start | Inference: learners fall behind | Same | **already have**: commit first, as the crash course rows decided | none | | A cherry-pick command keeps your commits and adds the lesson's on top | Keeps local work, at the cost of conflicts | Inference: learners want to keep their changes | Our pull-request `ready` updates a branch by merging the base, or by a lease-pinned rebase only on an attended run that allows a rewrite; an unattended run never rewrites | **already have** | none | -| Cherry-picking keeps your own model choice while taking his changes | Protects local settings from checkpoint updates | Inference: learners customize the model | His runner hard-codes the model in a tracked file his checkpoints also edit, so a changed model conflicts | **hold**: rechecked 2026-11-18 at two later course commits: the runner still hard-codes the model in a tracked file, so the conflict stands and there is nothing to take | none | +| Cherry-picking keeps your own model choice while taking his changes | Protects local settings from checkpoint updates | Inference: learners customize the model | His runner hard-codes the model in a tracked file his checkpoints also edit, so a changed model conflicts | **no change**: rechecked 2026-11-18 at the course branch tip that workshops 07 and 08 use: the runner still hard-codes the model in a tracked file, so the conflict stands and there is nothing to take | none | | A pull command merges announced fixes and keeps your work | Delivers course fixes | Inference: course code has bugs | Not our case | **already have** | none | | Install, migrate and seed again after a reset | Brings dependencies and schema in line with the code | Inference: a reset changes source only | Nothing re-runs these after a checkout | **already have**: the open check, whether `/app-control:drive ready` spots pending migrations, stays with the crash-course absorption | none | @@ -137,8 +137,6 @@ record decisions kept outside this repository. counts default WSL as no isolation level and WSL2 with the per-command sandbox as `L1` ([#16702](https://github.com/melodic-software/claude-code-plugins/pull/16702)); the stack for a Windows-only machine is still to be decided. -- Cherry-picking against a hard-coded runner model: rechecked 2026-11-18 at two later course - commits; the hold in the lesson table stands. ## License notice diff --git a/lib/target-repo.sh b/lib/target-repo.sh new file mode 100755 index 00000000000..02e438a2924 --- /dev/null +++ b/lib/target-repo.sh @@ -0,0 +1,99 @@ +#!/usr/bin/env bash +set -uo pipefail + +lower() { printf '%s' "$1" | tr '[:upper:]' '[:lower:]'; } + +github_repo_of() { + local url="${1%/}" + url="${url%.git}" + case "$(lower "$url")" in + https://github.com/*) echo "${url:19}" ;; + ssh://git@github.com/*) echo "${url:21}" ;; + git@github.com:*) echo "${url:15}" ;; + *) ;; + esac +} + +usage() { + echo "usage: target-repo.sh [--dir ]" >&2 + exit 2 +} + +DIR=. +while [[ $# -gt 0 ]]; do + case "$1" in + --dir) + [[ $# -ge 2 ]] || usage + DIR="$2" + shift 2 + ;; + *) usage ;; + esac +done +cd "$DIR" || exit 1 + +found() { + echo "target repository: $1" >&2 + echo "$1" + exit 0 +} + +if [[ -n "${GH_REPO:-}" ]]; then + repo="${GH_REPO%/}" + [[ "$repo" == */*/* ]] && repo="${repo#*/}" + found "$repo" +fi + +ERR_FILE=$(mktemp) || exit 1 +trap 'rm -f "$ERR_FILE"' EXIT + +VIEW=$(gh repo set-default --view 2>"$ERR_FILE" | tr -d '\r') || { + cat "$ERR_FILE" >&2 + exit 1 +} +if [[ -z "$VIEW" ]] && ! grep -q "No default remote repository has been set" "$ERR_FILE"; then + echo "target-repo: gh repo set-default --view printed no repository:" >&2 + cat "$ERR_FILE" >&2 + exit 1 +fi + +SEEN=" " +REPOS=() +for remote in origin $(git remote | grep -vx origin); do + repo=$(github_repo_of "$(git remote get-url "$remote" 2>/dev/null)") + [[ -n "$repo" && "$SEEN" != *" $(lower "$repo") "* ]] || continue + SEEN+="$(lower "$repo") " + REPOS+=("$repo") +done + +if ((${#REPOS[@]} > 1)); then + for repo in "${REPOS[@]}"; do + parent=$(gh api "repos/$repo" --jq 'select(.permissions.push == true) | .parent.full_name // empty' | tr -d '\r') || { + echo "target-repo: could not read repository $repo from GitHub; see gh's error above." >&2 + exit 1 + } + [[ -n "$parent" ]] || continue + parent=$(lower "$parent") + if [[ "$parent" == "$(lower "$VIEW")" || "$SEEN" == *" $parent "* ]]; then + found "$repo" + fi + done +fi + +[[ -n "$VIEW" ]] && found "$VIEW" + +case ${#REPOS[@]} in +1) found "${REPOS[0]}" ;; +0) + REPO=$(gh api 'repos/{owner}/{repo}' --jq .full_name | tr -d '\r') && [[ -n "$REPO" ]] || exit 1 + found "$REPO" + ;; +*) + { + echo "target-repo: the remotes name ${#REPOS[@]} GitHub repositories (${REPOS[*]}), none a fork of another that you can push to, and no gh default is set, so gh would pick one by remote name." + echo "Choose the repository this checkout's issues and pull requests belong to, then run:" + echo " gh repo set-default /" + } >&2 + exit 1 + ;; +esac diff --git a/plugins/source-control/skills/pull-request/scripts/target-repo.sh b/plugins/source-control/skills/pull-request/scripts/target-repo.sh index 87ef3e2bab1..902136a1ac6 100755 --- a/plugins/source-control/skills/pull-request/scripts/target-repo.sh +++ b/plugins/source-control/skills/pull-request/scripts/target-repo.sh @@ -1,31 +1,20 @@ #!/usr/bin/env bash -# Resolve the GitHub repository a pull-request phase reads and writes, once, so -# every later gh call names it (`-R /` for gh pr and gh issue, -# `repos///...` for gh api) instead of leaving gh to choose. -# -# Resolution order: -# 0. GH_REPO, when set (a HOST/ prefix is dropped); -# 1. when the remotes name two or more github.com repositories, the fork among -# them: a remote's repository the caller can push to whose parent is the -# recorded gh default or another remote's repository, `origin` checked -# first; a failed repository read stops with exit 1. A checkout of a fork -# works on the fork, even after a stock fork clone recorded upstream as gh's -# default; a pull request meant for upstream names its base explicitly; -# 2. the default `gh repo set-default` recorded for this checkout; -# 3. with none recorded, the one github.com repository the remotes name; -# 4. with no github.com remote at all, gh's own resolution (an Enterprise host). -# Two or more github.com repositories, none a pushable fork of another, with no -# recorded default is refused: gh would then pick one by remote name. -# -# Usage: target-repo.sh [--dir ] -# Exit 0: owner/repo on stdout, "target repository: owner/repo" on stderr. -# Exit 1: refused or unresolved; the reason, and the fix when there is one, on stderr. -# Exit 2: usage error. +# GENERATED from lib/target-repo.sh by scripts/sync-shared-copies.sh. Do not edit this copy: +# edit the canonical source, then rerun the script. set -uo pipefail -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -# shellcheck source=../../../lib/github-remote.sh -source "${SCRIPT_DIR}/../../../lib/github-remote.sh" +lower() { printf '%s' "$1" | tr '[:upper:]' '[:lower:]'; } + +github_repo_of() { + local url="${1%/}" + url="${url%.git}" + case "$(lower "$url")" in + https://github.com/*) echo "${url:19}" ;; + ssh://git@github.com/*) echo "${url:21}" ;; + git@github.com:*) echo "${url:15}" ;; + *) ;; + esac +} usage() { echo "usage: target-repo.sh [--dir ]" >&2 @@ -51,8 +40,6 @@ found() { exit 0 } -# GH_REPO is the caller's choice, or the repository an earlier block resolved -# and exported, so it wins. It may carry a HOST/ prefix. if [[ -n "${GH_REPO:-}" ]]; then repo="${GH_REPO%/}" [[ "$repo" == */*/* ]] && repo="${repo#*/}" @@ -62,7 +49,6 @@ fi ERR_FILE=$(mktemp) || exit 1 trap 'rm -f "$ERR_FILE"' EXIT -# --view exits 0 whether or not a default is recorded, so read its output. VIEW=$(gh repo set-default --view 2>"$ERR_FILE" | tr -d '\r') || { cat "$ERR_FILE" >&2 exit 1 @@ -73,12 +59,12 @@ if [[ -z "$VIEW" ]] && ! grep -q "No default remote repository has been set" "$E exit 1 fi -declare -A SEEN=() +SEEN=" " REPOS=() for remote in origin $(git remote | grep -vx origin); do repo=$(github_repo_of "$(git remote get-url "$remote" 2>/dev/null)") - [[ -n "$repo" && -z "${SEEN[${repo,,}]:-}" ]] || continue - SEEN[${repo,,}]=1 + [[ -n "$repo" && "$SEEN" != *" $(lower "$repo") "* ]] || continue + SEEN+="$(lower "$repo") " REPOS+=("$repo") done @@ -89,7 +75,8 @@ if ((${#REPOS[@]} > 1)); then exit 1 } [[ -n "$parent" ]] || continue - if [[ "${parent,,}" == "${VIEW,,}" || -n "${SEEN[${parent,,}]:-}" ]]; then + parent=$(lower "$parent") + if [[ "$parent" == "$(lower "$VIEW")" || "$SEEN" == *" $parent "* ]]; then found "$repo" fi done diff --git a/plugins/work-items/tools/work-item-tracker/CONTRACT.md b/plugins/work-items/tools/work-item-tracker/CONTRACT.md index 27d5acaf67a..d863f0bb174 100644 --- a/plugins/work-items/tools/work-item-tracker/CONTRACT.md +++ b/plugins/work-items/tools/work-item-tracker/CONTRACT.md @@ -193,14 +193,18 @@ value (named by file, key and value; the personal override when it set the value - Optional `docs` (either layer): a free-form self-describing pointer naming what the file is and where its contract lives, so the binding file explains itself to a teammate who finds it. `/work-items:setup` writes it by default; the seam never reads it. -- Owner/repo are never recorded in the binding. They are derived at runtime by `gh`'s own - repository resolution for the working directory (`gh api repos/{owner}/{repo}`): `GH_REPO` - first, then the remote `gh repo set-default` recorded, then, with no default and no terminal, - the first remote in gh's order by remote name, which is not necessarily `origin`. In a fork - checkout (an `origin` fork beside an `upstream` remote), the repo therefore follows the gh - default, and a stock `gh repo clone` of a fork records `upstream`; `/work-items:setup check` - reports when the remotes name more than one repository. Verbs that need a repo context accept an - explicit `--repo /` override (conformance, cross-repo tooling). Pointer: +- Owner/repo are never recorded in the binding. The GitHub adapter derives them at runtime with + its copy of the shared `target-repo.sh` (canonical `lib/target-repo.sh`), the resolver + `/source-control:pull-request` uses: `GH_REPO` first; then, when the remotes name two or more + GitHub repositories, the one you can push to whose parent is the recorded gh default or another + remote's repository (so a fork checkout targets the fork, even after a stock `gh repo clone` + recorded `upstream` as gh's default); then the remote `gh repo set-default` recorded; then the + one GitHub repository the remotes name. Remotes naming two or more GitHub repositories, none a + pushable fork of another, with no default recorded exit `3` with the + `gh repo set-default /` fix, instead of letting gh pick one by remote name. Each + resolution prints `target repository: /` on stderr. `/work-items:setup check` + reports the same cases before any write. Verbs that need a repo context accept an explicit + `--repo /` override (conformance, cross-repo tooling). Pointer: [`gh repo set-default`](https://cli.github.com/manual/gh_repo_set-default); as of 2026-11-11 (gh 2.103.3); recheck when a gh release changes how commands pick the base repository. - No binding found → exit `3` and stderr points here; the seam runs no inline wizard. The diff --git a/plugins/work-items/tools/work-item-tracker/adapters/github/common.sh b/plugins/work-items/tools/work-item-tracker/adapters/github/common.sh index 2c107a2d9e1..741440caef9 100755 --- a/plugins/work-items/tools/work-item-tracker/adapters/github/common.sh +++ b/plugins/work-items/tools/work-item-tracker/adapters/github/common.sh @@ -177,17 +177,27 @@ wit_run_gh_native() { WIT_GH_NATIVE_TRY=1 wit_run_gh "$@" } -# wit_resolve_repo <--repo value or empty> — echo owner/repo: the explicit override, else -# gh's own resolution for the CWD (GH_REPO, the `gh repo set-default` remote, else gh's -# first remote by name, not necessarily origin) per CONTRACT.md "Setup (binding file)". wit_resolve_repo() { - local explicit="${1:-}" + local explicit="${1:-}" out err rc errfile if [[ -n "$explicit" ]]; then printf '%s\n' "$explicit" return 0 fi - # REST, not `gh repo view`: that is GraphQL-backed (README "View item"). - wit_run_gh read api 'repos/{owner}/{repo}' --jq .full_name + errfile="$(mktemp)" + # shellcheck source=target-repo.sh + out="$( + set -- + . "$WIT_GH_ADAPTER_DIR/target-repo.sh" 2>"$errfile" + )" + rc=$? + err="$(<"$errfile")" + rm -f "$errfile" + [[ -n "$err" ]] && printf '%s\n' "$err" >&2 + if ((rc != 0)); then + [[ "$err" == *"target-repo: the remotes name"* ]] && exit 3 + exit "$(wit_map_gh_error "$err")" + fi + wit_run_gh read api "repos/$(printf '%s' "$out" | wit_strip_cr)" --jq .full_name printf '%s\n' "$WIT_GH_OUT" } diff --git a/plugins/work-items/tools/work-item-tracker/adapters/github/common.test.sh b/plugins/work-items/tools/work-item-tracker/adapters/github/common.test.sh index 7b1d5e318f4..4efada0c2ee 100755 --- a/plugins/work-items/tools/work-item-tracker/adapters/github/common.test.sh +++ b/plugins/work-items/tools/work-item-tracker/adapters/github/common.test.sh @@ -79,12 +79,19 @@ issue) exit 1 ;; repo) + if [[ "$2 $3" == "set-default --view" ]]; then + printf 'No default remote repository has been set for this directory.\n' >&2 + exit 0 + fi printf 'HTTP 403: GitHub GraphQL is not available from Claude Code sessions\n' >&2 exit 1 ;; api) case "$*" in *"repos/{owner}/{repo}"*) printf 'o/r\n' ;; + *"--jq .full_name") printf "%s\n" "${2#repos/}" | sed 's#^o/r-old$#o/r#' ;; + "api repos/me/r --jq"*) printf 'up/r\n' ;; + "api repos/up/r --jq"* | "api repos/a/r --jq"* | "api repos/b/r --jq"*) ;; *) printf '{"number":1,"title":"t","state":"open","assignees":[{"login":"u"}],"labels":[{"name":"a"}],"type":null,"html_url":"https://github.com/o/r/issues/1","created_at":"2026-03-01T10:00:00Z","milestone":{"number":1,"title":"After zero"}}\n' ;; esac exit 0 @@ -153,12 +160,38 @@ EOF (CLAUDE_CODE_REMOTE='' wit_require_graphql_session "--parent" >/dev/null 2>&1) assert_eq "wit_require_graphql_session passes in a local session" "0" "$?" + NO_REMOTE="$(mktemp -d "$TMP_ROOT/d.XXXXXX")" + git -C "$NO_REMOTE" init -q : >"$EMIT_STUB/calls.log" - REPO="$(GH_STUB_DIR="$EMIT_STUB" PATH="$EMIT_STUB:$PATH" wit_resolve_repo "")" - assert_eq "wit_resolve_repo resolves over REST" "o/r" "$REPO" + REPO="$(cd "$NO_REMOTE" && GH_STUB_DIR="$EMIT_STUB" PATH="$EMIT_STUB:$PATH" wit_resolve_repo "" 2>/dev/null)" + assert_eq "wit_resolve_repo with no github.com remote resolves over REST" "o/r" "$REPO" assert_not_contains "wit_resolve_repo skips repo view" "$(<"$EMIT_STUB/calls.log")" "repo view" assert_eq "wit_resolve_repo keeps an explicit override" "x/y" "$(wit_resolve_repo x/y)" + FORK="$(mktemp -d "$TMP_ROOT/d.XXXXXX")" + git -C "$FORK" init -q + git -C "$FORK" remote add origin git@github.com:me/r.git + git -C "$FORK" remote add upstream https://github.com/up/r + ERR="$(cd "$FORK" && GH_STUB_DIR="$EMIT_STUB" PATH="$EMIT_STUB:$PATH" wit_resolve_repo "" 2>&1 >/dev/null)" + REPO="$(cd "$FORK" && GH_STUB_DIR="$EMIT_STUB" PATH="$EMIT_STUB:$PATH" wit_resolve_repo "" 2>/dev/null)" + assert_eq "wit_resolve_repo in a fork checkout targets the fork, not upstream" "me/r" "$REPO" + assert_contains "wit_resolve_repo names the target on stderr" "$ERR" "target repository: me/r" + + RENAMED="$(mktemp -d "$TMP_ROOT/d.XXXXXX")" + git -C "$RENAMED" init -q + git -C "$RENAMED" remote add origin https://github.com/o/r-old + REPO="$(cd "$RENAMED" && GH_STUB_DIR="$EMIT_STUB" PATH="$EMIT_STUB:$PATH" wit_resolve_repo "" 2>/dev/null)" + assert_eq "wit_resolve_repo returns GitHub's canonical name, not the remote URL's" "o/r" "$REPO" + + TWO="$(mktemp -d "$TMP_ROOT/d.XXXXXX")" + git -C "$TWO" init -q + git -C "$TWO" remote add origin https://github.com/a/r + git -C "$TWO" remote add other https://github.com/b/r + ERR="$(cd "$TWO" && GH_STUB_DIR="$EMIT_STUB" PATH="$EMIT_STUB:$PATH" wit_resolve_repo "" 2>&1 >/dev/null)" + rc=$? + assert_eq "wit_resolve_repo refuses undecidable remotes with exit 3" "3" "$rc" + assert_contains "wit_resolve_repo refusal prints the set-default fix" "$ERR" "gh repo set-default /" + rm -rf "$EMIT_STUB" # In a job (RUNNER_TEMP set) sourcing common.sh wraps gh: a read goes with diff --git a/plugins/work-items/tools/work-item-tracker/adapters/github/create-item.test.sh b/plugins/work-items/tools/work-item-tracker/adapters/github/create-item.test.sh index db58c1a4ee4..2fbaeb4cd60 100755 --- a/plugins/work-items/tools/work-item-tracker/adapters/github/create-item.test.sh +++ b/plugins/work-items/tools/work-item-tracker/adapters/github/create-item.test.sh @@ -4,6 +4,7 @@ set -uo pipefail # In a CI job the adapter wraps its gh calls and records into the job's # RUNNER_TEMP; the stubs here answer the unwrapped calls. unset RUNNER_TEMP +unset GIT_DIR GIT_WORK_TREE GIT_CONFIG TMP_ROOT="$(mktemp -d)" trap 'rm -rf "$TMP_ROOT"' EXIT @@ -22,6 +23,7 @@ assert_usage_error "$S" --title x --type # --type needs a value if command -v jq >/dev/null 2>&1; then STUB="$(mktemp -d "$TMP_ROOT/d.XXXXXX")" PROJECT="$(mktemp -d "$TMP_ROOT/d.XXXXXX")" + git -C "$PROJECT" init -q cat >"$STUB/gh" <<'EOF' #!/usr/bin/env bash if [[ "$1" == "--version" ]]; then @@ -55,7 +57,15 @@ case "$1 $2" in ;; esac case "$1" in -issue | repo) +repo) + if [[ "$2 $3" == "set-default --view" ]]; then + printf 'No default remote repository has been set for this directory.\n' >&2 + exit 0 + fi + printf 'HTTP 403: GitHub GraphQL is not available from Claude Code sessions\n' >&2 + exit 1 + ;; +issue) printf 'HTTP 403: GitHub GraphQL is not available from Claude Code sessions\n' >&2 exit 1 ;; @@ -64,6 +74,7 @@ api) *"--method POST"*) printf '42\n' ;; *"/labels?"*) printf '%b' "${GH_STUB_LABELS-type: bug\ntype: feature\ntype: task\n}" ;; *"repos/{owner}/{repo}"*) printf 'o/r\n' ;; + *"--jq .full_name") printf '%s\n' "${2#repos/}" ;; *) printf '{"number":42,"title":"t","state":"open","assignees":[],"labels":[{"name":"a"},{"name":"type: task"}],"type":{"name":"Task"},"html_url":"https://github.com/o/r/issues/42"}\n' ;; esac exit 0 @@ -75,7 +86,7 @@ EOF chmod +x "$STUB/gh" run_create() { : >"$STUB/calls.log" - CLAUDE_PROJECT_DIR="$PROJECT" GH_STUB_DIR="$STUB" PATH="$STUB:$PATH" bash "$S" "$@" + (cd "$PROJECT" && CLAUDE_PROJECT_DIR="$PROJECT" GH_STUB_DIR="$STUB" PATH="$STUB:$PATH" bash "$S" "$@") } OUT="$(GH_STUB_VERSION=2.45.0 run_create --title t --body b --labels a,b --repo o/r)" diff --git a/plugins/work-items/tools/work-item-tracker/adapters/github/target-repo.sh b/plugins/work-items/tools/work-item-tracker/adapters/github/target-repo.sh new file mode 100755 index 00000000000..902136a1ac6 --- /dev/null +++ b/plugins/work-items/tools/work-item-tracker/adapters/github/target-repo.sh @@ -0,0 +1,101 @@ +#!/usr/bin/env bash +# GENERATED from lib/target-repo.sh by scripts/sync-shared-copies.sh. Do not edit this copy: +# edit the canonical source, then rerun the script. +set -uo pipefail + +lower() { printf '%s' "$1" | tr '[:upper:]' '[:lower:]'; } + +github_repo_of() { + local url="${1%/}" + url="${url%.git}" + case "$(lower "$url")" in + https://github.com/*) echo "${url:19}" ;; + ssh://git@github.com/*) echo "${url:21}" ;; + git@github.com:*) echo "${url:15}" ;; + *) ;; + esac +} + +usage() { + echo "usage: target-repo.sh [--dir ]" >&2 + exit 2 +} + +DIR=. +while [[ $# -gt 0 ]]; do + case "$1" in + --dir) + [[ $# -ge 2 ]] || usage + DIR="$2" + shift 2 + ;; + *) usage ;; + esac +done +cd "$DIR" || exit 1 + +found() { + echo "target repository: $1" >&2 + echo "$1" + exit 0 +} + +if [[ -n "${GH_REPO:-}" ]]; then + repo="${GH_REPO%/}" + [[ "$repo" == */*/* ]] && repo="${repo#*/}" + found "$repo" +fi + +ERR_FILE=$(mktemp) || exit 1 +trap 'rm -f "$ERR_FILE"' EXIT + +VIEW=$(gh repo set-default --view 2>"$ERR_FILE" | tr -d '\r') || { + cat "$ERR_FILE" >&2 + exit 1 +} +if [[ -z "$VIEW" ]] && ! grep -q "No default remote repository has been set" "$ERR_FILE"; then + echo "target-repo: gh repo set-default --view printed no repository:" >&2 + cat "$ERR_FILE" >&2 + exit 1 +fi + +SEEN=" " +REPOS=() +for remote in origin $(git remote | grep -vx origin); do + repo=$(github_repo_of "$(git remote get-url "$remote" 2>/dev/null)") + [[ -n "$repo" && "$SEEN" != *" $(lower "$repo") "* ]] || continue + SEEN+="$(lower "$repo") " + REPOS+=("$repo") +done + +if ((${#REPOS[@]} > 1)); then + for repo in "${REPOS[@]}"; do + parent=$(gh api "repos/$repo" --jq 'select(.permissions.push == true) | .parent.full_name // empty' | tr -d '\r') || { + echo "target-repo: could not read repository $repo from GitHub; see gh's error above." >&2 + exit 1 + } + [[ -n "$parent" ]] || continue + parent=$(lower "$parent") + if [[ "$parent" == "$(lower "$VIEW")" || "$SEEN" == *" $parent "* ]]; then + found "$repo" + fi + done +fi + +[[ -n "$VIEW" ]] && found "$VIEW" + +case ${#REPOS[@]} in +1) found "${REPOS[0]}" ;; +0) + REPO=$(gh api 'repos/{owner}/{repo}' --jq .full_name | tr -d '\r') && [[ -n "$REPO" ]] || exit 1 + found "$REPO" + ;; +*) + { + echo "target-repo: the remotes name ${#REPOS[@]} GitHub repositories (${REPOS[*]}), none a fork of another that you can push to, and no gh default is set, so gh would pick one by remote name." + echo "Choose the repository this checkout's issues and pull requests belong to, then run:" + echo " gh repo set-default /" + } >&2 + exit 1 + ;; +esac diff --git a/scripts/shared-copies.txt b/scripts/shared-copies.txt index 76f81a95c50..d5c9d52fce6 100644 --- a/scripts/shared-copies.txt +++ b/scripts/shared-copies.txt @@ -1027,3 +1027,5 @@ docs/contracts/config-cascade/locations.md plugins/overengineering/reference/con docs/contracts/config-cascade/locations.md plugins/plugin-quality/reference/config-locations.md docs/contracts/config-cascade/locations.md plugins/source-control/reference/config-locations.md docs/contracts/config-cascade/locations.md plugins/testing/reference/config-locations.md +lib/target-repo.sh plugins/source-control/skills/pull-request/scripts/target-repo.sh +lib/target-repo.sh plugins/work-items/tools/work-item-tracker/adapters/github/target-repo.sh