-
Notifications
You must be signed in to change notification settings - Fork 16
/
circleci.go
71 lines (61 loc) · 1.89 KB
/
circleci.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"path"
)
func getCircleAPIJSON(url string, destination interface{}) {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
fatalMsg("error calling CircleCI API at %v: %v", url, err)
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(destination)
if err != nil {
fatalMsg("error parsing CircleCI JSON at %v: %v", url, err)
}
}
type circleCIBranchList []struct {
BuildNum int `json:"build_num"`
}
type circleCITestResults struct {
Tests []struct {
File string `json:"file"`
RunTime float64 `json:"run_time"`
} `json:"tests"`
}
func circleCIAPIURL() string {
return fmt.Sprintf("https://circleci.com/api/v1.1/project/%s", circleCIProjectPrefix)
}
func getCircleCIBranchBuilds(branchName string) circleCIBranchList {
buildsURL := fmt.Sprintf("%s/tree/%s?filter=successful&circle-token=%s", circleCIAPIURL(), branchName, circleCIAPIKey)
var branchList circleCIBranchList
getCircleAPIJSON(buildsURL, &branchList)
return branchList
}
func getCircleCITestResults(buildNum int) circleCITestResults {
testResultsURL := fmt.Sprintf("%s/%d/tests?circle-token=%s", circleCIAPIURL(), buildNum, circleCIAPIKey)
var testResults circleCITestResults
getCircleAPIJSON(testResultsURL, &testResults)
return testResults
}
func getFileTimesFromCircleCI(fileTimes map[string]float64) {
builds := getCircleCIBranchBuilds(circleCIBranchName)
if len(builds) == 0 {
builds = getCircleCIBranchBuilds("master")
}
if len(builds) > 0 {
buildNum := builds[0].BuildNum
printMsg("using test timings from CircleCI build %d\n", buildNum)
testResults := getCircleCITestResults(buildNum)
for _, test := range testResults.Tests {
filePath := path.Clean(test.File)
fileTimes[filePath] += test.RunTime
}
}
}