internal/httpd/releaseactions.go
110 lines · 3819 bytes
4 symbols in this file
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 title != "" {
77 argv = append(argv, "--title", title)
78 }
79 // edit needs at least one field; create takes the tag alone.
80 if notes != "" || verb == "edit" {
81 argv = append(argv, "--notes", notes)
82 }
83 _, msg, code := s.runControlCode(u, argv)
84 s.done(w, r, code, msg, back)
85}
86
87func (s *Server) buildTriggerSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
88 repo := r.PathValue("owner") + "/" + r.PathValue("repo")
89 job := strings.TrimSpace(r.FormValue("job"))
90 if job == "" {
91 s.backTo(w, r, "builds", "pick a job")
92 return
93 }
94 _, msg, code := s.runControlCode(u, []string{"build", "trigger", repo, job})
95 s.done(w, r, code, msg, func(w http.ResponseWriter, r *http.Request, msg string) { s.backTo(w, r, "builds", msg) })
96}
97
98// buildCancelSubmit withdraws a build. The page only offers the control
99// while a build is still queued; the command decides for real, so a stale
100// page posting against a build that has since finished sees the refusal
101// instead of a silent no-op.
102func (s *Server) buildCancelSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
103 repo := r.PathValue("owner") + "/" + r.PathValue("repo")
104 n := r.PathValue("n")
105 back := func(w http.ResponseWriter, r *http.Request, msg string) {
106 s.backTo(w, r, "builds/"+n, msg)
107 }
108 _, msg, code := s.runControlCode(u, []string{"build", "cancel", repo, n})
109 s.done(w, r, code, msg, back)
110}