package web
import (
"net/http"
"net/url"
"strings"
"craftdesign.group/quarry/internal/ops"
)
// gitMirrorPanel is the Mirror to Git section as the page draws it.
type gitMirrorPanel struct {
On bool `json:"on"`
Remote string `json:"remote"`
Branch string `json:"branch"`
PublicKey string `json:"public_key"`
Pushed string `json:"pushed"`
PushedAt string `json:"pushed_at"`
LastErr string `json:"error"`
LastErrAt string `json:"error_at"`
// Err is this section's own refusal, back from its POST.
Err string `json:"-"`
}
// gitMirrorPanelFor reads the mirror through `repo.gitmirror.get`, the
// op the API serves. nil, so no section, when the forge keeps no
// mirrors or this viewer may not configure one: the op refuses both.
func gitMirrorPanelFor(r *ops.Registry, req *http.Request, repo string) *gitMirrorPanel {
out, err := invoke(req.Context(), r, "repo.gitmirror.get", url.Values{"repo": {repo}})
if err != nil {
return nil
}
var p gitMirrorPanel
remarshal(out, &p)
p.Err = req.URL.Query().Get("gme")
if !p.On {
// A refused first enable keeps what was typed, so the admin
// fixes the address rather than typing it again.
p.Remote, p.Branch = req.URL.Query().Get("remote"), req.URL.Query().Get("branch")
}
return &p
}
// opRefusal is an op's error as a sentence for the page: the class the
// API answers with is a status code there, not words for a person.
func opRefusal(err error) string {
msg := err.Error()
for _, e := range []error{ops.ErrInvalid, ops.ErrForbidden, ops.ErrNotFound, ops.ErrUnavailable} {
msg = strings.TrimSuffix(msg, ": "+e.Error())
}
return msg
}