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