github.com/google/go-github/v71@v71.0.0/github/pulls.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  	"bytes"
    10  	"context"
    11  	"errors"
    12  	"fmt"
    13  )
    14  
    15  // PullRequestsService handles communication with the pull request related
    16  // methods of the GitHub API.
    17  //
    18  // GitHub API docs: https://docs.github.com/rest/pulls/
    19  type PullRequestsService service
    20  
    21  // PullRequestAutoMerge represents the "auto_merge" response for a PullRequest.
    22  type PullRequestAutoMerge struct {
    23  	EnabledBy     *User   `json:"enabled_by,omitempty"`
    24  	MergeMethod   *string `json:"merge_method,omitempty"`
    25  	CommitTitle   *string `json:"commit_title,omitempty"`
    26  	CommitMessage *string `json:"commit_message,omitempty"`
    27  }
    28  
    29  // PullRequest represents a GitHub pull request on a repository.
    30  type PullRequest struct {
    31  	ID                 *int64                `json:"id,omitempty"`
    32  	Number             *int                  `json:"number,omitempty"`
    33  	State              *string               `json:"state,omitempty"`
    34  	Locked             *bool                 `json:"locked,omitempty"`
    35  	Title              *string               `json:"title,omitempty"`
    36  	Body               *string               `json:"body,omitempty"`
    37  	CreatedAt          *Timestamp            `json:"created_at,omitempty"`
    38  	UpdatedAt          *Timestamp            `json:"updated_at,omitempty"`
    39  	ClosedAt           *Timestamp            `json:"closed_at,omitempty"`
    40  	MergedAt           *Timestamp            `json:"merged_at,omitempty"`
    41  	Labels             []*Label              `json:"labels,omitempty"`
    42  	User               *User                 `json:"user,omitempty"`
    43  	Draft              *bool                 `json:"draft,omitempty"`
    44  	URL                *string               `json:"url,omitempty"`
    45  	HTMLURL            *string               `json:"html_url,omitempty"`
    46  	IssueURL           *string               `json:"issue_url,omitempty"`
    47  	StatusesURL        *string               `json:"statuses_url,omitempty"`
    48  	DiffURL            *string               `json:"diff_url,omitempty"`
    49  	PatchURL           *string               `json:"patch_url,omitempty"`
    50  	CommitsURL         *string               `json:"commits_url,omitempty"`
    51  	CommentsURL        *string               `json:"comments_url,omitempty"`
    52  	ReviewCommentsURL  *string               `json:"review_comments_url,omitempty"`
    53  	ReviewCommentURL   *string               `json:"review_comment_url,omitempty"`
    54  	Assignee           *User                 `json:"assignee,omitempty"`
    55  	Assignees          []*User               `json:"assignees,omitempty"`
    56  	Milestone          *Milestone            `json:"milestone,omitempty"`
    57  	AuthorAssociation  *string               `json:"author_association,omitempty"`
    58  	NodeID             *string               `json:"node_id,omitempty"`
    59  	RequestedReviewers []*User               `json:"requested_reviewers,omitempty"`
    60  	AutoMerge          *PullRequestAutoMerge `json:"auto_merge,omitempty"`
    61  
    62  	// These fields are not populated by the List operation.
    63  	Merged              *bool   `json:"merged,omitempty"`
    64  	Mergeable           *bool   `json:"mergeable,omitempty"`
    65  	MergeableState      *string `json:"mergeable_state,omitempty"`
    66  	Rebaseable          *bool   `json:"rebaseable,omitempty"`
    67  	MergedBy            *User   `json:"merged_by,omitempty"`
    68  	MergeCommitSHA      *string `json:"merge_commit_sha,omitempty"`
    69  	Comments            *int    `json:"comments,omitempty"`
    70  	Commits             *int    `json:"commits,omitempty"`
    71  	Additions           *int    `json:"additions,omitempty"`
    72  	Deletions           *int    `json:"deletions,omitempty"`
    73  	ChangedFiles        *int    `json:"changed_files,omitempty"`
    74  	MaintainerCanModify *bool   `json:"maintainer_can_modify,omitempty"`
    75  	ReviewComments      *int    `json:"review_comments,omitempty"`
    76  
    77  	// RequestedTeams is populated as part of the PullRequestEvent.
    78  	// See, https://docs.github.com/developers/webhooks-and-events/github-event-types#pullrequestevent for an example.
    79  	RequestedTeams []*Team `json:"requested_teams,omitempty"`
    80  
    81  	Links *PRLinks           `json:"_links,omitempty"`
    82  	Head  *PullRequestBranch `json:"head,omitempty"`
    83  	Base  *PullRequestBranch `json:"base,omitempty"`
    84  
    85  	// ActiveLockReason is populated only when LockReason is provided while locking the pull request.
    86  	// Possible values are: "off-topic", "too heated", "resolved", and "spam".
    87  	ActiveLockReason *string `json:"active_lock_reason,omitempty"`
    88  }
    89  
    90  func (p PullRequest) String() string {
    91  	return Stringify(p)
    92  }
    93  
    94  // PRLink represents a single link object from GitHub pull request _links.
    95  type PRLink struct {
    96  	HRef *string `json:"href,omitempty"`
    97  }
    98  
    99  // PRLinks represents the "_links" object in a GitHub pull request.
   100  type PRLinks struct {
   101  	Self           *PRLink `json:"self,omitempty"`
   102  	HTML           *PRLink `json:"html,omitempty"`
   103  	Issue          *PRLink `json:"issue,omitempty"`
   104  	Comments       *PRLink `json:"comments,omitempty"`
   105  	ReviewComments *PRLink `json:"review_comments,omitempty"`
   106  	ReviewComment  *PRLink `json:"review_comment,omitempty"`
   107  	Commits        *PRLink `json:"commits,omitempty"`
   108  	Statuses       *PRLink `json:"statuses,omitempty"`
   109  }
   110  
   111  // PullRequestBranch represents a base or head branch in a GitHub pull request.
   112  type PullRequestBranch struct {
   113  	Label *string     `json:"label,omitempty"`
   114  	Ref   *string     `json:"ref,omitempty"`
   115  	SHA   *string     `json:"sha,omitempty"`
   116  	Repo  *Repository `json:"repo,omitempty"`
   117  	User  *User       `json:"user,omitempty"`
   118  }
   119  
   120  // PullRequestListOptions specifies the optional parameters to the
   121  // PullRequestsService.List method.
   122  type PullRequestListOptions struct {
   123  	// State filters pull requests based on their state. Possible values are:
   124  	// open, closed, all. Default is "open".
   125  	State string `url:"state,omitempty"`
   126  
   127  	// Head filters pull requests by head user and branch name in the format of:
   128  	// "user:ref-name".
   129  	Head string `url:"head,omitempty"`
   130  
   131  	// Base filters pull requests by base branch name.
   132  	Base string `url:"base,omitempty"`
   133  
   134  	// Sort specifies how to sort pull requests. Possible values are: created,
   135  	// updated, popularity, long-running. Default is "created".
   136  	Sort string `url:"sort,omitempty"`
   137  
   138  	// Direction in which to sort pull requests. Possible values are: asc, desc.
   139  	// If Sort is "created" or not specified, Default is "desc", otherwise Default
   140  	// is "asc"
   141  	Direction string `url:"direction,omitempty"`
   142  
   143  	ListOptions
   144  }
   145  
   146  // List the pull requests for the specified repository.
   147  //
   148  // GitHub API docs: https://docs.github.com/rest/pulls/pulls#list-pull-requests
   149  //
   150  //meta:operation GET /repos/{owner}/{repo}/pulls
   151  func (s *PullRequestsService) List(ctx context.Context, owner string, repo string, opts *PullRequestListOptions) ([]*PullRequest, *Response, error) {
   152  	u := fmt.Sprintf("repos/%v/%v/pulls", owner, repo)
   153  	u, err := addOptions(u, opts)
   154  	if err != nil {
   155  		return nil, nil, err
   156  	}
   157  
   158  	req, err := s.client.NewRequest("GET", u, nil)
   159  	if err != nil {
   160  		return nil, nil, err
   161  	}
   162  
   163  	var pulls []*PullRequest
   164  	resp, err := s.client.Do(ctx, req, &pulls)
   165  	if err != nil {
   166  		return nil, resp, err
   167  	}
   168  
   169  	return pulls, resp, nil
   170  }
   171  
   172  // ListPullRequestsWithCommit returns pull requests associated with a commit SHA
   173  // or branch name.
   174  //
   175  // The results may include open and closed pull requests. If the commit SHA is
   176  // not present in the repository's default branch, the result will only include
   177  // open pull requests.
   178  //
   179  // GitHub API docs: https://docs.github.com/rest/commits/commits#list-pull-requests-associated-with-a-commit
   180  //
   181  //meta:operation GET /repos/{owner}/{repo}/commits/{commit_sha}/pulls
   182  func (s *PullRequestsService) ListPullRequestsWithCommit(ctx context.Context, owner, repo, sha string, opts *ListOptions) ([]*PullRequest, *Response, error) {
   183  	u := fmt.Sprintf("repos/%v/%v/commits/%v/pulls", owner, repo, sha)
   184  	u, err := addOptions(u, opts)
   185  	if err != nil {
   186  		return nil, nil, err
   187  	}
   188  
   189  	req, err := s.client.NewRequest("GET", u, nil)
   190  	if err != nil {
   191  		return nil, nil, err
   192  	}
   193  
   194  	// TODO: remove custom Accept header when this API fully launches.
   195  	req.Header.Set("Accept", mediaTypeListPullsOrBranchesForCommitPreview)
   196  	var pulls []*PullRequest
   197  	resp, err := s.client.Do(ctx, req, &pulls)
   198  	if err != nil {
   199  		return nil, resp, err
   200  	}
   201  
   202  	return pulls, resp, nil
   203  }
   204  
   205  // Get a single pull request.
   206  //
   207  // GitHub API docs: https://docs.github.com/rest/pulls/pulls#get-a-pull-request
   208  //
   209  //meta:operation GET /repos/{owner}/{repo}/pulls/{pull_number}
   210  func (s *PullRequestsService) Get(ctx context.Context, owner string, repo string, number int) (*PullRequest, *Response, error) {
   211  	u := fmt.Sprintf("repos/%v/%v/pulls/%d", owner, repo, number)
   212  	req, err := s.client.NewRequest("GET", u, nil)
   213  	if err != nil {
   214  		return nil, nil, err
   215  	}
   216  
   217  	pull := new(PullRequest)
   218  	resp, err := s.client.Do(ctx, req, pull)
   219  	if err != nil {
   220  		return nil, resp, err
   221  	}
   222  
   223  	return pull, resp, nil
   224  }
   225  
   226  // GetRaw gets a single pull request in raw (diff or patch) format.
   227  //
   228  // GitHub API docs: https://docs.github.com/rest/pulls/pulls#get-a-pull-request
   229  //
   230  //meta:operation GET /repos/{owner}/{repo}/pulls/{pull_number}
   231  func (s *PullRequestsService) GetRaw(ctx context.Context, owner string, repo string, number int, opts RawOptions) (string, *Response, error) {
   232  	u := fmt.Sprintf("repos/%v/%v/pulls/%d", owner, repo, number)
   233  	req, err := s.client.NewRequest("GET", u, nil)
   234  	if err != nil {
   235  		return "", nil, err
   236  	}
   237  
   238  	switch opts.Type {
   239  	case Diff:
   240  		req.Header.Set("Accept", mediaTypeV3Diff)
   241  	case Patch:
   242  		req.Header.Set("Accept", mediaTypeV3Patch)
   243  	default:
   244  		return "", nil, fmt.Errorf("unsupported raw type %d", opts.Type)
   245  	}
   246  
   247  	var buf bytes.Buffer
   248  	resp, err := s.client.Do(ctx, req, &buf)
   249  	if err != nil {
   250  		return "", resp, err
   251  	}
   252  
   253  	return buf.String(), resp, nil
   254  }
   255  
   256  // NewPullRequest represents a new pull request to be created.
   257  type NewPullRequest struct {
   258  	Title *string `json:"title,omitempty"`
   259  	// The name of the branch where your changes are implemented. For
   260  	// cross-repository pull requests in the same network, namespace head with
   261  	// a user like this: username:branch.
   262  	Head     *string `json:"head,omitempty"`
   263  	HeadRepo *string `json:"head_repo,omitempty"`
   264  	// The name of the branch you want the changes pulled into. This should be
   265  	// an existing branch on the current repository. You cannot submit a pull
   266  	// request to one repository that requests a merge to a base of another
   267  	// repository.
   268  	Base                *string `json:"base,omitempty"`
   269  	Body                *string `json:"body,omitempty"`
   270  	Issue               *int    `json:"issue,omitempty"`
   271  	MaintainerCanModify *bool   `json:"maintainer_can_modify,omitempty"`
   272  	Draft               *bool   `json:"draft,omitempty"`
   273  }
   274  
   275  // Create a new pull request on the specified repository.
   276  //
   277  // GitHub API docs: https://docs.github.com/rest/pulls/pulls#create-a-pull-request
   278  //
   279  //meta:operation POST /repos/{owner}/{repo}/pulls
   280  func (s *PullRequestsService) Create(ctx context.Context, owner string, repo string, pull *NewPullRequest) (*PullRequest, *Response, error) {
   281  	u := fmt.Sprintf("repos/%v/%v/pulls", owner, repo)
   282  	req, err := s.client.NewRequest("POST", u, pull)
   283  	if err != nil {
   284  		return nil, nil, err
   285  	}
   286  
   287  	p := new(PullRequest)
   288  	resp, err := s.client.Do(ctx, req, p)
   289  	if err != nil {
   290  		return nil, resp, err
   291  	}
   292  
   293  	return p, resp, nil
   294  }
   295  
   296  // PullRequestBranchUpdateOptions specifies the optional parameters to the
   297  // PullRequestsService.UpdateBranch method.
   298  type PullRequestBranchUpdateOptions struct {
   299  	// ExpectedHeadSHA specifies the most recent commit on the pull request's branch.
   300  	// Default value is the SHA of the pull request's current HEAD ref.
   301  	ExpectedHeadSHA *string `json:"expected_head_sha,omitempty"`
   302  }
   303  
   304  // PullRequestBranchUpdateResponse specifies the response of pull request branch update.
   305  type PullRequestBranchUpdateResponse struct {
   306  	Message *string `json:"message,omitempty"`
   307  	URL     *string `json:"url,omitempty"`
   308  }
   309  
   310  // UpdateBranch updates the pull request branch with latest upstream changes.
   311  //
   312  // This method might return an AcceptedError and a status code of
   313  // 202. This is because this is the status that GitHub returns to signify that
   314  // it has now scheduled the update of the pull request branch in a background task.
   315  // A follow up request, after a delay of a second or so, should result
   316  // in a successful request.
   317  //
   318  // GitHub API docs: https://docs.github.com/rest/pulls/pulls#update-a-pull-request-branch
   319  //
   320  //meta:operation PUT /repos/{owner}/{repo}/pulls/{pull_number}/update-branch
   321  func (s *PullRequestsService) UpdateBranch(ctx context.Context, owner, repo string, number int, opts *PullRequestBranchUpdateOptions) (*PullRequestBranchUpdateResponse, *Response, error) {
   322  	u := fmt.Sprintf("repos/%v/%v/pulls/%d/update-branch", owner, repo, number)
   323  
   324  	req, err := s.client.NewRequest("PUT", u, opts)
   325  	if err != nil {
   326  		return nil, nil, err
   327  	}
   328  
   329  	// TODO: remove custom Accept header when this API fully launches.
   330  	req.Header.Set("Accept", mediaTypeUpdatePullRequestBranchPreview)
   331  
   332  	p := new(PullRequestBranchUpdateResponse)
   333  	resp, err := s.client.Do(ctx, req, p)
   334  	if err != nil {
   335  		return nil, resp, err
   336  	}
   337  
   338  	return p, resp, nil
   339  }
   340  
   341  type pullRequestUpdate struct {
   342  	Title               *string `json:"title,omitempty"`
   343  	Body                *string `json:"body,omitempty"`
   344  	State               *string `json:"state,omitempty"`
   345  	Base                *string `json:"base,omitempty"`
   346  	MaintainerCanModify *bool   `json:"maintainer_can_modify,omitempty"`
   347  }
   348  
   349  // Edit a pull request.
   350  // pull must not be nil.
   351  //
   352  // The following fields are editable: Title, Body, State, Base.Ref and MaintainerCanModify.
   353  // Base.Ref updates the base branch of the pull request.
   354  //
   355  // GitHub API docs: https://docs.github.com/rest/pulls/pulls#update-a-pull-request
   356  //
   357  //meta:operation PATCH /repos/{owner}/{repo}/pulls/{pull_number}
   358  func (s *PullRequestsService) Edit(ctx context.Context, owner string, repo string, number int, pull *PullRequest) (*PullRequest, *Response, error) {
   359  	if pull == nil {
   360  		return nil, nil, errors.New("pull must be provided")
   361  	}
   362  
   363  	u := fmt.Sprintf("repos/%v/%v/pulls/%d", owner, repo, number)
   364  
   365  	update := &pullRequestUpdate{
   366  		Title:               pull.Title,
   367  		Body:                pull.Body,
   368  		State:               pull.State,
   369  		MaintainerCanModify: pull.MaintainerCanModify,
   370  	}
   371  	// avoid updating the base branch when closing the Pull Request
   372  	// - otherwise the GitHub API server returns a "Validation Failed" error:
   373  	// "Cannot change base branch of closed pull request".
   374  	if pull.Base != nil && pull.GetState() != "closed" {
   375  		update.Base = pull.Base.Ref
   376  	}
   377  
   378  	req, err := s.client.NewRequest("PATCH", u, update)
   379  	if err != nil {
   380  		return nil, nil, err
   381  	}
   382  
   383  	p := new(PullRequest)
   384  	resp, err := s.client.Do(ctx, req, p)
   385  	if err != nil {
   386  		return nil, resp, err
   387  	}
   388  
   389  	return p, resp, nil
   390  }
   391  
   392  // ListCommits lists the commits in a pull request.
   393  //
   394  // GitHub API docs: https://docs.github.com/rest/pulls/pulls#list-commits-on-a-pull-request
   395  //
   396  //meta:operation GET /repos/{owner}/{repo}/pulls/{pull_number}/commits
   397  func (s *PullRequestsService) ListCommits(ctx context.Context, owner string, repo string, number int, opts *ListOptions) ([]*RepositoryCommit, *Response, error) {
   398  	u := fmt.Sprintf("repos/%v/%v/pulls/%d/commits", owner, repo, number)
   399  	u, err := addOptions(u, opts)
   400  	if err != nil {
   401  		return nil, nil, err
   402  	}
   403  
   404  	req, err := s.client.NewRequest("GET", u, nil)
   405  	if err != nil {
   406  		return nil, nil, err
   407  	}
   408  
   409  	var commits []*RepositoryCommit
   410  	resp, err := s.client.Do(ctx, req, &commits)
   411  	if err != nil {
   412  		return nil, resp, err
   413  	}
   414  
   415  	return commits, resp, nil
   416  }
   417  
   418  // ListFiles lists the files in a pull request.
   419  //
   420  // GitHub API docs: https://docs.github.com/rest/pulls/pulls#list-pull-requests-files
   421  //
   422  //meta:operation GET /repos/{owner}/{repo}/pulls/{pull_number}/files
   423  func (s *PullRequestsService) ListFiles(ctx context.Context, owner string, repo string, number int, opts *ListOptions) ([]*CommitFile, *Response, error) {
   424  	u := fmt.Sprintf("repos/%v/%v/pulls/%d/files", owner, repo, number)
   425  	u, err := addOptions(u, opts)
   426  	if err != nil {
   427  		return nil, nil, err
   428  	}
   429  
   430  	req, err := s.client.NewRequest("GET", u, nil)
   431  	if err != nil {
   432  		return nil, nil, err
   433  	}
   434  
   435  	var commitFiles []*CommitFile
   436  	resp, err := s.client.Do(ctx, req, &commitFiles)
   437  	if err != nil {
   438  		return nil, resp, err
   439  	}
   440  
   441  	return commitFiles, resp, nil
   442  }
   443  
   444  // IsMerged checks if a pull request has been merged.
   445  //
   446  // GitHub API docs: https://docs.github.com/rest/pulls/pulls#check-if-a-pull-request-has-been-merged
   447  //
   448  //meta:operation GET /repos/{owner}/{repo}/pulls/{pull_number}/merge
   449  func (s *PullRequestsService) IsMerged(ctx context.Context, owner string, repo string, number int) (bool, *Response, error) {
   450  	u := fmt.Sprintf("repos/%v/%v/pulls/%d/merge", owner, repo, number)
   451  	req, err := s.client.NewRequest("GET", u, nil)
   452  	if err != nil {
   453  		return false, nil, err
   454  	}
   455  
   456  	resp, err := s.client.Do(ctx, req, nil)
   457  	merged, err := parseBoolResponse(err)
   458  	return merged, resp, err
   459  }
   460  
   461  // PullRequestMergeResult represents the result of merging a pull request.
   462  type PullRequestMergeResult struct {
   463  	SHA     *string `json:"sha,omitempty"`
   464  	Merged  *bool   `json:"merged,omitempty"`
   465  	Message *string `json:"message,omitempty"`
   466  }
   467  
   468  // PullRequestOptions lets you define how a pull request will be merged.
   469  type PullRequestOptions struct {
   470  	CommitTitle string // Title for the automatic commit message. (Optional.)
   471  	SHA         string // SHA that pull request head must match to allow merge. (Optional.)
   472  
   473  	// The merge method to use. Possible values include: "merge", "squash", and "rebase" with the default being merge. (Optional.)
   474  	MergeMethod string
   475  
   476  	// If false, an empty string commit message will use the default commit message. If true, an empty string commit message will be used.
   477  	DontDefaultIfBlank bool
   478  }
   479  
   480  type pullRequestMergeRequest struct {
   481  	CommitMessage *string `json:"commit_message,omitempty"`
   482  	CommitTitle   string  `json:"commit_title,omitempty"`
   483  	MergeMethod   string  `json:"merge_method,omitempty"`
   484  	SHA           string  `json:"sha,omitempty"`
   485  }
   486  
   487  // Merge a pull request.
   488  // commitMessage is an extra detail to append to automatic commit message.
   489  //
   490  // GitHub API docs: https://docs.github.com/rest/pulls/pulls#merge-a-pull-request
   491  //
   492  //meta:operation PUT /repos/{owner}/{repo}/pulls/{pull_number}/merge
   493  func (s *PullRequestsService) Merge(ctx context.Context, owner string, repo string, number int, commitMessage string, options *PullRequestOptions) (*PullRequestMergeResult, *Response, error) {
   494  	u := fmt.Sprintf("repos/%v/%v/pulls/%d/merge", owner, repo, number)
   495  
   496  	pullRequestBody := &pullRequestMergeRequest{}
   497  	if commitMessage != "" {
   498  		pullRequestBody.CommitMessage = &commitMessage
   499  	}
   500  	if options != nil {
   501  		pullRequestBody.CommitTitle = options.CommitTitle
   502  		pullRequestBody.MergeMethod = options.MergeMethod
   503  		pullRequestBody.SHA = options.SHA
   504  		if options.DontDefaultIfBlank && commitMessage == "" {
   505  			pullRequestBody.CommitMessage = &commitMessage
   506  		}
   507  	}
   508  	req, err := s.client.NewRequest("PUT", u, pullRequestBody)
   509  	if err != nil {
   510  		return nil, nil, err
   511  	}
   512  
   513  	mergeResult := new(PullRequestMergeResult)
   514  	resp, err := s.client.Do(ctx, req, mergeResult)
   515  	if err != nil {
   516  		return nil, resp, err
   517  	}
   518  
   519  	return mergeResult, resp, nil
   520  }