forked from vmware-archive/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
101 lines (80 loc) · 1.85 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
package getjob
import (
"html/template"
"net/http"
"strconv"
"github.com/concourse/atc/web"
"code.cloudfoundry.org/lager"
)
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 {
TeamName string
PipelineName string
JobName string
Since int
Until int
QueryGroups []string
}
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
}
teamName := r.FormValue(":team_name")
pipelineName := r.FormValue(":pipeline_name")
client := handler.clientFactory.Build(r)
team := client.Team(teamName)
_, pipelineFound, err := team.Pipeline(pipelineName)
if err != nil {
return err
}
if !pipelineFound {
w.WriteHeader(http.StatusNotFound)
return nil
}
_, jobFound, err := team.Job(pipelineName, jobName)
if err != nil {
return err
}
if !jobFound {
w.WriteHeader(http.StatusNotFound)
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
}
templateData := TemplateData{
TeamName: teamName,
PipelineName: pipelineName,
JobName: jobName,
Since: since,
Until: until,
QueryGroups: nil,
}
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
}