-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitbucketApiClient.go
144 lines (116 loc) · 3.62 KB
/
bitbucketApiClient.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"github.com/sethgrid/pester"
)
// BitbucketAPIClient communicates with the Bitbucket api
type BitbucketAPIClient interface {
SetBuildStatus(accessToken, repoFullname, gitRevision, status, buildVersion, releaseName, releaseAction string) error
}
type bitbucketAPIClientImpl struct {
}
func newBitbucketAPIClient() BitbucketAPIClient {
return &bitbucketAPIClientImpl{}
}
type buildStatusRequestBody struct {
State string `json:"state"`
Key string `json:"key"`
Name string `json:"name,omitempty"`
URL string `json:"url"`
Description string `json:"description,omitempty"`
}
// SetBuildStatus sets the build status for a specific revision
func (gh *bitbucketAPIClientImpl) SetBuildStatus(accessToken, repoFullname, gitRevision, status, buildVersion, releaseName, releaseAction string) (err error) {
// https://confluence.atlassian.com/bitbucket/buildstatus-resource-779295267.html
// estafette status: succeeded|failed|pending
// bitbucket stat: INPROGRESS|SUCCESSFUL|FAILED|STOPPED
state := "SUCCESSFUL"
switch status {
case "succeeded":
state = "SUCCESSFUL"
case "failed":
state = "FAILED"
case "pending":
state = "INPROGRESS"
}
logsURL := fmt.Sprintf(
"%vpipelines/%v/%v/builds/%v/logs",
*ciBaseURL,
*gitRepoSource,
repoFullname,
*estafetteBuildID,
)
// set description depending on status
description := fmt.Sprintf("Build version %v %v.", *estafetteBuildVersion, status)
if releaseName != "" {
description = fmt.Sprintf("Release %v to %v %v.", *estafetteBuildVersion, releaseName, status)
if releaseAction != "" {
description = fmt.Sprintf("Release %v to %v with %v %v.", *estafetteBuildVersion, releaseName, releaseAction, status)
}
}
params := buildStatusRequestBody{
State: state,
Key: "estafette",
Name: "Estafette",
URL: logsURL,
Description: description,
}
// {
// "state": "<INPROGRESS|SUCCESSFUL|FAILED>",
// "key": "<build-key>",
// "name": "<build-name>",
// "url": "<build-url>",
// "description": "<build-description>"
// }
log.Printf("Setting logs url %v", params.URL)
_, err = callBitbucketAPI("POST", fmt.Sprintf("https://api.bitbucket.org/2.0/repositories/%v/commit/%v/statuses/build", repoFullname, gitRevision), params, "Bearer", accessToken)
return
}
func callBitbucketAPI(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("Content-Type", "application/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.Printf("Deserializing response for '%v' Bitbucket api call failed. Body: %v. Error: %v", url, string(body), err)
return
}
log.Printf("Received successful response for '%v' Bitbucket api call with status code %v", url, response.StatusCode)
return
}