-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstoplight.go
301 lines (244 loc) · 7.4 KB
/
stoplight.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package main
import (
"encoding/xml"
"fmt"
"io"
"log"
"net/http"
"net/url"
_ "os"
"regexp"
_ "regexp"
"strings"
"time"
rice "github.com/GeertJohan/go.rice"
"github.com/labstack/echo/v4"
"github.com/spf13/viper"
)
// Filter is a filter function applied to a single record.
type Filter func(Project) bool
// FilterBulk is a bulk filter function applied to an entire slice of records.
type FilterBulk func([]Project) []Project
// Projects XML & JSON structure
// LastBuildStatus <implement comment>
type LastBuildStatus string
// CurrentStatus <implement comment>
type CurrentStatus string
// Projects <implement comment>
type Projects struct {
XMLName xml.Name `xml:"Projects"`
Projects []Project `xml:"Project"`
}
// Project describes the XML structure of a project
type Project struct {
XMLName xml.Name `xml:"Project"`
Name string `json:"name" xml:"name,attr"`
BuildURL string `json:"build_url" xml:"webUrl,attr"`
LastBuildID string `json:"last_build_id" xml:"lastBuildLabel,attr"`
LastBuildTime string `json:"last_build_time" xml:"lastBuildTime,attr"`
LastBuildStatus LastBuildStatus `json:"last_build_status" xml:"lastBuildStatus,attr"`
CurrentStatus CurrentStatus `json:"current_status" xml:"activity,attr"`
}
// UnmarshalXMLAttr converts the XML value to JSON value
func (s *LastBuildStatus) UnmarshalXMLAttr(attr xml.Attr) error {
status := attr.Value
var attrValue string
switch strings.ToLower(status) {
default:
attrValue = "unknown"
case "failure":
attrValue = "failed"
case "success":
attrValue = "passed"
}
*s = LastBuildStatus(attrValue)
return nil
}
// UnmarshalXMLAttr converts the XML value to JSON value
func (s *CurrentStatus) UnmarshalXMLAttr(attr xml.Attr) error {
status := attr.Value
var attrValue string
switch strings.ToLower(status) {
default:
attrValue = "unknown"
case "building":
attrValue = "building"
case "sleeping":
attrValue = "done"
}
*s = CurrentStatus(attrValue)
return nil
}
func config() {
viper.AddConfigPath(".")
viper.SetConfigName("config")
viper.SetDefault("Port", 1323)
viper.SetDefault("Host", "0.0.0.0")
// Show all projects by default
viper.SetDefault("server.projects", ".*")
viper.SetDefault("server.ignored_projects", "")
if err := viper.ReadInConfig(); err != nil {
log.Fatalf("Error reading config file, %s", err)
}
fmt.Printf("Using config: %s\n", viper.ConfigFileUsed())
// ENV Variables Setup
// Prefix with "STOPLIGHT_"
viper.SetEnvPrefix("stoplight")
viper.AutomaticEnv()
}
func main() {
config()
// HTTP
host := viper.GetString("Host")
port := viper.GetInt("Port")
address := fmt.Sprintf("%s:%v", host, port)
fmt.Printf("Got server address http://%v\n", address)
s := &http.Server{
Addr: address,
}
e := echo.New()
e.HideBanner = true
// the file server for rice. "public" is the folder where the files come from.
assetHandler := http.FileServer(rice.MustFindBox("public").HTTPBox())
// serves the index.html from rice
e.GET("/", echo.WrapHandler(assetHandler))
// servers other static files
e.GET("/public/*", echo.WrapHandler(http.StripPrefix("/public/", assetHandler)))
e.GET("/projects.json", FetchProjects)
e.Logger.Fatal(e.StartServer(s))
}
// FetchProjects e.GET("/projects.json", FetchProjects)
// CONNECT -> FETCH CC XML -> PARSE -> FILTER -> JSON
func FetchProjects(c echo.Context) error {
server := viper.GetString("server.url")
if !viper.IsSet("server") {
log.Fatal("missing a server to connect to. Aborting...")
}
// Parse URL
uri, err := url.Parse(server)
if err != nil {
log.Fatal("Failed on parsing URL", err)
}
// Fetch XML from CI server
client := &http.Client{
Timeout: time.Second * 10,
}
req, err := http.NewRequest("GET", uri.String(), nil)
if err != nil {
log.Fatal("Failed to create GET request", err)
}
if viper.IsSet("server.username") && viper.IsSet("server.password") {
username := viper.GetString("server.username")
password := viper.GetString("server.password")
req.SetBasicAuth(username, password)
}
res, err := client.Do(req)
if err != nil {
log.Fatal("Failed on client", err)
}
ccxml, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal("Failed on parsing Body", err)
}
// Parse XML
data := &Projects{}
err = xml.Unmarshal(ccxml, data)
if err != nil {
// TODO ct 2019-04-23 Handle empty responses gracefully
// eg. so project.json shows an empty JSON object
log.Println("Failed on XML Unmarshal:", err, "Please check your XML")
}
// fmt.Println("DEBUG data")
// fmt.Printf("DEBUG %T", data.Project)
filteredProjects := FilterProjects(data.Projects)
// Return JSON
return c.JSON(http.StatusOK, filteredProjects)
}
// FilterProjects applies a set of filters removing any unwanted projects
func FilterProjects(records []Project) []Project {
return ApplyBulkFilters(
ApplyFilters(records,
FilterIncludeProjects,
FilterExcludedProjects,
),
FilterDuplicates,
)
}
// ApplyFilters applies a set of filters to a record list.
// Each record will be checked against each filter.
// The filters are applied in the order they are passed in.
func ApplyFilters(records []Project, filters ...Filter) []Project {
// Make sure there are actually filters to be applied.
if len(filters) == 0 {
return records
}
filteredRecords := make([]Project, 0, len(records))
// Range over the records and apply all the filters to each record.
// If the record passes all the filters, add it to the final slice.
for _, r := range records {
keep := true
for _, f := range filters {
if !f(r) {
keep = false
break
}
}
if keep {
filteredRecords = append(filteredRecords, r)
}
}
return filteredRecords
}
// ApplyBulkFilters applies a set of filters to the entire slice of records.
// Used when each record filter requires knowledge of the other records, e.g. de-duping.
func ApplyBulkFilters(records []Project, filters ...FilterBulk) []Project {
for _, f := range filters {
records = f(records)
}
return records
}
// FilterDuplicates is a bulk filter to remove any duplicates from the set.
func FilterDuplicates(records []Project) []Project {
recordMap := map[string]bool{}
filteredRecords := []Project{}
for _, record := range records {
if ok := recordMap[record.Name]; ok {
continue
}
recordMap[record.Name] = true
filteredRecords = append(filteredRecords, record)
}
return filteredRecords
}
// FilterIncludeProjects makes sure *only* these projects are listed.
func FilterIncludeProjects(record Project) bool {
patterns := viper.GetStringSlice("server.projects")
// Compile regular expressions just once
regexps := make([]*regexp.Regexp, len(patterns))
for i := range patterns {
regexps[i] = regexp.MustCompile(patterns[i])
}
for _, regexp := range regexps {
if regexp.MatchString(record.Name) {
// log.Printf("allowing element %s because it matches allow pattern\n", record.Name)
return true
}
}
return false
}
// FilterExcludedProjects filters out all excluded projects.
func FilterExcludedProjects(record Project) bool {
patterns := viper.GetStringSlice("server.ignored_projects")
regexps := make([]*regexp.Regexp, len(patterns))
for i := range patterns {
regexps[i] = regexp.MustCompile(patterns[i])
}
for _, regexp := range regexps {
if regexp.MatchString(record.Name) {
// log.Printf("deleting element %s due to ignore pattern\n", record.Name)
return false
}
}
return true
}