1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "fmt"
11 "time"
12 )
13
14
15
16
17
18 type IssuesService service
19
20
21
22
23
24
25
26
27 type Issue struct {
28 ID *int64 `json:"id,omitempty"`
29 Number *int `json:"number,omitempty"`
30 State *string `json:"state,omitempty"`
31
32 StateReason *string `json:"state_reason,omitempty"`
33 Locked *bool `json:"locked,omitempty"`
34 Title *string `json:"title,omitempty"`
35 Body *string `json:"body,omitempty"`
36 AuthorAssociation *string `json:"author_association,omitempty"`
37 User *User `json:"user,omitempty"`
38 Labels []*Label `json:"labels,omitempty"`
39 Assignee *User `json:"assignee,omitempty"`
40 Comments *int `json:"comments,omitempty"`
41 ClosedAt *Timestamp `json:"closed_at,omitempty"`
42 CreatedAt *Timestamp `json:"created_at,omitempty"`
43 UpdatedAt *Timestamp `json:"updated_at,omitempty"`
44 ClosedBy *User `json:"closed_by,omitempty"`
45 URL *string `json:"url,omitempty"`
46 HTMLURL *string `json:"html_url,omitempty"`
47 CommentsURL *string `json:"comments_url,omitempty"`
48 EventsURL *string `json:"events_url,omitempty"`
49 LabelsURL *string `json:"labels_url,omitempty"`
50 RepositoryURL *string `json:"repository_url,omitempty"`
51 Milestone *Milestone `json:"milestone,omitempty"`
52 PullRequestLinks *PullRequestLinks `json:"pull_request,omitempty"`
53 Repository *Repository `json:"repository,omitempty"`
54 Reactions *Reactions `json:"reactions,omitempty"`
55 Assignees []*User `json:"assignees,omitempty"`
56 NodeID *string `json:"node_id,omitempty"`
57
58
59
60 TextMatches []*TextMatch `json:"text_matches,omitempty"`
61
62
63
64 ActiveLockReason *string `json:"active_lock_reason,omitempty"`
65 }
66
67 func (i Issue) String() string {
68 return Stringify(i)
69 }
70
71
72
73
74 func (i Issue) IsPullRequest() bool {
75 return i.PullRequestLinks != nil
76 }
77
78
79
80
81 type IssueRequest struct {
82 Title *string `json:"title,omitempty"`
83 Body *string `json:"body,omitempty"`
84 Labels *[]string `json:"labels,omitempty"`
85 Assignee *string `json:"assignee,omitempty"`
86 State *string `json:"state,omitempty"`
87
88 StateReason *string `json:"state_reason,omitempty"`
89 Milestone *int `json:"milestone,omitempty"`
90 Assignees *[]string `json:"assignees,omitempty"`
91 }
92
93
94
95 type IssueListOptions struct {
96
97
98 Filter string `url:"filter,omitempty"`
99
100
101
102 State string `url:"state,omitempty"`
103
104
105 Labels []string `url:"labels,comma,omitempty"`
106
107
108
109 Sort string `url:"sort,omitempty"`
110
111
112
113 Direction string `url:"direction,omitempty"`
114
115
116 Since time.Time `url:"since,omitempty"`
117
118 ListOptions
119 }
120
121
122
123 type PullRequestLinks struct {
124 URL *string `json:"url,omitempty"`
125 HTMLURL *string `json:"html_url,omitempty"`
126 DiffURL *string `json:"diff_url,omitempty"`
127 PatchURL *string `json:"patch_url,omitempty"`
128 }
129
130
131
132
133
134
135
136
137 func (s *IssuesService) List(ctx context.Context, all bool, opts *IssueListOptions) ([]*Issue, *Response, error) {
138 var u string
139 if all {
140 u = "issues"
141 } else {
142 u = "user/issues"
143 }
144 return s.listIssues(ctx, u, opts)
145 }
146
147
148
149
150
151 func (s *IssuesService) ListByOrg(ctx context.Context, org string, opts *IssueListOptions) ([]*Issue, *Response, error) {
152 u := fmt.Sprintf("orgs/%v/issues", org)
153 return s.listIssues(ctx, u, opts)
154 }
155
156 func (s *IssuesService) listIssues(ctx context.Context, u string, opts *IssueListOptions) ([]*Issue, *Response, error) {
157 u, err := addOptions(u, opts)
158 if err != nil {
159 return nil, nil, err
160 }
161
162 req, err := s.client.NewRequest("GET", u, nil)
163 if err != nil {
164 return nil, nil, err
165 }
166
167
168 req.Header.Set("Accept", mediaTypeReactionsPreview)
169
170 var issues []*Issue
171 resp, err := s.client.Do(ctx, req, &issues)
172 if err != nil {
173 return nil, resp, err
174 }
175
176 return issues, resp, nil
177 }
178
179
180
181 type IssueListByRepoOptions struct {
182
183
184
185 Milestone string `url:"milestone,omitempty"`
186
187
188
189 State string `url:"state,omitempty"`
190
191
192
193
194 Assignee string `url:"assignee,omitempty"`
195
196
197 Creator string `url:"creator,omitempty"`
198
199
200 Mentioned string `url:"mentioned,omitempty"`
201
202
203 Labels []string `url:"labels,omitempty,comma"`
204
205
206
207 Sort string `url:"sort,omitempty"`
208
209
210
211 Direction string `url:"direction,omitempty"`
212
213
214 Since time.Time `url:"since,omitempty"`
215
216 ListOptions
217 }
218
219
220
221
222 func (s *IssuesService) ListByRepo(ctx context.Context, owner string, repo string, opts *IssueListByRepoOptions) ([]*Issue, *Response, error) {
223 u := fmt.Sprintf("repos/%v/%v/issues", owner, repo)
224 u, err := addOptions(u, opts)
225 if err != nil {
226 return nil, nil, err
227 }
228
229 req, err := s.client.NewRequest("GET", u, nil)
230 if err != nil {
231 return nil, nil, err
232 }
233
234
235 req.Header.Set("Accept", mediaTypeReactionsPreview)
236
237 var issues []*Issue
238 resp, err := s.client.Do(ctx, req, &issues)
239 if err != nil {
240 return nil, resp, err
241 }
242
243 return issues, resp, nil
244 }
245
246
247
248
249 func (s *IssuesService) Get(ctx context.Context, owner string, repo string, number int) (*Issue, *Response, error) {
250 u := fmt.Sprintf("repos/%v/%v/issues/%d", owner, repo, number)
251 req, err := s.client.NewRequest("GET", u, nil)
252 if err != nil {
253 return nil, nil, err
254 }
255
256
257 req.Header.Set("Accept", mediaTypeReactionsPreview)
258
259 issue := new(Issue)
260 resp, err := s.client.Do(ctx, req, issue)
261 if err != nil {
262 return nil, resp, err
263 }
264
265 return issue, resp, nil
266 }
267
268
269
270
271 func (s *IssuesService) Create(ctx context.Context, owner string, repo string, issue *IssueRequest) (*Issue, *Response, error) {
272 u := fmt.Sprintf("repos/%v/%v/issues", owner, repo)
273 req, err := s.client.NewRequest("POST", u, issue)
274 if err != nil {
275 return nil, nil, err
276 }
277
278 i := new(Issue)
279 resp, err := s.client.Do(ctx, req, i)
280 if err != nil {
281 return nil, resp, err
282 }
283
284 return i, resp, nil
285 }
286
287
288
289
290 func (s *IssuesService) Edit(ctx context.Context, owner string, repo string, number int, issue *IssueRequest) (*Issue, *Response, error) {
291 u := fmt.Sprintf("repos/%v/%v/issues/%d", owner, repo, number)
292 req, err := s.client.NewRequest("PATCH", u, issue)
293 if err != nil {
294 return nil, nil, err
295 }
296
297 i := new(Issue)
298 resp, err := s.client.Do(ctx, req, i)
299 if err != nil {
300 return nil, resp, err
301 }
302
303 return i, resp, nil
304 }
305
306
307
308
309
310
311 func (s *IssuesService) RemoveMilestone(ctx context.Context, owner, repo string, issueNumber int) (*Issue, *Response, error) {
312 u := fmt.Sprintf("repos/%v/%v/issues/%v", owner, repo, issueNumber)
313 req, err := s.client.NewRequest("PATCH", u, &struct {
314 Milestone *Milestone `json:"milestone"`
315 }{})
316 if err != nil {
317 return nil, nil, err
318 }
319
320 i := new(Issue)
321 resp, err := s.client.Do(ctx, req, i)
322 if err != nil {
323 return nil, resp, err
324 }
325
326 return i, resp, nil
327 }
328
329
330
331 type LockIssueOptions struct {
332
333
334
335 LockReason string `json:"lock_reason,omitempty"`
336 }
337
338
339
340
341 func (s *IssuesService) Lock(ctx context.Context, owner string, repo string, number int, opts *LockIssueOptions) (*Response, error) {
342 u := fmt.Sprintf("repos/%v/%v/issues/%d/lock", owner, repo, number)
343 req, err := s.client.NewRequest("PUT", u, opts)
344 if err != nil {
345 return nil, err
346 }
347
348 return s.client.Do(ctx, req, nil)
349 }
350
351
352
353
354 func (s *IssuesService) Unlock(ctx context.Context, owner string, repo string, number int) (*Response, error) {
355 u := fmt.Sprintf("repos/%v/%v/issues/%d/lock", owner, repo, number)
356 req, err := s.client.NewRequest("DELETE", u, nil)
357 if err != nil {
358 return nil, err
359 }
360
361 return s.client.Do(ctx, req, nil)
362 }
363
View as plain text