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