internal/httpd/builds.go

ed734c0b70c72713ef80e474011b910af745e233
gitbay/internal/httpd/builds.go

213 lines · 6251 bytes

  1package httpd
  2
  3import (
  4	"net/http"
  5	"net/url"
  6	"strconv"
  7
  8	"gitbay.org/gitbay/internal/control"
  9)
 10
 11// buildFilter is the builds page's GET filter: branch, status and job,
 12// each optional and independent (#224).
 13type buildFilter struct {
 14	Ref    string
 15	Status string
 16	Job    string
 17}
 18
 19// buildFilterLink is one entry in the nav.filters row above the build
 20// list: a status or a job, with the other two parameters carried along so
 21// clicking one never drops another.
 22type buildFilterLink struct {
 23	Label  string
 24	Href   string
 25	Active bool
 26}
 27
 28// buildStatuses is the fixed vocabulary a build's status takes, in the
 29// order the nav.filters row offers them.
 30var buildStatuses = []string{"pending", "running", "success", "failure", "cancelled"}
 31
 32// filterLinks builds the nav.filters row: "all" (clears status and job),
 33// one link per status, and one per job the repository's CI config names.
 34// Each link keeps the filter's other two parameters and net/url encodes
 35// them, so a branch name or job name with an odd character does not break
 36// the query string it lands in.
 37func filterLinks(f buildFilter, jobs []control.JobOut) []buildFilterLink {
 38	href := func(status, job string) string {
 39		q := url.Values{}
 40		if f.Ref != "" {
 41			q.Set("ref", f.Ref)
 42		}
 43		if status != "" {
 44			q.Set("status", status)
 45		}
 46		if job != "" {
 47			q.Set("job", job)
 48		}
 49		return "?" + q.Encode()
 50	}
 51	links := []buildFilterLink{
 52		{Label: "all", Href: href("", ""), Active: f.Status == "" && f.Job == ""},
 53	}
 54	for _, s := range buildStatuses {
 55		links = append(links, buildFilterLink{Label: s, Href: href(s, f.Job), Active: f.Status == s})
 56	}
 57	for _, j := range jobs {
 58		links = append(links, buildFilterLink{Label: j.Name, Href: href(f.Status, j.Name), Active: f.Job == j.Name})
 59	}
 60	return links
 61}
 62
 63// distinctRefs lists each ref among builds once, in order, plus the
 64// current filter value if it is not already there. It backs the branch
 65// field's <datalist> suggestions, not a claim about what branches exist:
 66// a ref that matched nothing under the current status/job filter still
 67// belongs in the list the person typed it from.
 68func distinctRefs(builds []control.BuildOut, current string) []string {
 69	seen := map[string]bool{}
 70	var refs []string
 71	add := func(ref string) {
 72		if ref != "" && !seen[ref] {
 73			seen[ref] = true
 74			refs = append(refs, ref)
 75		}
 76	}
 77	for _, b := range builds {
 78		add(b.Ref)
 79	}
 80	add(current)
 81	return refs
 82}
 83
 84// buildRun is one commit's builds, grouped for display: the builds tab
 85// reads by commit, not by job, so a push that runs three jobs shows as one
 86// row with three chips rather than three unrelated rows (#224).
 87type buildRun struct {
 88	SHA       string
 89	Ref       string
 90	CreatedAt string
 91	Status    string
 92	Builds    []control.BuildOut
 93}
 94
 95// runStatusPriority orders combinedStatus's worst-first check: a run reads
 96// as its least finished or least successful build.
 97var runStatusPriority = []string{"failure", "cancelled", "running", "pending"}
 98
 99// combinedStatus is the run's status: the worst of its builds' statuses,
