internal/httpd/settings.go

387b381242a77e37af3a58b7301f6365b176603b
gitbay/internal/httpd/settings.go history · blame · raw

126 lines · 4007 bytes

  1package httpd
  2
  3import (
  4	"fmt"
  5	"net/http"
  6	"strings"
  7
  8	"gitbay.org/gitbay/internal/control"
  9	"gitbay.org/gitbay/internal/gitutil"
 10	"gitbay.org/gitbay/internal/store"
 11)
 12
 13// Repository settings for repo admins. Every control dispatches the
 14// command the CLI runs; the page only groups them. Destructive lifecycle
 15// — delete and transfer — stays on the CLI, where a typed confirmation
 16// is the norm.
 17
 18type settingsPage struct {
 19	repoPage
 20	Topics      []string
 21	Branches    []gitutil.Ref
 22	DepsEnabled bool
 23	Deps        control.DepsOut
 24	Notice      string
 25}
 26
 27func (s *Server) settingsForm(w http.ResponseWriter, r *http.Request, u store.User) {
 28	repo, ok := s.repoForUser(w, r, u, policyCanAdmin)
 29	if !ok {
 30		return
 31	}
 32	p, ok := s.repoFor(w, r, "")
 33	if !ok {
 34		return
 35	}
 36	p.Tab = "settings"
 37	topics, _ := s.st.ListTopics(repo.ID)
 38	branches, _ := gitutil.Refs(p.Dir, "heads")
 39	// The toggle's state comes from the store; what the last run found
 40	// comes from the command, so the page shows the same report the CLI
 41	// prints (#164).
 42	var deps control.DepsOut
 43	s.runControlInto(u, []string{"repo", "deps", "status", repo.Path()}, &deps)
 44	s.render(w, "settings.html", settingsPage{
 45		repoPage: p, Topics: topics, Branches: branches,
 46		DepsEnabled: deps.Enabled, Deps: deps,
 47		Notice: s.takeFlash(w, r),
 48	})
 49}
 50
 51func (s *Server) settingsRedirect(w http.ResponseWriter, r *http.Request, msg string) {
 52	dest := fmt.Sprintf("/%s/%s/settings", r.PathValue("owner"), r.PathValue("repo"))
 53	s.setFlash(w, msg)
 54	http.Redirect(w, r, dest, http.StatusSeeOther)
 55}
 56
 57// settingsSubmit routes one form to its command. Keeping the mapping in
 58// one place makes what the page can reach obvious.
 59func (s *Server) settingsSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
 60	repo := r.PathValue("owner") + "/" + r.PathValue("repo")
 61	v := func(k string) string { return strings.TrimSpace(r.FormValue(k)) }
 62
 63	var argv []string
 64	switch r.FormValue("field") {
 65	case "description":
 66		argv = []string{"repo", "settings", "description", repo, v("description")}
 67	case "website":
 68		argv = []string{"repo", "settings", "website", repo, v("website")}
 69	case "visibility":
 70		argv = []string{"repo", "settings", "visibility", repo, v("visibility")}
 71	case "git-daemon":
 72		argv = []string{"repo", "settings", "git-daemon", repo, onOff(v("git-daemon"))}
 73	case "require-checks":
 74		argv = []string{"repo", "settings", "require-checks", repo, onOff(v("require-checks"))}
 75	case "require-resolved":
 76		argv = []string{"repo", "settings", "require-resolved", repo, onOff(v("require-resolved"))}
 77	case "require-codeowners":
 78		argv = []string{"repo", "settings", "require-codeowners", repo, onOff(v("require-codeowners"))}
 79	case "require-signed":
 80		argv = []string{"repo", "settings", "require-signed", repo, onOff(v("require-signed"))}
 81	case "require-approvals":
 82		argv = []string{"repo", "settings", "require-approvals", repo, v("approvals")}
 83	case "protect":
 84		argv = []string{"repo", "settings", "protect", repo, v("branch")}
 85	case "unprotect":
 86		argv = []string{"repo", "settings", "unprotect", repo, v("branch")}
 87	case "deps":
 88		verb := "disable"
 89		if v("deps") == "on" {
 90			verb = "enable"
 91		}
 92		argv = []string{"repo", "deps", verb, repo}
 93	case "archive":
 94		verb := "archive"
 95		if v("archive") != "on" {
 96			verb = "unarchive"
 97		}
 98		argv = []string{"repo", verb, repo}
 99	case "topics":
100		if add := strings.Fields(v("add")); len(add) > 0 {
101			argv = append([]string{"repo", "topics", "add", repo}, add...)
102		} else if rm := strings.Fields(v("remove")); len(rm) > 0 {
103			argv = append([]string{"repo", "topics", "remove", repo}, rm...)
104		} else {
105			s.settingsRedirect(w, r, "name at least one topic")
106			return
107		}
108	default:
109		s.settingsRedirect(w, r, "unknown setting")
110		return
111	}
112
113	_, msg, ok := s.runControl(u, argv)
114	if ok {
115		msg = ""
116	}
117	s.settingsRedirect(w, r, msg)
118}
119
120// onOff normalises a checkbox to the on|off the commands take.
121func onOff(v string) string {
122	if v == "on" || v == "true" {
123		return "on"
124	}
125	return "off"
126}