...

Source file src/github.com/google/go-github/v47/github/issues.go

Documentation: github.com/google/go-github/v47/github

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

View as plain text