pulls.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. package github
  6. import (
  7. "bytes"
  8. "context"
  9. "fmt"
  10. "strings"
  11. "time"
  12. )
  13. // PullRequestsService handles communication with the pull request related
  14. // methods of the GitHub API.
  15. //
  16. // GitHub API docs: https://developer.github.com/v3/pulls/
  17. type PullRequestsService service
  18. // PullRequest represents a GitHub pull request on a repository.
  19. type PullRequest struct {
  20. ID *int64 `json:"id,omitempty"`
  21. Number *int `json:"number,omitempty"`
  22. State *string `json:"state,omitempty"`
  23. Title *string `json:"title,omitempty"`
  24. Body *string `json:"body,omitempty"`
  25. CreatedAt *time.Time `json:"created_at,omitempty"`
  26. UpdatedAt *time.Time `json:"updated_at,omitempty"`
  27. ClosedAt *time.Time `json:"closed_at,omitempty"`
  28. MergedAt *time.Time `json:"merged_at,omitempty"`
  29. Labels []*Label `json:"labels,omitempty"`
  30. User *User `json:"user,omitempty"`
  31. Merged *bool `json:"merged,omitempty"`
  32. Mergeable *bool `json:"mergeable,omitempty"`
  33. MergeableState *string `json:"mergeable_state,omitempty"`
  34. MergedBy *User `json:"merged_by,omitempty"`
  35. MergeCommitSHA *string `json:"merge_commit_sha,omitempty"`
  36. Comments *int `json:"comments,omitempty"`
  37. Commits *int `json:"commits,omitempty"`
  38. Additions *int `json:"additions,omitempty"`
  39. Deletions *int `json:"deletions,omitempty"`
  40. ChangedFiles *int `json:"changed_files,omitempty"`
  41. URL *string `json:"url,omitempty"`
  42. HTMLURL *string `json:"html_url,omitempty"`
  43. IssueURL *string `json:"issue_url,omitempty"`
  44. StatusesURL *string `json:"statuses_url,omitempty"`
  45. DiffURL *string `json:"diff_url,omitempty"`
  46. PatchURL *string `json:"patch_url,omitempty"`
  47. CommitsURL *string `json:"commits_url,omitempty"`
  48. CommentsURL *string `json:"comments_url,omitempty"`
  49. ReviewCommentsURL *string `json:"review_comments_url,omitempty"`
  50. ReviewCommentURL *string `json:"review_comment_url,omitempty"`
  51. Assignee *User `json:"assignee,omitempty"`
  52. Assignees []*User `json:"assignees,omitempty"`
  53. Milestone *Milestone `json:"milestone,omitempty"`
  54. MaintainerCanModify *bool `json:"maintainer_can_modify,omitempty"`
  55. AuthorAssociation *string `json:"author_association,omitempty"`
  56. NodeID *string `json:"node_id,omitempty"`
  57. RequestedReviewers []*User `json:"requested_reviewers,omitempty"`
  58. Head *PullRequestBranch `json:"head,omitempty"`
  59. Base *PullRequestBranch `json:"base,omitempty"`
  60. }
  61. func (p PullRequest) String() string {
  62. return Stringify(p)
  63. }
  64. // PullRequestBranch represents a base or head branch in a GitHub pull request.
  65. type PullRequestBranch struct {
  66. Label *string `json:"label,omitempty"`
  67. Ref *string `json:"ref,omitempty"`
  68. SHA *string `json:"sha,omitempty"`
  69. Repo *Repository `json:"repo,omitempty"`
  70. User *User `json:"user,omitempty"`
  71. }
  72. // PullRequestListOptions specifies the optional parameters to the
  73. // PullRequestsService.List method.
  74. type PullRequestListOptions struct {
  75. // State filters pull requests based on their state. Possible values are:
  76. // open, closed. Default is "open".
  77. State string `url:"state,omitempty"`
  78. // Head filters pull requests by head user and branch name in the format of:
  79. // "user:ref-name".
  80. Head string `url:"head,omitempty"`
  81. // Base filters pull requests by base branch name.
  82. Base string `url:"base,omitempty"`
  83. // Sort specifies how to sort pull requests. Possible values are: created,
  84. // updated, popularity, long-running. Default is "created".
  85. Sort string `url:"sort,omitempty"`
  86. // Direction in which to sort pull requests. Possible values are: asc, desc.
  87. // If Sort is "created" or not specified, Default is "desc", otherwise Default
  88. // is "asc"
  89. Direction string `url:"direction,omitempty"`
  90. ListOptions
  91. }
  92. // List the pull requests for the specified repository.
  93. //
  94. // GitHub API docs: https://developer.github.com/v3/pulls/#list-pull-requests
  95. func (s *PullRequestsService) List(ctx context.Context, owner string, repo string, opt *PullRequestListOptions) ([]*PullRequest, *Response, error) {
  96. u := fmt.Sprintf("repos/%v/%v/pulls", owner, repo)
  97. u, err := addOptions(u, opt)
  98. if err != nil {
  99. return nil, nil, err
  100. }
  101. req, err := s.client.NewRequest("GET", u, nil)
  102. if err != nil {
  103. return nil, nil, err
  104. }
  105. // TODO: remove custom Accept header when this API fully launches.
  106. acceptHeaders := []string{mediaTypeGraphQLNodeIDPreview, mediaTypeLabelDescriptionSearchPreview}
  107. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  108. var pulls []*PullRequest
  109. resp, err := s.client.Do(ctx, req, &pulls)
  110. if err != nil {
  111. return nil, resp, err
  112. }
  113. return pulls, resp, nil
  114. }
  115. // Get a single pull request.
  116. //
  117. // GitHub API docs: https://developer.github.com/v3/pulls/#get-a-single-pull-request
  118. func (s *PullRequestsService) Get(ctx context.Context, owner string, repo string, number int) (*PullRequest, *Response, error) {
  119. u := fmt.Sprintf("repos/%v/%v/pulls/%d", owner, repo, number)
  120. req, err := s.client.NewRequest("GET", u, nil)
  121. if err != nil {
  122. return nil, nil, err
  123. }
  124. // TODO: remove custom Accept header when this API fully launches.
  125. acceptHeaders := []string{mediaTypeGraphQLNodeIDPreview, mediaTypeLabelDescriptionSearchPreview}
  126. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  127. pull := new(PullRequest)
  128. resp, err := s.client.Do(ctx, req, pull)
  129. if err != nil {
  130. return nil, resp, err
  131. }
  132. return pull, resp, nil
  133. }
  134. // GetRaw gets a single pull request in raw (diff or patch) format.
  135. func (s *PullRequestsService) GetRaw(ctx context.Context, owner string, repo string, number int, opt RawOptions) (string, *Response, error) {
  136. u := fmt.Sprintf("repos/%v/%v/pulls/%d", owner, repo, number)
  137. req, err := s.client.NewRequest("GET", u, nil)
  138. if err != nil {
  139. return "", nil, err
  140. }
  141. switch opt.Type {
  142. case Diff:
  143. req.Header.Set("Accept", mediaTypeV3Diff)
  144. case Patch:
  145. req.Header.Set("Accept", mediaTypeV3Patch)
  146. default:
  147. return "", nil, fmt.Errorf("unsupported raw type %d", opt.Type)
  148. }
  149. var buf bytes.Buffer
  150. resp, err := s.client.Do(ctx, req, &buf)
  151. if err != nil {
  152. return "", resp, err
  153. }
  154. return buf.String(), resp, nil
  155. }
  156. // NewPullRequest represents a new pull request to be created.
  157. type NewPullRequest struct {
  158. Title *string `json:"title,omitempty"`
  159. Head *string `json:"head,omitempty"`
  160. Base *string `json:"base,omitempty"`
  161. Body *string `json:"body,omitempty"`
  162. Issue *int `json:"issue,omitempty"`
  163. MaintainerCanModify *bool `json:"maintainer_can_modify,omitempty"`
  164. }
  165. // Create a new pull request on the specified repository.
  166. //
  167. // GitHub API docs: https://developer.github.com/v3/pulls/#create-a-pull-request
  168. func (s *PullRequestsService) Create(ctx context.Context, owner string, repo string, pull *NewPullRequest) (*PullRequest, *Response, error) {
  169. u := fmt.Sprintf("repos/%v/%v/pulls", owner, repo)
  170. req, err := s.client.NewRequest("POST", u, pull)
  171. if err != nil {
  172. return nil, nil, err
  173. }
  174. // TODO: remove custom Accept header when this API fully launches.
  175. acceptHeaders := []string{mediaTypeGraphQLNodeIDPreview, mediaTypeLabelDescriptionSearchPreview}
  176. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  177. p := new(PullRequest)
  178. resp, err := s.client.Do(ctx, req, p)
  179. if err != nil {
  180. return nil, resp, err
  181. }
  182. return p, resp, nil
  183. }
  184. type pullRequestUpdate struct {
  185. Title *string `json:"title,omitempty"`
  186. Body *string `json:"body,omitempty"`
  187. State *string `json:"state,omitempty"`
  188. Base *string `json:"base,omitempty"`
  189. MaintainerCanModify *bool `json:"maintainer_can_modify,omitempty"`
  190. }
  191. // Edit a pull request.
  192. // pull must not be nil.
  193. //
  194. // The following fields are editable: Title, Body, State, Base.Ref and MaintainerCanModify.
  195. // Base.Ref updates the base branch of the pull request.
  196. //
  197. // GitHub API docs: https://developer.github.com/v3/pulls/#update-a-pull-request
  198. func (s *PullRequestsService) Edit(ctx context.Context, owner string, repo string, number int, pull *PullRequest) (*PullRequest, *Response, error) {
  199. if pull == nil {
  200. return nil, nil, fmt.Errorf("pull must be provided")
  201. }
  202. u := fmt.Sprintf("repos/%v/%v/pulls/%d", owner, repo, number)
  203. update := &pullRequestUpdate{
  204. Title: pull.Title,
  205. Body: pull.Body,
  206. State: pull.State,
  207. MaintainerCanModify: pull.MaintainerCanModify,
  208. }
  209. if pull.Base != nil {
  210. update.Base = pull.Base.Ref
  211. }
  212. req, err := s.client.NewRequest("PATCH", u, update)
  213. if err != nil {
  214. return nil, nil, err
  215. }
  216. // TODO: remove custom Accept header when this API fully launches.
  217. acceptHeaders := []string{mediaTypeGraphQLNodeIDPreview, mediaTypeLabelDescriptionSearchPreview}
  218. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  219. p := new(PullRequest)
  220. resp, err := s.client.Do(ctx, req, p)
  221. if err != nil {
  222. return nil, resp, err
  223. }
  224. return p, resp, nil
  225. }
  226. // ListCommits lists the commits in a pull request.
  227. //
  228. // GitHub API docs: https://developer.github.com/v3/pulls/#list-commits-on-a-pull-request
  229. func (s *PullRequestsService) ListCommits(ctx context.Context, owner string, repo string, number int, opt *ListOptions) ([]*RepositoryCommit, *Response, error) {
  230. u := fmt.Sprintf("repos/%v/%v/pulls/%d/commits", owner, repo, number)
  231. u, err := addOptions(u, opt)
  232. if err != nil {
  233. return nil, nil, err
  234. }
  235. req, err := s.client.NewRequest("GET", u, nil)
  236. if err != nil {
  237. return nil, nil, err
  238. }
  239. // TODO: remove custom Accept header when this API fully launches.
  240. req.Header.Set("Accept", mediaTypeGitSigningPreview)
  241. var commits []*RepositoryCommit
  242. resp, err := s.client.Do(ctx, req, &commits)
  243. if err != nil {
  244. return nil, resp, err
  245. }
  246. return commits, resp, nil
  247. }
  248. // ListFiles lists the files in a pull request.
  249. //
  250. // GitHub API docs: https://developer.github.com/v3/pulls/#list-pull-requests-files
  251. func (s *PullRequestsService) ListFiles(ctx context.Context, owner string, repo string, number int, opt *ListOptions) ([]*CommitFile, *Response, error) {
  252. u := fmt.Sprintf("repos/%v/%v/pulls/%d/files", owner, repo, number)
  253. u, err := addOptions(u, opt)
  254. if err != nil {
  255. return nil, nil, err
  256. }
  257. req, err := s.client.NewRequest("GET", u, nil)
  258. if err != nil {
  259. return nil, nil, err
  260. }
  261. var commitFiles []*CommitFile
  262. resp, err := s.client.Do(ctx, req, &commitFiles)
  263. if err != nil {
  264. return nil, resp, err
  265. }
  266. return commitFiles, resp, nil
  267. }
  268. // IsMerged checks if a pull request has been merged.
  269. //
  270. // GitHub API docs: https://developer.github.com/v3/pulls/#get-if-a-pull-request-has-been-merged
  271. func (s *PullRequestsService) IsMerged(ctx context.Context, owner string, repo string, number int) (bool, *Response, error) {
  272. u := fmt.Sprintf("repos/%v/%v/pulls/%d/merge", owner, repo, number)
  273. req, err := s.client.NewRequest("GET", u, nil)
  274. if err != nil {
  275. return false, nil, err
  276. }
  277. resp, err := s.client.Do(ctx, req, nil)
  278. merged, err := parseBoolResponse(err)
  279. return merged, resp, err
  280. }
  281. // PullRequestMergeResult represents the result of merging a pull request.
  282. type PullRequestMergeResult struct {
  283. SHA *string `json:"sha,omitempty"`
  284. Merged *bool `json:"merged,omitempty"`
  285. Message *string `json:"message,omitempty"`
  286. }
  287. // PullRequestOptions lets you define how a pull request will be merged.
  288. type PullRequestOptions struct {
  289. CommitTitle string // Extra detail to append to automatic commit message. (Optional.)
  290. SHA string // SHA that pull request head must match to allow merge. (Optional.)
  291. // The merge method to use. Possible values include: "merge", "squash", and "rebase" with the default being merge. (Optional.)
  292. MergeMethod string
  293. }
  294. type pullRequestMergeRequest struct {
  295. CommitMessage string `json:"commit_message"`
  296. CommitTitle string `json:"commit_title,omitempty"`
  297. MergeMethod string `json:"merge_method,omitempty"`
  298. SHA string `json:"sha,omitempty"`
  299. }
  300. // Merge a pull request (Merge Button™).
  301. // commitMessage is the title for the automatic commit message.
  302. //
  303. // GitHub API docs: https://developer.github.com/v3/pulls/#merge-a-pull-request-merge-buttontrade
  304. func (s *PullRequestsService) Merge(ctx context.Context, owner string, repo string, number int, commitMessage string, options *PullRequestOptions) (*PullRequestMergeResult, *Response, error) {
  305. u := fmt.Sprintf("repos/%v/%v/pulls/%d/merge", owner, repo, number)
  306. pullRequestBody := &pullRequestMergeRequest{CommitMessage: commitMessage}
  307. if options != nil {
  308. pullRequestBody.CommitTitle = options.CommitTitle
  309. pullRequestBody.MergeMethod = options.MergeMethod
  310. pullRequestBody.SHA = options.SHA
  311. }
  312. req, err := s.client.NewRequest("PUT", u, pullRequestBody)
  313. if err != nil {
  314. return nil, nil, err
  315. }
  316. mergeResult := new(PullRequestMergeResult)
  317. resp, err := s.client.Do(ctx, req, mergeResult)
  318. if err != nil {
  319. return nil, resp, err
  320. }
  321. return mergeResult, resp, nil
  322. }