internal/httpd/releaseactions.go
55 lines · 1849 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 verb := "create"
31 if r.FormValue("action") == "edit" {
32 verb = "edit"
33 }
34 argv := []string{"release", verb, repo, tag}
35 if title != "" {
36 argv = append(argv, "--title", title)
37 }
38 // edit needs at least one field; create takes the tag alone.
39 if notes != "" || verb == "edit" {
40 argv = append(argv, "--notes", notes)
41 }
42 _, msg, code := s.runControlCode(u, argv)
43 s.done(w, r, code, msg, func(w http.ResponseWriter, r *http.Request, msg string) { s.backTo(w, r, "releases", msg) })
44}
45
46func (s *Server) buildTriggerSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
47 repo := r.PathValue("owner") + "/" + r.PathValue("repo")
48 job := strings.TrimSpace(r.FormValue("job"))
49 if job == "" {
50 s.backTo(w, r, "builds", "pick a job")
51 return
52 }
53 _, msg, code := s.runControlCode(u, []string{"build", "trigger", repo, job})
54 s.done(w, r, code, msg, func(w http.ResponseWriter, r *http.Request, msg string) { s.backTo(w, r, "builds", msg) })
55}