mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
Previewing a hub from the builder landed on an empty "pick a hub" state: the builder and the browser kept separate copies of the drafts and the browser's copy went stale. Merging both into one page removes the split, so your own hub is shown exactly as others will see it and edited in place. Hub entries can now be added from any repository URL, not only from installed skills, with a branch or tag picked from the remote. The version is written into the source URL, the only place the hub format keeps it, and a new /api/hub/refs endpoint lists refs and rewrites sources so the UI never builds tree/<ref> URLs itself. Skills added from installed ones keep the version they were installed from. Draft reads and saves read refs from the URLs only, so opening a hub never waits on a remote. Added hubs get unique labels, since the default hub is stored by label.
74 lines
1.9 KiB
Go
74 lines
1.9 KiB
Go
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)
|
|
}
|