PageRenderTime 182ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/api/merge_requests.rb

https://gitlab.com/timothyandrew/gitlab-ee
Ruby | 312 lines | 241 code | 48 blank | 23 comment | 16 complexity | c791b9ae25046e2db5e413ffa9fe3ed8 MD5 | raw file
  1. module API
  2. class MergeRequests < Grape::API
  3. DEPRECATION_MESSAGE = 'This endpoint is deprecated and will be removed in GitLab 9.0.'.freeze
  4. before { authenticate! }
  5. params do
  6. requires :id, type: String, desc: 'The ID of a project'
  7. end
  8. resource :projects do
  9. helpers do
  10. def handle_merge_request_errors!(errors)
  11. if errors[:project_access].any?
  12. error!(errors[:project_access], 422)
  13. elsif errors[:branch_conflict].any?
  14. error!(errors[:branch_conflict], 422)
  15. elsif errors[:validate_fork].any?
  16. error!(errors[:validate_fork], 422)
  17. elsif errors[:validate_branches].any?
  18. conflict!(errors[:validate_branches])
  19. end
  20. render_api_error!(errors, 400)
  21. end
  22. params :optional_params do
  23. optional :description, type: String, desc: 'The description of the merge request'
  24. optional :assignee_id, type: Integer, desc: 'The ID of a user to assign the merge request'
  25. optional :milestone_id, type: Integer, desc: 'The ID of a milestone to assign the merge request'
  26. optional :labels, type: String, desc: 'Comma-separated list of label names'
  27. optional :approvals_before_merge, type: Integer, desc: 'Number of approvals required before this can be merged'
  28. end
  29. end
  30. desc 'List merge requests' do
  31. success Entities::MergeRequest
  32. end
  33. params do
  34. optional :state, type: String, values: %w[opened closed merged all], default: 'all',
  35. desc: 'Return opened, closed, merged, or all merge requests'
  36. optional :order_by, type: String, values: %w[created_at updated_at], default: 'created_at',
  37. desc: 'Return merge requests ordered by `created_at` or `updated_at` fields.'
  38. optional :sort, type: String, values: %w[asc desc], default: 'desc',
  39. desc: 'Return merge requests sorted in `asc` or `desc` order.'
  40. optional :iid, type: Array[Integer], desc: 'The IID of the merge requests'
  41. end
  42. get ":id/merge_requests" do
  43. authorize! :read_merge_request, user_project
  44. merge_requests = user_project.merge_requests.inc_notes_with_associations
  45. merge_requests = filter_by_iid(merge_requests, params[:iid]) if params[:iid].present?
  46. merge_requests =
  47. case params[:state]
  48. when 'opened' then merge_requests.opened
  49. when 'closed' then merge_requests.closed
  50. when 'merged' then merge_requests.merged
  51. else merge_requests
  52. end
  53. merge_requests = merge_requests.reorder(params[:order_by] => params[:sort])
  54. present paginate(merge_requests), with: Entities::MergeRequest, current_user: current_user, project: user_project
  55. end
  56. desc 'Create a merge request' do
  57. success Entities::MergeRequest
  58. end
  59. params do
  60. requires :title, type: String, desc: 'The title of the merge request'
  61. requires :source_branch, type: String, desc: 'The source branch'
  62. requires :target_branch, type: String, desc: 'The target branch'
  63. optional :target_project_id, type: Integer,
  64. desc: 'The target project of the merge request defaults to the :id of the project'
  65. use :optional_params
  66. end
  67. post ":id/merge_requests" do
  68. authorize! :create_merge_request, user_project
  69. mr_params = declared_params
  70. # Validate label names in advance
  71. if (errors = validate_label_params(mr_params)).any?
  72. render_api_error!({ labels: errors }, 400)
  73. end
  74. merge_request = ::MergeRequests::CreateService.new(user_project, current_user, mr_params).execute
  75. if merge_request.valid?
  76. present merge_request, with: Entities::MergeRequest, current_user: current_user, project: user_project
  77. else
  78. handle_merge_request_errors! merge_request.errors
  79. end
  80. end
  81. desc 'Delete a merge request'
  82. params do
  83. requires :merge_request_id, type: Integer, desc: 'The ID of a merge request'
  84. end
  85. delete ":id/merge_requests/:merge_request_id" do
  86. merge_request = user_project.merge_requests.find_by(id: params[:merge_request_id])
  87. authorize!(:destroy_merge_request, merge_request)
  88. merge_request.destroy
  89. end
  90. # Routing "merge_request/:merge_request_id/..." is DEPRECATED and WILL BE REMOVED in version 9.0
  91. # Use "merge_requests/:merge_request_id/..." instead.
  92. #
  93. params do
  94. requires :merge_request_id, type: Integer, desc: 'The ID of a merge request'
  95. end
  96. { ":id/merge_request/:merge_request_id" => :deprecated, ":id/merge_requests/:merge_request_id" => :ok }.each do |path, status|
  97. desc 'Get a single merge request' do
  98. if status == :deprecated
  99. detail DEPRECATION_MESSAGE
  100. end
  101. success Entities::MergeRequest
  102. end
  103. get path do
  104. merge_request = user_project.merge_requests.find(params[:merge_request_id])
  105. authorize! :read_merge_request, merge_request
  106. present merge_request, with: Entities::MergeRequest, current_user: current_user, project: user_project
  107. end
  108. desc 'Get the commits of a merge request' do
  109. success Entities::RepoCommit
  110. end
  111. get "#{path}/commits" do
  112. merge_request = user_project.merge_requests.find(params[:merge_request_id])
  113. authorize! :read_merge_request, merge_request
  114. present merge_request.commits, with: Entities::RepoCommit
  115. end
  116. desc 'Show the merge request changes' do
  117. success Entities::MergeRequestChanges
  118. end
  119. get "#{path}/changes" do
  120. merge_request = user_project.merge_requests.find(params[:merge_request_id])
  121. authorize! :read_merge_request, merge_request
  122. present merge_request, with: Entities::MergeRequestChanges, current_user: current_user
  123. end
  124. desc 'Update a merge request' do
  125. success Entities::MergeRequest
  126. end
  127. params do
  128. optional :title, type: String, desc: 'The title of the merge request'
  129. optional :target_branch, type: String, desc: 'The target branch'
  130. optional :state_event, type: String, values: %w[close reopen merge],
  131. desc: 'Status of the merge request'
  132. use :optional_params
  133. at_least_one_of :title, :target_branch, :description, :assignee_id,
  134. :milestone_id, :labels, :state_event, :approvals_before_merge
  135. end
  136. put path do
  137. merge_request = user_project.merge_requests.find(params.delete(:merge_request_id))
  138. authorize! :update_merge_request, merge_request
  139. mr_params = declared_params(include_missing: false)
  140. # Validate label names in advance
  141. if (errors = validate_label_params(mr_params)).any?
  142. render_api_error!({ labels: errors }, 400)
  143. end
  144. merge_request = ::MergeRequests::UpdateService.new(user_project, current_user, mr_params).execute(merge_request)
  145. if merge_request.valid?
  146. present merge_request, with: Entities::MergeRequest, current_user: current_user, project: user_project
  147. else
  148. handle_merge_request_errors! merge_request.errors
  149. end
  150. end
  151. desc 'Merge a merge request' do
  152. success Entities::MergeRequest
  153. end
  154. params do
  155. optional :merge_commit_message, type: String, desc: 'Custom merge commit message'
  156. optional :should_remove_source_branch, type: Boolean,
  157. desc: 'When true, the source branch will be deleted if possible'
  158. optional :merge_when_build_succeeds, type: Boolean,
  159. desc: 'When true, this merge request will be merged when the build succeeds'
  160. optional :sha, type: String, desc: 'When present, must have the HEAD SHA of the source branch'
  161. end
  162. put "#{path}/merge" do
  163. merge_request = user_project.merge_requests.find(params[:merge_request_id])
  164. # Merge request can not be merged
  165. # because user dont have permissions to push into target branch
  166. unauthorized! unless merge_request.can_be_merged_by?(current_user)
  167. not_allowed! unless merge_request.mergeable_state?
  168. render_api_error!('Branch cannot be merged', 406) unless merge_request.mergeable?
  169. if params[:sha] && merge_request.diff_head_sha != params[:sha]
  170. render_api_error!("SHA does not match HEAD of source branch: #{merge_request.diff_head_sha}", 409)
  171. end
  172. merge_params = {
  173. commit_message: params[:merge_commit_message],
  174. should_remove_source_branch: params[:should_remove_source_branch]
  175. }
  176. if params[:merge_when_build_succeeds] && merge_request.pipeline && merge_request.pipeline.active?
  177. ::MergeRequests::MergeWhenBuildSucceedsService.new(merge_request.target_project, current_user, merge_params).
  178. execute(merge_request)
  179. else
  180. ::MergeRequests::MergeService.new(merge_request.target_project, current_user, merge_params).
  181. execute(merge_request)
  182. end
  183. present merge_request, with: Entities::MergeRequest, current_user: current_user, project: user_project
  184. end
  185. desc 'Cancel merge if "Merge when build succeeds" is enabled' do
  186. success Entities::MergeRequest
  187. end
  188. post "#{path}/cancel_merge_when_build_succeeds" do
  189. merge_request = user_project.merge_requests.find(params[:merge_request_id])
  190. unauthorized! unless merge_request.can_cancel_merge_when_build_succeeds?(current_user)
  191. ::MergeRequest::MergeWhenBuildSucceedsService.new(merge_request.target_project, current_user).cancel(merge_request)
  192. end
  193. desc 'Get the comments of a merge request' do
  194. detail 'Duplicate. DEPRECATED and WILL BE REMOVED in 9.0'
  195. success Entities::MRNote
  196. end
  197. get "#{path}/comments" do
  198. merge_request = user_project.merge_requests.find(params[:merge_request_id])
  199. authorize! :read_merge_request, merge_request
  200. present paginate(merge_request.notes.fresh), with: Entities::MRNote
  201. end
  202. desc 'Post a comment to a merge request' do
  203. detail 'Duplicate. DEPRECATED and WILL BE REMOVED in 9.0'
  204. success Entities::MRNote
  205. end
  206. params do
  207. requires :note, type: String, desc: 'The text of the comment'
  208. end
  209. post "#{path}/comments" do
  210. merge_request = user_project.merge_requests.find(params[:merge_request_id])
  211. authorize! :create_note, merge_request
  212. opts = {
  213. note: params[:note],
  214. noteable_type: 'MergeRequest',
  215. noteable_id: merge_request.id
  216. }
  217. note = ::Notes::CreateService.new(user_project, current_user, opts).execute
  218. if note.save
  219. present note, with: Entities::MRNote
  220. else
  221. render_api_error!("Failed to save note #{note.errors.messages}", 400)
  222. end
  223. end
  224. desc 'List issues that will be closed on merge' do
  225. success Entities::MRNote
  226. end
  227. get "#{path}/closes_issues" do
  228. merge_request = user_project.merge_requests.find(params[:merge_request_id])
  229. issues = ::Kaminari.paginate_array(merge_request.closes_issues(current_user))
  230. present paginate(issues), with: issue_entity(user_project), current_user: current_user
  231. end
  232. # Get the status of the merge request's approvals
  233. #
  234. # Parameters:
  235. # id (required) - The ID of a project
  236. # merge_request_id (required) - ID of MR
  237. # Examples:
  238. # GET /projects/:id/merge_requests/:merge_request_id/approvals
  239. #
  240. get "#{path}/approvals" do
  241. merge_request = user_project.merge_requests.find(params[:merge_request_id])
  242. authorize! :read_merge_request, merge_request
  243. present merge_request, with: Entities::MergeRequestApprovals, current_user: current_user
  244. end
  245. # Approve a merge request
  246. #
  247. # Parameters:
  248. # id (required) - The ID of a project
  249. # merge_request_id (required) - ID of MR
  250. # Examples:
  251. # POST /projects/:id/merge_requests/:merge_request_id/approvals
  252. #
  253. post "#{path}/approve" do
  254. merge_request = user_project.merge_requests.find(params[:merge_request_id])
  255. unauthorized! unless merge_request.can_approve?(current_user)
  256. ::MergeRequests::ApprovalService
  257. .new(user_project, current_user)
  258. .execute(merge_request)
  259. present merge_request, with: Entities::MergeRequestApprovals, current_user: current_user
  260. end
  261. end
  262. end
  263. end
  264. end