diff options
author | Rémy Coutable <remy@rymai.me> | 2016-07-19 10:31:56 +0000 |
---|---|---|
committer | Rémy Coutable <remy@rymai.me> | 2016-07-19 10:31:56 +0000 |
commit | d6bd412be4e3063c5f8844ef2c15f736f173b2f1 (patch) | |
tree | 1641ae67ef742197aee7cf0990a7ec9865f7e268 /lib | |
parent | cd546a784434a8d1a872bc37e5a0c252b030f73c (diff) | |
parent | 3e281f95907686ba4a923b8825dc32afb22df038 (diff) | |
download | gitlab-ce-d6bd412be4e3063c5f8844ef2c15f736f173b2f1.tar.gz |
Merge branch 'api-dev-can-push' into 'master'
API: Expose 'developers_can_push' for branches
## What does this MR do?
Adds support for the `developers_can_push` flag for the branches API. It also supports creating protected branches with that flag.
## Are there points in the code the reviewer needs to double check?
The API call requires an optional boolean parameter `developers_can_push`. If it is not either `true` or `false`, it should not update the value. I'm not sure if this is the right way to do it.
Right now it always returns `200`. Maybe the `PUT` method should return different status codes depending if it really created or updated a branch.
## What are the relevant issue numbers?
Closes #12735
## Does this MR meet the acceptance criteria?
- [x] [CHANGELOG](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/CHANGELOG) entry added
- [x] [Documentation created/updated](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/development/doc_styleguide.md)
- [x] API support added
- Tests
- [x] Added for this feature/bug
- [x] All builds are passing
- [x] Conform by the [style guides](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/CONTRIBUTING.md#style-guides)
- [x] Branch has no merge conflicts with `master` (if you do - rebase it please)
- [x] [Squashed related commits together](https://git-scm.com/book/en/Git-Tools-Rewriting-History#Squashing-Commits)
See merge request !5208
Diffstat (limited to 'lib')
-rw-r--r-- | lib/api/branches.rb | 17 | ||||
-rw-r--r-- | lib/api/entities.rb | 16 | ||||
-rw-r--r-- | lib/api/helpers.rb | 7 |
3 files changed, 36 insertions, 4 deletions
diff --git a/lib/api/branches.rb b/lib/api/branches.rb index d467eb9d474..35efe4f2e4a 100644 --- a/lib/api/branches.rb +++ b/lib/api/branches.rb @@ -36,6 +36,8 @@ module API # Parameters: # id (required) - The ID of a project # branch (required) - The name of the branch + # developers_can_push (optional) - Flag if developers can push to that branch + # developers_can_merge (optional) - Flag if developers can merge to that branch # Example Request: # PUT /projects/:id/repository/branches/:branch/protect put ':id/repository/branches/:branch/protect', @@ -43,9 +45,20 @@ module API authorize_admin_project @branch = user_project.repository.find_branch(params[:branch]) - not_found!("Branch") unless @branch + not_found!('Branch') unless @branch protected_branch = user_project.protected_branches.find_by(name: @branch.name) - user_project.protected_branches.create(name: @branch.name) unless protected_branch + developers_can_push = to_boolean(params[:developers_can_push]) + developers_can_merge = to_boolean(params[:developers_can_merge]) + + if protected_branch + protected_branch.developers_can_push = developers_can_push unless developers_can_push.nil? + protected_branch.developers_can_merge = developers_can_merge unless developers_can_merge.nil? + protected_branch.save + else + user_project.protected_branches.create(name: @branch.name, + developers_can_push: developers_can_push || false, + developers_can_merge: developers_can_merge || false) + end present @branch, with: Entities::RepoObject, project: user_project end diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 3c79a00eb8c..d6fed1a1eed 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -125,9 +125,21 @@ module API end end - expose :protected do |repo, options| + expose :protected do |repo_obj, options| if options[:project] - options[:project].protected_branch? repo.name + options[:project].protected_branch? repo_obj.name + end + end + + expose :developers_can_push do |repo_obj, options| + if options[:project] + options[:project].developers_can_push_to_protected_branch? repo_obj.name + end + end + + expose :developers_can_merge do |repo_obj, options| + if options[:project] + options[:project].developers_can_merge_to_protected_branch? repo_obj.name end end end diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index 73557cf7db6..d6e4eb2afd7 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -9,6 +9,13 @@ module API [ true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON' ].include?(value) end + def to_boolean(value) + return true if value =~ /^(true|t|yes|y|1|on)$/i + return false if value =~ /^(false|f|no|n|0|off)$/i + + nil + end + def find_user_by_private_token token_string = (params[PRIVATE_TOKEN_PARAM] || env[PRIVATE_TOKEN_HEADER]).to_s User.find_by_authentication_token(token_string) || User.find_by_personal_access_token(token_string) |