forked from vmware-archive/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
146 lines (121 loc) · 2.82 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
package getjob
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 TemplateData struct {
JobName string
Since int
Until int
GroupStates []group.State
PipelineName string
TeamName string
}
var ErrConfigNotFound = errors.New("could not find config")
var ErrJobConfigNotFound = errors.New("could not find job")
func FetchTemplateData(
pipelineName string,
team concourse.Team,
jobName string,
page concourse.Page,
) (TemplateData, error) {
pipeline, pipelineFound, err := team.Pipeline(pipelineName)
if err != nil {
return TemplateData{}, err
}
if !pipelineFound {
return TemplateData{}, ErrConfigNotFound
}
_, jobFound, err := team.Job(pipelineName, jobName)
if err != nil {
return TemplateData{}, err
}
if !jobFound {
return TemplateData{}, ErrJobConfigNotFound
}
return TemplateData{
TeamName: team.Name(),
PipelineName: pipelineName,
JobName: jobName,
Since: page.Since,
Until: page.Until,
GroupStates: group.States(pipeline.Groups, func(g atc.GroupConfig) bool {
for _, groupJob := range g.Jobs {
if groupJob == jobName {
return true
}
}
return false
}),
}, nil
}
func (handler *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) error {
log := handler.logger.Session("job")
jobName := r.FormValue(":job")
if len(jobName) == 0 {
w.WriteHeader(http.StatusBadRequest)
return nil
}
since, parseErr := strconv.Atoi(r.FormValue("since"))
if parseErr != nil {
since = 0
}
until, parseErr := strconv.Atoi(r.FormValue("until"))
if parseErr != nil {
until = 0
}
teamName := r.FormValue(":team_name")
client := handler.clientFactory.Build(r)
templateData, err := FetchTemplateData(
r.FormValue(":pipeline_name"),
client.Team(teamName),
jobName,
concourse.Page{
Since: since,
Until: until,
Limit: atc.PaginationWebLimit,
},
)
switch err {
case ErrJobConfigNotFound:
log.Info("could-not-find-job-in-config", lager.Data{
"job": jobName,
})
w.WriteHeader(http.StatusNotFound)
return nil
case nil:
break
default:
log.Error("failed-to-build-template-data", err, lager.Data{
"job": jobName,
})
return err
}
err = handler.template.Execute(w, templateData)
if err != nil {
log.Fatal("failed-to-build-template", err, lager.Data{
"template-data": templateData,
})
return err
}
return nil
}