-
Notifications
You must be signed in to change notification settings - Fork 0
/
bamboo.go
135 lines (123 loc) · 3.97 KB
/
bamboo.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
package bamboo
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
)
// Service is a struct which exposes functinality for talking to the Attlasian Bamboo REST API
type Service struct {
Username string
Password string
BaseURL string
requestHandler RequestHandler
}
// New initializes a new service struct
func New(userName string, password string, baseURL string, requestHandler RequestHandler) Service {
b := Service{
Username: userName,
Password: password,
BaseURL: baseURL,
}
if requestHandler == nil {
b.requestHandler = b
} else {
b.requestHandler = requestHandler
}
return b
}
// GetBuildResults retrieves the last n(max) build results
func (b Service) GetBuildResults(max int) Results {
endpoint := b.BaseURL + apiURL + latestResults
req := b.requestHandler.CreateRequest("GET", endpoint)
if max > 0 {
values := req.URL.Query()
values.Add(maxResultsKey, strconv.Itoa(max))
req.URL.RawQuery = values.Encode()
}
req.SetBasicAuth(b.Username, b.Password)
body := b.requestHandler.ProcessRequest(req)
var jsonData Results
json.Unmarshal([]byte(body), &jsonData)
return jsonData
}
// GetPlanResult retrieves the build result for a particular plan
func (b Service) GetPlanResult(planKey string) PlanResult {
endpoint := b.BaseURL + apiURL + latestResults + planKey
req := b.requestHandler.CreateRequest("GET", endpoint)
req.SetBasicAuth(b.Username, b.Password)
values := req.URL.Query()
req.URL.RawQuery = values.Encode()
body := b.requestHandler.ProcessRequest(req)
var jsonData PlanResult
json.Unmarshal([]byte(body), &jsonData)
return jsonData
}
// GetProjects retrieves all projects
func (b Service) GetProjects() Projects {
endpoint := b.BaseURL + apiURL + projectEndpoint
req := b.requestHandler.CreateRequest("GET", endpoint)
req.SetBasicAuth(b.Username, b.Password)
body := b.requestHandler.ProcessRequest(req)
var jsonData Projects
json.Unmarshal([]byte(body), &jsonData)
return jsonData
}
// GetPlans retrieves the last n(max) plans
func (b Service) GetPlans(max int) Plans {
endpoint := b.BaseURL + apiURL + planEndpoint
req := b.requestHandler.CreateRequest("GET", endpoint)
req.SetBasicAuth(b.Username, b.Password)
if max > 0 {
values := req.URL.Query()
values.Add(maxResultsKey, strconv.Itoa(max))
req.URL.RawQuery = values.Encode()
}
body := b.requestHandler.ProcessRequest(req)
var jsonData Plans
json.Unmarshal([]byte(body), &jsonData)
return jsonData
}
// GetPlanDetails retrieves the PlanDetails for an individual plan
func (b Service) GetPlanDetails(planKey string) PlanDetails {
endpoint := b.BaseURL + apiURL + planEndpoint + "/" + planKey
req := b.requestHandler.CreateRequest("GET", endpoint)
req.SetBasicAuth(b.Username, b.Password)
body := b.requestHandler.ProcessRequest(req)
var jsonData PlanDetails
json.Unmarshal([]byte(body), &jsonData)
return jsonData
}
// GetPlanBranches retrieves all child branches of a particular plan
func (b Service) GetPlanBranches(planKey string) PlanBranches {
endpoint := b.BaseURL + apiURL + fmt.Sprintf(listPlanBranchesEndpoint, planKey)
req := b.requestHandler.CreateRequest("GET", endpoint)
req.SetBasicAuth(b.Username, b.Password)
body := b.requestHandler.ProcessRequest(req)
var jsonData PlanBranches
json.Unmarshal([]byte(body), &jsonData)
return jsonData
}
// CreateRequest builds a json based http request
func (b Service) CreateRequest(requestType string, url string) *http.Request {
req, _ := http.NewRequest(requestType, url, nil)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
return req
}
// ProcessRequest excecutes a http request and returns the body as a string
func (b Service) ProcessRequest(req *http.Request) string {
client := http.Client{}
results, _ := client.Do(req)
return b.getBody(results)
}
func (b Service) getBody(response *http.Response) string {
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
return string(body)
}