internal/httpd/releaseactions.go

v1.14.0
gitbay/internal/httpd/releaseactions.go history · blame · raw

78 lines · 2843 bytes

 1package httpd
 2
 3import (
 4	"fmt"
 5	"net/http"
 6	"strings"
 7
 8	"gitbay.org/gitbay/internal/store"
 9)
10
11// Release and build actions. As elsewhere, the browser chooses arguments
12// and the command decides: tag existence, access, and the gates around
13// archived repositories all stay in one implementation.
14
15func (s *Server) backTo(w http.ResponseWriter, r *http.Request, page, msg string) {
16	dest := fmt.Sprintf("/%s/%s/%s", r.PathValue("owner"), r.PathValue("repo"), page)
17	s.setFlash(w, msg)
18	http.Redirect(w, r, dest, http.StatusSeeOther)
19}
20
21func (s *Server) releaseSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
22	repo := r.PathValue("owner") + "/" + r.PathValue("repo")
23	tag := strings.TrimSpace(r.FormValue("tag"))
24	title := strings.TrimSpace(r.FormValue("title"))
25	notes := strings.TrimSpace(r.FormValue("notes"))
26	if tag == "" {
27		s.backTo(w, r, "releases", "pick a tag")
28		return
29	}
30	back := func(w http.ResponseWriter, r *http.Request, msg string) { s.backTo(w, r, "releases", msg) }
31	// The CLI's --yes guards against a mistyped tag; here the tag comes from
32	// the page and the button sits behind a disclosure, so the click is the
33	// deliberate act.
34	if r.FormValue("action") == "delete" {
35		_, msg, code := s.runControlCode(u, []string{"release", "delete", repo, tag, "--yes"})
36		s.done(w, r, code, msg, back)
37		return
38	}
39	verb := "create"
40	if r.FormValue("action") == "edit" {
41		verb = "edit"
42	}
43	argv := []string{"release", verb, repo, tag}
44	if title != "" {
45		argv = append(argv, "--title", title)
46	}
47	// edit needs at least one field; create takes the tag alone.
48	if notes != "" || verb == "edit" {
49		argv = append(argv, "--notes", notes)
50	}
51	_, msg, code := s.runControlCode(u, argv)
52	s.done(w, r, code, msg, back)
53}
54
55func (s *Server) buildTriggerSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
56	repo := r.PathValue("owner") + "/" + r.PathValue("repo")
57	job := strings.TrimSpace(r.FormValue("job"))
58	if job == "" {
59		s.backTo(w, r, "builds", "pick a job")
60		return
61	}
62	_, msg, code := s.runControlCode(u, []string{"build", "trigger", repo, job})
63	s.done(w, r, code, msg, func(w http.ResponseWriter, r *http.Request, msg string) { s.backTo(w, r, "builds", msg) })
64}
65
66// buildCancelSubmit withdraws a build. The page only offers the control
67// while a build is still queued; the command decides for real, so a stale
68// page posting against a build that has since finished sees the refusal
69// instead of a silent no-op.
70func (s *Server) buildCancelSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
71	repo := r.PathValue("owner") + "/" + r.PathValue("repo")
72	n := r.PathValue("n")
73	back := func(w http.ResponseWriter, r *http.Request, msg string) {
74		s.backTo(w, r, "builds/"+n, msg)
75	}
76	_, msg, code := s.runControlCode(u, []string{"build", "cancel", repo, n})
77	s.done(w, r, code, msg, back)
78}