package xrpc import ( "net/http" "tangled.org/core/knotserver/vcs" xrpcerr "tangled.org/core/xrpc/errors" ) // PijulChannelListResponse is the response for listing Pijul channels type PijulChannelListResponse struct { Channels []PijulChannel `json:"channels"` } // PijulChannel represents a Pijul channel type PijulChannel struct { Name string `json:"name"` IsCurrent bool `json:"is_current,omitempty"` } // RepoChannelList handles the sh.tangled.repo.channelList endpoint // Kept for backwards compatibility - delegates to the unified Branches interface. func (x *Xrpc) RepoChannelList(w http.ResponseWriter, r *http.Request) { repo := r.URL.Query().Get("repo") repoPath, err := x.parseRepoParam(repo) if err != nil { writeError(w, err.(xrpcerr.XrpcError), http.StatusBadRequest) return } rv, err := vcs.PlainOpen(repoPath) if err != nil { writeError(w, xrpcerr.NewXrpcError( xrpcerr.WithTag("RepoNotFound"), xrpcerr.WithMessage("failed to open repository"), ), http.StatusNotFound) return } branches, err := rv.Branches(nil) if err != nil { x.Logger.Error("fetching channels", "error", err.Error()) writeError(w, xrpcerr.NewXrpcError( xrpcerr.WithTag("InternalServerError"), xrpcerr.WithMessage("failed to list channels"), ), http.StatusInternalServerError) return } channelList := make([]PijulChannel, len(branches)) for i, b := range branches { channelList[i] = PijulChannel{ Name: b.Name, IsCurrent: b.IsDefault, } } writeJson(w, PijulChannelListResponse{Channels: channelList}) } // PijulGetDefaultChannelResponse is the response for getting the default channel type PijulGetDefaultChannelResponse struct { Channel string `json:"channel"` } // RepoGetDefaultChannel handles the sh.tangled.repo.getDefaultChannel endpoint func (x *Xrpc) RepoGetDefaultChannel(w http.ResponseWriter, r *http.Request) { repo := r.URL.Query().Get("repo") repoPath, err := x.parseRepoParam(repo) if err != nil { writeError(w, err.(xrpcerr.XrpcError), http.StatusBadRequest) return } rv, err := vcs.PlainOpen(repoPath) if err != nil { writeError(w, xrpcerr.NewXrpcError( xrpcerr.WithTag("RepoNotFound"), xrpcerr.WithMessage("failed to open repository"), ), http.StatusNotFound) return } channel, err := rv.DefaultBranch() if err != nil { x.Logger.Error("finding default channel", "error", err.Error()) writeError(w, xrpcerr.NewXrpcError( xrpcerr.WithTag("InternalServerError"), xrpcerr.WithMessage("failed to find default channel"), ), http.StatusInternalServerError) return } writeJson(w, PijulGetDefaultChannelResponse{Channel: channel}) }