forked from rcarmstrong/go-bamboo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
labels.go
45 lines (35 loc) · 1.06 KB
/
labels.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
package bamboo
import (
"fmt"
"net/http"
)
// LabelService handles communication with the labels on a plan result
type LabelService service
// Label is a single label on a result
type Label struct {
Name string `json:"name"`
ResultKey string `json:"-"`
}
func (lb Label) isEmpty() bool {
return lb.ResultKey == "" || lb.Name == ""
}
// AddLabel will add a label to the given result.
func (c *LabelService) AddLabel(label *Label) (bool, *http.Response, error) {
if label == nil || label.isEmpty() {
return false, nil, &simpleError{"Label cannot be nil or empty"}
}
u := fmt.Sprintf("result/%s/label.json", label.ResultKey)
request, err := c.client.NewRequest(http.MethodPost, u, label)
if err != nil {
return false, nil, err
}
request.Header.Add("Accept", "application/json")
response, err := c.client.Do(request, nil)
if err != nil {
return false, response, err
}
if !(response.StatusCode == 204) {
return false, response, &simpleError{fmt.Sprintf("Adding Label to %s returned %s", label.ResultKey, response.Status)}
}
return true, response, nil
}