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