-
Notifications
You must be signed in to change notification settings - Fork 7
/
circle-ci.go
63 lines (53 loc) · 1.55 KB
/
circle-ci.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/pkg/errors"
)
// Artifact is type for path info
type Artifact struct {
Path string
PrettyPath string
NodeIndex int
URL string
}
// APIEndpoint is circleci endpoint
const APIEndpoint = "https://circleci.com/api/v1"
func readReportFileFromCircleCI(apiToken, userName, repositoryName string) ([]byte, error) {
client := &http.Client{}
artifactAPIURL := fmt.Sprintf(APIEndpoint+"/project/%v/%v/latest/artifacts?circle-token=%v",
userName, repositoryName, apiToken)
req, err := http.NewRequest("GET", artifactAPIURL, nil)
req.Header.Add("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "artifacts API failed")
}
var artifacts []Artifact
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&artifacts)
if err != nil {
resp.Body.Close()
return nil, errors.Wrap(err, "artifacts API response parse failed")
}
resp.Body.Close()
resp, err = http.Get(artifacts[0].URL)
if err != nil {
return nil, errors.Wrap(err, "artifacts get failed")
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}
func triggerBuild(apiToken, userName, repositoryName string) error {
client := &http.Client{}
triggerBuildAPIURL := fmt.Sprintf(APIEndpoint+"/project/%v/%v/tree/master?circle-token=%v",
userName, repositoryName, apiToken)
req, err := http.NewRequest("POST", triggerBuildAPIURL, nil)
_, err = client.Do(req)
if err != nil {
return errors.Wrap(err, "trigger build API failed")
}
return nil
}