Skip to content

setup.sh: detect the org from origin, not from gh's remote preference - #3

Merged
brettheap merged 1 commit into
mainfrom
fix/setup-org-detection
Sep 2, 2026
Merged

brettheap merged 1 commit into
mainfrom
fix/setup-org-detection

Conversation

@brettheap

@brettheap brettheap commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

The defect

Observed on the first real run: on a fork clone made with
gh repo fork opensoft/openRepoShape --org MedxSoft --clone, the clone
has TWO remotes — origin (MedxSoft/openRepoShape) and upstream
(opensoft/openRepoShape). setup.sh's "(2) organisation" step ran
gh repo view --json owner with no repository argument; gh resolves
the current repository by its own preference between the two remotes,
which can prefer upstream over origin. The run reported:

✓ organisation opensoft (from `gh repo view` on this clone's origin)

...followed by the upstream-org guard refusing the run. The fork's
origin was correct; the detection method was wrong.

The 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 ([email protected]:Owner/Repo.git) and HTTPS
    (https://github.com/Owner/Repo(.git)) forms, reusing the existing
    POSIX parameter-expansion parser (detect_org_from_url).
  • gh repo view is now consulted only as a fallback, and only ON the
    origin URL itself (gh repo view "$ORIGIN_URL" ...), so it can no
    longer resolve a different remote.
  • --org still overrides everything.
  • Step (2) now names which remote/URL the org came from, and prints a
    one-line hint when an upstream remote exists with a different
    owner than origin (the normal fork shape):
    upstream is opensoft; scaffolding into MedxSoft.
  • Local mode (--local-remote-dir) previously skipped org detection
    entirely (a localorg placeholder, always). It now runs the same
    origin-remote parsing (no gh call, so "gh not required" still
    holds) and falls back to the placeholder only when there is no
    origin remote to read.

README.md/AGENTS.md already described the intended behaviour
("org detected from the fork's origin") — no doc changes were
needed there; the implementation just didn't match the docs.

Tests

Two new cases in tests/test_setup_sh.py build a throwaway git repo
with an origin remote pointing at ExampleOrg and an upstream
remote 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>/work and no --org, and assert it does not refuse
and that the scaffolded project.yaml records ExampleOrg as the
owner of all three legs. Two existing local-mode tests that gave no
--org now pass --org demoorg 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 (this repo's own origin,
in CI). The hygiene test asserting no test here needs an authenticated
gh still passes untouched.

106 passed in 20.61s

🤖 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:

  • Detect the organisation from the clone’s origin remote before consulting GitHub CLI, preventing upstream remotes from causing valid fork setups to be rejected.
  • Apply origin-based organisation detection in local mode while retaining an explicit placeholder only when no origin remote exists.

Enhancements:

  • Report the source of the detected organisation and warn when origin and upstream belong to different organisations.
  • Add coverage for SSH and HTTPS fork remotes and update local-mode test invocations to specify their intended organisation.

Tests:

  • Add end-to-end tests verifying that scaffolding uses the origin organisation when both fork and upstream remotes are present.

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
Copilot AI lite review requested due to automatic review settings September 2, 2026 23:29

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@sonarqubecloud

sonarqubecloud Bot commented Sep 2, 2026

Copy link
Copy Markdown

@sourcery-ai

sourcery-ai Bot commented Sep 2, 2026

Copy link
Copy Markdown

Reviewer's Guide

The PR fixes fork-clone organisation detection by reading and parsing the invocation directory’s origin remote before consulting gh, adds explicit upstream/origin diagnostics, preserves local-mode and --org semantics, and validates SSH and HTTPS fork layouts with regression tests.

Sequence diagram for origin-based organisation detection

sequenceDiagram
    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
Loading

Flow diagram for organisation detection precedence

flowchart 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]
Loading

File-Level Changes

Change Details Files
Make organisation detection authoritative to the invocation directory’s origin remote.
  • Read origin with git -C "$INVOCATION_DIR" remote get-url origin.
  • Parse SSH and HTTPS GitHub URLs using the existing POSIX parser.
  • Preserve --org as the highest-priority override.
  • Use gh repo view only as a non-local fallback and pass the explicit origin URL.
  • Retain the local placeholder only when no origin exists, while avoiding all gh calls in local mode.
  • Fail clearly when no organisation can be determined.
setup.sh
Improve fork diagnostics by identifying divergent upstream ownership.
  • Read and parse the upstream remote from the invocation directory.
  • Report when upstream and origin owners differ, including the selected scaffolding organisation.
setup.sh
Add regression coverage for origin-based detection and isolate local-mode tests from repository remotes.
  • Allow test runs to execute from a supplied working directory.
  • Create temporary repositories with separate SSH and HTTPS origin/upstream remotes.
  • Verify scaffolding succeeds and all manifest repositories use the origin owner.
  • Make existing local-mode tests pass an explicit organisation where detection is not under test.
tests/test_setup_sh.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 origin from $INVOCATION_DIR, using gh repo view <origin-url> only as a fallback.
  • Emit a helpful hint when upstream exists and its owner differs from origin’s owner.
  • Expand end-to-end tests to cover fork-shaped repos with both origin and upstream remotes (SSH + HTTPS), and adjust existing local-mode tests to pass --org explicitly.
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.

Comment thread setup.sh
Comment on lines +199 to +213
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)"
Comment thread setup.sh
Comment on lines 214 to +216
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)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants