github.com/google/go-github/v64@v64.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 MergedAt *Timestamp `json:"merged_at,omitempty"` 130 } 131 132 // List the issues for the authenticated user. If all is true, list issues 133 // across all the user's visible repositories including owned, member, and 134 // organization repositories; if false, list only owned and member 135 // repositories. 136 // 137 // GitHub API docs: https://docs.github.com/rest/issues/issues#list-issues-assigned-to-the-authenticated-user 138 // GitHub API docs: https://docs.github.com/rest/issues/issues#list-user-account-issues-assigned-to-the-authenticated-user 139 // 140 //meta:operation GET /issues 141 //meta:operation GET /user/issues 142 func (s *IssuesService) List(ctx context.Context, all bool, opts *IssueListOptions) ([]*Issue, *Response, error) { 143 var u string 144 if all { 145 u = "issues" 146 } else { 147 u = "user/issues" 148 } 149 return s.listIssues(ctx, u, opts) 150 } 151 152 // ListByOrg fetches the issues in the specified organization for the 153 // authenticated user. 154 // 155 // GitHub API docs: https://docs.github.com/rest/issues/issues#list-organization-issues-assigned-to-the-authenticated-user 156 // 157 //meta:operation GET /orgs/{org}/issues 158 func (s *IssuesService) ListByOrg(ctx context.Context, org string, opts *IssueListOptions) ([]*Issue, *Response, error) { 159 u := fmt.Sprintf("orgs/%v/issues", org) 160 return s.listIssues(ctx, u, opts) 161 } 162 163 func (s *IssuesService) listIssues(ctx context.Context, u string, opts *IssueListOptions) ([]*Issue, *Response, error) { 164 u, err := addOptions(u, opts) 165 if err != nil { 166 return nil, nil, err 167 } 168 169 req, err := s.client.NewRequest("GET", u, nil) 170 if err != nil { 171 return nil, nil, err 172 } 173 174 // TODO: remove custom Accept header when this API fully launch. 175 req.Header.Set("Accept", mediaTypeReactionsPreview) 176 177 var issues []*Issue 178 resp, err := s.client.Do(ctx, req, &issues) 179 if err != nil { 180 return nil, resp, err 181 } 182 183 return issues, resp, nil 184 } 185 186 // IssueListByRepoOptions specifies the optional parameters to the 187 // IssuesService.ListByRepo method. 188 type IssueListByRepoOptions struct { 189 // Milestone limits issues for the specified milestone. Possible values are 190 // a milestone number, "none" for issues with no milestone, "*" for issues 191 // with any milestone. 192 Milestone string `url:"milestone,omitempty"` 193 194 // State filters issues based on their state. Possible values are: open, 195 // closed, all. Default is "open". 196 State string `url:"state,omitempty"` 197 198 // Assignee filters issues based on their assignee. Possible values are a 199 // user name, "none" for issues that are not assigned, "*" for issues with 200 // any assigned user. 201 Assignee string `url:"assignee,omitempty"` 202 203 // Creator filters issues based on their creator. 204 Creator string `url:"creator,omitempty"` 205 206 // Mentioned filters issues to those mentioned a specific user. 207 Mentioned string `url:"mentioned,omitempty"` 208 209 // Labels filters issues based on their label. 210 Labels []string `url:"labels,omitempty,comma"` 211 212 // Sort specifies how to sort issues. Possible values are: created, updated, 213 // and comments. Default value is "created". 214 Sort string `url:"sort,omitempty"` 215 216 // Direction in which to sort issues. Possible values are: asc, desc. 217 // Default is "desc". 218 Direction string `url:"direction,omitempty"` 219 220 // Since filters issues by time. 221 Since time.Time `url:"since,omitempty"` 222 223 ListOptions 224 } 225 226 // ListByRepo lists the issues for the specified repository. 227 // 228 // GitHub API docs: https://docs.github.com/rest/issues/issues#list-repository-issues 229 // 230 //meta:operation GET /repos/{owner}/{repo}/issues 231 func (s *IssuesService) ListByRepo(ctx context.Context, owner string, repo string, opts *IssueListByRepoOptions) ([]*Issue, *Response, error) { 232 u := fmt.Sprintf("repos/%v/%v/issues", owner, repo) 233 u, err := addOptions(u, opts) 234 if err != nil { 235 return nil, nil, err 236 } 237 238 req, err := s.client.NewRequest("GET", u, nil) 239 if err != nil { 240 return nil, nil, err 241 } 242 243 // TODO: remove custom Accept header when this API fully launches. 244 req.Header.Set("Accept", mediaTypeReactionsPreview) 245 246 var issues []*Issue 247 resp, err := s.client.Do(ctx, req, &issues) 248 if err != nil { 249 return nil, resp, err 250 } 251 252 return issues, resp, nil 253 } 254 255 // Get a single issue. 256 // 257 // GitHub API docs: https://docs.github.com/rest/issues/issues#get-an-issue 258 // 259 //meta:operation GET /repos/{owner}/{repo}/issues/{issue_number} 260 func (s *IssuesService) Get(ctx context.Context, owner string, repo string, number int) (*Issue, *Response, error) { 261 u := fmt.Sprintf("repos/%v/%v/issues/%d", owner, repo, number) 262 req, err := s.client.NewRequest("GET", u, nil) 263 if err != nil { 264 return nil, nil, err 265 } 266 267 // TODO: remove custom Accept header when this API fully launch. 268 req.Header.Set("Accept", mediaTypeReactionsPreview) 269 270 issue := new(Issue) 271 resp, err := s.client.Do(ctx, req, issue) 272 if err != nil { 273 return nil, resp, err 274 } 275 276 return issue, resp, nil 277 } 278 279 // Create a new issue on the specified repository. 280 // 281 // GitHub API docs: https://docs.github.com/rest/issues/issues#create-an-issue 282 // 283 //meta:operation POST /repos/{owner}/{repo}/issues 284 func (s *IssuesService) Create(ctx context.Context, owner string, repo string, issue *IssueRequest) (*Issue, *Response, error) { 285 u := fmt.Sprintf("repos/%v/%v/issues", owner, repo) 286 req, err := s.client.NewRequest("POST", u, issue) 287 if err != nil { 288 return nil, nil, err 289 } 290 291 i := new(Issue) 292 resp, err := s.client.Do(ctx, req, i) 293 if err != nil { 294 return nil, resp, err 295 } 296 297 return i, resp, nil 298 } 299 300 // Edit (update) an issue. 301 // 302 // GitHub API docs: https://docs.github.com/rest/issues/issues#update-an-issue 303 // 304 //meta:operation PATCH /repos/{owner}/{repo}/issues/{issue_number} 305 func (s *IssuesService) Edit(ctx context.Context, owner string, repo string, number int, issue *IssueRequest) (*Issue, *Response, error) { 306 u := fmt.Sprintf("repos/%v/%v/issues/%d", owner, repo, number) 307 req, err := s.client.NewRequest("PATCH", u, issue) 308 if err != nil { 309 return nil, nil, err 310 } 311 312 i := new(Issue) 313 resp, err := s.client.Do(ctx, req, i) 314 if err != nil { 315 return nil, resp, err 316 } 317 318 return i, resp, nil 319 } 320 321 // RemoveMilestone removes a milestone from an issue. 322 // 323 // This is a helper method to explicitly update an issue with a `null` milestone, thereby removing it. 324 // 325 // GitHub API docs: https://docs.github.com/rest/issues/issues#update-an-issue 326 // 327 //meta:operation PATCH /repos/{owner}/{repo}/issues/{issue_number} 328 func (s *IssuesService) RemoveMilestone(ctx context.Context, owner, repo string, issueNumber int) (*Issue, *Response, error) { 329 u := fmt.Sprintf("repos/%v/%v/issues/%v", owner, repo, issueNumber) 330 req, err := s.client.NewRequest("PATCH", u, &struct { 331 Milestone *Milestone `json:"milestone"` 332 }{}) 333 if err != nil { 334 return nil, nil, err 335 } 336 337 i := new(Issue) 338 resp, err := s.client.Do(ctx, req, i) 339 if err != nil { 340 return nil, resp, err 341 } 342 343 return i, resp, nil 344 } 345 346 // LockIssueOptions specifies the optional parameters to the 347 // IssuesService.Lock method. 348 type LockIssueOptions struct { 349 // LockReason specifies the reason to lock this issue. 350 // Providing a lock reason can help make it clearer to contributors why an issue 351 // was locked. Possible values are: "off-topic", "too heated", "resolved", and "spam". 352 LockReason string `json:"lock_reason,omitempty"` 353 } 354 355 // Lock an issue's conversation. 356 // 357 // GitHub API docs: https://docs.github.com/rest/issues/issues#lock-an-issue 358 // 359 //meta:operation PUT /repos/{owner}/{repo}/issues/{issue_number}/lock 360 func (s *IssuesService) Lock(ctx context.Context, owner string, repo string, number int, opts *LockIssueOptions) (*Response, error) { 361 u := fmt.Sprintf("repos/%v/%v/issues/%d/lock", owner, repo, number) 362 req, err := s.client.NewRequest("PUT", u, opts) 363 if err != nil { 364 return nil, err 365 } 366 367 return s.client.Do(ctx, req, nil) 368 } 369 370 // Unlock an issue's conversation. 371 // 372 // GitHub API docs: https://docs.github.com/rest/issues/issues#unlock-an-issue 373 // 374 //meta:operation DELETE /repos/{owner}/{repo}/issues/{issue_number}/lock 375 func (s *IssuesService) Unlock(ctx context.Context, owner string, repo string, number int) (*Response, error) { 376 u := fmt.Sprintf("repos/%v/%v/issues/%d/lock", owner, repo, number) 377 req, err := s.client.NewRequest("DELETE", u, nil) 378 if err != nil { 379 return nil, err 380 } 381 382 return s.client.Do(ctx, req, nil) 383 }