diff --git a/internal/hub/draft_candidates.go b/internal/hub/draft_candidates.go index 5582619ea..9f200319b 100644 --- a/internal/hub/draft_candidates.go +++ b/internal/hub/draft_candidates.go @@ -31,7 +31,7 @@ func DraftCandidates(sourcePath string) ([]DraftEntry, error) { if candidate == nil || !candidate.Tracked { continue } - parsed, err := install.ParseSource(candidate.Source) + parsed, err := parseInstalled(candidate) if err == nil && parsed.Subdir == "" { entry = candidate relative, err := filepath.Rel(parent, skill.RelPath) @@ -43,10 +43,10 @@ func DraftCandidates(sourcePath string) ([]DraftEntry, error) { } } if entry != nil && entry.Source != "" { - setField(data, "source", entry.Source) + setField(data, "source", installedSource(entry)) if selector != "" { setField(data, "skill", selector) - } else if parsed, err := install.ParseSource(entry.Source); err == nil && parsed.Subdir == "" && entry.Subdir != "" { + } else if parsed, err := parseInstalled(entry); err == nil && parsed.Subdir == "" && entry.Subdir != "" { setField(data, "skill", entry.Subdir) } } @@ -64,3 +64,29 @@ func DraftCandidates(sourcePath string) ([]DraftEntry, error) { sort.Slice(entries, func(i, j int) bool { return entries[i].ID < entries[j].ID }) return entries, nil } + +// parseInstalled parses the entry's source as it was installed, so a web URL +// whose ref contains "/" splits into ref and subdir as the install did. +func parseInstalled(entry *install.MetadataEntry) (*install.Source, error) { + parsed, err := install.ParseSource(entry.Source) + if err == nil && entry.Branch != "" { + parsed.ApplyRecordedBranch(entry.Branch) + } + return parsed, err +} + +// installedSource returns the entry's source pinned at the ref it was +// installed from, so the hub entry installs the same version. +func installedSource(entry *install.MetadataEntry) string { + if entry.Branch == "" { + return entry.Source + } + parsed, err := parseInstalled(entry) + if err != nil { + return entry.Source + } + if pinned, err := parsed.AtRef(entry.Branch); err == nil { + return pinned + } + return entry.Source +} diff --git a/internal/hub/draft_document.go b/internal/hub/draft_document.go index ef3f5e46f..e2a00323f 100644 --- a/internal/hub/draft_document.go +++ b/internal/hub/draft_document.go @@ -119,6 +119,19 @@ func validateDraft(d Draft) error { return nil } +// DraftRefs maps each entry whose source names a ref to that ref. It never +// asks a remote, since every read and save of a draft goes through it: a ref +// containing "/" shows only its first segment until the editor settles it. +func DraftRefs(d Draft, opts install.ParseOptions) map[string]string { + refs := make(map[string]string) + for _, e := range d.Entries { + if ref := install.SourceRef(field(e.Data, "source"), opts, nil); ref != "" { + refs[e.ID] = ref + } + } + return refs +} + // DraftProblems lists all blockers, including local skills; none are silently omitted. func DraftProblems(d Draft) []DraftProblem { problems := make([]DraftProblem, 0) diff --git a/internal/hub/drafts_test.go b/internal/hub/drafts_test.go index 7b0dfc4c7..9ee154dd1 100644 --- a/internal/hub/drafts_test.go +++ b/internal/hub/drafts_test.go @@ -201,3 +201,29 @@ func TestDraftCandidatesTrackedRepository(t *testing.T) { t.Fatalf("tracked source lost: %#v", entries[0]) } } + +func TestDraftCandidatesKeepInstalledRef(t *testing.T) { + root := t.TempDir() + for _, path := range []string{"review", "_team/skills/lint"} { + os.MkdirAll(filepath.Join(root, path), 0755) + os.WriteFile(filepath.Join(root, path, "SKILL.md"), []byte("---\nname: x\n---\nX."), 0644) + } + os.MkdirAll(filepath.Join(root, "_team", ".git"), 0755) + os.WriteFile(filepath.Join(root, ".metadata.json"), []byte(`{"version":1,"entries":{ + "review":{"source":"acme/repo","subdir":"skills/review","branch":"v1.2.0"}, + "_team":{"source":"github.com/acme/team/tree/release/2","tracked":true,"branch":"release/2"}}}`), 0644) + entries, err := DraftCandidates(root) + if err != nil || len(entries) != 2 { + t.Fatalf("candidates: %v %#v", err, entries) + } + got := map[string]string{} + for _, e := range entries { + got[e.ID] = field(e.Data, "source") + " " + field(e.Data, "skill") + } + if want := "github.com/acme/repo/tree/v1.2.0 skills/review"; got["review"] != want { + t.Errorf("review = %q, want %q", got["review"], want) + } + if want := "github.com/acme/team/tree/release/2 skills/lint"; got["_team/skills/lint"] != want { + t.Errorf("tracked = %q, want %q", got["_team/skills/lint"], want) + } +} diff --git a/internal/install/source.go b/internal/install/source.go index 7f93541fd..ba39c099a 100644 --- a/internal/install/source.go +++ b/internal/install/source.go @@ -140,6 +140,9 @@ type Source struct { // webRef is the {ref}/{path} tail of a tree/, blob/ or src/ web URL, // kept so a ref containing "/" can be resolved at install time. webRef webRef + // webHost is "gitlab" or "bitbucket" for HTTPS sources on those hosts, + // whose web URLs can name a ref. + webHost string } // GitHub URL pattern: github.com/owner/repo[/path/to/subdir] @@ -716,6 +719,11 @@ func parseGitHTTPS(matches []string, source *Source, opts ParseOptions) (*Source source.Type = SourceTypeGitHTTPS source.CloneURL = fmt.Sprintf("%s://%s/%s.git", schema, host, repoPath) + if strings.Contains(host, "bitbucket") { + source.webHost = "bitbucket" + } else if isGitLabHost(host, opts.GitLabHosts) { + source.webHost = "gitlab" + } if err := validateCloneURL(source.CloneURL); err != nil { return nil, err diff --git a/internal/install/web_ref.go b/internal/install/web_ref.go index 290737c23..900676fab 100644 --- a/internal/install/web_ref.go +++ b/internal/install/web_ref.go @@ -2,8 +2,12 @@ package install import ( "context" + "errors" "fmt" + "os/exec" "path" + "slices" + "strconv" "strings" ) @@ -53,10 +57,23 @@ func (s *Source) applyWebRef(w webRef) string { // RemoteRefs caches each remote's branch and tag names, so resolving many web // URLs from one repo lists its refs once. The zero value is ready to use. type RemoteRefs struct { - byURL map[string]map[string]bool + byURL map[string]*RefList } -func (r *RemoteRefs) list(s *Source) (map[string]bool, error) { +// RefList is a remote's default branch, branches and tags. Branches are +// sorted by name; tags newest version first. +type RefList struct { + DefaultBranch string + Branches []string + Tags []string +} + +func (l *RefList) has(ref string) bool { + return slices.Contains(l.Branches, ref) || slices.Contains(l.Tags, ref) +} + +// List returns the source remote's refs, listing each remote once. +func (r *RemoteRefs) List(s *Source) (*RefList, error) { if refs, ok := r.byURL[s.CloneURL]; ok { return refs, nil } @@ -65,7 +82,7 @@ func (r *RemoteRefs) list(s *Source) (map[string]bool, error) { return nil, err } if r.byURL == nil { - r.byURL = make(map[string]map[string]bool) + r.byURL = make(map[string]*RefList) } r.byURL[s.CloneURL] = refs return refs, nil @@ -95,17 +112,17 @@ func (r *RemoteRefs) Resolve(s *Source) error { return s.settleWebRef(first) } - refs, err := r.list(s) + refs, err := r.List(s) if err != nil { return nil // let the clone report the real problem } - if refs[first] { + if refs.has(first) { return s.settleWebRef(first) } override := s.Branch != "" && s.Branch != first segments := strings.Split(w.tail, "/") for i := len(segments); i > 1; i-- { - if ref := strings.Join(segments[:i], "/"); refs[ref] { + if ref := strings.Join(segments[:i], "/"); refs.has(ref) { if !override { s.Branch = ref } @@ -118,6 +135,17 @@ func (r *RemoteRefs) Resolve(s *Source) error { return fmt.Errorf("ref %q from the URL was not found on the remote; pass --branch to choose one (needed when a branch name contains \"/\")", first) } +// Settle resolves the source's URL ref like Resolve, but reads a ref the +// remote does not have, or a remote that cannot be listed, as the URL's first +// segment, so the source can be re-pinned with AtRef. +func (r *RemoteRefs) Settle(s *Source) error { + if err := r.Resolve(s); err == nil && !s.HasAmbiguousWebRef() { + return nil + } + first, _, _ := strings.Cut(s.webRef.tail, "/") + return s.settleWebRef(first) +} + // HasAmbiguousWebRef reports whether the source's URL ref might contain "/", // so its real ref and subdir are only known after Resolve. func (s *Source) HasAmbiguousWebRef() bool { @@ -157,33 +185,89 @@ func (s *Source) settleWebRef(ref string) error { return nil } -// listRemoteRefs returns the remote's branch and tag names. -func listRemoteRefs(s *Source) (map[string]bool, error) { +// listRemoteRefs returns the remote's default branch, branches and tags. +func listRemoteRefs(s *Source) (*RefList, error) { ctx, cancel := context.WithTimeout(context.Background(), gitCommandTimeout) defer cancel() - cmd := gitCommand(ctx, "ls-remote", "--heads", "--tags", s.CloneURL) + cmd := gitCommand(ctx, "ls-remote", "--symref", s.CloneURL, "HEAD", "refs/heads/*", "refs/tags/*") cmd.Env = append(cmd.Env, s.authEnv()...) out, err := cmd.Output() if err != nil { - return nil, err + var exitErr *exec.ExitError + if errors.As(err, &exitErr) && len(exitErr.Stderr) > 0 { + return nil, fmt.Errorf("git ls-remote: %s", strings.TrimSpace(string(exitErr.Stderr))) + } + return nil, fmt.Errorf("git ls-remote: %w", err) } - refs := make(map[string]bool) + refs := &RefList{Branches: []string{}, Tags: []string{}} for _, line := range strings.Split(string(out), "\n") { - _, name, ok := strings.Cut(strings.TrimSpace(line), "\t") + value, name, ok := strings.Cut(strings.TrimSpace(line), "\t") if !ok { continue } - name = strings.TrimSuffix(name, "^{}") - if b, ok := strings.CutPrefix(name, "refs/heads/"); ok { - refs[b] = true - } else if t, ok := strings.CutPrefix(name, "refs/tags/"); ok { - refs[t] = true + if target, ok := strings.CutPrefix(value, "ref: refs/heads/"); ok && name == "HEAD" { + refs.DefaultBranch = target + } else if b, ok := strings.CutPrefix(name, "refs/heads/"); ok { + refs.Branches = append(refs.Branches, b) + } else if t, ok := strings.CutPrefix(name, "refs/tags/"); ok && !strings.HasSuffix(t, "^{}") { + refs.Tags = append(refs.Tags, t) } } + slices.Sort(refs.Branches) + slices.SortFunc(refs.Tags, compareTagsNewestFirst) return refs, nil } +// compareTagsNewestFirst orders version tags (v1.2.0, 1.2) by descending +// version, before any other tags, which follow in reverse name order. +func compareTagsNewestFirst(a, b string) int { + va, okA := parseTagVersion(a) + vb, okB := parseTagVersion(b) + switch { + case okA && okB: + if c := slices.Compare(vb.numbers, va.numbers); c != 0 { + return c + } + // A release sorts before its pre-releases. + if (va.pre == "") != (vb.pre == "") { + if va.pre == "" { + return -1 + } + return 1 + } + return strings.Compare(b, a) + case okA: + return -1 + case okB: + return 1 + } + return strings.Compare(b, a) +} + +type tagVersion struct { + numbers []int + pre string +} + +func parseTagVersion(tag string) (tagVersion, bool) { + core, pre, _ := strings.Cut(strings.TrimPrefix(tag, "v"), "-") + core, _, _ = strings.Cut(core, "+") + parts := strings.Split(core, ".") + if len(parts) > 3 { + return tagVersion{}, false + } + numbers := make([]int, 3) + for i, p := range parts { + n, err := strconv.Atoi(p) + if err != nil || n < 0 { + return tagVersion{}, false + } + numbers[i] = n + } + return tagVersion{numbers: numbers, pre: pre}, true +} + // ApplyRecordedBranch sets the branch a skill was installed with, for // reinstalling it from its recorded source URL. An empty branch means the // remote default even when the URL names a ref: installs from before URL refs @@ -200,3 +284,107 @@ func (s *Source) ApplyRecordedBranch(branch string) { _ = s.settleWebRef(branch) } } + +// ErrRefNotPinnable reports a source whose URL cannot name a ref: only +// GitHub, GitLab and Bitbucket web URLs can. +var ErrRefNotPinnable = errors.New("only GitHub, GitLab and Bitbucket sources can pin a ref") + +// webRepo returns the repo's web URL and the path markers that come before a +// ref in tree and file URLs, or ok=false for hosts whose URLs cannot name a ref. +func (s *Source) webRepo() (repo, tree, blob string, ok bool) { + base := strings.TrimSuffix(s.CloneURL, ".git") + switch { + case s.Type == SourceTypeGitHub: + return strings.TrimPrefix(base, "https://"), "tree", "blob", true + case s.Type == SourceTypeGitHTTPS && s.webHost == "gitlab": + return base, "-/tree", "-/blob", true + case s.Type == SourceTypeGitHTTPS && s.webHost == "bitbucket": + return base, "src", "src", true + } + return "", "", "", false +} + +// PinnedRef returns the ref the source's URL names ("" for the remote +// default branch), and ok=false when the source cannot name one. A ref that +// may contain "/" is only its first segment until RemoteRefs.Resolve. +func (s *Source) PinnedRef() (ref string, ok bool) { + if _, _, _, ok := s.webRepo(); !ok { + return "", false + } + return s.Branch, true +} + +// AtRef returns the source as a web URL that pins ref, keeping its subdir and +// SKILL.md target. An empty ref means the remote default branch. +func (s *Source) AtRef(ref string) (string, error) { + repo, tree, blob, ok := s.webRepo() + if !ok { + return "", ErrRefNotPinnable + } + if s.HasAmbiguousWebRef() { + return "", fmt.Errorf("the ref in %q could not be resolved", s.Raw) + } + if ref != "" && (strings.ContainsAny(ref, " \t\r\n?#\\") || strings.Contains(ref, "..") || + strings.HasPrefix(ref, "/") || strings.HasSuffix(ref, "/") || strings.HasPrefix(ref, "-")) { + return "", fmt.Errorf("invalid ref %q", ref) + } + marker, tail := tree, s.Subdir + if s.ExplicitSkill { + marker, tail = blob, path.Join(s.Subdir, "SKILL.md") + } + if ref == "" { + // GitHub URLs need no ref. Elsewhere a repo root keeps ".git" and a path + // gets HEAD for the default branch, so a path joined on later is not + // read as part of the repo. + if !s.ExplicitSkill && s.Type == SourceTypeGitHub { + return joinURLPath(repo, s.Subdir), nil + } + if !s.ExplicitSkill && s.Subdir == "" { + return repo + ".git", nil + } + ref = "HEAD" + } + return joinURLPath(repo, marker, ref, tail), nil +} + +func joinURLPath(parts ...string) string { + return strings.Join(slices.DeleteFunc(parts, func(p string) bool { return p == "" }), "/") +} + +// SourceAtRef rewrites a GitHub, GitLab or Bitbucket source as a web URL +// pinned at ref (the remote default branch when ref is empty). A URL ref that +// may contain "/" is settled against the remote first. +func SourceAtRef(raw, ref string, opts ParseOptions) (string, error) { + s, err := ParseSourceWithOptions(raw, opts) + if err != nil { + return "", err + } + if _, _, _, ok := s.webRepo(); !ok { + return "", ErrRefNotPinnable + } + if err := (&RemoteRefs{}).Settle(s); err != nil { + return "", err + } + return s.AtRef(ref) +} + +// SourceRef returns the ref a source's URL names, or "" for the default +// branch, other hosts and unparseable sources. A ref that may contain "/" is +// settled against the remote, listed once per remote through refs; with nil +// refs it stays the URL's first segment and nothing is asked of the remote. +func SourceRef(raw string, opts ParseOptions, refs *RemoteRefs) string { + s, err := ParseSourceWithOptions(raw, opts) + if err != nil { + return "" + } + if _, ok := s.PinnedRef(); !ok { + return "" + } + if refs != nil { + // Settle only fails on an invalid subdir, which install reports; the + // ref it settled on still stands. + _ = refs.Settle(s) + } + ref, _ := s.PinnedRef() + return ref +} diff --git a/internal/install/web_ref_test.go b/internal/install/web_ref_test.go index a588d983d..3f00b5dcc 100644 --- a/internal/install/web_ref_test.go +++ b/internal/install/web_ref_test.go @@ -1,6 +1,8 @@ package install import ( + "errors" + "fmt" "os" "path/filepath" "strings" @@ -200,3 +202,98 @@ func TestApplyRecordedBranch(t *testing.T) { }) } } + +func TestRemoteRefs_ListSplitsBranchesAndTags(t *testing.T) { + remote := newRefsRemote(t) + for _, tag := range []string{"v1.10.0", "v1.2.0", "v1.2.0-rc.1", "nightly"} { + runGit(t, remote, "tag", "-a", tag, "-m", tag) + } + var refs RemoteRefs + list, err := refs.List(parseWithRemote(t, "github.com/o/r", remote)) + if err != nil { + t.Fatalf("List: %v", err) + } + got := fmt.Sprintf("%s %v %v", list.DefaultBranch, list.Branches, list.Tags) + want := "main [feature/x main] [v1.10.0 v1.2.0 v1.2.0-rc.1 v1.0 nightly]" + if got != want { + t.Errorf("refs = %s, want %s", got, want) + } +} + +func TestSourceAtRef(t *testing.T) { + tests := []struct { + name, raw, ref, want string + }{ + {"github shorthand", "o/r/skills/foo", "v1.2.0", "github.com/o/r/tree/v1.2.0/skills/foo"}, + {"github URL replaces its ref", "https://github.com/o/r/tree/v1.0/skills/foo", "main", "github.com/o/r/tree/main/skills/foo"}, + {"slash branch is replaced whole", "github.com/o/r/tree/feature/x/skills/foo", "v1.0", "github.com/o/r/tree/v1.0/skills/foo"}, + {"ref the remote lacks is replaced", "github.com/o/r/tree/master/skills/foo", "main", "github.com/o/r/tree/main/skills/foo"}, + {"github file URL stays a file URL", "github.com/o/r/blob/v1.0/skills/foo/SKILL.md", "v2", "github.com/o/r/blob/v2/skills/foo/SKILL.md"}, + {"github default branch drops the ref", "github.com/o/r/tree/v1.0/skills/foo", "", "github.com/o/r/skills/foo"}, + {"gitlab", "https://gitlab.com/g/r/-/tree/main/skills/foo", "v1.0", "https://gitlab.com/g/r/-/tree/v1.0/skills/foo"}, + {"gitlab default branch keeps the subdir apart", "https://gitlab.com/g/r/-/tree/v1.0/skills/foo", "", "https://gitlab.com/g/r/-/tree/HEAD/skills/foo"}, + {"gitlab repo root", "https://gitlab.com/g/r.git", "v1.0", "https://gitlab.com/g/r/-/tree/v1.0"}, + {"bitbucket", "https://bitbucket.org/o/r/src/main/skills/foo", "v1.0", "https://bitbucket.org/o/r/src/v1.0/skills/foo"}, + {"gitlab default branch at the root", "https://gitlab.com/g/r/-/tree/v1.0", "", "https://gitlab.com/g/r.git"}, + {"bitbucket default branch at the root", "https://bitbucket.org/o/r/src/v1.0", "", "https://bitbucket.org/o/r.git"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + source, err := ParseSource(tt.raw) + if err != nil { + t.Fatal(err) + } + // Stand in for the remote so no test touches the network. + refs := RemoteRefs{byURL: map[string]*RefList{ + source.CloneURL: {Branches: []string{"feature/x", "main"}, Tags: []string{"v1.0"}}, + }} + if err := refs.Settle(source); err != nil { + t.Fatalf("Settle: %v", err) + } + got, err := source.AtRef(tt.ref) + if err != nil { + t.Fatalf("AtRef: %v", err) + } + if got != tt.want { + t.Errorf("AtRef(%q) of %q = %q, want %q", tt.ref, tt.raw, got, tt.want) + } + if ref := SourceRef(got, ParseOptions{}, &refs); ref != tt.ref { + t.Errorf("SourceRef(%q) = %q, want %q", got, ref, tt.ref) + } + }) + } +} + +// The dashboard joins a skill's path onto a repo root pinned at the default +// branch, so that root must keep the repo and the path apart. +func TestSourceAtRef_DefaultBranchRootTakesAPath(t *testing.T) { + for _, raw := range []string{"https://gitlab.com/g/r/-/tree/v1.0", "https://bitbucket.org/o/r/src/v1.0"} { + source, err := ParseSource(raw) + if err != nil { + t.Fatal(err) + } + refs := RemoteRefs{byURL: map[string]*RefList{source.CloneURL: {Tags: []string{"v1.0"}}}} + if err := refs.Settle(source); err != nil { + t.Fatalf("Settle: %v", err) + } + root, err := source.AtRef("") + if err != nil { + t.Fatalf("AtRef: %v", err) + } + joined, err := ParseSource(root + "/skills/foo") + if err != nil { + t.Fatal(err) + } + if joined.CloneURL != source.CloneURL || joined.Subdir != "skills/foo" { + t.Errorf("%q/skills/foo parsed as repo %q subdir %q, want repo %q subdir skills/foo", root, joined.CloneURL, joined.Subdir, source.CloneURL) + } + } +} + +func TestSourceAtRef_RejectsOtherHosts(t *testing.T) { + for _, raw := range []string{"https://git.example.com/o/r", "git@github.com:o/r.git", "/tmp/skills", "file:///tmp/r"} { + if _, err := SourceAtRef(raw, "v1.0", ParseOptions{}); !errors.Is(err, ErrRefNotPinnable) { + t.Errorf("SourceAtRef(%q) err = %v, want ErrRefNotPinnable", raw, err) + } + } +} diff --git a/internal/server/handler_hub_drafts.go b/internal/server/handler_hub_drafts.go index 344769943..692888edb 100644 --- a/internal/server/handler_hub_drafts.go +++ b/internal/server/handler_hub_drafts.go @@ -30,8 +30,11 @@ func draftError(w http.ResponseWriter, err error) { writeError(w, status, err.Error()) } -func writeDraft(w http.ResponseWriter, d hub.Draft) { - writeJSON(w, map[string]any{"draft": d, "problems": hub.DraftProblems(d)}) +func (s *Server) writeDraft(w http.ResponseWriter, d hub.Draft) { + s.mu.RLock() + parseOpts := s.parseOpts() + s.mu.RUnlock() + writeJSON(w, map[string]any{"draft": d, "problems": hub.DraftProblems(d), "refs": hub.DraftRefs(d, parseOpts)}) } func decodeDraftBody(w http.ResponseWriter, r *http.Request, dst any) error { @@ -80,7 +83,7 @@ func (s *Server) handleHubDrafts(w http.ResponseWriter, r *http.Request) { return } s.writeOpsLog("hub-draft-create", "ok", start, map[string]any{"id": created.ID}, "") - writeDraft(w, created) + s.writeDraft(w, created) } func (s *Server) handleHubDraft(w http.ResponseWriter, r *http.Request) { @@ -93,7 +96,7 @@ func (s *Server) handleHubDraft(w http.ResponseWriter, r *http.Request) { draftError(w, err) return } - writeDraft(w, d) + s.writeDraft(w, d) case http.MethodPut: var d hub.Draft if err := decodeDraftBody(w, r, &d); err != nil { @@ -107,7 +110,7 @@ func (s *Server) handleHubDraft(w http.ResponseWriter, r *http.Request) { return } s.writeOpsLog("hub-draft-save", "ok", start, map[string]any{"id": id}, "") - writeDraft(w, saved) + s.writeDraft(w, saved) case http.MethodDelete: if err := store.Delete(id, r.URL.Query().Get("revision")); err != nil { draftError(w, err) @@ -136,7 +139,7 @@ func (s *Server) handleHubDraftImport(w http.ResponseWriter, r *http.Request) { return } s.writeOpsLog("hub-draft-import", "ok", start, map[string]any{"id": d.ID}, "") - writeDraft(w, d) + s.writeDraft(w, d) } func (s *Server) handleHubDraftExport(w http.ResponseWriter, r *http.Request) { diff --git a/internal/server/handler_hub_refs.go b/internal/server/handler_hub_refs.go new file mode 100644 index 000000000..01b6213db --- /dev/null +++ b/internal/server/handler_hub_refs.go @@ -0,0 +1,73 @@ +package server + +import ( + "encoding/json" + "net/http" + + "skillshare/internal/install" +) + +type hubRefsResponse struct { + Pinnable bool `json:"pinnable"` + Current string `json:"current"` + DefaultBranch string `json:"defaultBranch"` + Branches []string `json:"branches"` + Tags []string `json:"tags"` + Source *string `json:"source,omitempty"` +} + +// handleHubRefs lists the branches and tags a hub entry's source can pin, and +// with "ref" in the body also returns the source rewritten to pin that ref. +func (s *Server) handleHubRefs(w http.ResponseWriter, r *http.Request) { + s.mu.RLock() + parseOpts := s.parseOpts() + s.mu.RUnlock() + + var body struct { + Source string `json:"source"` + Ref *string `json:"ref"` + } + if err := json.NewDecoder(r.Body).Decode(&body); err != nil { + writeError(w, http.StatusBadRequest, "invalid JSON body") + return + } + if body.Source == "" { + writeError(w, http.StatusBadRequest, "source is required") + return + } + source, err := install.ParseSourceWithOptions(body.Source, parseOpts) + if err != nil { + writeError(w, http.StatusBadRequest, "invalid source: "+err.Error()) + return + } + + resp := hubRefsResponse{Branches: []string{}, Tags: []string{}} + if _, ok := source.PinnedRef(); !ok { + writeJSON(w, resp) + return + } + var refs install.RemoteRefs + list, err := refs.List(source) + if err != nil { + writeError(w, http.StatusInternalServerError, err.Error()) + return + } + if err := refs.Settle(source); err != nil { + writeError(w, http.StatusBadRequest, "invalid source: "+err.Error()) + return + } + resp.Pinnable = true + resp.Current, _ = source.PinnedRef() + resp.DefaultBranch = list.DefaultBranch + resp.Branches = list.Branches + resp.Tags = list.Tags + if body.Ref != nil { + pinned, err := source.AtRef(*body.Ref) + if err != nil { + writeError(w, http.StatusBadRequest, err.Error()) + return + } + resp.Source = &pinned + } + writeJSON(w, resp) +} diff --git a/internal/server/handler_hub_refs_test.go b/internal/server/handler_hub_refs_test.go new file mode 100644 index 000000000..3cf2d7785 --- /dev/null +++ b/internal/server/handler_hub_refs_test.go @@ -0,0 +1,87 @@ +package server + +import ( + "bytes" + "encoding/json" + "maps" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "testing" +) + +func postHubRefs(t *testing.T, s *Server, body string) *httptest.ResponseRecorder { + t.Helper() + rr := httptest.NewRecorder() + s.handler.ServeHTTP(rr, httptest.NewRequest("POST", "/api/hub/refs", bytes.NewBufferString(body))) + return rr +} + +func TestHubRefs_RejectsInvalidSource(t *testing.T) { + s, _ := newTestServer(t) + for _, body := range []string{`{}`, `{"source":""}`, `not json`} { + if rr := postHubRefs(t, s, body); rr.Code != http.StatusBadRequest { + t.Errorf("%s: status %d, want 400", body, rr.Code) + } + } +} + +func TestHubRefs_NotPinnable(t *testing.T) { + s, _ := newTestServer(t) + rr := postHubRefs(t, s, `{"source":"https://git.example.com/o/r/skills/foo","ref":"v1"}`) + if rr.Code != http.StatusOK { + t.Fatalf("status %d: %s", rr.Code, rr.Body.String()) + } + want := `{"pinnable":false,"current":"","defaultBranch":"","branches":[],"tags":[]}` + if got := bytes.TrimSpace(rr.Body.Bytes()); string(got) != want { + t.Errorf("body = %s, want %s", got, want) + } +} + +// Draft refs are read from the URLs alone, without asking the remote. +func TestHubDraftRefs(t *testing.T) { + s, _ := newTestServer(t) + rr := httptest.NewRecorder() + body := `{"schemaVersion":1,"skills":[` + + `{"name":"a","source":"github.com/acme/r/tree/v1.0/skills/a"},` + + `{"name":"b","source":"acme/r/skills/b"}]}` + s.handler.ServeHTTP(rr, httptest.NewRequest("POST", "/api/hub/drafts/import", bytes.NewBufferString(body))) + var resp struct { + Draft struct { + Entries []struct { + ID string `json:"id"` + } `json:"entries"` + } `json:"draft"` + Refs map[string]string `json:"refs"` + } + if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil || len(resp.Draft.Entries) != 2 { + t.Fatalf("import: %d %s", rr.Code, rr.Body.String()) + } + entries := resp.Draft.Entries + want := map[string]string{entries[0].ID: "v1.0"} + if !maps.Equal(resp.Refs, want) { + t.Errorf("refs = %v, want %v", resp.Refs, want) + } +} + +func TestHandleSearch_BuiltinResultsCarryRef(t *testing.T) { + s, src := newTestServer(t) + os.MkdirAll(filepath.Join(src, "foo"), 0755) + os.WriteFile(filepath.Join(src, "foo", "SKILL.md"), []byte("---\nname: foo\n---\nFoo."), 0644) + os.WriteFile(filepath.Join(src, ".metadata.json"), []byte(`{"version":1,"entries":{"foo":{"source":"github.com/acme/r/tree/v2.0/skills/foo"}}}`), 0644) + + rr := httptest.NewRecorder() + s.handler.ServeHTTP(rr, httptest.NewRequest("GET", "/api/search?q=foo&hub=@builtin", nil)) + var resp struct { + Results []struct { + Ref string `json:"ref"` + } `json:"results"` + } + if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil || len(resp.Results) != 1 { + t.Fatalf("search: %d %s", rr.Code, rr.Body.String()) + } + if resp.Results[0].Ref != "v2.0" { + t.Errorf("ref = %q, want v2.0", resp.Results[0].Ref) + } +} diff --git a/internal/server/handler_search.go b/internal/server/handler_search.go index 2621446a7..b50b1cdaf 100644 --- a/internal/server/handler_search.go +++ b/internal/server/handler_search.go @@ -16,6 +16,7 @@ func (s *Server) handleSearch(w http.ResponseWriter, r *http.Request) { // Snapshot config under RLock, then release before I/O. s.mu.RLock() source := s.skillsSource() + parseOpts := s.parseOpts() hubCfg := s.cfg.Hub if s.IsProjectMode() && s.projectCfg != nil { hubCfg = s.projectCfg.Hub @@ -63,6 +64,7 @@ func (s *Server) handleSearch(w http.ResponseWriter, r *http.Request) { Name string `json:"name"` Description string `json:"description"` Source string `json:"source"` + Ref string `json:"ref,omitempty"` Skill string `json:"skill,omitempty"` Stars int `json:"stars"` Owner string `json:"owner"` @@ -76,11 +78,14 @@ func (s *Server) handleSearch(w http.ResponseWriter, r *http.Request) { Name: r.Name, Description: r.Description, Source: r.Source, - Skill: r.Skill, - Stars: r.Stars, - Owner: r.Owner, - Repo: r.Repo, - Tags: r.Tags, + // A hosted index can hold hundreds of entries, so the remote is not + // asked; a branch containing "/" shows as its first segment. + Ref: install.SourceRef(r.Source, parseOpts, nil), + Skill: r.Skill, + Stars: r.Stars, + Owner: r.Owner, + Repo: r.Repo, + Tags: r.Tags, }) } diff --git a/internal/server/server.go b/internal/server/server.go index 7ffa23530..ee12d4580 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -476,6 +476,7 @@ func (s *Server) registerRoutes() { s.mux.HandleFunc("DELETE /api/hub/drafts/{id}", s.handleHubDraft) s.mux.HandleFunc("POST /api/hub/drafts/{id}/export", s.handleHubDraftExport) s.mux.HandleFunc("GET /api/hub/index", s.handleHubIndex) + s.mux.HandleFunc("POST /api/hub/refs", s.handleHubRefs) s.mux.HandleFunc("GET /api/hub/saved", s.handleGetHubSaved) s.mux.HandleFunc("PUT /api/hub/saved", s.handlePutHubSaved) s.mux.HandleFunc("POST /api/hub/saved", s.handlePostHubSaved) diff --git a/ui/src/api/client.ts b/ui/src/api/client.ts index 5b47a54c4..b2c33df15 100644 --- a/ui/src/api/client.ts +++ b/ui/src/api/client.ts @@ -1207,6 +1207,8 @@ export interface SearchResult { owner: string; repo: string; tags?: string[]; + /** Branch or tag the source names; empty or absent means the default branch. */ + ref?: string; } export interface SkillPreview { diff --git a/ui/src/api/hubDrafts.ts b/ui/src/api/hubDrafts.ts index da4f3692b..9d041ac0f 100644 --- a/ui/src/api/hubDrafts.ts +++ b/ui/src/api/hubDrafts.ts @@ -14,7 +14,21 @@ export interface HubDraft { fields: Record; } export interface HubProblem { entryId: string; code: string } -export interface DraftResponse { draft: HubDraft; problems: HubProblem[] } +export interface DraftResponse { + draft: HubDraft; + problems: HubProblem[]; + /** Branch or tag each entry's source names, keyed by entry id; entries on the default branch are absent. */ + refs?: Record; +} +/** Versions a source can be pinned to. `source` is set only when a `ref` was asked for. */ +export interface HubRefs { + pinnable: boolean; + current: string; + defaultBranch: string; + branches: string[]; + tags: string[]; + source?: string; +} const root = '/hub/drafts'; const json = (method: string, data: unknown) => ({ method, body: JSON.stringify(data) }); export const hubDrafts = { @@ -28,6 +42,10 @@ export const hubDrafts = { export: (draft: HubDraft) => apiFetch>(`${root}/${encodeURIComponent(draft.id)}/export`, json('POST', { revision: draft.revision })), }; +/** Lists a source's branches and tags; with `ref` ("" = default branch), also returns the source rewritten to it. */ +export const hubRefs = (source: string, ref?: string) => + apiFetch('/hub/refs', json('POST', ref === undefined ? { source } : { source, ref })); + export function hubAddCommand(location: string, label: string): string { const quote = (s: string) => `'${s.replace(/'/g, `'"'"'`)}'`; return `skillshare hub add ${quote(location.trim())}${label.trim() ? ` --label ${quote(label.trim())}` : ''}`; diff --git a/ui/src/components/hub/AddHubMenu.tsx b/ui/src/components/hub/AddHubMenu.tsx new file mode 100644 index 000000000..98a6d5c95 --- /dev/null +++ b/ui/src/components/hub/AddHubMenu.tsx @@ -0,0 +1,110 @@ +import { useEffect, useRef, useState } from 'react'; +import { FilePlus, Plus, Upload } from 'lucide-react'; +import Button from '../Button'; +import { useT } from '../../i18n'; + +interface Props { + /** Rejects with a message the menu shows under the field. */ + onAdd: (url: string) => Promise; + onCreate: () => void; + onImport: (file: File) => void; +} + +/** One entry point for every way a hub gets into the list. */ +export default function AddHubMenu({ onAdd, onCreate, onImport }: Props) { + const t = useT(); + const [open, setOpen] = useState(false); + const [url, setURL] = useState(''); + const [error, setError] = useState(''); + const [busy, setBusy] = useState(false); + const ref = useRef(null); + const fileInput = useRef(null); + + useEffect(() => { + if (!open) return; + const onDown = (e: MouseEvent) => { + if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false); + }; + const onKey = (e: KeyboardEvent) => { + if (e.key === 'Escape') setOpen(false); + }; + document.addEventListener('mousedown', onDown); + document.addEventListener('keydown', onKey); + return () => { + document.removeEventListener('mousedown', onDown); + document.removeEventListener('keydown', onKey); + }; + }, [open]); + + const add = async () => { + setBusy(true); + setError(''); + try { + await onAdd(url); + setURL(''); + setOpen(false); + } catch (e) { + setError((e as Error).message); + } finally { + setBusy(false); + } + }; + + return ( +
+ { + const file = e.target.files?.[0]; + e.target.value = ''; + if (file) onImport(file); + }} + /> + + {open && ( +
+
{ e.preventDefault(); void add(); }} + > + +
+ + { setURL(e.target.value); setError(''); }} + /> + + +
+ {error ? {error} : {t('hubs.add.hint')}} +
+
+ {[ + { icon: FilePlus, title: t('hubs.add.create'), hint: t('hubs.add.createHint'), run: onCreate }, + { icon: Upload, title: t('hubs.add.import'), hint: t('hubs.add.importHint'), run: () => fileInput.current?.click() }, + ].map(({ icon: Icon, title, hint, run }) => ( + + ))} +
+ )} +
+ ); +} diff --git a/ui/src/components/hub/AddSkillDialog.tsx b/ui/src/components/hub/AddSkillDialog.tsx new file mode 100644 index 000000000..64428d7c1 --- /dev/null +++ b/ui/src/components/hub/AddSkillDialog.tsx @@ -0,0 +1,230 @@ +import { useState } from 'react'; +import { useQuery } from '@tanstack/react-query'; +import { X } from 'lucide-react'; +import { api } from '../../api/client'; +import type { DiscoveredSkill } from '../../api/client'; +import { hubDrafts, hubRefs } from '../../api/hubDrafts'; +import type { HubEntry, HubRefs } from '../../api/hubDrafts'; +import Button from '../Button'; +import DialogShell from '../DialogShell'; +import Spinner from '../Spinner'; +import { Checkbox, Select } from '../Input'; +import { inHub, refOptions, skillSource } from './hubShared'; +import { queryKeys } from '../../lib/queryKeys'; +import { useT } from '../../i18n'; + +interface Props { + hubName: string; + /** What the hub already holds, so nothing is added twice. */ + entries: HubEntry[]; + onAdd: (data: HubEntry['data'][]) => void; + /** Adds an empty row to fill in by hand. */ + onManual: () => void; + onClose: () => void; +} + +interface Found { + /** What the user pasted. */ + source: string; + refs: HubRefs; + version: string; + /** The pasted source at the chosen version; each skill's source starts from it. */ + root: string; + skills: DiscoveredSkill[]; +} + +/** Adds skills from a Git URL, at a chosen version, or from what is installed here. */ +export default function AddSkillDialog({ hubName, entries, onAdd, onManual, onClose }: Props) { + const t = useT(); + const [tab, setTab] = useState<'url' | 'installed'>('url'); + const [url, setURL] = useState(''); + const [found, setFound] = useState(null); + const [busy, setBusy] = useState(false); + const [error, setError] = useState(''); + const [picked, setPicked] = useState([]); + const [query, setQuery] = useState(''); + const [installedPicked, setInstalledPicked] = useState([]); + + const candidates = useQuery({ + queryKey: queryKeys.hub.candidates, + queryFn: () => hubDrafts.candidates(), + enabled: tab === 'installed', + }); + + const foundSource = (skill: DiscoveredSkill) => skillSource(found!.root, skill.path); + const addable = (skills: DiscoveredSkill[], root: string) => + skills.filter((s) => !inHub(entries, skillSource(root, s.path), '', s.name)).map((s) => s.path); + + async function find() { + const source = url.trim(); + if (!source) return; + setBusy(true); + setError(''); + try { + const [refs, discovered] = await Promise.all([hubRefs(source), api.discover(source)]); + // A pasted GitLab or Bitbucket root has no marker between repo and path; + // the server's rewrite keeps the skill paths joined onto it apart. + const root = refs.pinnable ? (await hubRefs(source, refs.current)).source ?? source : source; + setFound({ source, refs, version: refs.current, root, skills: discovered.skills }); + setPicked(addable(discovered.skills, root)); + } catch (e) { + setFound(null); + setError((e as Error).message); + } finally { + setBusy(false); + } + } + + async function changeVersion(version: string) { + if (!found) return; + setBusy(true); + setError(''); + try { + const res = await hubRefs(found.source, version); + const root = res.source ?? found.source; + const discovered = await api.discover(root, version || undefined); + setFound({ ...found, version, root, skills: discovered.skills }); + setPicked(addable(discovered.skills, root)); + } catch (e) { + setError((e as Error).message); + } finally { + setBusy(false); + } + } + + const shownCandidates = (candidates.data ?? []).filter((c) => + `${c.data.name ?? ''} ${c.id}`.toLowerCase().includes(query.toLowerCase())); + + const count = tab === 'url' ? picked.length : installedPicked.length; + const add = () => { + if (tab === 'url' && found) { + onAdd(found.skills.filter((s) => picked.includes(s.path)).map((s) => ({ + name: s.name, + ...(s.description ? { description: s.description } : {}), + source: foundSource(s), + }))); + } else { + onAdd((candidates.data ?? []).filter((c) => installedPicked.includes(c.id)).map((c) => c.data)); + } + }; + + const toggle = (list: string[], id: string, on: boolean) => on ? [...list, id] : list.filter((x) => x !== id); + + return ( + +
+

{t('hubs.addSkill.title', { name: hubName })}

+ +
+
+
+ {(['url', 'installed'] as const).map((k) => ( + + ))} +
+ + {tab === 'url' ? ( + <> +
{ e.preventDefault(); void find(); }}> + +
+ + setURL(e.target.value)} /> + + +
+ {t('hubs.addSkill.urlHint')} +
+ + {error &&
{error}
} + {busy && ( +
+ + {t('hubs.browse.loading')} +
+ )} + + {found && ( + <> +
+ setQuery(e.target.value)} placeholder={t('hubBuilder.filter')} aria-label={t('hubBuilder.filter')} /> + + {candidates.isPending && } + {candidates.isError &&
{(candidates.error as Error).message}
} + {candidates.data?.length === 0 &&

{t('hubBuilder.noInstalled')}

} + {shownCandidates.length > 0 && ( +
+ {shownCandidates.map((c) => { + const already = inHub(entries, c.data.source ?? '', c.data.skill ?? '', c.data.name ?? ''); + return ( +
+ setInstalledPicked((l) => toggle(l, c.id, on))} className="mt-0.5" /> + + {c.data.name || c.id} + {c.data.source || c.id} + + {already && {t('hubs.addSkill.inHub')}} +
+ ); + })} +
+ )} + + )} +
+
+ + + +
+ + ); +} diff --git a/ui/src/components/hub/HubBrowser.tsx b/ui/src/components/hub/HubBrowser.tsx deleted file mode 100644 index 853d61717..000000000 --- a/ui/src/components/hub/HubBrowser.tsx +++ /dev/null @@ -1,352 +0,0 @@ -import { useMemo, useState } from 'react'; -import { useQuery, useQueryClient } from '@tanstack/react-query'; -import { Library, Link2, Plus, Search, Star, Trash2 } from 'lucide-react'; -import { api, type HubSavedEntry, type SearchResult } from '../../api/client'; -import { hubDrafts, type HubEntry } from '../../api/hubDrafts'; -import Button from '../Button'; -import ConfirmDialog from '../ConfirmDialog'; -import EmptyState from '../EmptyState'; -import InstallDialog from '../InstallDialog'; -import Spinner from '../Spinner'; -import { Select } from '../Select'; -import { queryKeys, staleTimes } from '../../lib/queryKeys'; -import { useT } from '../../i18n'; - -/** Shipped with skillshare, so it is offered even when the user has saved nothing. */ -const COMMUNITY: HubSavedEntry = { - label: 'Skillshare Hub', - url: 'https://raw.githubusercontent.com/runkids/skillshare-hub/main/skillshare-hub.json', - builtIn: true, -}; - -/** Prefix that marks a source as a local draft rather than a hosted index. */ -const DRAFT = 'draft:'; - -/** A draft entry already carries everything a hub row shows; nothing is fetched. */ -const draftResult = (entry: HubEntry): SearchResult => ({ - name: entry.data.name ?? '', - description: entry.data.description ?? '', - source: entry.data.source ?? '', - skill: entry.data.skill, - stars: 0, - owner: '', - repo: '', - tags: entry.data.tags, -}); - -const sameURL = (a: string, b: string) => a.trim().replace(/\/+$/, '') === b.trim().replace(/\/+$/, ''); - -interface Props { - /** Set when arriving from a draft's "preview as recipient"; not saved as a subscription. */ - previewURL?: string; - previewLabel?: string; -} - -/** - * The consuming half of hubs: pick a saved hub, see what is inside it, install from it. - * Installing hands the source to the install dialog so the audit gate stays in one place. - */ -export default function HubBrowser({ previewURL, previewLabel }: Props) { - const t = useT(); - const queryClient = useQueryClient(); - const [picked, setPicked] = useState(previewURL ?? null); - const [filter, setFilter] = useState(''); - const [tag, setTag] = useState(''); - const [url, setURL] = useState(''); - const [label, setLabel] = useState(''); - const [error, setError] = useState(''); - const [busy, setBusy] = useState(false); - const [removing, setRemoving] = useState(null); - const [installing, setInstalling] = useState(null); - - const { data: config } = useQuery({ - queryKey: queryKeys.hubConfig, - queryFn: () => api.getHubConfig(), - staleTime: staleTimes.config, - }); - - const { data: drafts = [] } = useQuery({ queryKey: ['hub', 'drafts'], queryFn: () => hubDrafts.list() }); - - const hubs = useMemo(() => { - const saved = (config?.hubs ?? []).filter((h) => !sameURL(h.url, COMMUNITY.url)); - const list = [COMMUNITY, ...saved]; - if (previewURL && !previewURL.startsWith(DRAFT) && !list.some((h) => sameURL(h.url, previewURL))) { - list.unshift({ label: previewLabel || t('hubs.preview.label'), url: previewURL }); - } - return list; - }, [config, previewURL, previewLabel, t]); - - const isDraft = Boolean(picked?.startsWith(DRAFT)); - const pickedDraft = isDraft ? drafts.find((d) => DRAFT + d.id === picked) : undefined; - const current = pickedDraft - ? { label: pickedDraft.name || t('hubBuilder.untitled'), url: picked!, builtIn: false } - : hubs.find((h) => h.url === picked) ?? null; - const isDefault = (hub: HubSavedEntry) => - Boolean(config?.default) && hub.label.toLowerCase() === config!.default.toLowerCase(); - - const contents = useQuery({ - queryKey: ['hub', 'contents', picked], - // An empty query returns every entry of a hosted index. - queryFn: async () => { - if (picked!.startsWith(DRAFT)) { - const { draft } = await hubDrafts.get(picked!.slice(DRAFT.length)); - return { results: draft.entries.map(draftResult) }; - } - return api.searchHub('', picked!); - }, - enabled: Boolean(picked), - staleTime: staleTimes.skills, - }); - - const { data: skillsData } = useQuery({ - queryKey: queryKeys.skills.all, - queryFn: () => api.listSkills(), - staleTime: staleTimes.skills, - }); - const installed = useMemo( - () => new Set((skillsData?.resources ?? []).map((r) => r.name)), - [skillsData], - ); - - /** Every tag the picked hub actually uses, so the dropdown never offers a dead option. */ - const tags = useMemo(() => { - const seen = new Set(); - for (const r of contents.data?.results ?? []) for (const name of r.tags ?? []) seen.add(name); - return [...seen].sort(); - }, [contents.data]); - - const shown = (contents.data?.results ?? []).filter( - (r: SearchResult) => - (!tag || (r.tags ?? []).includes(tag)) && - `${r.name} ${r.description} ${(r.tags ?? []).join(' ')}`.toLowerCase().includes(filter.toLowerCase()), - ); - - const save = async (next: HubSavedEntry[], defaultLabel: string) => { - const body = { hubs: next.filter((h) => !h.builtIn).map(({ label: l, url: u }) => ({ label: l, url: u })), default: defaultLabel }; - await api.putHubConfig(body); - queryClient.setQueryData(queryKeys.hubConfig, body); - }; - - const add = async () => { - const trimmed = url.trim(); - if (!trimmed) { setError(t('install.hubs.urlRequired')); return; } - if (hubs.some((h) => sameURL(h.url, trimmed))) { setError(t('install.hubs.urlExists')); return; } - setBusy(true); - setError(''); - try { - const name = label.trim() || trimmed.split('/').filter(Boolean).pop() || trimmed; - await save([...hubs, { label: name, url: trimmed }], config?.default ?? ''); - setURL(''); - setLabel(''); - setPicked(trimmed); - } catch (e) { - setError((e as Error).message); - } finally { - setBusy(false); - } - }; - - const remove = async (hub: HubSavedEntry) => { - const next = hubs.filter((h) => h.url !== hub.url); - await save(next, isDefault(hub) ? '' : config?.default ?? ''); - if (picked === hub.url) setPicked(null); - }; - - return ( -
-
-
-

{t('hubs.browse.sources')}

- {hubs.length} -
-
- {hubs.map((hub) => ( -
- - {!hub.builtIn && ( - - )} -
- ))} -
- - {drafts.length > 0 && ( - <> -
-

{t('hubs.browse.mine')}

- {drafts.length} -
-
- {drafts.map((d) => ( -
- -
- ))} -
- - )} - -
- {t('install.hubs.add')} - - - { setURL(e.target.value); setError(''); }} - placeholder={t('install.hubs.urlPlaceholder')} - aria-label={t('install.hubs.urlPlaceholder')} - disabled={busy} - /> - - - setLabel(e.target.value)} - placeholder={t('install.hubs.labelPlaceholder')} - aria-label={t('install.hubs.labelPlaceholder')} - disabled={busy} - /> - - {error && {error}} - - {t('install.hubs.hint')} -
-
- -
- {!current ? ( - - ) : ( - <> - {/* The filter has to stay reachable: this list runs to hundreds of rows. - The band runs wider than the content so the list's offset shadow scrolls under it too. */} -
-

{current.label}

- {contents.data && {contents.data.results.length}} - - {!isDraft && !current.builtIn && !isDefault(current) && ( - - )} - {tags.length > 0 && ( - setFilter(e.target.value)} - placeholder={t('hubs.browse.filter')} - aria-label={t('hubs.browse.filter')} - /> - - -
- - {isDraft && ( -
- {t('hubs.browse.localNote')} -
- )} - - {contents.isPending && ( -
- - {t('hubs.browse.loading')} -
- )} - {contents.isError && ( -
- {(contents.error as Error).message} -
- )} - {contents.data && shown.length === 0 && ( - - )} - {shown.length > 0 && ( -
- {shown.map((r) => ( -
- - - {r.name} - {(r.tags ?? []).slice(0, 3).map((name) => ( - {name} - ))} - - {r.description && {r.description}} - {r.source} - - {!r.source ? ( - {t('hubBuilder.noSource')} - ) : installed.has(r.name) ? ( - {t('hubs.browse.installed')} - ) : ( - - )} -
- ))} -
- )} - - )} -
- - {installing && ( - { - setInstalling(null); - void queryClient.invalidateQueries({ queryKey: queryKeys.skills.all }); - }} - /> - )} - - setRemoving(null)} - onConfirm={() => { const hub = removing; setRemoving(null); if (hub) void remove(hub); }} - /> -
- ); -} - diff --git a/ui/src/components/hub/HubBuilder.tsx b/ui/src/components/hub/HubBuilder.tsx deleted file mode 100644 index 98d7624f2..000000000 --- a/ui/src/components/hub/HubBuilder.tsx +++ /dev/null @@ -1,274 +0,0 @@ -import { useEffect, useRef, useState } from 'react'; -import { Link, useBeforeUnload, useBlocker } from 'react-router-dom'; -import { useQueryClient } from '@tanstack/react-query'; -import { AlertTriangle, CheckCircle2, Download, Eye, FilePlus, Plus, Upload } from 'lucide-react'; -import { api, ApiError, type HubSavedEntry } from '../../api/client'; -import { hubAddCommand, hubDrafts, type DraftResponse, type HubDraft, type HubEntry, type HubProblem } from '../../api/hubDrafts'; -import Button from '../Button'; -import ConfirmDialog from '../ConfirmDialog'; -import EmptyState from '../EmptyState'; -import HubEntryEditor from './HubEntryEditor'; -import InstalledSkillPicker from './InstalledSkillPicker'; -import { queryKeys } from '../../lib/queryKeys'; -import { useT } from '../../i18n'; - -type HubAction = { type: 'open'; id: string } | { type: 'import'; file: File } | { type: 'create' | 'save' | 'delete' | 'download' | 'copy' | 'chooseFile' | 'subscribe' }; - -export default function HubBuilder() { - const t = useT(); - const queryClient = useQueryClient(); - const [drafts, setDrafts] = useState([]); - const [draft, setDraft] = useState(null); - const [saved, setSaved] = useState(''); - const [problems, setProblems] = useState([]); - const [candidates, setCandidates] = useState([]); - const [picker, setPicker] = useState(false); - const [query, setQuery] = useState(''); - const [open, setOpen] = useState([]); - const [loading, setLoading] = useState(true); - const [error, setError] = useState(''); - const [notice, setNotice] = useState(''); - const [confirm, setConfirm] = useState<{ kind: 'delete' | 'leave'; action: HubAction } | null>(null); - const [busy, setBusy] = useState(false); - const fileInput = useRef(null); - const dirty = draft !== null && JSON.stringify(draft) !== saved; - // Where the index is hosted belongs to the draft, so a reopened hub remembers it. - const location = String(draft?.fields?.publishUrl ?? ''); - const setLocation = (value: string) => - setDraft(current => current && ({ ...current, fields: { ...current.fields, publishUrl: value } })); - const blocker = useBlocker(dirty || busy); - useBeforeUnload((event) => { if (dirty || busy) { event.preventDefault(); event.returnValue = ''; } }); - - useEffect(() => { - let active = true; - Promise.all([hubDrafts.list(), hubDrafts.candidates()]).then(([items, skills]) => { - if (active) { setDrafts(items); setCandidates(skills); } - }).catch((err: Error) => { if (active) setError(err.message); }) - .finally(() => { if (active) setLoading(false); }); - return () => { active = false; }; - }, []); - - function accept(response: DraftResponse) { - setDraft(response.draft); setSaved(JSON.stringify(response.draft)); setProblems(response.problems); - setDrafts(items => [response.draft, ...items.filter(item => item.id !== response.draft.id)]); - setQuery(''); setOpen([]); - } - function requestAction(action: HubAction) { - if (action.type === 'delete') setConfirm({ kind: 'delete', action }); - else if (dirty && ['open', 'create', 'chooseFile'].includes(action.type)) setConfirm({ kind: 'leave', action }); - else void performAction(action); - } - async function performAction(action: HubAction) { - if (action.type === 'chooseFile') { fileInput.current?.click(); return; } - setBusy(true); setError(''); setNotice(''); - try { - switch (action.type) { - case 'open': accept(await hubDrafts.get(action.id)); break; - case 'create': - accept(await hubDrafts.create({ name: t('hubBuilder.untitled'), description: '', entries: [], fields: {} })); break; - case 'import': - if (action.file.size > 4 * 1024 * 1024) throw new Error(t('hubBuilder.fileTooLarge')); - accept(await hubDrafts.import(await action.file.text())); break; - case 'save': - if (draft) { accept(await hubDrafts.save(draft)); setNotice(t('hubBuilder.saved')); } break; - case 'delete': - if (draft) { - await hubDrafts.remove(draft); setDrafts(items => items.filter(item => item.id !== draft.id)); - setDraft(null); setSaved(''); - } break; - case 'download': - if (draft) downloadIndex(await hubDrafts.export(draft)); break; - case 'copy': - if (draft) { await navigator.clipboard.writeText(hubAddCommand(location, draft.name)); setNotice(t('hubBuilder.copied')); } break; - case 'subscribe': - if (draft) { - // Same config the CLI and the Browse tab read, so it shows up in both at once. - const config = await api.getHubConfig(); - const url = location.trim(); - const next: HubSavedEntry[] = [...config.hubs.filter(h => h.url !== url), { label: draft.name, url }]; - await api.putHubConfig({ hubs: next, default: config.default }); - queryClient.setQueryData(queryKeys.hubConfig, { hubs: next, default: config.default }); - setNotice(t('hubBuilder.subscribed')); - } break; - } - } catch (err) { - setError(err instanceof ApiError && err.status === 409 ? t('hubBuilder.conflict') : err instanceof Error ? err.message : String(err)); - } finally { setBusy(false); } - } - function changeEntry(id: string, key: string, value: unknown) { - setDraft(current => current && ({ ...current, entries: current.entries.map(entry => { - if (entry.id !== id) return entry; - const data = { ...entry.data, [key]: value }; - if (key === 'source' || key === 'skill') { - delete data.riskScore; delete data.riskLabel; delete data.auditedAt; - } - return { ...entry, data }; - }) })); - } - function addEntries(entries: HubEntry[]) { - const added = entries.map(entry => ({ ...entry, id: Array.from(crypto.getRandomValues(new Uint8Array(16)), byte => byte.toString(16).padStart(2, '0')).join('') })); - setDraft(current => current && ({ ...current, entries: [...current.entries, ...added] })); - setPicker(false); setQuery(''); - // A hand-added entry has nothing in it yet, so open it straight away. - if (added.length === 1 && !added[0].data.source) setOpen(list => [...list, added[0].id]); - } - - const shown = draft?.entries.filter(entry => `${entry.data.name ?? ''} ${entry.data.source ?? ''} ${(entry.data.tags ?? []).join(' ')}`.toLowerCase().includes(query.toLowerCase())) ?? []; - const publishLocation = /^(https?:\/\/|ssh:\/\/|[^\s@]+@[^\s:]+:)/.test(location.trim()) && !Array.from(location).some(char => char.charCodeAt(0) < 32 || char.charCodeAt(0) === 127); - const blocked = problems.length > 0; - - const fileField = ( - { - const file = event.target.files?.[0]; event.target.value = ''; if (file) requestAction({ type: 'import', file }); - }} /> - ); - - if (loading) return

{t('hubBuilder.loading')}

; - - return ( -
- {fileField} - - - - {!draft ? ( - requestAction({ type: 'create' })} disabled={busy}>{t('hubBuilder.create')}} /> - ) : ( -
- {error &&
{error}
} - {notice &&

{notice}

} - -
- {blocked ? : } - {blocked ? t(problems.length === 1 ? 'hubBuilder.blocked.one' : 'hubBuilder.blocked.other', { count: problems.length }) : dirty ? t('hubBuilder.unsaved') : t('hubBuilder.ready')} -
- -
-
-
-
- - setDraft({ ...draft, name: event.target.value })} /> -
-
- - setDraft({ ...draft, description: event.target.value })} /> -
-
- {t('hubBuilder.metadataHint')} -
- -
-
-

