diff options
author | Douwe Maan <douwe@gitlab.com> | 2016-10-24 12:55:02 +0000 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2016-10-24 12:55:02 +0000 |
commit | 6e693771e81c7e1f8e80f6364e4b8d9a1e000dc1 (patch) | |
tree | 84ec93a246e8b902df81599dd26c894948089011 /lib | |
parent | f2644adf8b2352ddadc14687ee0dc172a8416d1c (diff) | |
parent | db0182e261936c3e800b546d307a3d3834ff9927 (diff) | |
download | gitlab-ce-6e693771e81c7e1f8e80f6364e4b8d9a1e000dc1.tar.gz |
Merge branch '21513-fix-branch-protection-api' into 'master'
Fix branch protection API.
## What does this MR do?
- Fixes the branch protection API.
- Closes #21513
- EE Merge Request: gitlab-org/gitlab-ee!718
## Tasks
- [ ] #21513 !6215 Protected branches API bug
- [x] Investigate
- [x] Test + Fix
- [x] Changelog
- [x] MR
- [x] Wait for build to pass
- [x] Review
- [x] Check for EE conflicts
- [x] Create EE MR
- [x] Refactor + Fix
- [x] Rebase EE MR against EE master
- [x] Wait for builds to pass
- [x] Assign to dbalexandre/douwe
- [x] Implement latest review comments
- [x] Wait for Douwe's review
- [x] Implement changes
- [x] Port changes to EE MR
- [x] Assign both back to Douwe
- [ ] Wait for merge
- [ ] Merge gitlab-org/gitlab-ee!718
See merge request !6215
Diffstat (limited to 'lib')
-rw-r--r-- | lib/api/branches.rb | 48 |
1 files changed, 15 insertions, 33 deletions
diff --git a/lib/api/branches.rb b/lib/api/branches.rb index b615703df93..6d827448994 100644 --- a/lib/api/branches.rb +++ b/lib/api/branches.rb @@ -54,43 +54,25 @@ module API not_found!('Branch') unless @branch protected_branch = user_project.protected_branches.find_by(name: @branch.name) - developers_can_merge = to_boolean(params[:developers_can_merge]) - developers_can_push = to_boolean(params[:developers_can_push]) - protected_branch_params = { - name: @branch.name + name: @branch.name, + developers_can_push: to_boolean(params[:developers_can_push]), + developers_can_merge: to_boolean(params[:developers_can_merge]) } - # If `developers_can_merge` is switched off, _all_ `DEVELOPER` - # merge_access_levels need to be deleted. - if developers_can_merge == false - protected_branch.merge_access_levels.where(access_level: Gitlab::Access::DEVELOPER).destroy_all - end + service_args = [user_project, current_user, protected_branch_params] - # If `developers_can_push` is switched off, _all_ `DEVELOPER` - # push_access_levels need to be deleted. - if developers_can_push == false - protected_branch.push_access_levels.where(access_level: Gitlab::Access::DEVELOPER).destroy_all - end + protected_branch = if protected_branch + ProtectedBranches::ApiUpdateService.new(*service_args).execute(protected_branch) + else + ProtectedBranches::ApiCreateService.new(*service_args).execute + end - protected_branch_params.merge!( - merge_access_levels_attributes: [{ - access_level: developers_can_merge ? Gitlab::Access::DEVELOPER : Gitlab::Access::MASTER - }], - push_access_levels_attributes: [{ - access_level: developers_can_push ? Gitlab::Access::DEVELOPER : Gitlab::Access::MASTER - }] - ) - - if protected_branch - service = ProtectedBranches::UpdateService.new(user_project, current_user, protected_branch_params) - service.execute(protected_branch) + if protected_branch.valid? + present @branch, with: Entities::RepoBranch, project: user_project else - service = ProtectedBranches::CreateService.new(user_project, current_user, protected_branch_params) - service.execute + render_api_error!(protected_branch.errors.full_messages, 422) end - - present @branch, with: Entities::RepoBranch, project: user_project end # Unprotect a single branch @@ -123,7 +105,7 @@ module API post ":id/repository/branches" do authorize_push_project result = CreateBranchService.new(user_project, current_user). - execute(params[:branch_name], params[:ref]) + execute(params[:branch_name], params[:ref]) if result[:status] == :success present result[:branch], @@ -142,10 +124,10 @@ module API # Example Request: # DELETE /projects/:id/repository/branches/:branch delete ":id/repository/branches/:branch", - requirements: { branch: /.+/ } do + requirements: { branch: /.+/ } do authorize_push_project result = DeleteBranchService.new(user_project, current_user). - execute(params[:branch]) + execute(params[:branch]) if result[:status] == :success { |