internal/httpd/builds.go

846b17ebe7687ac4d34c9bffc2276da0a38ce28e
gitbay/internal/httpd/builds.go history · blame · raw

62 lines · 1447 bytes

 1package httpd
 2
 3import (
 4	"net/http"
 5	"strconv"
 6
 7	"gitbay.org/gitbay/internal/ci"
 8	"gitbay.org/gitbay/internal/gitutil"
 9	"gitbay.org/gitbay/internal/store"
10)
11
12func (s *Server) builds(w http.ResponseWriter, r *http.Request) {
13	p, ok := s.repoFor(w, r, "")
14	if !ok {
15		return
16	}
17	p.Tab = "builds"
18	builds, _ := s.st.ListBuilds(p.Repo.ID, 50)
19	// The jobs a trigger can name come from the config on the default
20	// branch, the same file the scheduler reads.
21	var jobs []string
22	if sha, err := gitutil.ResolveRef(p.Dir, "refs/heads/"+p.Repo.DefaultBranch); err == nil {
23		if raw, err := gitutil.ReadBlob(p.Dir, sha, ci.ConfigPath, 1<<16); err == nil {
24			if parsed, err := ci.Parse(raw); err == nil {
25				for _, j := range parsed {
26					jobs = append(jobs, j.Name)
27				}
28			}
29		}
30	}
31	s.render(w, "builds.html", struct {
32		repoPage
33		Builds   []store.Build
34		Jobs     []string
35		CanWrite bool
36		Notice   string
37	}{p, builds, jobs, s.canWriteRepo(r, p.Repo), r.URL.Query().Get("e")})
38}
39
40func (s *Server) build(w http.ResponseWriter, r *http.Request) {
41	p, ok := s.repoFor(w, r, "")
42	if !ok {
43		return
44	}
45	p.Tab = "builds"
46	n, err := strconv.ParseInt(r.PathValue("n"), 10, 64)
47	if err != nil {
48		s.notFound(w, r)
49		return
50	}
51	b, err := s.st.BuildByNumber(p.Repo.ID, n)
52	if err != nil {
53		s.notFound(w, r)
54		return
55	}
56	log, _ := s.st.BuildLog(b.ID)
57	s.render(w, "build.html", struct {
58		repoPage
59		Build store.Build
60		Log   string
61	}{p, b, string(log)})
62}