internal/httpd/settings.go

185f434bf23123c61a922129270c2db4cfebd493
gitbay/internal/httpd/settings.go history · blame · raw

252 lines · 7480 bytes

  1package httpd
  2
  3import (
  4	"fmt"
  5	"net/http"
  6	"net/url"
  7	"slices"
  8	"strings"
  9
 10	"gitbay.org/gitbay/internal/control"
 11	"gitbay.org/gitbay/internal/gitutil"
 12	"gitbay.org/gitbay/internal/store"
 13)
 14
 15// Repository settings for repo admins. Every control dispatches the
 16// command the CLI runs; the page only groups them. Destructive lifecycle
 17// — delete and transfer — stays on the CLI, where a typed confirmation
 18// is the norm.
 19
 20type settingsPage struct {
 21	repoPage
 22	Topics      []string
 23	Branches    []gitutil.Ref
 24	DepsEnabled bool
 25	Deps        control.DepsOut
 26	Runners     []store.RepoRunner
 27	Notice      string
 28	Saved       bool
 29	Submitted   map[string]string
 30}
 31
 32func (s *Server) settingsForm(w http.ResponseWriter, r *http.Request, u store.User) {
 33	s.settingsFormWith(w, r, u, s.takeFlash(w, r), nil)
 34}
 35
 36// settingsFormWith renders the page with the given notice. submitted is
 37// nil on a plain GET; on a failed POST it carries the values the visitor
 38// typed, so a rejected value is not silently dropped.
 39func (s *Server) settingsFormWith(w http.ResponseWriter, r *http.Request, u store.User, notice string, submitted url.Values) {
 40	repo, ok := s.repoForUser(w, r, u, policyCanAdmin)
 41	if !ok {
 42		return
 43	}
 44	p, ok := s.repoFor(w, r, "")
 45	if !ok {
 46		return
 47	}
 48	p.Tab = "settings"
 49	topics, _ := s.st.ListTopics(repo.ID)
 50	branches, _ := gitutil.Refs(p.Dir, "heads")
 51	// The toggle's state comes from the store; what the last run found
 52	// comes from the command, so the page shows the same report the CLI
 53	// prints (#164).
 54	var deps control.DepsOut
 55	s.runControlInto(u, []string{"repo", "deps", "status", repo.Path()}, &deps)
 56	var runners []store.RepoRunner
 57	s.runControlInto(u, []string{"repo", "runner", "list", repo.Path()}, &runners)
 58	var subm map[string]string
 59	if submitted != nil {
 60		subm = map[string]string{
 61			"description": submitted.Get("description"),
 62			"website":     submitted.Get("website"),
 63			"topics":      submitted.Get("topics"),
 64		}
 65	}
 66	s.render(w, "settings.html", settingsPage{
 67		repoPage: p, Topics: topics, Branches: branches,
 68		DepsEnabled: deps.Enabled, Deps: deps,
 69		Runners:   runners,
 70		Notice:    notice,
 71		Saved:     strings.HasPrefix(notice, "Saved "),
 72		Submitted: subm,
 73	})
 74}
 75
 76func (s *Server) settingsRedirect(w http.ResponseWriter, r *http.Request, msg string) {
 77	dest := fmt.Sprintf("/%s/%s/settings", r.PathValue("owner"), r.PathValue("repo"))
 78	s.setFlash(w, msg)
 79	http.Redirect(w, r, dest, http.StatusSeeOther)
 80}
 81
 82// settingsSubmit routes one form to its command. Keeping the mapping in
 83// one place makes what the page can reach obvious.
 84func (s *Server) settingsSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
 85	repo := r.PathValue("owner") + "/" + r.PathValue("repo")
 86	v := func(k string) string { return strings.TrimSpace(r.FormValue(k)) }
 87	field := r.FormValue("field")
 88
 89	var argv []string
 90	switch field {
 91	case "description":
 92		argv = []string{"repo", "settings", "description", repo, v("description")}
 93	case "website":
 94		argv = []string{"repo", "settings", "website", repo, v("website")}
 95	case "visibility":
 96		argv = []string{"repo", "settings", "visibility", repo, v("visibility")}
 97	case "default-branch":
 98		argv = []string{"repo", "settings", "default-branch", repo, v("default-branch")}
 99	case "git-daemon":
100		argv = []string{"repo", "settings", "git-daemon", repo, onOff(v("git-daemon"))}
101	case "require-checks":
102		argv = []string{"repo", "settings", "require-checks", repo, onOff(v("require-checks"))}
103	case "require-resolved":
104		argv = []string{"repo", "settings", "require-resolved", repo, onOff(v("require-resolved"))}
105	case "require-codeowners":
106		argv = []string{"repo", "settings", "require-codeowners", repo, onOff(v("require-codeowners"))}
107	case "require-mr":
108		argv = []string{"repo", "settings", "require-mr", repo, onOff(v("require-mr"))}
109	case "require-signed":
110		argv = []string{"repo", "settings", "require-signed", repo, onOff(v("require-signed"))}
111	case "require-approvals":
112		argv = []string{"repo", "settings", "require-approvals", repo, v("approvals")}
113	case "protect":
114		argv = []string{"repo", "settings", "protect", repo, v("branch")}
115	case "unprotect":
116		argv = []string{"repo", "settings", "unprotect", repo, v("branch")}
117	case "protect-tag":
118		argv = []string{"repo", "settings", "protect-tag", repo, v("glob")}
119	case "unprotect-tag":
120		argv = []string{"repo", "settings", "unprotect-tag", repo, v("glob")}
121	case "deps":
122		verb := "disable"
123		if v("deps") == "on" {
124			verb = "enable"
125		}
126		argv = []string{"repo", "deps", verb, repo}
127	case "archive":
128		verb := "archive"
129		if v("archive") != "on" {
130			verb = "unarchive"
131		}
132		argv = []string{"repo", verb, repo}
133	case "topics":
134		row, err := s.st.RepoByPath(repo)
135		if err != nil {
136			http.NotFound(w, r)
137			return
138		}
139		want := map[string]bool{}
140		var order []string
141		for _, t := range strings.Split(v("topics"), ",") {
142			if t = strings.ToLower(strings.TrimSpace(t)); t != "" && !want[t] {
143				want[t] = true
144				order = append(order, t)
145			}
146		}
147		have, err := s.st.ListTopics(row.ID)
148		if err != nil {
149			s.settingsRedirect(w, r, err.Error())
150			return
151		}
152		var add, remove []string
153		for _, t := range order {
154			if !slices.Contains(have, t) {
155				add = append(add, t)
156			}
157		}
158		for _, t := range have {
159			if !want[t] {
160				remove = append(remove, t)
161			}
162		}
163		if len(remove) > 0 {
164			if _, msg, ok := s.runControl(u, append([]string{"repo", "topics", "remove", repo}, remove...)); !ok {
165				s.settingsFormWith(w, r, u, msg, r.Form)
166				return
167			}
168		}
169		if len(add) > 0 {
170			argv = append([]string{"repo", "topics", "add", repo}, add...)
171		} else {
172			s.settingsRedirect(w, r, "Saved the topics.")
173			return
174		}
175	case "runner-add":
176		body := v("key")
177		if body == "" {
178			s.settingsRedirect(w, r, "paste the runner's public key")
179			return
180		}
181		msg, ok := s.runControlStdin(u, []string{"repo", "runner", "add", repo}, body+"\n")
182		if ok {
183			msg = ""
184		}
185		s.settingsRedirect(w, r, msg)
186		return
187	case "runner-remove":
188		argv = []string{"repo", "runner", "remove", repo, v("fingerprint")}
189	default:
190		s.settingsRedirect(w, r, "unknown setting")
191		return
192	}
193
194	_, msg, ok := s.runControl(u, argv)
195	if ok {
196		s.settingsRedirect(w, r, "Saved the "+fieldLabel(field)+".")
197		return
198	}
199	s.settingsFormWith(w, r, u, msg, r.Form)
200}
201
202// fieldLabel names a settings field for the saved flash and, on
203// rejection, the error notice — lower case, matching the label beside
204// its control.
205func fieldLabel(field string) string {
206	switch field {
207	case "description":
208		return "description"
209	case "website":
210		return "website"
211	case "visibility":
212		return "visibility"
213	case "default-branch":
214		return "default branch"
215	case "git-daemon":
216		return "git:// serving"
217	case "require-checks":
218		return "required checks"
219	case "require-approvals":
220		return "approvals"
221	case "require-resolved":
222		return "review threads"
223	case "require-codeowners":
224		return "CODEOWNERS"
225	case "require-mr":
226		return "require-MR"
227	case "require-signed":
228		return "signed commits"
229	case "protect", "unprotect":
230		return "protected branch"
231	case "protect-tag", "unprotect-tag":
232		return "protected tag"
233	case "deps":
234		return "dependency scanning"
235	case "archive":
236		return "archive"
237	case "topics":
238		return "topics"
239	case "runner-add", "runner-remove":
240		return "runner"
241	default:
242		return field
243	}
244}
245
246// onOff normalises a checkbox to the on|off the commands take.
247func onOff(v string) string {
248	if v == "on" || v == "true" {
249		return "on"
250	}
251	return "off"
252}