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