internal/httpd/releaseactions.go
61 lines · 1943 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, code := s.runControlCode(u, argv)
49 s.done(w, r, code, msg, func(w http.ResponseWriter, r *http.Request, msg string) { s.backTo(w, r, "releases", msg) })
50}
51
52func (s *Server) buildTriggerSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
53 repo := r.PathValue("owner") + "/" + r.PathValue("repo")
54 job := strings.TrimSpace(r.FormValue("job"))
55 if job == "" {
56 s.backTo(w, r, "builds", "pick a job")
57 return
58 }
59 _, msg, code := s.runControlCode(u, []string{"build", "trigger", repo, job})
60 s.done(w, r, code, msg, func(w http.ResponseWriter, r *http.Request, msg string) { s.backTo(w, r, "builds", msg) })
61}