-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
309 lines (263 loc) · 6.35 KB
/
main.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
302
303
304
305
306
307
308
309
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
)
func main() {
versions, _ := getVersionDeps()
e := gin.Default()
// bind website
{
e.LoadHTMLFiles("finder.html", "validator.html")
e.GET("/validate", func(c *gin.Context) {
c.HTML(http.StatusOK, "validator.html", ValidateResponse{
GoVersion: "1.16.4",
KrakendVersion: "v1.4.1",
})
})
e.POST("/validate/:krakend_version/:go_version", func(c *gin.Context) {
version := c.Param("krakend_version")
goVersion := c.Param("go_version")
a, ok := versions[version]
if !ok {
c.AbortWithStatus(404)
return
}
diffs := checkLines(a, goVersion, parseSumFile(c.Request.Body))
c.HTML(http.StatusOK, "validator.html", ValidateResponse{
GoVersion: goVersion,
KrakendVersion: version,
Result: diffs,
})
})
e.GET("/versions/:version", func(c *gin.Context) {
version := c.Param("version")
if v, ok := versions[version]; ok {
c.HTML(http.StatusOK, "finder.html", VersionResponse{Name: version, Version: v})
return
}
c.AbortWithStatus(404)
})
e.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "finder.html", VersionResponse{Name: "v1.4.1", Version: versions["v1.4.1"]})
})
}
// bind api
{
e.GET("/api/versions/:version", func(c *gin.Context) {
version := c.Param("version")
if v, ok := versions[version]; ok {
c.JSON(200, v)
return
}
c.AbortWithStatus(404)
})
e.GET("/api/versions", func(c *gin.Context) {
c.JSON(200, versions)
})
e.POST("/api/versions/:krakend_version/:go_version", func(c *gin.Context) {
version := c.Param("krakend_version")
goVersion := c.Param("go_version")
a, ok := versions[version]
if !ok {
c.AbortWithStatus(404)
return
}
diffs := checkLines(a, goVersion, parseSumFile(c.Request.Body))
if len(diffs) == 0 {
c.JSON(200, []string{})
return
}
c.JSON(400, diffs)
})
}
e.Run(":8080")
}
func checkLines(version Version, goVersion string, lines []string) []Diff {
deps := map[string]string{}
for _, dep := range lines {
parts := strings.Split(dep, " ")
if len(parts) < 2 {
log.Println("dep ignored:", dep)
continue
}
cleanedVersion := cleanVersion(parts[1])
if deps[parts[0]] >= cleanedVersion {
continue
}
deps[parts[0]] = cleanedVersion
}
b := Version{
Go: goVersion,
Deps: deps,
}
return checkVersion(version, b)
}
func getVersionDeps() (map[string]Version, error) {
b, err := ioutil.ReadFile("versions.json")
if err != nil {
return getUpdatedVersionsDeps(), nil
}
versions := map[string]Version{}
if err := json.Unmarshal(b, &versions); err != nil {
return getUpdatedVersionsDeps(), nil
}
return versions, nil
}
func getUpdatedVersionsDeps() map[string]Version {
client := newHTTPClient()
versions := map[string]Version{}
for _, v := range getTags(client) {
fmt.Println("checking version:", v)
versions[v] = getUpdatedVersionDeps(readSumFileLines(client, v))
}
return versions
}
func getUpdatedVersionDeps(in []string) Version {
deps := map[string]string{}
for _, dep := range in {
parts := strings.Split(dep, " ")
cleanedVersion := cleanVersion(parts[1])
if deps[parts[0]] >= cleanedVersion {
continue
}
deps[parts[0]] = cleanedVersion
}
return Version{
Go: "",
Deps: deps,
}
}
type httpClient func(*http.Request) (*http.Response, error)
func newHTTPClient() httpClient {
timer := time.NewTicker(3 * time.Minute)
type response struct {
resp *http.Response
err error
}
type request struct {
req *http.Request
out chan response
}
in := make(chan request)
go func() {
for {
<-timer.C
r := <-in
resp, err := http.DefaultClient.Do(r.req)
r.out <- response{resp, err}
}
}()
return func(req *http.Request) (*http.Response, error) {
out := make(chan response)
in <- request{req: req, out: out}
r := <-out
return r.resp, r.err
}
}
func cleanVersion(v string) string {
if l := len(v); l > 7 && v[l-7:] == "/go.mod" {
return v[:l-7]
}
return v
}
func getTags(client httpClient) []string {
req, _ := http.NewRequest("GET", "https://api.github.com/repos/devopsfaith/krakend-ce/tags", nil)
req.Header.Add("user-agent", userAgent)
resp, err := client(req)
if err != nil {
fmt.Println(err.Error())
return []string{}
}
tags := Tags{}
if err := json.NewDecoder(resp.Body).Decode(&tags); err != nil {
fmt.Println(err.Error())
return []string{}
}
defer resp.Body.Close()
result := make([]string, len(tags))
for i, t := range tags {
result[i] = t.Name
}
return result
}
type Tags []struct {
Name string `json:"name"`
ZipballURL string `json:"zipball_url"`
TarballURL string `json:"tarball_url"`
Commit struct {
Sha string `json:"sha"`
URL string `json:"url"`
} `json:"commit"`
NodeID string `json:"node_id"`
}
type ValidateResponse struct {
GoVersion string
KrakendVersion string
Result []Diff
}
type VersionResponse struct {
Name string
Version Version
}
type Version struct {
Go string
Deps map[string]string
}
type Diff struct {
Name string
Expected string
Have string
}
func checkVersion(a, b Version) []Diff {
diffs := []Diff{}
if a.Go != b.Go {
diffs = append(diffs, Diff{Name: "go", Expected: a.Go, Have: b.Go})
}
for k, expect := range a.Deps {
have, ok := b.Deps[k]
if !ok {
continue
}
if have != expect {
diffs = append(diffs, Diff{Name: k, Expected: expect, Have: have})
}
}
return diffs
}
func readSumFileLines(client httpClient, version string) []string {
lines := []string{}
url := fmt.Sprintf("https://raw.githubusercontent.com/devopsfaith/krakend-ce/%s/go.sum", version)
req, nil := http.NewRequest("GET", url, nil)
req.Header.Add("user-agent", userAgent)
resp, err := client(req)
if err != nil {
fmt.Println(err.Error())
return lines
}
lines = parseSumFile(resp.Body)
resp.Body.Close()
return lines
}
func parseSumFile(r io.Reader) []string {
lines := []string{}
scanner := bufio.NewScanner(r)
for scanner.Scan() {
lines = append(lines, scanner.Text()) // Println will add back the final '\n'
}
if err := scanner.Err(); err != nil {
fmt.Println("reading standard input:", err.Error())
}
return lines
}
const (
userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36"
)