2016-09-06 01:05:34 -04:00
|
|
|
# The protected branches API still uses the `developers_can_push` and `developers_can_merge`
|
|
|
|
# flags for backward compatibility, and so performs translation between that format and the
|
|
|
|
# internal data model (separate access levels). The translation code is non-trivial, and so
|
|
|
|
# lives in this service.
|
|
|
|
module ProtectedBranches
|
|
|
|
class ApiUpdateService < BaseService
|
2016-10-24 02:02:09 -04:00
|
|
|
def execute(protected_branch)
|
2016-09-30 03:44:46 -04:00
|
|
|
@developers_can_push = params.delete(:developers_can_push)
|
2016-10-24 02:02:09 -04:00
|
|
|
@developers_can_merge = params.delete(:developers_can_merge)
|
2016-09-06 01:05:34 -04:00
|
|
|
|
|
|
|
@protected_branch = protected_branch
|
|
|
|
|
|
|
|
protected_branch.transaction do
|
2016-09-13 22:34:29 -04:00
|
|
|
delete_redundant_access_levels
|
2016-09-06 01:05:34 -04:00
|
|
|
|
2016-09-13 22:34:29 -04:00
|
|
|
case @developers_can_push
|
|
|
|
when true
|
2016-09-06 01:05:34 -04:00
|
|
|
params.merge!(push_access_levels_attributes: [{ access_level: Gitlab::Access::DEVELOPER }])
|
2016-09-13 22:34:29 -04:00
|
|
|
when false
|
2016-09-06 01:05:34 -04:00
|
|
|
params.merge!(push_access_levels_attributes: [{ access_level: Gitlab::Access::MASTER }])
|
|
|
|
end
|
|
|
|
|
2016-09-13 22:34:29 -04:00
|
|
|
case @developers_can_merge
|
|
|
|
when true
|
2016-09-06 01:05:34 -04:00
|
|
|
params.merge!(merge_access_levels_attributes: [{ access_level: Gitlab::Access::DEVELOPER }])
|
2016-09-13 22:34:29 -04:00
|
|
|
when false
|
2016-09-06 01:05:34 -04:00
|
|
|
params.merge!(merge_access_levels_attributes: [{ access_level: Gitlab::Access::MASTER }])
|
|
|
|
end
|
|
|
|
|
|
|
|
service = ProtectedBranches::UpdateService.new(@project, @current_user, @params)
|
|
|
|
service.execute(protected_branch)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-09-13 22:34:29 -04:00
|
|
|
def delete_redundant_access_levels
|
2016-09-30 03:44:46 -04:00
|
|
|
unless @developers_can_merge.nil?
|
2016-09-06 01:05:34 -04:00
|
|
|
@protected_branch.merge_access_levels.destroy_all
|
|
|
|
end
|
|
|
|
|
2016-09-30 03:44:46 -04:00
|
|
|
unless @developers_can_push.nil?
|
2016-09-06 01:05:34 -04:00
|
|
|
@protected_branch.push_access_levels.destroy_all
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|