github.com/google/go-github/v71@v71.0.0/github/issues.go (about) 1 // Copyright 2013 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 package github 7 8 import ( 9 "context" 10 "fmt" 11 "time" 12 ) 13 14 // IssuesService handles communication with the issue related 15 // methods of the GitHub API. 16 // 17 // GitHub API docs: https://docs.github.com/rest/issues/ 18 type IssuesService service 19 20 // Issue represents a GitHub issue on a repository. 21 // 22 // Note: As far as the GitHub API is concerned, every pull request is an issue, 23 // but not every issue is a pull request. Some endpoints, events, and webhooks 24 // may also return pull requests via this struct. If PullRequestLinks is nil, 25 // this is an issue, and if PullRequestLinks is not nil, this is a pull request. 26 // The IsPullRequest helper method can be used to check that. 27 type Issue struct { 28 ID *int64 `json:"id,omitempty"` 29 Number *int `json:"number,omitempty"` 30 State *string `json:"state,omitempty"` 31 // StateReason can be one of: "completed", "not_planned", "reopened". 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 Draft *bool `json:"draft,omitempty"` 58 Type *IssueType `json:"type,omitempty"` 59 60 // TextMatches is only populated from search results that request text matches 61 // See: search.go and https://docs.github.com/rest/search/#text-match-metadata 62 TextMatches []*TextMatch `json:"text_matches,omitempty"` 63 64 // ActiveLockReason is populated only when LockReason is provided while locking the issue. 65 // Possible values are: "off-topic", "too heated", "resolved", and "spam". 66 ActiveLockReason *string `json:"active_lock_reason,omitempty"` 67 } 68 69 func (i Issue) String() string { 70 return Stringify(i) 71 } 72 73 // IsPullRequest reports whether the issue is also a pull request. It uses the 74 // method recommended by GitHub's API documentation, which is to check whether 75 // PullRequestLinks is non-nil. 76 func (i Issue) IsPullRequest() bool { 77 return i.PullRequestLinks != nil 78 } 79 80 // IssueRequest represents a request to create/edit an issue. 81 // It is separate from Issue above because otherwise Labels 82 // and Assignee fail to serialize to the correct JSON. 83 type IssueRequest struct { 84 Title *string `json:"title,omitempty"` 85 Body *string `json:"body,omitempty"` 86 Labels *[]string `json:"labels,omitempty"` 87 Assignee *string `json:"assignee,omitempty"` 88 State *string `json:"state,omitempty"` 89 // StateReason can be 'completed' or 'not_planned'. 90 StateReason *string `json:"state_reason,omitempty"` 91 Milestone *int `json:"milestone,omitempty"` 92 Assignees *[]string `json:"assignees,omitempty"` 93 } 94 95 // IssueListOptions specifies the optional parameters to the IssuesService.List 96 // and IssuesService.ListByOrg methods. 97 type IssueListOptions struct { 98 // Filter specifies which issues to list. Possible values are: assigned, 99 // created, mentioned, subscribed, all. Default is "assigned". 100 Filter string `url:"filter,omitempty"` 101 102 // State filters issues based on their state. Possible values are: open, 103 // closed, all. Default is "open". 104 State string `url:"state,omitempty"` 105 106 // Labels filters issues based on their label. 107 Labels []string `url:"labels,comma,omitempty"` 108 109 // Sort specifies how to sort issues. Possible values are: created, updated, 110 // and comments. Default value is "created". 111 Sort string `url:"sort,omitempty"` 112 113 // Direction in which to sort issues. Possible values are: asc, desc. 114 // Default is "desc". 115 Direction string `url:"direction,omitempty"` 116 117 // Since filters issues by time. 118 Since time.Time `url:"since,omitempty"` 119 120 ListOptions 121 } 122 123 // PullRequestLinks object is added to the Issue object when it's an issue included 124 // in the IssueCommentEvent webhook payload, if the webhook is fired by a comment on a PR. 125 type PullRequestLinks struct { 126 URL *string `json:"url,omitempty"` 127 HTMLURL *string `json:"html_url,omitempty"` 128 DiffURL *string `json:"diff_url,omitempty"` 129 PatchURL *string `json:"patch_url,omitempty"` 130 MergedAt *Timestamp `json:"merged_at,omitempty"` 131 } 132 133 // IssueType represents the type of issue. 134 // For now it shows up when receiveing an Issue event. 135 type IssueType struct { 136 ID *int64 `json:"id,omitempty"` 137 NodeID *string `json:"node_id,omitempty"` 138 Name *string `json:"name,omitempty"` 139 Description *string `json:"description,omitempty"` 140 Color *string `json:"color,omitempty"` 141 CreatedAt *Timestamp `json:"created_at,omitempty"` 142 UpdatedAt *Timestamp `json:"updated_at,omitempty"` 143 } 144 145 // List the issues for the authenticated user. If all is true, list issues 146 // across all the user's visible repositories including owned, member, and 147 // organization repositories; if false, list only owned and member 148 // repositories. 149 // 150 // GitHub API docs: https://docs.github.com/rest/issues/issues#list-issues-assigned-to-the-authenticated-user 151 // GitHub API docs: https://docs.github.com/rest/issues/issues#list-user-account-issues-assigned-to-the-authenticated-user 152 // 153 //meta:operation GET /issues 154 //meta:operation GET /user/issues 155 func (s *IssuesService) List(ctx context.Context, all bool, opts *IssueListOptions) ([]*Issue, *Response, error) { 156 var u string 157 if all { 158 u = "issues" 159 } else { 160 u = "user/issues" 161 } 162 return s.listIssues(ctx, u, opts) 163 } 164 165 // ListByOrg fetches the issues in the specified organization for the 166 // authenticated user. 167 // 168 // GitHub API docs: https://docs.github.com/rest/issues/issues#list-organization-issues-assigned-to-the-authenticated-user 169 // 170 //meta:operation GET /orgs/{org}/issues 171 func (s *IssuesService) ListByOrg(ctx context.Context, org string, opts *IssueListOptions) ([]*Issue, *Response, error) { 172 u := fmt.Sprintf("orgs/%v/issues", org) 173 return s.listIssues(ctx, u, opts) 174 } 175 176 func (s *IssuesService) listIssues(ctx context.Context, u string, opts *IssueListOptions) ([]*Issue, *Response, error) { 177 u, err := addOptions(u, opts) 178 if err != nil { 179 return nil, nil, err 180 } 181 182 req, err := s.client.NewRequest("GET", u, nil) 183 if err != nil { 184 return nil, nil, err 185 } 186 187 // TODO: remove custom Accept header when this API fully launch. 188 req.Header.Set("Accept", mediaTypeReactionsPreview) 189 190 var issues []*Issue 191 resp, err := s.client.Do(ctx, req, &issues) 192 if err != nil { 193 return nil, resp, err 194 } 195 196 return issues, resp, nil 197 } 198 199 // IssueListByRepoOptions specifies the optional parameters to the 200 // IssuesService.ListByRepo method. 201 type IssueListByRepoOptions struct { 202 // Milestone limits issues for the specified milestone. Possible values are 203 // a milestone number, "none" for issues with no milestone, "*" for issues 204 // with any milestone. 205 Milestone string `url:"milestone,omitempty"` 206 207 // State filters issues based on their state. Possible values are: open, 208 // closed, all. Default is "open". 209 State string `url:"state,omitempty"` 210 211 // Assignee filters issues based on their assignee. Possible values are a 212 // user name, "none" for issues that are not assigned, "*" for issues with 213 // any assigned user. 214 Assignee string `url:"assignee,omitempty"` 215 216 // Creator filters issues based on their creator. 217 Creator string `url:"creator,omitempty"` 218 219 // Mentioned filters issues to those mentioned a specific user. 220 Mentioned string `url:"mentioned,omitempty"` 221 222 // Labels filters issues based on their label. 223 Labels []string `url:"labels,omitempty,comma"` 224 225 // Sort specifies how to sort issues. Possible values are: created, updated, 226 // and comments. Default value is "created". 227 Sort string `url:"sort,omitempty"` 228 229 // Direction in which to sort issues. Possible values are: asc, desc. 230 // Default is "desc". 231 Direction string `url:"direction,omitempty"` 232 233 // Since filters issues by time. 234 Since time.Time `url:"since,omitempty"` 235 236 ListOptions 237 } 238 239 // ListByRepo lists the issues for the specified repository. 240 // 241 // GitHub API docs: https://docs.github.com/rest/issues/issues#list-repository-issues 242 // 243 //meta:operation GET /repos/{owner}/{repo}/issues 244 func (s *IssuesService) ListByRepo(ctx context.Context, owner string, repo string, opts *IssueListByRepoOptions) ([]*Issue, *Response, error) { 245 u := fmt.Sprintf("repos/%v/%v/issues", owner, repo) 246 u, err := addOptions(u, opts) 247 if err != nil { 248 return nil, nil, err 249 } 250 251 req, err := s.client.NewRequest("GET", u, nil) 252 if err != nil { 253 return nil, nil, err 254 } 255 256 // TODO: remove custom Accept header when this API fully launches. 257 req.Header.Set("Accept", mediaTypeReactionsPreview) 258 259 var issues []*Issue 260 resp, err := s.client.Do(ctx, req, &issues) 261 if err != nil { 262 return nil, resp, err 263 } 264 265 return issues, resp, nil 266 } 267 268 // Get a single issue. 269 // 270 // GitHub API docs: https://docs.github.com/rest/issues/issues#get-an-issue 271 // 272 //meta:operation GET /repos/{owner}/{repo}/issues/{issue_number} 273 func (s *IssuesService) Get(ctx context.Context, owner string, repo string, number int) (*Issue, *Response, error) { 274 u := fmt.Sprintf("repos/%v/%v/issues/%d", owner, repo, number) 275 req, err := s.client.NewRequest("GET", u, nil) 276 if err != nil { 277 return nil, nil, err 278 } 279 280 // TODO: remove custom Accept header when this API fully launch. 281 req.Header.Set("Accept", mediaTypeReactionsPreview) 282 283 issue := new(Issue) 284 resp, err := s.client.Do(ctx, req, issue) 285 if err != nil { 286 return nil, resp, err 287 } 288 289 return issue, resp, nil 290 } 291 292 // Create a new issue on the specified repository. 293 // 294 // GitHub API docs: https://docs.github.com/rest/issues/issues#create-an-issue 295 // 296 //meta:operation POST /repos/{owner}/{repo}/issues 297 func (s *IssuesService) Create(ctx context.Context, owner string, repo string, issue *IssueRequest) (*Issue, *Response, error) { 298 u := fmt.Sprintf("repos/%v/%v/issues", owner, repo) 299 req, err := s.client.NewRequest("POST", u, issue) 300 if err != nil { 301 return nil, nil, err 302 } 303 304 i := new(Issue) 305 resp, err := s.client.Do(ctx, req, i) 306 if err != nil { 307 return nil, resp, err 308 } 309 310 return i, resp, nil 311 } 312 313 // Edit (update) an issue. 314 // 315 // GitHub API docs: https://docs.github.com/rest/issues/issues#update-an-issue 316 // 317 //meta:operation PATCH /repos/{owner}/{repo}/issues/{issue_number} 318 func (s *IssuesService) Edit(ctx context.Context, owner string, repo string, number int, issue *IssueRequest) (*Issue, *Response, error) { 319 u := fmt.Sprintf("repos/%v/%v/issues/%d", owner, repo, number) 320 req, err := s.client.NewRequest("PATCH", u, issue) 321 if err != nil { 322 return nil, nil, err 323 } 324 325 i := new(Issue) 326 resp, err := s.client.Do(ctx, req, i) 327 if err != nil { 328 return nil, resp, err 329 } 330 331 return i, resp, nil 332 } 333 334 // RemoveMilestone removes a milestone from an issue. 335 // 336 // This is a helper method to explicitly update an issue with a `null` milestone, thereby removing it. 337 // 338 // GitHub API docs: https://docs.github.com/rest/issues/issues#update-an-issue 339 // 340 //meta:operation PATCH /repos/{owner}/{repo}/issues/{issue_number} 341 func (s *IssuesService) RemoveMilestone(ctx context.Context, owner, repo string, issueNumber int) (*Issue, *Response, error) { 342 u := fmt.Sprintf("repos/%v/%v/issues/%v", owner, repo, issueNumber) 343 req, err := s.client.NewRequest("PATCH", u, &struct { 344 Milestone *Milestone `json:"milestone"` 345 }{}) 346 if err != nil { 347 return nil, nil, err 348 } 349 350 i := new(Issue) 351 resp, err := s.client.Do(ctx, req, i) 352 if err != nil { 353 return nil, resp, err 354 } 355 356 return i, resp, nil 357 } 358 359 // LockIssueOptions specifies the optional parameters to the 360 // IssuesService.Lock method. 361 type LockIssueOptions struct { 362 // LockReason specifies the reason to lock this issue. 363 // Providing a lock reason can help make it clearer to contributors why an issue 364 // was locked. Possible values are: "off-topic", "too heated", "resolved", and "spam". 365 LockReason string `json:"lock_reason,omitempty"` 366 } 367 368 // Lock an issue's conversation. 369 // 370 // GitHub API docs: https://docs.github.com/rest/issues/issues#lock-an-issue 371 // 372 //meta:operation PUT /repos/{owner}/{repo}/issues/{issue_number}/lock 373 func (s *IssuesService) Lock(ctx context.Context, owner string, repo string, number int, opts *LockIssueOptions) (*Response, error) { 374 u := fmt.Sprintf("repos/%v/%v/issues/%d/lock", owner, repo, number) 375 req, err := s.client.NewRequest("PUT", u, opts) 376 if err != nil { 377 return nil, err 378 } 379 380 return s.client.Do(ctx, req, nil) 381 } 382 383 // Unlock an issue's conversation. 384 // 385 // GitHub API docs: https://docs.github.com/rest/issues/issues#unlock-an-issue 386 // 387 //meta:operation DELETE /repos/{owner}/{repo}/issues/{issue_number}/lock 388 func (s *IssuesService) Unlock(ctx context.Context, owner string, repo string, number int) (*Response, error) { 389 u := fmt.Sprintf("repos/%v/%v/issues/%d/lock", owner, repo, number) 390 req, err := s.client.NewRequest("DELETE", u, nil) 391 if err != nil { 392 return nil, err 393 } 394 395 return s.client.Do(ctx, req, nil) 396 }