/
build.go
176 lines (142 loc) · 3.71 KB
/
build.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
package build
import (
"encoding/json"
"fmt"
"net/http"
"os"
"sort"
"strconv"
"strings"
"time"
"github.com/hailo-platform/H2O/build-service/models"
mhttp "github.com/mreiferson/go-httpclient"
)
// Use one client for all requests
var client *http.Client
var BuildUrl string = "https://build-api.elasticride.com"
var apiKey string = os.Getenv("BUILD_SERVICE_API_KEY")
func init() {
transport := &mhttp.Transport{
ConnectTimeout: 1 * time.Second,
RequestTimeout: 5 * time.Second,
ResponseHeaderTimeout: 5 * time.Second,
}
client = &http.Client{Transport: transport}
}
func GetAllBuilt() []string {
builds, _ := callBuildNames("")
return builds
}
func GetAllVersions(service string) []string {
builds, err := callBuilds(service)
if err != nil {
return make([]string, 0)
}
buildstr := make([]string, len(builds))
for i, bld := range builds {
buildstr[i] = bld.Version
}
return buildstr
}
var bufferedBranch map[string]string = make(map[string]string)
var bufferedGitHash map[string]string = make(map[string]string)
func GetBranchBuffered(service string, version string) (string, string) {
if bufferedBranch[service+version] == "" {
GetBranch(service, version)
} else {
go GetBranch(service, version)
}
return bufferedBranch[service+version], bufferedGitHash[service+version]
}
func GetBranch(service string, version string) (string, string) {
bld, err := callBuild(service, version)
if err != nil {
fmt.Println("Call to build service failed. Is your VPN connected to Global01-test?")
return "", ""
}
if bld.Version == version {
bufferedBranch[service+version] = bld.Branch
bufferedGitHash[service+version] = strings.TrimPrefix(strings.SplitAfterN(bld.SourceURL, "commit", 2)[1], "/")[:7]
return bufferedBranch[service+version], bufferedGitHash[service+version]
}
return "", ""
}
func GetLatestVersion(service string) string {
buildArr := GetAllVersions(service)
if len(buildArr) == 0 {
return ""
}
sort.Strings(buildArr)
return buildArr[len(buildArr)-1]
}
func GetLatestVersionBranch(service, branch string) int {
builds, err := callBuilds(service)
if err != nil {
fmt.Printf("Unable to get latest branch version: %v \n", err)
return 0
}
latestVersion := 0
for _, build := range builds {
if build.Branch == branch {
v, _ := strconv.Atoi(build.Version)
if v > latestVersion {
latestVersion = v
}
}
}
return latestVersion
}
func callBuild(service string, version string) (*models.Build, error) {
build := &models.Build{}
req, _ := http.NewRequest("GET", BuildUrl+"/"+service+"/"+version, nil)
if apiKey != "" {
req.Header.Add("x-api-key", apiKey)
}
resp, err := client.Do(req)
if err != nil {
return build, err
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(build)
if err != nil {
return build, err
}
return build, nil
}
func callBuilds(service string) ([]models.Build, error) {
builds := make([]models.Build, 0)
req, _ := http.NewRequest("GET", BuildUrl+"/"+service, nil)
if apiKey != "" {
req.Header.Add("x-api-key", apiKey)
}
resp, err := client.Do(req)
if err != nil {
return builds, err
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&builds)
if err != nil {
return builds, err
}
return builds, nil
}
func callBuildNames(filter string) ([]string, error) {
builds := make([]string, 0)
if len(filter) != 0 {
filter = fmt.Sprintf("?filter=%s", filter)
}
req, _ := http.NewRequest("GET", BuildUrl+"/names"+filter, nil)
if apiKey != "" {
req.Header.Add("x-api-key", apiKey)
}
resp, err := client.Do(req)
if err != nil {
return builds, err
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&builds)
if err != nil {
return builds, err
}
return builds, nil
}