Something went wrong. Try again.
An unofficial terminal client for Tangled, optimized for humans and agents. forked from aly.codes/tg
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191package app
import ( "context" "encoding/json" "fmt" "strings"
"github.com/alyraffauf/tg/internal/tangledlex" "github.com/alyraffauf/tg/tangled")
// defaultListLimit is the page size used when listing issues, pulls, and// records via the appview or PDS.const defaultListLimit int64 = 100
// recordView holds the fields common to an issue or pull-request record,// decoded from a tangled.ListItem's raw Value.type recordView struct { Title string Body string CreatedAt string SourceBranch string TargetBranch string}
func decodeIssue(raw json.RawMessage) (recordView, error) { var r tangledlex.RepoIssue if err := json.Unmarshal(raw, &r); err != nil { return recordView{}, err } return recordView{Title: r.Title, Body: stringValue(r.Body), CreatedAt: r.CreatedAt}, nil}
func decodePull(raw json.RawMessage) (recordView, error) { var r tangledlex.RepoPull if err := json.Unmarshal(raw, &r); err != nil { return recordView{}, err } return recordView{ Title: r.Title, Body: stringValue(r.Body), CreatedAt: r.CreatedAt, SourceBranch: pullSourceBranch(r.Source), TargetBranch: pullTargetBranch(r.Target), }, nil}
func stringValue(value *string) string { if value == nil { return "" } return *value}
func pullSourceBranch(source *tangledlex.RepoPull_Source) string { if source == nil { return "" } return source.Branch}
func pullTargetBranch(target *tangledlex.RepoPull_Target) string { if target == nil { return "" } return target.Branch}
// resolveAuthor resolves a DID to an author, falling back to the raw DID// string for Handle if resolution fails.func (s *Service) resolveAuthor(ctx context.Context, did string) Author { result := Author{DID: did} if ident, err := s.resolver.ResolveDID(ctx, did); err == nil { result.Handle = ident.Handle.String() } else { result.Handle = did } return result}
// buildItems decodes a listing's items into display/JSON-ready items,// silently skipping any whose Value fails to decode. decode is decodeIssue// or decodePull depending on the resource being listed.func (s *Service) buildItems(ctx context.Context, items []tangled.ListItem, decode func(json.RawMessage) (recordView, error)) ([]Item, []RecordWarning) { result := make([]Item, 0, len(items)) var warnings []RecordWarning
for _, listItem := range items { decoded, err := decode(listItem.Value) if err != nil { warnings = append(warnings, RecordWarning{URI: listItem.URI, Error: fmt.Sprintf("decode record: %v", err)}) continue }
updated := listItem.StateUpdatedAt if updated == "" { updated = decoded.CreatedAt }
title := decoded.Title if title == "" { title = "(no title)" }
result = append(result, Item{ Rkey: extractRKey(listItem.URI), URI: listItem.URI, Title: title, State: listItem.State, Author: s.resolveAuthor(ctx, extractDID(listItem.URI)), CreatedAt: decoded.CreatedAt, UpdatedAt: updated, CommentCount: listItem.CommentCount, SourceBranch: decoded.SourceBranch, TargetBranch: decoded.TargetBranch, }) }
return result, warnings}
func recordWarnings(warnings []tangled.RecordWarning) []RecordWarning { result := make([]RecordWarning, 0, len(warnings)) for _, warning := range warnings { result = append(result, RecordWarning{URI: warning.URI, Error: warning.Error}) } return result}
func extractDID(uri string) string { uri = strings.TrimPrefix(uri, "at://") did, _, _ := strings.Cut(uri, "/") return did}
func extractRKey(uri string) string { if idx := strings.LastIndex(uri, "/"); idx != -1 { return uri[idx+1:] } return uri}
// findByRKey finds the listing item whose URI ends in "/"+rkey. what names// the resource kind (e.g. "issue", "pull request") for the error message.func findByRKey(items []tangled.ListItem, rkey, what string) (*tangled.ListItem, error) { for i := range items { if strings.HasSuffix(items[i].URI, "/"+rkey) { return &items[i], nil } } return nil, fmt.Errorf("%s %q not found", what, rkey)}
// targetRecord resolves t, finds the issue/pull record rkey, and returns the// record URI and the repo record URI. collection selects issues or pulls.func (s *Service) targetRecord(ctx context.Context, t Target, collection, rkey string) (string, string, error) { repoRecord, err := s.resolveRepo(ctx, t) if err != nil { return "", "", err } repoDID := stringValue(repoRecord.Value.RepoDid) if repoDID == "" { return "", "", fmt.Errorf("repository %q has no repository DID", t.String()) }
var items []tangled.ListItem var recordType string if collection == issueCollection { issues, err := s.appview.ListIssues(ctx, repoDID, tangled.ListOpts{Limit: defaultListLimit}) if err != nil { return "", "", fmt.Errorf("list issues for %s: %w", t, err) } items = issues.Items recordType = "issue" } else { pulls, err := s.appview.ListPulls(ctx, repoDID, tangled.ListOpts{Limit: defaultListLimit}) if err != nil { return "", "", fmt.Errorf("list pull requests for %s: %w", t, err) } items = pulls.Items recordType = "pull request" }
record, err := findByRKey(items, rkey, recordType) if err != nil { return "", "", err } return record.URI, repoRecord.URI, nil}