pulls.go 12 KB

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