debian-mirror-gitlab/app/services/protected_branches/legacy_api_update_service.rb

53 lines
1.9 KiB
Ruby
Raw Normal View History

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
2019-07-07 11:18:12 +05:30
attr_reader :protected_branch, :developers_can_push, :developers_can_merge
2016-11-03 12:29:30 +05:30
def execute(protected_branch)
2019-07-07 11:18:12 +05:30
@protected_branch = protected_branch
2016-11-03 12:29:30 +05:30
@developers_can_push = params.delete(:developers_can_push)
@developers_can_merge = params.delete(:developers_can_merge)
protected_branch.transaction do
delete_redundant_access_levels
2019-07-07 11:18:12 +05:30
case developers_can_push
2016-11-03 12:29:30 +05:30
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
2019-07-07 11:18:12 +05:30
case developers_can_merge
2016-11-03 12:29:30 +05:30
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
2019-07-07 11:18:12 +05:30
service = ProtectedBranches::UpdateService.new(project, current_user, params)
2016-11-03 12:29:30 +05:30
service.execute(protected_branch)
end
end
private
def delete_redundant_access_levels
2019-07-07 11:18:12 +05:30
unless developers_can_merge.nil?
2020-06-23 00:09:42 +05:30
protected_branch.merge_access_levels.destroy_all # rubocop: disable Cop/DestroyAll
2016-11-03 12:29:30 +05:30
end
2019-07-07 11:18:12 +05:30
unless developers_can_push.nil?
2020-06-23 00:09:42 +05:30
protected_branch.push_access_levels.destroy_all # rubocop: disable Cop/DestroyAll
2016-11-03 12:29:30 +05:30
end
end
end
end
2019-12-04 20:38:33 +05:30
ProtectedBranches::LegacyApiUpdateService.prepend_if_ee('EE::ProtectedBranches::LegacyApiUpdateService')