internal/httpd/releaseactions.go

c7cebb36f27f7d136074c5392f87231786a06d56
gitbay/internal/httpd/releaseactions.go history · blame · raw

67 lines · 1805 bytes

 1package httpd
 2
 3import (
 4	"fmt"
 5	"net/http"
 6	"net/url"
 7	"strings"
 8
 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	if msg != "" {
19		if len(msg) > 300 {
20			msg = msg[:300]
21		}
22		dest += "?e=" + url.QueryEscape(msg)
23	}
24	http.Redirect(w, r, dest, http.StatusSeeOther)
25}
26
27func (s *Server) releaseSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
28	repo := r.PathValue("owner") + "/" + r.PathValue("repo")
29	tag := strings.TrimSpace(r.FormValue("tag"))
30	title := strings.TrimSpace(r.FormValue("title"))
31	notes := strings.TrimSpace(r.FormValue("notes"))
32	if tag == "" {
33		s.backTo(w, r, "releases", "pick a tag")
34		return
35	}
36	verb := "create"
37	if r.FormValue("action") == "edit" {
38		verb = "edit"
39	}
40	argv := []string{"release", verb, repo, tag}
41	if title != "" {
42		argv = append(argv, "--title", title)
43	}
44	// edit needs at least one field; create takes the tag alone.
45	if notes != "" || verb == "edit" {
46		argv = append(argv, "--notes", notes)
47	}
48	_, msg, ok := s.runControl(u, argv)
49	if ok {
50		msg = ""
51	}
52	s.backTo(w, r, "releases", msg)
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, ok := s.runControl(u, []string{"build", "trigger", repo, job})
63	if ok {
64		msg = ""
65	}
66	s.backTo(w, r, "builds", msg)
67}