Repository navigation
setup.sh: detect the org from origin, not from gh's remote preference - #3
Conversation
On a fork clone made with `gh repo fork opensoft/openRepoShape --org
<org> --clone`, the clone has TWO remotes: `origin` (the fork) and
`upstream` (opensoft/openRepoShape). Step (2) ran `gh repo view --json
owner` with no repository argument; gh resolves the "current
repository" by its own preference between the remotes, which can pick
`upstream` over `origin` — reporting `opensoft` for a perfectly
correct fork and tripping the upstream-org guard on a clone that was
never wrong.
Fix: read `origin`'s URL directly (`git remote get-url origin`,
against the directory setup.sh was invoked from) and parse the owner
by hand for both SSH and HTTPS remote forms, reusing the existing
POSIX parameter-expansion parser. `gh repo view` is now consulted only
as a fallback, and only ON the origin URL itself, so it can no longer
pick a different remote. `--org` still overrides everything. Step (2)
now also names which remote/URL the org came from, and prints a
one-line hint ("upstream is opensoft; scaffolding into <org>") when an
`upstream` remote exists with a different owner than `origin` — the
normal fork shape.
Local mode (`--local-remote-dir`) previously skipped org detection
entirely, using a `localorg` placeholder unconditionally. It now runs
the same origin-remote parsing (no `gh` call, so the "gh not required"
promise holds) and falls back to the placeholder only when there is no
`origin` remote to read — which is what lets the new tests exercise
real parsing without a network or an authenticated `gh`.
Tests: two new cases in tests/test_setup_sh.py build a throwaway git
repo with an `origin` remote pointing at one org and an `upstream`
remote pointing at opensoft (SSH and HTTPS URL forms), run setup.sh
from it with no `--org`, and assert it does not refuse and that the
scaffolded project.yaml records the origin's owner for all three legs.
Two existing local-mode tests that gave no `--org` now pass one
explicitly, since local mode's org detection is no longer a no-op and
would otherwise read whatever `origin` the suite happens to run
inside.
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01HGKwdQsmvRZx4fGQESdFGZ
There was a problem hiding this comment.
Sorry @brettheap, you've used your own review budget of 250,000 diff characters for the last 7 days.
You can request another review in 17 hours and 35 minutes by commenting @sourcery-ai review. Upgrade to get a review now.
|
Reviewer's GuideThe PR fixes fork-clone organisation detection by reading and parsing the invocation directory’s Sequence diagram for origin-based organisation detectionsequenceDiagram
participant Setup as setup.sh
participant Git as Git repository
participant GH as gh CLI
Setup->>Git: git -C INVOCATION_DIR remote get-url origin
Git-->>Setup: ORIGIN_URL
Setup->>Setup: detect_org_from_url ORIGIN_URL
alt owner parsed from origin URL
Setup->>Setup: Set ORG to parsed owner
else owner cannot be parsed and non-local mode
Setup->>GH: gh repo view ORIGIN_URL --json owner --jq .owner.login
GH-->>Setup: owner
Setup->>Setup: Set ORG to returned owner
else no usable origin
Setup->>Setup: Set localorg or abort
end
Setup->>Git: git -C INVOCATION_DIR remote get-url upstream
Git-->>Setup: UPSTREAM_REMOTE_URL
Setup->>Setup: Compare upstream owner with origin owner
Flow diagram for organisation detection precedenceflowchart TD
A[Start organisation detection] --> B{--org provided?}
B -- Yes --> C[Use ORG from --org]
B -- No --> D[Read origin URL from INVOCATION_DIR]
D --> E{Origin owner parsed?}
E -- Yes --> F[Use parsed origin owner]
E -- No --> G{Origin URL and non-local mode?}
G -- Yes --> H[gh repo view ORIGIN_URL]
H --> I{Owner returned?}
I -- Yes --> J[Use gh owner]
I -- No --> K{Local mode?}
G -- No --> K
K -- Yes --> L[Use localorg placeholder]
K -- No --> M[Abort and request --org]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
🟡 Changes recommended
The new org-detection output can leak credentials by printing full remote URLs, and local-mode fallback messaging/logic can be misleading when origin exists but can’t be parsed.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR fixes setup.sh organisation detection by making origin’s remote URL the authoritative source (instead of relying on gh’s implicit “current repo” resolution), preventing fork clones with both origin and upstream remotes from being misidentified as upstream.
Changes:
- Detect org by parsing
git remote get-url originfrom$INVOCATION_DIR, usinggh repo view <origin-url>only as a fallback. - Emit a helpful hint when
upstreamexists and its owner differs fromorigin’s owner. - Expand end-to-end tests to cover fork-shaped repos with both
originandupstreamremotes (SSH + HTTPS), and adjust existing local-mode tests to pass--orgexplicitly.
File summaries
| File | Description |
|---|---|
| setup.sh | Switch org detection to parse the origin remote URL (with a constrained gh fallback) and add an upstream vs origin informational hint. |
| tests/test_setup_sh.py | Add coverage for origin-based org detection in fork-shaped clones (SSH/HTTPS) and make local-mode fixtures/tests deterministic via explicit --org. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 2
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| ORIGIN_URL="$(git -C "$INVOCATION_DIR" remote get-url origin 2>/dev/null || true)" | ||
| ORIGIN_ORG="" | ||
| if [ -n "$ORIGIN_URL" ]; then | ||
| ORIGIN_ORG="$(detect_org_from_url "$ORIGIN_URL")" | ||
| fi | ||
|
|
||
| if [ -n "$ORG" ]; then | ||
| ok "organisation $ORG (from --org)" | ||
| elif [ -n "$ORIGIN_ORG" ]; then | ||
| ORG="$ORIGIN_ORG" | ||
| ok "organisation $ORG (from the \`origin\` remote: $ORIGIN_URL)" | ||
| elif [ -n "$ORIGIN_URL" ] && [ "$LOCAL_MODE" -ne 1 ] \ | ||
| && ORG="$(gh repo view "$ORIGIN_URL" --json owner --jq .owner.login 2>/dev/null)" \ | ||
| && [ -n "$ORG" ]; then | ||
| ok "organisation $ORG (from \`gh repo view\` on the origin URL: $ORIGIN_URL; could not parse it by hand)" |
| elif [ "$LOCAL_MODE" -eq 1 ]; then | ||
| ORG="localorg" | ||
| ok "organisation $ORG (placeholder; --local-remote-dir creates nothing on GitHub)" | ||
| ok "organisation $ORG (placeholder; no \`origin\` remote to read, and --local-remote-dir creates nothing on GitHub)" |



