2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
# The branches#protect API still uses the `developers_can_push` and `developers_can_merge`
|
2016-11-03 12:29:30 +05:30
|
|
|
# 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
|
2018-03-17 18:26:18 +05:30
|
|
|
class LegacyApiUpdateService < BaseService
|
2016-11-03 12:29:30 +05:30
|
|
|
def execute(protected_branch)
|
|
|
|
@developers_can_push = params.delete(:developers_can_push)
|
|
|
|
@developers_can_merge = params.delete(:developers_can_merge)
|
|
|
|
|
|
|
|
@protected_branch = protected_branch
|
|
|
|
|
|
|
|
protected_branch.transaction do
|
|
|
|
delete_redundant_access_levels
|
|
|
|
|
|
|
|
case @developers_can_push
|
|
|
|
when true
|
2017-08-17 22:00:37 +05:30
|
|
|
params[:push_access_levels_attributes] = [{ access_level: Gitlab::Access::DEVELOPER }]
|
2016-11-03 12:29:30 +05:30
|
|
|
when false
|
2018-11-18 11:00:15 +05:30
|
|
|
params[:push_access_levels_attributes] = [{ access_level: Gitlab::Access::MAINTAINER }]
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
case @developers_can_merge
|
|
|
|
when true
|
2017-08-17 22:00:37 +05:30
|
|
|
params[:merge_access_levels_attributes] = [{ access_level: Gitlab::Access::DEVELOPER }]
|
2016-11-03 12:29:30 +05:30
|
|
|
when false
|
2018-11-18 11:00:15 +05:30
|
|
|
params[:merge_access_levels_attributes] = [{ access_level: Gitlab::Access::MAINTAINER }]
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
service = ProtectedBranches::UpdateService.new(@project, @current_user, @params)
|
|
|
|
service.execute(protected_branch)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def delete_redundant_access_levels
|
|
|
|
unless @developers_can_merge.nil?
|
2018-11-20 20:47:30 +05:30
|
|
|
@protected_branch.merge_access_levels.destroy_all # rubocop: disable DestroyAll
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
unless @developers_can_push.nil?
|
2018-11-20 20:47:30 +05:30
|
|
|
@protected_branch.push_access_levels.destroy_all # rubocop: disable DestroyAll
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|