merge_request_approvals.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package gitlab
  2. import (
  3. "fmt"
  4. "net/url"
  5. "time"
  6. )
  7. // MergeRequestApprovalsService handles communication with the merge request
  8. // approvals related methods of the GitLab API. This includes reading/updating
  9. // approval settings and approve/unapproving merge requests
  10. //
  11. // GitLab API docs: https://docs.gitlab.com/ee/api/merge_request_approvals.html
  12. type MergeRequestApprovalsService struct {
  13. client *Client
  14. }
  15. // MergeRequestApprovals represents GitLab merge request approvals.
  16. //
  17. // GitLab API docs:
  18. // https://docs.gitlab.com/ee/api/merge_request_approvals.html#merge-request-level-mr-approvals
  19. type MergeRequestApprovals struct {
  20. ID int `json:"id"`
  21. ProjectID int `json:"project_id"`
  22. Title string `json:"title"`
  23. Description string `json:"description"`
  24. State string `json:"state"`
  25. CreatedAt *time.Time `json:"created_at"`
  26. UpdatedAt *time.Time `json:"updated_at"`
  27. MergeStatus string `json:"merge_status"`
  28. ApprovalsBeforeMerge int `json:"approvals_before_merge"`
  29. ApprovalsRequired int `json:"approvals_required"`
  30. ApprovalsLeft int `json:"approvals_left"`
  31. ApprovedBy []struct {
  32. User struct {
  33. ID int `json:"id"`
  34. Name string `json:"name"`
  35. Username string `json:"username"`
  36. State string `json:"state"`
  37. AvatarURL string `json:"avatar_url"`
  38. WebURL string `json:"web_url"`
  39. } `json:"user"`
  40. } `json:"approved_by"`
  41. Approvers []struct {
  42. User struct {
  43. ID int `json:"id"`
  44. Name string `json:"name"`
  45. Username string `json:"username"`
  46. State string `json:"state"`
  47. AvatarURL string `json:"avatar_url"`
  48. WebURL string `json:"web_url"`
  49. } `json:"user"`
  50. } `json:"approvers"`
  51. ApproverGroups []struct {
  52. Group struct {
  53. ID int `json:"id"`
  54. Name string `json:"name"`
  55. Path string `json:"path"`
  56. Description string `json:"description"`
  57. Visibility string `json:"visibility"`
  58. AvatarURL string `json:"avatar_url"`
  59. WebURL string `json:"web_url"`
  60. FullName string `json:"full_name"`
  61. FullPath string `json:"full_path"`
  62. LFSEnabled bool `json:"lfs_enabled"`
  63. RequestAccessEnabled bool `json:"request_access_enabled"`
  64. } `json:"group"`
  65. } `json:"approver_group"`
  66. }
  67. func (m MergeRequestApprovals) String() string {
  68. return Stringify(m)
  69. }
  70. // ApproveMergeRequestOptions represents the available ApproveMergeRequest() options.
  71. //
  72. // GitLab API docs:
  73. // https://docs.gitlab.com/ee/api/merge_request_approvals.html#approve-merge-request
  74. type ApproveMergeRequestOptions struct {
  75. SHA *string `url:"sha,omitempty" json:"sha,omitempty"`
  76. }
  77. // ApproveMergeRequest approves a merge request on GitLab. If a non-empty sha
  78. // is provided then it must match the sha at the HEAD of the MR.
  79. //
  80. // GitLab API docs:
  81. // https://docs.gitlab.com/ee/api/merge_request_approvals.html#approve-merge-request
  82. func (s *MergeRequestApprovalsService) ApproveMergeRequest(pid interface{}, mr int, opt *ApproveMergeRequestOptions, options ...OptionFunc) (*MergeRequestApprovals, *Response, error) {
  83. project, err := parseID(pid)
  84. if err != nil {
  85. return nil, nil, err
  86. }
  87. u := fmt.Sprintf("projects/%s/merge_requests/%d/approve", url.QueryEscape(project), mr)
  88. req, err := s.client.NewRequest("POST", u, opt, options)
  89. if err != nil {
  90. return nil, nil, err
  91. }
  92. m := new(MergeRequestApprovals)
  93. resp, err := s.client.Do(req, m)
  94. if err != nil {
  95. return nil, resp, err
  96. }
  97. return m, resp, err
  98. }
  99. // UnapproveMergeRequest unapproves a previously approved merge request on GitLab.
  100. //
  101. // GitLab API docs:
  102. // https://docs.gitlab.com/ee/api/merge_request_approvals.html#unapprove-merge-request
  103. func (s *MergeRequestApprovalsService) UnapproveMergeRequest(pid interface{}, mr int, options ...OptionFunc) (*Response, error) {
  104. project, err := parseID(pid)
  105. if err != nil {
  106. return nil, err
  107. }
  108. u := fmt.Sprintf("projects/%s/merge_requests/%d/unapprove", url.QueryEscape(project), mr)
  109. req, err := s.client.NewRequest("POST", u, nil, options)
  110. if err != nil {
  111. return nil, err
  112. }
  113. return s.client.Do(req, nil)
  114. }