gitlab-org--gitlab-foss/app/services/protected_branches/base_service.rb
Timothy Andrew 01d190a84a Have the branches API work with the new protected branches data model.
1. The new data model moves from `developers_can_{push,merge}` to
   `allowed_to_{push,merge}`.

2. The API interface has not been changed. It still accepts
   `developers_can_push` and `developers_can_merge` as options. These
   attributes are inferred from the new data model.

3. Modify the protected branch create/update services to translate from
   the API interface to our current data model.
2016-07-29 15:20:39 +05:30

60 lines
1.5 KiB
Ruby

module ProtectedBranches
class BaseService < ::BaseService
include API::Helpers
def initialize(project, current_user, params = {})
super(project, current_user, params)
@allowed_to_push = params[:allowed_to_push]
@allowed_to_merge = params[:allowed_to_merge]
end
def set_access_levels!
translate_api_params!
set_merge_access_levels!
set_push_access_levels!
end
protected
def set_merge_access_levels!
case @allowed_to_merge
when 'masters'
@protected_branch.merge_access_level.masters!
when 'developers'
@protected_branch.merge_access_level.developers!
end
end
def set_push_access_levels!
case @allowed_to_push
when 'masters'
@protected_branch.push_access_level.masters!
when 'developers'
@protected_branch.push_access_level.developers!
when 'no_one'
@protected_branch.push_access_level.no_one!
end
end
# The `branches` API still uses `developers_can_push` and `developers_can_merge`,
# which need to be translated to `allowed_to_push` and `allowed_to_merge`,
# respectively.
def translate_api_params!
@allowed_to_push ||=
case to_boolean(params[:developers_can_push])
when true
'developers'
when false
'masters'
end
@allowed_to_merge ||=
case to_boolean(params[:developers_can_merge])
when true
'developers'
when false
'masters'
end
end
end
end