-
Notifications
You must be signed in to change notification settings - Fork 0
/
githubApiClient.go
123 lines (97 loc) · 2.81 KB
/
githubApiClient.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"github.com/rs/zerolog/log"
"github.com/sethgrid/pester"
)
// GithubAPIClient allows to communicate with the Github api
type GithubAPIClient interface {
SetBuildStatus(string, string, string, string) error
}
type githubAPIClientImpl struct {
}
func newGithubAPIClient() GithubAPIClient {
return &githubAPIClientImpl{}
}
type buildStatusRequestBody struct {
State string `json:"state"`
TargetURL string `json:"target_url,omitempty"`
Description string `json:"description,omitempty"`
Context string `json:"context,omitempty"`
}
// SetBuildStatus sets the build status for a specific revision
func (gh *githubAPIClientImpl) SetBuildStatus(accessToken, repoFullname, gitRevision, status string) (err error) {
// https://developer.github.com/v3/repos/statuses/
// estafette status: succeeded|failed|pending
// github stat: success|failure|error|pending
state := "success"
switch status {
case "succeeded":
state = "success"
case "failed":
state = "failure"
case "pending":
state = "pending"
}
logsURL := fmt.Sprintf(
"%vpipelines/%v/%v/builds/%v/logs",
*ciBaseURL,
*gitRepoSource,
repoFullname,
*estafetteBuildID,
)
params := buildStatusRequestBody{
State: state,
TargetURL: logsURL,
}
log.Info().Msgf("Setting logs url %v", params.TargetURL)
_, err = callGithubAPI("POST", fmt.Sprintf("https://api.github.com/repos/%v/statuses/%v", repoFullname, gitRevision), params, "token", accessToken)
return
}
func callGithubAPI(method, url string, params interface{}, authorizationType, token string) (body []byte, err error) {
// convert params to json if they're present
var requestBody io.Reader
if params != nil {
data, err := json.Marshal(params)
if err != nil {
return body, err
}
requestBody = bytes.NewReader(data)
}
// create client, in order to add headers
client := pester.New()
client.MaxRetries = 3
client.Backoff = pester.ExponentialJitterBackoff
client.KeepLog = true
request, err := http.NewRequest(method, url, requestBody)
if err != nil {
return
}
// add headers
request.Header.Add("Authorization", fmt.Sprintf("%v %v", authorizationType, token))
request.Header.Add("Accept", "application/vnd.github.machine-man-preview+json")
// perform actual request
response, err := client.Do(request)
if err != nil {
return
}
defer response.Body.Close()
body, err = ioutil.ReadAll(response.Body)
if err != nil {
return
}
// unmarshal json body
var b interface{}
err = json.Unmarshal(body, &b)
if err != nil {
log.Info().Err(err).Str("body", string(body)).Msgf("Deserializing response for '%v' Github api call failed", url)
return
}
log.Info().Msgf("Received successful response for '%v' Github api call with status code %v", url, response.StatusCode)
return
}