repo clone: thread lean ResolvedPlacement through the picker · Entire

repo clone: thread lean ResolvedPlacement through the picker

e076709→main·

matthiaswenz·yesterday·2 files·+48 added/-73 removed

Simplify follow-up: drop the single-impl mirrorResolver interface (the test uses a real client over httptest, not a fake) and the ResolvedPlacement→coreapi.Mirror mapping shim. selectCloneTarget and mirrorCellLabel now take coreapi.ResolvedPlacement directly, so resolvePullablePlacements returns the server's placements verbatim — no dead Owner/Repo/Provider/MirrorId fields populated just to fit the old Mirror shape. listMirrorsForRepo (and coreapi.Mirror) stay for the three routing callers.

Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com

Changes

2

113 unmodified lines

return fmt.Errorf("invalid <repo>: %w", err)

var mirrors []coreapi.Mirror
var placements []coreapi.ResolvedPlacement
lister := func(ctx context.Context, c *coreapi.Client) error {
    ms, err := resolvePullablePlacements(ctx, c, owner, repo)
    ps, err := resolvePullablePlacements(ctx, c, owner, repo)
    if err != nil {
        return err
    }
    mirrors = ms
    placements = ps
    return nil
}

if len(mirrors) == 0 {
    if len(placements) == 0 {
        return fmt.Errorf("no mirror found for /gh/%s/%s; run 'entire repo mirror create github.com/%s/%s' to onboard it", owner, repo, owner, repo)
    }

chosen, err := selectCloneTarget(cmd, mirrors, cluster)
    chosen, err := selectCloneTarget(cmd, placements, cluster)
    if err != nil {
        return err
    }
}

return matched, nil
}

// mirrorResolver is the subset of the control-plane client
// resolvePullablePlacements needs. Narrowing to an interface lets tests inject
// a fake; *coreapi.Client satisfies it.
type mirrorResolver interface {
    ResolveMirrorPlacements(ctx context.Context, params coreapi.ResolveMirrorPlacementsParams) (*coreapi.ResolvePlacementsOutputBody, error)
}

// resolvePullablePlacements returns every cluster placement of one GitHub
// upstream the caller may pull (clone). It backs `repo clone /gh/<owner>/<repo>`
// and deliberately differs from listMirrorsForRepo: that reads the

// anything clonable resolves, public or private-with-grant.
//
// owner/repo arrive already lowercased from parseMirrorCloneRef; the server
// matches case-insensitively regardless. Results map into coreapi.Mirror so the
// cluster picker (selectCloneTarget) is untouched — only the fields it reads
// (ClusterHost, Cell, Jurisdiction) plus the coords are populated. An empty
// result means not mirrored or not pullable, and the caller surfaces that.
func resolvePullablePlacements(ctx context.Context, c mirrorResolver, owner, repo string) ([]coreapi.Mirror, error) {
    out, err := c.ResolveMirrorPlacements(ctx, coreapi.ResolveMirrorPlacementsParams{
        Provider: coreapi.ResolveMirrorPlacementsProviderGithub,
        Owner:    owner,

if err != nil {
        return nil, fmt.Errorf("resolve mirror placements: %w", err)
    }
    mirrors := make([]coreapi.Mirror, 0, len(out.Placements))
    for _, p := range out.Placements {
        mirrors = append(mirrors, coreapi.Mirror{
            MirrorId:     p.MirrorId,
            Provider:     mirrorCloneProviderGitHub,
            Owner:        owner,
            Repo:         repo,
            ClusterHost:  p.ClusterHost,
            Cell:         p.Cell,
            Jurisdiction: p.Jurisdiction,
        })
    }
    return mirrors, nil
}

// selectCloneTarget resolves which mirror placement to clone from. With one
// placement it returns it directly. With --cluster it picks the matching one (or
// errors listing the available hosts). With more than one and no flag it prompts
// interactively, failing fast with a --cluster pointer when there's no terminal.
func selectCloneTarget(cmd *cobra.Command, mirrors []coreapi.Mirror, clusterFlag string) (coreapi.Mirror, error) {
    byHost := make(map[string]coreapi.Mirror, len(mirrors))
    hosts := make([]string, 0, len(mirrors))
    for _, m := range mirrors {
        key := strings.ToLower(m.ClusterHost)

if clusterFlag != "" {
        m, ok := byHost[strings.ToLower(strings.TrimSpace(clusterFlag))]
        if !ok {
            return coreapi.Mirror{}, fmt.Errorf("repo is not mirrored on %q; available: %s", clusterFlag, strings.Join(hosts, ", "))
        }
        return m, nil
    }

if len(hosts) == 1 {
    }

if !interactive.CanPromptInteractively() {
        return coreapi.Mirror{}, fmt.Errorf("repo is mirrored on %d clusters; pass --cluster to choose one of: %s", len(hosts), strings.Join(hosts, ", "))
    }

options := make([]huh.Option[string], len(hosts))

if cerr := handleFormCancellation(cmd.ErrOrStderr(), "Clone", err); cerr != nil {
        return coreapi.Mirror{}, cerr
    }
    return coreapi.Mirror{}, NewSilentError(errors.New("clone cancelled"))
}
    m, ok := byHost[selected]
    if !ok {
        return coreapi.Mirror{}, NewSilentError(errors.New("clone cancelled"))
    }
    return m, nil
}

// mirrorCellLabel is the human label for a mirror placement in the clone picker:
// the physical cell and jurisdiction when known, always anchored by the cluster
// host that goes into the clone URL.
func mirrorCellLabel(m coreapi.Mirror) string {
    cell := strings.TrimSpace(m.Cell.Or(""))
    jur := strings.TrimSpace(m.Jurisdiction.Or(""))
        return fmt.Sprintf("%s (%s) — %s", cell, jur, m.ClusterHost)
}

Mcmd/entire/cli/repo_clone.go+30/-51

76 unmodified lines

}

func TestSelectCloneTarget(t *testing.T) {
t.Parallel()

tests := []struct {
name   string
mirror coreapi.Mirror
want   string
}{
{
name:   "host only",
mirror: coreapi.Mirror{ClusterHost: "aws-us-east-2.entire.io"},
want:   "aws-us-east-2.entire.io",
},
{
name: "cell and jurisdiction",
mirror: coreapi.Mirror{
{
name: "cell without jurisdiction",
mirror: coreapi.Mirror{

test, err := selectCloneTarget(newCloneTestCmd(), []coreapi.Mirror{usEast}, "")
require.NoError(t, err)
require.Equal(t, "aws-us-east-2.entire.io", got.ClusterHost)
}

// TestResolvePullablePlacements_MapsPlacements verifies the clone-discovery
// resolver hits the pull-gated /mirrors/placements endpoint with the upstream
// coords and maps every returned placement into a coreapi.Mirror the picker can
// consume (host + cell + jurisdiction + coords).
func TestResolvePullablePlacements_MapsPlacements(t *testing.T) {

t.Parallel()
var gotPath, gotQuery string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// coords and returns every placement (host + cell + jurisdiction) for the
// picker.
require.Equal(t, "aws-us-east-2", got[0].Cell.Or(""))
require.Equal(t, "us", got[0].Jurisdiction.Or(""))
require.Equal(t, "01AAA", got[0].MirrorId)
require.Equal(t, "karthik-rameshkumar", got[0].Owner)
require.Equal(t, "my-entire", got[0].Repo)
require.Equal(t, "aws-eu-west-1.entire.io", got[1].ClusterHost)
}

Mcmd/entire/cli/repo_clone_test.go+18/-22