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

update_service.rb « container_expiration_policies « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2f34941d692bc97201c3ab662ad6bb1323d7fa3e (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
# frozen_string_literal: true

module ContainerExpirationPolicies
  class UpdateService < BaseContainerService
    include Gitlab::Utils::StrongMemoize

    ALLOWED_ATTRIBUTES = %i[enabled cadence older_than keep_n name_regex name_regex_keep].freeze

    def execute
      return ServiceResponse.error(message: 'Access Denied', http_status: 403) unless allowed?

      if container_expiration_policy.update(container_expiration_policy_params)
        ServiceResponse.success(payload: { container_expiration_policy: container_expiration_policy })
      else
        ServiceResponse.error(
          message: container_expiration_policy.errors.full_messages.to_sentence || 'Bad request',
          http_status: 400
        )
      end
    end

    private

    def container_expiration_policy
      strong_memoize(:container_expiration_policy) do
        @container.container_expiration_policy || @container.build_container_expiration_policy
      end
    end

    def allowed?
      Ability.allowed?(current_user, :destroy_container_image, @container)
    end

    def container_expiration_policy_params
      @params.slice(*ALLOWED_ATTRIBUTES)
    end
  end
end