From 31e4a8e54582518bdd46fef016caf94bc47b6ee2 Mon Sep 17 00:00:00 2001 From: Aly Raffauf Date: Wed, 15 Jul 2026 12:38:56 -0400 Subject: [PATCH] cli: share repository record resolution --- internal/cli/pr_create.go | 47 +--------------------- internal/cli/{write.go => repo_records.go} | 2 +- internal/cli/target.go | 24 ++--------- 3 files changed, 7 insertions(+), 66 deletions(-) rename internal/cli/{write.go => repo_records.go} (96%) diff --git a/internal/cli/pr_create.go b/internal/cli/pr_create.go index dd551cf..6bb1432 100644 --- a/internal/cli/pr_create.go +++ b/internal/cli/pr_create.go @@ -9,7 +9,6 @@ import ( "github.com/alyraffauf/tg/atproto" "github.com/alyraffauf/tg/internal/gitutil" - "github.com/alyraffauf/tg/tangled" "github.com/bluesky-social/indigo/atproto/syntax" "github.com/spf13/cobra" ) @@ -50,7 +49,7 @@ var prCreateCmd = &cobra.Command{ if err != nil { return err } - body, err := prBody() + body, err := commandBody(prCreateBody, prCreateBodyFile) if err != nil { return err } @@ -63,7 +62,7 @@ var prCreateCmd = &cobra.Command{ if err != nil { return err } - target, err := findTargetRepo(ctx, handle, repo) + target, err := resolveRepoRecord(ctx, handle, repo) if err != nil { return err } @@ -176,48 +175,6 @@ func prTargetBranch(ctx context.Context, repoDir string) (string, error) { return branch, nil } -func prBody() (string, error) { - if prCreateBodyFile == "" { - return prCreateBody, nil - } - if prCreateBody != "" { - return "", fmt.Errorf("--body and --body-file cannot be used together") - } - body, err := os.ReadFile(prCreateBodyFile) - if err != nil { - return "", fmt.Errorf("read pull request body: %w", err) - } - return string(body), nil -} - -func findTargetRepo(ctx context.Context, handle, name string) (*tangled.Repo, error) { - ident, err := resolver.ResolveHandle(ctx, handle) - if err != nil { - return nil, fmt.Errorf("resolve handle %q: %w", handle, err) - } - - uri := fmt.Sprintf("at://%s/sh.tangled.repo/%s", ident.DID, name) - if repo, err := client.GetRepo(ctx, uri); err == nil { - if repo.URI == "" { - repo.URI = uri - } - return repo, nil - } else if !isNotFoundError(err) { - return nil, fmt.Errorf("get repository %q: %w", name, err) - } - - repos, err := client.ListRepos(ctx, ident.DID.String()) - if err != nil { - return nil, fmt.Errorf("list repositories for %q: %w", handle, err) - } - for _, repo := range repos.Items { - if repo.Value.Name == name || strings.HasSuffix(repo.URI, "/"+name) { - return &repo, nil - } - } - return nil, fmt.Errorf("repo %q not found for handle %q", name, handle) -} - func createPullRecord(ctx context.Context, atClient *atproto.ATProto, did string, input prCreateRecord) (string, error) { now := time.Now().UTC().Format(time.RFC3339) record := pullRecord{ diff --git a/internal/cli/write.go b/internal/cli/repo_records.go similarity index 96% rename from internal/cli/write.go rename to internal/cli/repo_records.go index 50771c1..8c1368d 100644 --- a/internal/cli/write.go +++ b/internal/cli/repo_records.go @@ -25,7 +25,7 @@ func resolveRepoRecord(ctx context.Context, handle, name string) (*tangled.Repo, } return repo, nil } else if !isNotFoundError(err) { - return nil, err + return nil, fmt.Errorf("get repository %q: %w", name, err) } repos, err := client.ListRepos(ctx, ident.DID.String()) diff --git a/internal/cli/target.go b/internal/cli/target.go index c95d8de..6cd3a46 100644 --- a/internal/cli/target.go +++ b/internal/cli/target.go @@ -31,27 +31,11 @@ func resolveTarget(ctx context.Context, args []string) (string, string, error) { } // findRepoDid resolves handle/repo to the repo's repoDid, which listIssues is -// keyed by. It looks the record up directly by name (current schema uses the -// name as the rkey), falling back to a listing for legacy repos whose rkey is a -// TID with the name in the body. +// keyed by. func findRepoDid(ctx context.Context, handle, repo string) (string, error) { - ident, err := resolver.ResolveHandle(ctx, handle) + record, err := resolveRepoRecord(ctx, handle, repo) if err != nil { - return "", fmt.Errorf("resolve handle %q: %w", handle, err) + return "", err } - - repoURI := fmt.Sprintf("at://%s/sh.tangled.repo/%s", ident.DID, repo) - if got, err := client.GetRepo(ctx, repoURI); err == nil { - return got.Value.RepoDid, nil - } - - if repos, err := client.ListRepos(ctx, ident.DID.String()); err == nil { - for _, candidate := range repos.Items { - if candidate.Value.Name == repo || strings.HasSuffix(candidate.URI, "/"+repo) { - return candidate.Value.RepoDid, nil - } - } - } - - return "", fmt.Errorf("repo %q not found for handle %q", repo, handle) + return record.Value.RepoDid, nil } -- 2.51.2