internal/httpd/releaseactions.go

6521399c1c78577eb00f1bcf8947c0900b9cb62e
gitbay/internal/httpd/releaseactions.go

113 lines · 3894 bytes

  1package httpd
  2
  3import (
  4	"fmt"
  5	"net/http"
  6	"strings"
  7
  8	"gitbay.org/gitbay/internal/protocol"
  9	"gitbay.org/gitbay/internal/store"
 10)
 11
 12// Release and build actions. As elsewhere, the browser chooses arguments
 13// and the command decides: tag existence, access, and the gates around
 14// archived repositories all stay in one implementation.
 15
 16func (s *Server) backTo(w http.ResponseWriter, r *http.Request, page, msg string) {
 17	dest := fmt.Sprintf("/%s/%s/%s", r.PathValue("owner"), r.PathValue("repo"), page)
 18	s.setFlash(w, msg)
 19	http.Redirect(w, r, dest, http.StatusSeeOther)
 20}
 21
 22func (s *Server) releaseSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
 23	repo := r.PathValue("owner") + "/" + r.PathValue("repo")
 24	tag := strings.TrimSpace(r.FormValue("tag"))
 25	title := strings.TrimSpace(r.FormValue("title"))
 26	notes := strings.TrimSpace(r.FormValue("notes"))
 27	if tag == "" {
 28		s.backTo(w, r, "releases", "pick a tag")
 29		return
 30	}
 31	// Preview: the page back with the notes rendered, nothing written.
 32	// The form name carries the tag, because every release on the page
 33	// has an edit box and only the one submitted shows its draft.
 34	if wantsPreview(r) {
 35		form := "release"
 36		if r.FormValue("action") == "edit" {
 37			form = "release:" + tag
 38		}
 39		s.releasesPage(w, r, form)
 40		return
 41	}
 42	back := func(w http.ResponseWriter, r *http.Request, msg string) { s.backTo(w, r, "releases", msg) }
 43	// Summarize changes: the create form back with one line per merge
 44	// request since the previous release appended to the notes, nothing
 45	// written (#331). An empty range reports why instead of a silent
 46	// round trip.
 47	if r.FormValue("summarize") != "" {
 48		out, msg, code := s.runControlCode(u, []string{"release", "notes", repo, tag})
 49		if code != protocol.ExitOK || out == "" {
 50			back(w, r, msg)
 51			return
 52		}
 53		if notes != "" {
 54			out = notes + "\n\n" + out
 55		}
 56		r.Form.Set("notes", out)
 57		s.releasesPage(w, r, "release")
 58		return
 59	}
 60	// The CLI's --yes guards against a mistyped tag; here the tag comes
 61	// from the page, so the browser's own confirm field stands in.
 62	if r.FormValue("action") == "delete" {
 63		if ok, msg := confirmed(r, tag); !ok {
 64			back(w, r, msg)
 65			return
 66		}
 67		_, msg, code := s.runControlCode(u, []string{"release", "delete", repo, tag, "--yes"})
 68		s.done(w, r, code, msg, back)
 69		return
 70	}
 71	verb := "create"
 72	if r.FormValue("action") == "edit" {
 73		verb = "edit"
 74	}
 75	argv := []string{"release", verb, repo, tag}
 76	if verb == "create" {
 77		argv = append(argv, "--format", bodyFormat(r))
 78	}
 79	if title != "" {
 80		argv = append(argv, "--title", title)
 81	}
 82	// edit needs at least one field; create takes the tag alone.
 83	if notes != "" || verb == "edit" {
 84		argv = append(argv, "--notes", notes)
 85	}
 86	_, msg, code := s.runControlCode(u, argv)
 87	s.done(w, r, code, msg, back)
 88}
 89
 90func (s *Server) buildTriggerSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
 91	repo := r.PathValue("owner") + "/" + r.PathValue("repo")
 92	job := strings.TrimSpace(r.FormValue("job"))
 93	if job == "" {
 94		s.backTo(w, r, "builds", "pick a job")
 95		return
 96	}
 97	_, msg, code := s.runControlCode(u, []string{"build", "trigger", repo, job})
 98	s.done(w, r, code, msg, func(w http.ResponseWriter, r *http.Request, msg string) { s.backTo(w, r, "builds", msg) })
 99}
100
101// buildCancelSubmit withdraws a build. The page only offers the control
102// while a build is still queued; the command decides for real, so a stale
103// page posting against a build that has since finished sees the refusal
104// instead of a silent no-op.
105func (s *Server) buildCancelSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
106	repo := r.PathValue("owner") + "/" + r.PathValue("repo")
107	n := r.PathValue("n")
108	back := func(w http.ResponseWriter, r *http.Request, msg string) {
109		s.backTo(w, r, "builds/"+n, msg)
110	}
111	_, msg, code := s.runControlCode(u, []string{"build", "cancel", repo, n})
112	s.done(w, r, code, msg, back)
113}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form