-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathannotations.go
More file actions
145 lines (124 loc) · 4.72 KB
/
annotations.go
File metadata and controls
145 lines (124 loc) · 4.72 KB
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
package buildkite
import (
"context"
"fmt"
"net/http"
)
// AnnotationsService handles communication with the annotation related
// methods of the buildkite API.
//
// buildkite API docs: https://buildkite.com/docs/api/annotations
type AnnotationsService struct {
client *Client
}
// Annotation represents an annotation which has been stored from a build
type Annotation struct {
ID string `json:"id,omitempty"`
Context string `json:"context,omitempty"`
Style string `json:"style,omitempty"`
Scope string `json:"scope,omitempty"`
Priority int `json:"priority,omitempty"`
BodyHTML string `json:"body_html,omitempty"`
CreatedAt *Timestamp `json:"created_at,omitempty"`
UpdatedAt *Timestamp `json:"updated_at,omitempty"`
}
type AnnotationCreate struct {
Body string `json:"body,omitempty"`
Context string `json:"context,omitempty"`
Style string `json:"style,omitempty"`
Priority int `json:"priority,omitempty"`
Append bool `json:"append"`
}
// AnnotationListOptions specifies the optional parameters to the
// AnnoationsService.List method.
type AnnotationListOptions struct {
ListOptions
}
// ListByBuild gets annotations for a specific build
//
// buildkite API docs: https://buildkite.com/docs/apis/rest-api/annotations#list-annotations-for-a-build
func (as *AnnotationsService) ListByBuild(ctx context.Context, org string, pipeline string, build string, opt *AnnotationListOptions) ([]Annotation, *Response, error) {
u := fmt.Sprintf("v2/organizations/%s/pipelines/%s/builds/%s/annotations", org, pipeline, build)
u, err := addOptions(u, opt)
if err != nil {
return nil, nil, err
}
req, err := as.client.NewRequest(ctx, "GET", u, nil)
if err != nil {
return nil, nil, err
}
var annotations []Annotation
resp, err := as.client.Do(req, &annotations)
if err != nil {
return nil, resp, err
}
return annotations, resp, err
}
func (as *AnnotationsService) Create(ctx context.Context, org, pipeline, build string, ac AnnotationCreate) (Annotation, *Response, error) {
u := fmt.Sprintf("v2/organizations/%s/pipelines/%s/builds/%s/annotations", org, pipeline, build)
req, err := as.client.NewRequest(ctx, "POST", u, ac)
if err != nil {
return Annotation{}, nil, err
}
var annotation Annotation
resp, err := as.client.Do(req, &annotation)
if err != nil {
return Annotation{}, resp, err
}
return annotation, resp, err
}
func (as *AnnotationsService) Delete(ctx context.Context, org, pipeline, build, annotationUUID string) (*Response, error) {
u := fmt.Sprintf("v2/organizations/%s/pipelines/%s/builds/%s/annotations/%s", org, pipeline, build, annotationUUID)
req, err := as.client.NewRequest(ctx, http.MethodDelete, u, nil)
if err != nil {
return nil, err
}
return as.client.Do(req, nil)
}
// ListByJob gets annotations for a specific job
//
// buildkite API docs: https://buildkite.com/docs/apis/rest-api/annotations#list-annotations-for-a-job
func (as *AnnotationsService) ListByJob(ctx context.Context, org, pipeline, build, jobID string, opt *AnnotationListOptions) ([]Annotation, *Response, error) {
u := fmt.Sprintf("v2/organizations/%s/pipelines/%s/builds/%s/jobs/%s/annotations", org, pipeline, build, jobID)
u, err := addOptions(u, opt)
if err != nil {
return nil, nil, err
}
req, err := as.client.NewRequest(ctx, "GET", u, nil)
if err != nil {
return nil, nil, err
}
var annotations []Annotation
resp, err := as.client.Do(req, &annotations)
if err != nil {
return nil, resp, err
}
return annotations, resp, err
}
// CreateForJob creates an annotation scoped to a specific job
//
// buildkite API docs: https://buildkite.com/docs/apis/rest-api/annotations#create-an-annotation-on-a-job
func (as *AnnotationsService) CreateForJob(ctx context.Context, org, pipeline, build, jobID string, ac AnnotationCreate) (Annotation, *Response, error) {
u := fmt.Sprintf("v2/organizations/%s/pipelines/%s/builds/%s/jobs/%s/annotations", org, pipeline, build, jobID)
req, err := as.client.NewRequest(ctx, "POST", u, ac)
if err != nil {
return Annotation{}, nil, err
}
var annotation Annotation
resp, err := as.client.Do(req, &annotation)
if err != nil {
return Annotation{}, resp, err
}
return annotation, resp, err
}
// DeleteForJob deletes an annotation on a job
//
// buildkite API docs: https://buildkite.com/docs/apis/rest-api/annotations#delete-an-annotation-on-a-job
func (as *AnnotationsService) DeleteForJob(ctx context.Context, org, pipeline, build, jobID, annotationUUID string) (*Response, error) {
u := fmt.Sprintf("v2/organizations/%s/pipelines/%s/builds/%s/jobs/%s/annotations/%s", org, pipeline, build, jobID, annotationUUID)
req, err := as.client.NewRequest(ctx, http.MethodDelete, u, nil)
if err != nil {
return nil, err
}
return as.client.Do(req, nil)
}