internal/httpd/settings.go

00033f022fed6fe0b36e1d7a1e2b3f1df6139ccd
gitbay/internal/httpd/settings.go history · blame · raw

120 lines · 3715 bytes

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