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