{t('hubBuilder.skills')}

- {draft.entries.length} - - {draft.entries.length > 0 && ( - - setQuery(event.target.value)} placeholder={t('hubBuilder.filter')} aria-label={t('hubBuilder.filter')} /> - - )} - - - -
- - {draft.entries.length === 0 ? ( -

{t('hubBuilder.noEntries')}

- ) : ( -
- {shown.map(entry => ( - problem.entryId === entry.id)} - expanded={open.includes(entry.id)} - onToggle={() => setOpen(list => list.includes(entry.id) ? list.filter(id => id !== entry.id) : [...list, entry.id])} - onChange={(key, value) => changeEntry(entry.id, key, value)} - onRemove={() => setDraft({ ...draft, entries: draft.entries.filter(item => item.id !== entry.id) })} /> - ))} -
- )} -
-
- -
- {t('hubBuilder.accessHint')} -
- - - {t('hubBuilder.previewInBrowse')} - - -
-
- -
-
-

{t('hubBuilder.shareTitle')}

- {t('hubBuilder.shareHint')} -
-
- - setLocation(event.target.value)} /> -
- {publishLocation && ( - <> -
{hubAddCommand(location, draft.name)}
-
- - -
- - )} -
-
- )} - - {picker && setPicker(false)} onAdd={addEntries} />} - setConfirm(null)} onConfirm={() => { const action = confirm?.action; setConfirm(null); if (action) void performAction(action); }} /> - blocker.state === 'blocked' && blocker.reset()} onConfirm={() => blocker.state === 'blocked' && blocker.proceed()} /> -
- ); -} - -function downloadIndex(data: Record) { - const url = URL.createObjectURL(new Blob([JSON.stringify(data, null, 2) + '\n'], { type: 'application/json' })); - const anchor = document.createElement('a'); anchor.href = url; anchor.download = 'skillshare-hub.json'; - document.body.append(anchor); anchor.click(); anchor.remove(); - setTimeout(() => URL.revokeObjectURL(url), 1000); -} diff --git a/ui/src/components/hub/HubEditor.tsx b/ui/src/components/hub/HubEditor.tsx new file mode 100644 index 000000000..4361e831b --- /dev/null +++ b/ui/src/components/hub/HubEditor.tsx @@ -0,0 +1,189 @@ +import { useMemo, useState } from 'react'; +import { useBeforeUnload, useBlocker } from 'react-router-dom'; +import { useQueryClient } from '@tanstack/react-query'; +import { Plus } from 'lucide-react'; +import { ApiError } from '../../api/client'; +import { hubDrafts } from '../../api/hubDrafts'; +import type { DraftResponse, HubDraft, HubEntry } from '../../api/hubDrafts'; +import Button from '../Button'; +import ConfirmDialog from '../ConfirmDialog'; +import AddSkillDialog from './AddSkillDialog'; +import HubEntryEditor from './HubEntryEditor'; +import { newEntryId } from './hubShared'; +import { queryKeys } from '../../lib/queryKeys'; +import { useT } from '../../i18n'; + +interface Props { + response: DraftResponse; + onDone: () => void; +} + +/** Counts what differs from the saved hub: the name, the description, and each added, removed or edited skill. */ +function countChanges(saved: HubDraft, draft: HubDraft) { + const before = new Map(saved.entries.map((e) => [e.id, JSON.stringify(e)])); + const now = new Set(draft.entries.map((e) => e.id)); + let n = Number(saved.name !== draft.name) + Number(saved.description !== draft.description); + for (const e of draft.entries) if (before.get(e.id) !== JSON.stringify(e)) n++; + for (const id of before.keys()) if (!now.has(id)) n++; + return n; +} + +/** The user's own hub, edited in place where it is viewed. */ +export default function HubEditor({ response, onDone }: Props) { + const t = useT(); + const queryClient = useQueryClient(); + const [base, setBase] = useState(response); + const [draft, setDraft] = useState(response.draft); + const [versions, setVersions] = useState>(response.refs ?? {}); + const [open, setOpen] = useState([]); + const [adding, setAdding] = useState(false); + const [busy, setBusy] = useState(false); + const [error, setError] = useState(''); + + const savedJSON = useMemo(() => JSON.stringify(base.draft), [base]); + const dirty = JSON.stringify(draft) !== savedJSON; + const blocker = useBlocker(dirty || busy); + useBeforeUnload((event) => { if (dirty || busy) { event.preventDefault(); event.returnValue = ''; } }); + + // A problem the server reported only holds while its entry is unchanged. + const savedEntries = useMemo(() => new Map(base.draft.entries.map((e) => [e.id, JSON.stringify(e)])), [base]); + const savedSources = useMemo(() => new Map(base.draft.entries.map((e) => [e.id, e.data.source])), [base]); + const problemsOf = (entry: HubEntry) => + savedEntries.get(entry.id) === JSON.stringify(entry) ? base.problems.filter((p) => p.entryId === entry.id) : []; + const blocked = draft.entries.filter((e) => problemsOf(e).length > 0).length; + const changes = Math.max(1, countChanges(base.draft, draft)); + + const setEntries = (update: (entries: HubEntry[]) => HubEntry[]) => + setDraft((current) => ({ ...current, entries: update(current.entries) })); + + function changeEntry(id: string, key: string, value: unknown) { + setEntries((entries) => entries.map((entry) => { + if (entry.id !== id) return entry; + const data = { ...entry.data, [key]: value }; + if (key === 'source' || key === 'skill') { + delete data.riskScore; delete data.riskLabel; delete data.auditedAt; + } + return { ...entry, data }; + })); + // A hand-edited source may name another version; it is read again when the dropdown opens. + if (key === 'source') setVersions((v) => { const rest = { ...v }; delete rest[id]; return rest; }); + } + + function addEntries(added: HubEntry['data'][]) { + setEntries((entries) => [...entries, ...added.map((data) => ({ id: newEntryId(), data }))]); + setAdding(false); + } + + async function save() { + setBusy(true); + setError(''); + try { + const res = await hubDrafts.save(draft); + queryClient.setQueryData(queryKeys.hub.draft(res.draft.id), res); + void queryClient.invalidateQueries({ queryKey: queryKeys.hub.drafts, exact: true }); + setBase(res); + setDraft(res.draft); + setVersions(res.refs ?? {}); + // Stay while something still needs fixing, so the red rows say what. + if (!res.problems.some((p) => p.entryId)) onDone(); + } catch (err) { + setError(err instanceof ApiError && err.status === 409 ? t('hubs.conflict') : err instanceof Error ? err.message : String(err)); + } finally { + setBusy(false); + } + } + + function cancel() { + // Reload what is saved, which also recovers from an edit made in another window. + void queryClient.invalidateQueries({ queryKey: queryKeys.hub.draft(draft.id) }); + onDone(); + } + + return ( +
+
+

{t('hubs.edit.title', { name: draft.name || t('hubBuilder.untitled') })}

+ + + {!dirty && } + +
+ + {error &&
{error}
} + +
+
+
+ + setDraft({ ...draft, name: e.target.value })} /> +
+
+ + setDraft({ ...draft, description: e.target.value })} /> +
+
+ + {draft.entries.length === 0 ? ( +

{t('hubBuilder.noEntries')}

+ ) : ( +
+
+ {t('hubs.edit.name')} + {t('hubs.edit.source')} + {t('hubs.view.version')} +
+ {draft.entries.map((entry) => ( + setOpen((list) => list.includes(entry.id) ? list.filter((id) => id !== entry.id) : [...list, entry.id])} + onChange={(key, value) => changeEntry(entry.id, key, value)} + onVersion={(source, version) => { + changeEntry(entry.id, 'source', source); + setVersions((v) => ({ ...v, [entry.id]: version })); + }} + onRemove={() => setEntries((entries) => entries.filter((item) => item.id !== entry.id))} + /> + ))} +
+ )} +
+ + {dirty && ( +
+ + + {t(changes === 1 ? 'hubs.edit.changes.one' : 'hubs.edit.changes.other', { count: changes })} + {blocked > 0 && ` · ${t(blocked === 1 ? 'hubs.edit.blocked.one' : 'hubs.edit.blocked.other', { count: blocked })}`} + + + +
+ )} + + {adding && ( + addEntries([{ name: '', source: '' }])} + onClose={() => setAdding(false)} + /> + )} + blocker.state === 'blocked' && blocker.reset()} + onConfirm={() => blocker.state === 'blocked' && blocker.proceed()} + /> +
+ ); +} diff --git a/ui/src/components/hub/HubEntryEditor.tsx b/ui/src/components/hub/HubEntryEditor.tsx index 081c7cd68..c2fbb4065 100644 --- a/ui/src/components/hub/HubEntryEditor.tsx +++ b/ui/src/components/hub/HubEntryEditor.tsx @@ -1,90 +1,126 @@ -import { Fragment } from 'react'; -import { ChevronDown, X } from 'lucide-react'; +import { useEffect, useState } from 'react'; +import { useQuery, useQueryClient } from '@tanstack/react-query'; +import { AlertCircle, ChevronDown, Trash2 } from 'lucide-react'; +import { hubRefs } from '../../api/hubDrafts'; import type { HubEntry, HubProblem } from '../../api/hubDrafts'; +import { Select } from '../Select'; +import { refOptions } from './hubShared'; +import { queryKeys } from '../../lib/queryKeys'; import { useT } from '../../i18n'; interface Props { entry: HubEntry; + /** Only problems that still apply: the entry is unchanged since the server reported them. */ problems: HubProblem[]; - showProblems: boolean; + /** Branch or tag the source names, as last known; "" is the default branch, undefined not known yet. */ + version?: string; expanded: boolean; onToggle: () => void; onChange: (key: string, value: unknown) => void; + /** A version was picked: the source rewritten to it, and the version itself. */ + onVersion: (source: string, version: string) => void; onRemove: () => void; } /** - * One row per skill. The fields only appear when the row is open, so a hub with a - * dozen entries stays a list instead of a dozen stacked forms. + * One skill per row: name, source and version on one line. The rarely edited + * fields fold out underneath, so a long hub stays a list rather than a stack of forms. */ -export default function HubEntryEditor({ entry, problems, showProblems, expanded, onToggle, onChange, onRemove }: Props) { +export default function HubEntryEditor({ entry, problems, version, expanded, onToggle, onChange, onVersion, onRemove }: Props) { const t = useT(); + const queryClient = useQueryClient(); const name = entry.data.name || t('hubBuilder.newEntry'); const source = entry.data.source ?? ''; - const blocked = showProblems && problems.length > 0; const field = (key: string) => `hub-${entry.id}-${key}`; + // Branches and tags are only listed once someone reaches for the dropdown. + const [wanted, setWanted] = useState(false); + const [error, setError] = useState(''); + // A new or re-typed source has no known version, so it is looked up once typing pauses. + const [settled, setSettled] = useState(source); + useEffect(() => { + const timer = setTimeout(() => setSettled(source), 500); + return () => clearTimeout(timer); + }, [source]); + const refs = useQuery({ + queryKey: queryKeys.hub.refs(source), + queryFn: () => hubRefs(source), + enabled: (wanted || (version === undefined && settled === source)) && Boolean(source.trim()), + staleTime: 5 * 60 * 1000, + }); + const current = refs.data?.current ?? version ?? ''; + const fixed = refs.data?.pinnable === false; + + const pick = async (next: string) => { + setError(''); + try { + const res = await hubRefs(source, next); + // A source that cannot be pinned comes back without one; it stays as it is. + if (!res.pinnable || res.source === undefined) return; + const pinned = res.source; + if (refs.data) queryClient.setQueryData(queryKeys.hub.refs(pinned), { ...refs.data, current: next, source: undefined }); + onVersion(pinned, next); + } catch (e) { + setError((e as Error).message); + } + }; + + const message = problems.length > 0 + ? problems.map((p) => t(`hubBuilder.problem.${p.code}`)).join(' ') + : error || (refs.isError ? (refs.error as Error).message : ''); return ( - -
- - - {name} - {blocked ? ( - {problems.map(problem => t(`hubBuilder.problem.${problem.code}`)).join(' ')} - ) : ( - {source || t('hubBuilder.noSource')} - )} +
0 ? 'bg-bad-bg' : ''}`}> +
+ + onChange('name', e.target.value)} /> - {!blocked && source && {t('hubBuilder.installable')}} -
- +
-