forked from vmware-archive/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
201 lines (167 loc) · 4.68 KB
/
handler.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package getresource
import (
"errors"
"html/template"
"net/http"
"strconv"
"code.cloudfoundry.org/lager"
"github.com/concourse/atc"
"github.com/concourse/atc/web"
"github.com/concourse/atc/web/group"
"github.com/concourse/go-concourse/concourse"
)
type Handler struct {
logger lager.Logger
clientFactory web.ClientFactory
template *template.Template
}
func NewHandler(logger lager.Logger, clientFactory web.ClientFactory, template *template.Template) *Handler {
return &Handler{
logger: logger,
clientFactory: clientFactory,
template: template,
}
}
type VersionedResourceWithInputsAndOutputs struct {
VersionedResource atc.VersionedResource
InputsTo map[string][]atc.Build
OutputsOf map[string][]atc.Build
}
type TemplateData struct {
Resource atc.Resource
Versions []VersionedResourceWithInputsAndOutputs
GroupStates []group.State
PipelineName string
TeamName string
Pagination concourse.Pagination
}
var ErrConfigNotFound = errors.New("could not find config")
var ErrResourceNotFound = errors.New("could not find resource")
func FetchTemplateData(
pipelineName string,
resourceName string,
team concourse.Team,
page concourse.Page,
) (TemplateData, error) {
pipeline, pipelineFound, err := team.Pipeline(pipelineName)
if err != nil {
return TemplateData{}, err
}
if !pipelineFound {
return TemplateData{}, ErrConfigNotFound
}
resource, resourceFound, err := team.Resource(pipelineName, resourceName)
if err != nil {
return TemplateData{}, err
}
if !resourceFound {
return TemplateData{}, ErrResourceNotFound
}
versionedResources, pagination, resourceVersionsFound, err := team.ResourceVersions(pipelineName, resourceName, page)
if err != nil {
return TemplateData{}, err
}
if !resourceVersionsFound {
return TemplateData{}, ErrResourceNotFound
}
versions := []VersionedResourceWithInputsAndOutputs{}
for _, versionedResource := range versionedResources {
inputs, _, err := team.BuildsWithVersionAsInput(pipelineName, resourceName, versionedResource.ID)
if err != nil {
return TemplateData{}, err
}
outputs, _, err := team.BuildsWithVersionAsOutput(pipelineName, resourceName, versionedResource.ID)
if err != nil {
return TemplateData{}, err
}
inputsTo := map[string][]atc.Build{}
outputsOf := map[string][]atc.Build{}
for _, input := range inputs {
if _, ok := inputsTo[input.JobName]; !ok {
inputsTo[input.JobName] = []atc.Build{}
}
inputsTo[input.JobName] = append(inputsTo[input.JobName], input)
}
for _, output := range outputs {
if _, ok := outputsOf[output.JobName]; !ok {
outputsOf[output.JobName] = []atc.Build{}
}
outputsOf[output.JobName] = append(outputsOf[output.JobName], output)
}
versions = append(versions, VersionedResourceWithInputsAndOutputs{
VersionedResource: versionedResource,
InputsTo: inputsTo,
OutputsOf: outputsOf,
})
}
return TemplateData{
Resource: resource,
Versions: versions,
PipelineName: pipelineName,
TeamName: team.Name(),
Pagination: pagination,
GroupStates: group.States(pipeline.Groups, func(g atc.GroupConfig) bool {
for _, groupResource := range g.Resources {
if groupResource == resource.Name {
return true
}
}
return false
}),
}, nil
}
func (handler *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) error {
session := handler.logger.Session("get-resource")
teamName := r.FormValue(":team_name")
pipelineName := r.FormValue(":pipeline_name")
resourceName := r.FormValue(":resource")
since, parseErr := strconv.Atoi(r.FormValue("since"))
if parseErr != nil {
since = 0
}
until, parseErr := strconv.Atoi(r.FormValue("until"))
if parseErr != nil {
until = 0
}
client := handler.clientFactory.Build(r)
templateData, err := FetchTemplateData(
pipelineName,
resourceName,
client.Team(teamName),
concourse.Page{
Since: since,
Until: until,
Limit: atc.PaginationWebLimit,
},
)
switch err {
case ErrResourceNotFound:
session.Error("could-not-find-resource", ErrResourceNotFound, lager.Data{
"resource": resourceName,
})
w.WriteHeader(http.StatusNotFound)
return nil
case ErrConfigNotFound:
session.Error("could-not-find-config", ErrConfigNotFound, lager.Data{
"pipeline": pipelineName,
})
w.WriteHeader(http.StatusNotFound)
return nil
case nil:
break
default:
session.Error("failed-to-build-template-data", err, lager.Data{
"resource": resourceName,
"pipeline": pipelineName,
})
return err
}
err = handler.template.Execute(w, templateData)
if err != nil {
session.Fatal("failed-to-build-template", err, lager.Data{
"template-data": templateData,
})
return err
}
return nil
}