100// success only when every one of them is.
101func combinedStatus(builds []control.BuildOut) string {
102	statuses := make([]string, len(builds))
103	for i, b := range builds {
104		statuses[i] = b.Status
105	}
106	return worstStatus(statuses)
107}
108
109// worstStatus is combinedStatus's ordering rule, factored out so the
110// dashboard feed can apply the same worst-first precedence to a folded
111// build run (D04).
112func worstStatus(statuses []string) string {
113	has := map[string]bool{}
114	for _, s := range statuses {
115		has[s] = true
116	}
117	for _, s := range runStatusPriority {
118		if has[s] {
119			return s
120		}
121	}
122	return "success"
123}
124
125// groupRuns folds consecutive builds of the same commit into one run.
126// build list orders builds newest first, so one push's jobs are adjacent;
127// this does not sort or otherwise assume anything beyond that adjacency.
128func groupRuns(builds []control.BuildOut) []buildRun {
129	var runs []buildRun
130	for _, b := range builds {
131		if n := len(runs); n > 0 && runs[n-1].SHA == b.SHA {
132			runs[n-1].Builds = append(runs[n-1].Builds, b)
133			continue
134		}
135		runs = append(runs, buildRun{SHA: b.SHA, Ref: b.Ref, CreatedAt: b.CreatedAt, Builds: []control.BuildOut{b}})
136	}
137	for i := range runs {
138		runs[i].Status = combinedStatus(runs[i].Builds)
139	}
140	return runs
141}
142
143func (s *Server) builds(w http.ResponseWriter, r *http.Request) {
144	p, ok := s.repoFor(w, r, "")
145	if !ok {
146		return
147	}
148	p.Tab = "builds"
149	viewer := s.webViewer(r)
150
151	qv := r.URL.Query()
152	filter := buildFilter{Ref: qv.Get("ref"), Status: qv.Get("status"), Job: qv.Get("job")}
153	argv := []string{"build", "list", p.Repo.Path()}
154	if filter.Ref != "" {
155		argv = append(argv, "--ref", filter.Ref)
156	}
157	if filter.Status != "" {
158		argv = append(argv, "--status", filter.Status)
159	}
160	if filter.Job != "" {
161		argv = append(argv, "--job", filter.Job)
162	}
163
164	var builds []control.BuildOut
165	s.runControlInto(viewer, argv, &builds)
166
167	// The jobs a trigger can name. A repo without a CI config has none;
168	// that is not an error for this page.
169	var jobs []control.JobOut
170	s.runControlInto(viewer, []string{"build", "jobs", p.Repo.Path()}, &jobs)
171
172	s.render(w, "builds.html", struct {
173		repoPage
174		Builds      []control.BuildOut
175		Jobs        []control.JobOut
176		Runs        []buildRun
177		Filter      buildFilter
178		FilterLinks []buildFilterLink
179		Refs        []string
180		CanWrite    bool
181		Notice      string
182	}{p, builds, jobs, groupRuns(builds), filter, filterLinks(filter, jobs), distinctRefs(builds, filter.Ref),
183		s.canWriteRepo(r, p.Repo), s.takeFlash(w, r)})
184}
185
186func (s *Server) build(w http.ResponseWriter, r *http.Request) {
187	p, ok := s.repoFor(w, r, "")
188	if !ok {
189		return
190	}
191	p.Tab = "builds"
192	if _, err := strconv.ParseInt(r.PathValue("n"), 10, 64); err != nil {
193		s.notFound(w, r)
194		return
195	}
196	n := r.PathValue("n")
197	viewer := s.webViewer(r)
198
199	var b control.BuildOut
200	if _, ok := s.runControlInto(viewer, []string{"build", "show", p.Repo.Path(), n}, &b); !ok {
201		s.notFound(w, r)
202		return
203	}
204	log, _, _ := s.runControl(viewer, []string{"build", "log", p.Repo.Path(), n})
205
206	s.render(w, "build.html", struct {
207		repoPage
208		Build    control.BuildOut
209		Log      string
210		CanWrite bool
211		Notice   string
212	}{p, b, log, s.canWriteRepo(r, p.Repo), s.takeFlash(w, r)})
213}