forked from concourse/concourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_to.go
56 lines (45 loc) · 1.48 KB
/
input_to.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
package versionserver
import (
"encoding/json"
"net/http"
"strconv"
"code.cloudfoundry.org/lager"
"github.com/pf-qiu/concourse/v6/atc"
"github.com/pf-qiu/concourse/v6/atc/api/present"
"github.com/pf-qiu/concourse/v6/atc/db"
)
func (s *Server) ListBuildsWithVersionAsInput(pipeline db.Pipeline) http.Handler {
logger := s.logger.Session("list-builds-with-version-as-input")
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
versionIDString := r.FormValue(":resource_config_version_id")
resourceName := r.FormValue(":resource_name")
versionID, _ := strconv.Atoi(versionIDString)
resource, found, err := pipeline.Resource(resourceName)
if err != nil {
logger.Error("failed-to-find-resource", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if !found {
logger.Debug("resource-not-found", lager.Data{"resource-name": resourceName})
w.WriteHeader(http.StatusNotFound)
return
}
builds, err := pipeline.GetBuildsWithVersionAsInput(resource.ID(), versionID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
presentedBuilds := []atc.Build{}
for _, build := range builds {
presentedBuilds = append(presentedBuilds, present.Build(build))
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
err = json.NewEncoder(w).Encode(presentedBuilds)
if err != nil {
logger.Error("failed-to-encode-builds", err)
w.WriteHeader(http.StatusInternalServerError)
}
})
}