Welcome to mirror list, hosted at ThFree Co, Russian Federation.

create_rule_service.rb « protection « packages « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f0bd7f3d90051de853e7e4520c683501b15e5920 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# frozen_string_literal: true

module Packages
  module Protection
    class CreateRuleService < BaseService
      ALLOWED_ATTRIBUTES = %i[
        package_name_pattern
        package_type
        push_protected_up_to_access_level
      ].freeze

      def execute
        unless can?(current_user, :admin_package, project)
          error_message = _('Unauthorized to create a package protection rule')
          return service_response_error(message: error_message)
        end

        package_protection_rule = project.package_protection_rules.create(params.slice(*ALLOWED_ATTRIBUTES))

        unless package_protection_rule.persisted?
          return service_response_error(message: package_protection_rule.errors.full_messages)
        end

        ServiceResponse.success(payload: { package_protection_rule: package_protection_rule })
      rescue StandardError => e
        service_response_error(message: e.message)
      end

      private

      def service_response_error(message:)
        ServiceResponse.error(
          message: message,
          payload: { package_protection_rule: nil }
        )
      end
    end
  end
end