forked from vmware-archive/atc
/
list_builds.go
109 lines (92 loc) · 2.44 KB
/
list_builds.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package jobserver
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/concourse/atc"
"github.com/concourse/atc/api/present"
"github.com/concourse/atc/db"
)
func (s *Server) ListJobBuilds(pipeline db.Pipeline) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var (
builds []db.Build
err error
until int
since int
limit int
)
logger := s.logger.Session("list-job-builds")
jobName := r.FormValue(":job_name")
teamName := r.FormValue(":team_name")
urlUntil := r.FormValue(atc.PaginationQueryUntil)
until, _ = strconv.Atoi(urlUntil)
urlSince := r.FormValue(atc.PaginationQuerySince)
since, _ = strconv.Atoi(urlSince)
urlLimit := r.FormValue(atc.PaginationQueryLimit)
limit, _ = strconv.Atoi(urlLimit)
if limit == 0 {
limit = atc.PaginationAPIDefaultLimit
}
job, found, err := pipeline.Job(jobName)
if err != nil {
logger.Error("failed-to-get-job", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if !found {
w.WriteHeader(http.StatusNotFound)
return
}
builds, pagination, err := job.Builds(db.Page{
Since: since,
Until: until,
Limit: limit,
})
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
if pagination.Next != nil {
s.addNextLink(w, teamName, pipeline.Name(), jobName, *pagination.Next)
}
if pagination.Previous != nil {
s.addPreviousLink(w, teamName, pipeline.Name(), jobName, *pagination.Previous)
}
w.WriteHeader(http.StatusOK)
jobBuilds := make([]atc.Build, len(builds))
for i := 0; i < len(builds); i++ {
jobBuilds[i] = present.Build(builds[i])
}
json.NewEncoder(w).Encode(jobBuilds)
})
}
func (s *Server) addNextLink(w http.ResponseWriter, teamName, pipelineName, jobName string, page db.Page) {
w.Header().Add("Link", fmt.Sprintf(
`<%s/api/v1/teams/%s/pipelines/%s/jobs/%s/builds?%s=%d&%s=%d>; rel="%s"`,
s.externalURL,
teamName,
pipelineName,
jobName,
atc.PaginationQuerySince,
page.Since,
atc.PaginationQueryLimit,
page.Limit,
atc.LinkRelNext,
))
}
func (s *Server) addPreviousLink(w http.ResponseWriter, teamName, pipelineName, jobName string, page db.Page) {
w.Header().Add("Link", fmt.Sprintf(
`<%s/api/v1/teams/%s/pipelines/%s/jobs/%s/builds?%s=%d&%s=%d>; rel="%s"`,
s.externalURL,
teamName,
pipelineName,
jobName,
atc.PaginationQueryUntil,
page.Until,
atc.PaginationQueryLimit,
page.Limit,
atc.LinkRelPrevious,
))
}