The defect
Observed on the first real run: on a fork clone made with
gh repo fork opensoft/openRepoShape --org MedxSoft --clone, the clonehas TWO remotes —
origin(MedxSoft/openRepoShape) andupstream(
opensoft/openRepoShape). setup.sh's "(2) organisation" step rangh repo view --json ownerwith no repository argument;ghresolvesthe current repository by its own preference between the two remotes,
which can prefer
upstreamoverorigin. The run reported:...followed by the upstream-org guard refusing the run. The fork's
originwas correct; the detection method was wrong.The fix
origin's URL directly (git remote get-url origin, againstthe directory setup.sh was invoked from) and parse the owner by hand
for both SSH (
[email protected]:Owner/Repo.git) and HTTPS(
https://github.com/Owner/Repo(.git)) forms, reusing the existingPOSIX parameter-expansion parser (
detect_org_from_url).gh repo viewis now consulted only as a fallback, and only ON theorigin URL itself (
gh repo view "$ORIGIN_URL" ...), so it can nolonger resolve a different remote.
--orgstill overrides everything.one-line hint when an
upstreamremote exists with a differentowner than
origin(the normal fork shape):upstream is opensoft; scaffolding into MedxSoft.--local-remote-dir) previously skipped org detectionentirely (a
localorgplaceholder, always). It now runs the sameorigin-remote parsing (no
ghcall, so "gh not required" stillholds) and falls back to the placeholder only when there is no
originremote to read.README.md/AGENTS.mdalready described the intended behaviour("org detected from the fork's
origin") — no doc changes wereneeded there; the implementation just didn't match the docs.
Tests
Two new cases in
tests/test_setup_sh.pybuild a throwaway git repowith an
originremote pointing atExampleOrgand anupstreamremote pointing at
opensoft(one SSH-URL case, one HTTPS-URL case),run setup.sh from it with
--project Sample --yes --local-remote-dir <tmp> --into <tmp>/workand no--org, and assert it does not refuseand that the scaffolded
project.yamlrecordsExampleOrgas theowner of all three legs. Two existing local-mode tests that gave no
--orgnow pass--org demoorgexplicitly, since local mode's orgdetection is no longer a no-op and would otherwise read whatever
originthe suite happens to run inside (this repo's ownorigin,in CI). The hygiene test asserting no test here needs an authenticated
ghstill passes untouched.🤖 Generated with Claude Code
https://claude.ai/code/session_01HGKwdQsmvRZx4fGQESdFGZ
Summary by Sourcery
Use the clone’s origin remote as the authoritative source for organisation detection during setup.
Bug Fixes:
Enhancements:
Tests: