-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
210 lines (177 loc) · 4.71 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"sort"
"time"
"github.com/Masterminds/semver"
"github.com/markbates/pkger"
"github.com/robfig/cron"
)
type providerAPI struct {
Provider string `json:"name"`
Url string `json:"url"`
}
type apiVersion struct {
Provider string
Version string `json:"api_version"`
}
type capiMap struct {
Date string `json:"published_at"`
Body string `json:"body"`
Draft bool `json:"draft"`
Prerelease bool `json:"prerelease"`
}
type parsedCapiMap struct {
Date string
Version string
}
type finalMap struct {
Provider string
Version string
Date string
}
func convergeData(providerVersions []apiVersion, capiVersions []parsedCapiMap) []finalMap {
var finalArray []finalMap
latestCAPIRelease := capiVersions[0]
finalArray = append(finalArray, finalMap{Provider: "latest", Version: latestCAPIRelease.Version, Date: latestCAPIRelease.Date})
for _, provider := range providerVersions {
for _, release := range capiVersions {
if provider.Version == release.Version {
finalArray = append(finalArray, finalMap{Provider: provider.Provider, Version: provider.Version, Date: release.Date})
}
}
}
return finalArray
}
func getAPIVersions(client http.Client, apis []providerAPI) ([]apiVersion, error) {
var versions []apiVersion
for _, api := range apis {
var v apiVersion
v.Provider = api.Provider
url := api.Url
resp, err := client.Get(url)
if err != nil {
continue
}
err = json.NewDecoder(resp.Body).Decode(&v)
if err != nil {
continue
}
versions = append(versions, v)
}
return versions, nil
}
func constructCapiArray(client http.Client) ([]parsedCapiMap, error) {
var m []capiMap
var o []parsedCapiMap
req, err := http.NewRequest("GET", "https://api.github.com/repos/cloudfoundry/capi-release/releases?per_page=100", nil)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", fmt.Sprintf("token %s", os.Getenv("TOKEN")))
resp, err := client.Do(req)
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
err = json.Unmarshal(buf.Bytes(), &m)
if err != nil {
return nil, err
}
for _, release := range m {
if release.Prerelease || release.Draft {
continue
}
r := regexp.MustCompile(`CC API Version: ([0-9]+\.[0-9]+\.[0-9]+)`)
match := r.FindStringSubmatch(release.Body)
if len(match) < 2 {
continue
}
o = append(o, parsedCapiMap{Date: release.Date, Version: match[1]})
}
return o, nil
}
func generateVersions(apis []providerAPI) {
timeout := time.Duration(5 * time.Second)
client := http.Client{Timeout: timeout}
fmt.Println("INFO: getting provider api versions")
versions, err := getAPIVersions(client, apis)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
return
}
fmt.Println("INFO: getting capi versions")
capiArray, err := constructCapiArray(client)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
return
}
fmt.Println("INFO: converging json")
finalMap := convergeData(versions, capiArray)
sort.Slice(finalMap, func(i, j int) bool {
v1, _ := semver.NewVersion(finalMap[i].Version)
v2, _ := semver.NewVersion(finalMap[j].Version)
return v1.Compare(v2) <= 0
})
finalBytes, err := json.MarshalIndent(finalMap, "", " ")
if err != nil {
fmt.Printf("ERROR: %s\n", err)
return
}
fmt.Println("INFO: writing versions JSON")
err = ioutil.WriteFile("./static/versions.json", finalBytes, 0666)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
return
}
fmt.Println("INFO: writing versions file")
finalBytes = append([]byte("var versions = "), finalBytes...)
err = ioutil.WriteFile("./static/versions.js", finalBytes, 0666)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
return
}
fmt.Println("INFO: writing timestamp file")
timestamp := time.Now().UTC().String()
timestampContents := fmt.Sprintf("var timestamp = \"Backend data last generated at: %s\"", timestamp)
timestampBytes := []byte(timestampContents)
err = ioutil.WriteFile("./static/timestamp.js", timestampBytes, 0666)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
return
}
}
func main() {
var apis []providerAPI
providersFile, err := pkger.Open("/assets/providers.json")
if err != nil {
log.Fatal(err)
}
providersBytes, err := ioutil.ReadAll(providersFile)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(providersBytes, &apis)
if err != nil {
log.Fatal(err)
}
generateVersions(apis)
c := cron.New()
c.AddFunc("@every 1h", func() { generateVersions(apis) })
c.Start()
fmt.Println("INFO: starting server")
http.Handle("/", http.FileServer(http.Dir("static")))
port, set := os.LookupEnv("PORT")
if !set {
port = "8080"
}
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}