-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathplans.go
190 lines (157 loc) · 4.94 KB
/
plans.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package bamboo
import (
"fmt"
"net/http"
"strconv"
)
// PlanService handles communication with the plan related methods
type PlanService service
// PlanCreateBranchOptions specifies the optional parameters
// for the CreatePlanBranch method
type PlanCreateBranchOptions struct {
VCSBranch string
}
// PlanResponse encapsultes a response from the plan service
type PlanResponse struct {
*ResourceMetadata
Plans *Plans `json:"plans"`
}
// Plans is a collection of Plan objects
type Plans struct {
*CollectionMetadata
PlanList []*Plan `json:"plan"`
}
// Plan is the definition of a single plan
type Plan struct {
ShortName string `json:"shortName,omitempty"`
ShortKey string `json:"shortKey,omitempty"`
Type string `json:"type,omitempty"`
Enabled bool `json:"enabled,omitempty"`
Link *Link `json:"link,omitempty"`
Key string `json:"key,omitempty"`
Name string `json:"name,omitempty"`
PlanKey *PlanKey `json:"planKey,omitempty"`
}
// PlanKey holds the plan-key for a plan
type PlanKey struct {
Key string `json:"key,omitempty"`
}
// CreatePlanBranch will create a plan branch with the given branch name for the specified build
func (p *PlanService) CreatePlanBranch(planKey, branchName string, options *PlanCreateBranchOptions) (bool, *http.Response, error) {
var u string
if !emptyStrings(planKey, branchName) {
u = fmt.Sprintf("plan/%s/branch/%s.json", planKey, branchName)
} else {
return false, nil, &simpleError{"Project key and/or branch name cannot be empty"}
}
request, err := p.client.NewRequest(http.MethodPut, u, nil)
if err != nil {
return false, nil, err
}
if options != nil && options.VCSBranch != "" {
values := request.URL.Query()
values.Add("vcsBranch", options.VCSBranch)
request.URL.RawQuery = values.Encode()
}
response, err := p.client.Do(request, nil)
if err != nil {
return false, response, err
}
if !(response.StatusCode == 200) {
return false, response, &simpleError{fmt.Sprintf("Create returned %d", response.StatusCode)}
}
return true, response, nil
}
// NumberOfPlans returns the number of plans on the Bamboo server
func (p *PlanService) NumberOfPlans() (int, *http.Response, error) {
request, err := p.client.NewRequest(http.MethodGet, "plan.json", nil)
if err != nil {
return 0, nil, err
}
// Restrict results to one for speed
values := request.URL.Query()
values.Add("max-results", "1")
request.URL.RawQuery = values.Encode()
planResp := PlanResponse{}
response, err := p.client.Do(request, &planResp)
if err != nil {
return 0, response, err
}
if response.StatusCode != 200 {
return 0, response, &simpleError{fmt.Sprintf("Getting the number of plans returned %s", response.Status)}
}
return planResp.Plans.Size, response, nil
}
// ListPlans gets information on all plans
func (p *PlanService) ListPlans() ([]*Plan, *http.Response, error) {
// Get number of plans to set max-results
numPlans, resp, err := p.NumberOfPlans()
if err != nil {
return nil, resp, err
}
request, err := p.client.NewRequest(http.MethodGet, "plan.json", nil)
if err != nil {
return nil, nil, err
}
q := request.URL.Query()
q.Add("max-results", strconv.Itoa(numPlans))
request.URL.RawQuery = q.Encode()
planResp := PlanResponse{}
response, err := p.client.Do(request, &planResp)
if err != nil {
return nil, response, err
}
if response.StatusCode != 200 {
return nil, response, &simpleError{fmt.Sprintf("Getting plan information returned %s", response.Status)}
}
return planResp.Plans.PlanList, response, nil
}
// ListPlanKeys get all the plan keys for all build plans on Bamboo
func (p *PlanService) ListPlanKeys() ([]string, *http.Response, error) {
plans, response, err := p.ListPlans()
if err != nil {
return nil, response, err
}
keys := make([]string, len(plans))
for i, p := range plans {
keys[i] = p.Key
}
return keys, response, nil
}
// ListPlanNames returns a list of ShortNames of all plans
func (p *PlanService) ListPlanNames() ([]string, *http.Response, error) {
plans, response, err := p.ListPlans()
if err != nil {
return nil, response, err
}
names := make([]string, len(plans))
for i, p := range plans {
names[i] = p.ShortName
}
return names, response, nil
}
// PlanNameMap returns a map[string]string where the PlanKey is the key and the ShortName is the value
func (p *PlanService) PlanNameMap() (map[string]string, *http.Response, error) {
plans, response, err := p.ListPlans()
if err != nil {
return nil, response, err
}
planMap := make(map[string]string, len(plans))
for _, p := range plans {
planMap[p.Key] = p.ShortName
}
return planMap, response, nil
}
// DisablePlan will disable a plan or plan branch
func (p *PlanService) DisablePlan(planKey string) (*http.Response, error) {
u := fmt.Sprintf("plan/%s/enable", planKey)
request, err := p.client.NewRequest(http.MethodDelete, u, nil)
if err != nil {
return nil, err
}
response, err := p.client.Do(request, nil)
if err != nil {
return response, err
}
return response, nil
}