forked from concourse/concourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
133 lines (110 loc) · 3.33 KB
/
get.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
package ccserver
import (
"encoding/xml"
"fmt"
"net/http"
"net/url"
"time"
"code.cloudfoundry.org/lager"
"github.com/pf-qiu/concourse/v6/atc"
"github.com/pf-qiu/concourse/v6/atc/db"
"github.com/tedsuo/rata"
)
type Project struct {
Activity string `xml:"activity,attr"`
LastBuildLabel string `xml:"lastBuildLabel,attr"`
LastBuildStatus string `xml:"lastBuildStatus,attr"`
LastBuildTime string `xml:"lastBuildTime,attr"`
Name string `xml:"name,attr"`
WebUrl string `xml:"webUrl,attr"`
}
type ProjectsContainer struct {
XMLName xml.Name `xml:"Projects"`
Projects []Project `xml:"Project"`
}
func (s *Server) GetCC(w http.ResponseWriter, r *http.Request) {
logger := s.logger.Session("get-cc")
teamName := rata.Param(r, "team_name")
team, found, err := s.teamFactory.FindTeam(teamName)
if err != nil {
logger.Error("failed-to-find-team", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if !found {
logger.Debug("team-not-found", lager.Data{"team": teamName})
w.WriteHeader(http.StatusNotFound)
return
}
pipelines, err := team.Pipelines()
if err != nil {
logger.Error("failed-to-get-all-active-pipelines", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
var projects []Project
for _, pipeline := range pipelines {
dashboards, err := pipeline.Dashboard()
if err != nil {
logger.Error("failed-to-get-dashboards", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
for _, dashboardJob := range dashboards {
if dashboardJob.FinishedBuild != nil {
projects = append(projects, s.buildProject(dashboardJob))
}
}
}
w.Header().Set("Content-Type", "application/xml")
err = xml.NewEncoder(w).Encode(ProjectsContainer{Projects: projects})
if err != nil {
logger.Error("failed-to-serialize-projects", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
func (s *Server) buildProject(j atc.JobSummary) Project {
var lastBuildStatus string
switch {
case db.BuildStatus(j.FinishedBuild.Status) == db.BuildStatusSucceeded:
lastBuildStatus = "Success"
case db.BuildStatus(j.FinishedBuild.Status) == db.BuildStatusFailed:
lastBuildStatus = "Failure"
default:
lastBuildStatus = "Exception"
}
var activity string
if j.NextBuild != nil {
activity = "Building"
} else {
activity = "Sleeping"
}
pipelineRef := atc.PipelineRef{
Name: j.PipelineName,
InstanceVars: j.PipelineInstanceVars,
}
return Project{
Activity: activity,
LastBuildLabel: fmt.Sprint(j.FinishedBuild.Name),
LastBuildStatus: lastBuildStatus,
LastBuildTime: time.Unix(j.FinishedBuild.EndTime, 0).UTC().Format(time.RFC3339),
Name: fmt.Sprintf("%s/%s", pipelineRef.String(), j.Name),
WebUrl: s.createWebUrl(j.TeamName, j.Name, pipelineRef),
}
}
func (s *Server) createWebUrl(teamName, jobName string, pipelineRef atc.PipelineRef) string {
externalURL, err := url.Parse(s.externalURL)
if err != nil {
fmt.Println("Could not parse externalURL")
}
queryParams := pipelineRef.QueryParams().Encode()
if queryParams != "" {
queryParams = "?" + queryParams
}
pipelineURL, err := url.Parse("/teams/" + teamName + "/pipelines/" + pipelineRef.Name + "/jobs/" + jobName + queryParams)
if err != nil {
fmt.Println("Could not parse pipelineURL")
}
return externalURL.ResolveReference(pipelineURL).String()
}