You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pull.go 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package gitea
  5. import (
  6. "bytes"
  7. "encoding/json"
  8. "fmt"
  9. "time"
  10. )
  11. // PullRequest represents a pull request
  12. type PullRequest struct {
  13. ID int64 `json:"id"`
  14. URL string `json:"url"`
  15. Index int64 `json:"number"`
  16. Poster *User `json:"user"`
  17. Title string `json:"title"`
  18. Body string `json:"body"`
  19. Labels []*Label `json:"labels"`
  20. Milestone *Milestone `json:"milestone"`
  21. Assignee *User `json:"assignee"`
  22. State StateType `json:"state"`
  23. Comments int `json:"comments"`
  24. HTMLURL string `json:"html_url"`
  25. DiffURL string `json:"diff_url"`
  26. PatchURL string `json:"patch_url"`
  27. Mergeable bool `json:"mergeable"`
  28. HasMerged bool `json:"merged"`
  29. // swagger:strfmt date-time
  30. Merged *time.Time `json:"merged_at"`
  31. MergedCommitID *string `json:"merge_commit_sha"`
  32. MergedBy *User `json:"merged_by"`
  33. Base *PRBranchInfo `json:"base"`
  34. Head *PRBranchInfo `json:"head"`
  35. MergeBase string `json:"merge_base"`
  36. // swagger:strfmt date-time
  37. Created *time.Time `json:"created_at"`
  38. // swagger:strfmt date-time
  39. Updated *time.Time `json:"updated_at"`
  40. }
  41. // PRBranchInfo information about a branch
  42. type PRBranchInfo struct {
  43. Name string `json:"label"`
  44. Ref string `json:"ref"`
  45. Sha string `json:"sha"`
  46. RepoID int64 `json:"repo_id"`
  47. Repository *Repository `json:"repo"`
  48. }
  49. // ListPullRequestsOptions options for listing pull requests
  50. type ListPullRequestsOptions struct {
  51. Page int `json:"page"`
  52. State string `json:"state"`
  53. }
  54. // ListRepoPullRequests list PRs of one repository
  55. func (c *Client) ListRepoPullRequests(owner, repo string, opt ListPullRequestsOptions) ([]*PullRequest, error) {
  56. body, err := json.Marshal(&opt)
  57. if err != nil {
  58. return nil, err
  59. }
  60. prs := make([]*PullRequest, 0, 10)
  61. return prs, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls", owner, repo), jsonHeader, bytes.NewReader(body), &prs)
  62. }
  63. // GetPullRequest get information of one PR
  64. func (c *Client) GetPullRequest(owner, repo string, index int64) (*PullRequest, error) {
  65. pr := new(PullRequest)
  66. return pr, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls/%d", owner, repo, index), nil, nil, pr)
  67. }
  68. // CreatePullRequestOption options when creating a pull request
  69. type CreatePullRequestOption struct {
  70. Head string `json:"head" binding:"Required"`
  71. Base string `json:"base" binding:"Required"`
  72. Title string `json:"title" binding:"Required"`
  73. Body string `json:"body"`
  74. Assignee string `json:"assignee"`
  75. Milestone int64 `json:"milestone"`
  76. Labels []int64 `json:"labels"`
  77. }
  78. // CreatePullRequest create pull request with options
  79. func (c *Client) CreatePullRequest(owner, repo string, opt CreatePullRequestOption) (*PullRequest, error) {
  80. body, err := json.Marshal(&opt)
  81. if err != nil {
  82. return nil, err
  83. }
  84. pr := new(PullRequest)
  85. return pr, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/pulls", owner, repo),
  86. jsonHeader, bytes.NewReader(body), pr)
  87. }
  88. // EditPullRequestOption options when modify pull request
  89. type EditPullRequestOption struct {
  90. Title string `json:"title"`
  91. Body string `json:"body"`
  92. Assignee string `json:"assignee"`
  93. Milestone int64 `json:"milestone"`
  94. Labels []int64 `json:"labels"`
  95. State *string `json:"state"`
  96. }
  97. // EditPullRequest modify pull request with PR id and options
  98. func (c *Client) EditPullRequest(owner, repo string, index int64, opt EditPullRequestOption) (*PullRequest, error) {
  99. body, err := json.Marshal(&opt)
  100. if err != nil {
  101. return nil, err
  102. }
  103. pr := new(PullRequest)
  104. return pr, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index),
  105. jsonHeader, bytes.NewReader(body), pr)
  106. }
  107. // MergePullRequest merge a PR to repository by PR id
  108. func (c *Client) MergePullRequest(owner, repo string, index int64) error {
  109. _, err := c.getResponse("POST", fmt.Sprintf("/repos/%s/%s/pulls/%d/merge", owner, repo, index), nil, nil)
  110. return err
  111. }
  112. // IsPullRequestMerged test if one PR is merged to one repository
  113. func (c *Client) IsPullRequestMerged(owner, repo string, index int64) (bool, error) {
  114. statusCode, err := c.getStatusCode("GET", fmt.Sprintf("/repos/%s/%s/pulls/%d/merge", owner, repo, index), nil, nil)
  115. if err != nil {
  116. return false, err
  117. }
  118. return statusCode == 204, nil
  119. }