2018-07-18 16:03:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-11-22 15:16:08 +00:00
|
|
|
# The branches#protect API still uses the `developers_can_push` and `developers_can_merge`
|
2016-09-06 05:05:34 +00:00
|
|
|
# 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
|
2017-11-22 15:16:08 +00:00
|
|
|
class LegacyApiUpdateService < BaseService
|
2019-02-06 12:41:17 +00:00
|
|
|
attr_reader :protected_branch, :developers_can_push, :developers_can_merge
|
|
|
|
|
2016-10-24 06:02:09 +00:00
|
|
|
def execute(protected_branch)
|
2019-02-06 12:41:17 +00:00
|
|
|
@protected_branch = protected_branch
|
2016-09-30 07:44:46 +00:00
|
|
|
@developers_can_push = params.delete(:developers_can_push)
|
2016-10-24 06:02:09 +00:00
|
|
|
@developers_can_merge = params.delete(:developers_can_merge)
|
2016-09-06 05:05:34 +00:00
|
|
|
|
|
|
|
protected_branch.transaction do
|
2016-09-14 02:34:29 +00:00
|
|
|
delete_redundant_access_levels
|
2016-09-06 05:05:34 +00:00
|
|
|
|
2019-02-06 12:41:17 +00:00
|
|
|
case developers_can_push
|
2016-09-14 02:34:29 +00:00
|
|
|
when true
|
2017-02-21 23:50:22 +00:00
|
|
|
params[:push_access_levels_attributes] = [{ access_level: Gitlab::Access::DEVELOPER }]
|
2016-09-14 02:34:29 +00:00
|
|
|
when false
|
2018-07-11 14:36:08 +00:00
|
|
|
params[:push_access_levels_attributes] = [{ access_level: Gitlab::Access::MAINTAINER }]
|
2016-09-06 05:05:34 +00:00
|
|
|
end
|
|
|
|
|
2019-02-06 12:41:17 +00:00
|
|
|
case developers_can_merge
|
2016-09-14 02:34:29 +00:00
|
|
|
when true
|
2017-02-21 23:50:22 +00:00
|
|
|
params[:merge_access_levels_attributes] = [{ access_level: Gitlab::Access::DEVELOPER }]
|
2016-09-14 02:34:29 +00:00
|
|
|
when false
|
2018-07-11 14:36:08 +00:00
|
|
|
params[:merge_access_levels_attributes] = [{ access_level: Gitlab::Access::MAINTAINER }]
|
2016-09-06 05:05:34 +00:00
|
|
|
end
|
|
|
|
|
2019-02-06 12:41:17 +00:00
|
|
|
service = ProtectedBranches::UpdateService.new(project, current_user, params)
|
2016-09-06 05:05:34 +00:00
|
|
|
service.execute(protected_branch)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-09-14 02:34:29 +00:00
|
|
|
def delete_redundant_access_levels
|
2019-02-06 12:41:17 +00:00
|
|
|
unless developers_can_merge.nil?
|
|
|
|
protected_branch.merge_access_levels.destroy_all # rubocop: disable DestroyAll
|
2016-09-06 05:05:34 +00:00
|
|
|
end
|
|
|
|
|
2019-02-06 12:41:17 +00:00
|
|
|
unless developers_can_push.nil?
|
|
|
|
protected_branch.push_access_levels.destroy_all # rubocop: disable DestroyAll
|
2016-09-06 05:05:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
|
|
|
ProtectedBranches::LegacyApiUpdateService.prepend_if_ee('EE::ProtectedBranches::LegacyApiUpdateService')
|