summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/pipeline/quota/deployments.rb
blob: ed32d0d3d492292edb96733b24c690a10dc335ce (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
40
41
42
43
44
45
46
47
48
49
50
# frozen_string_literal: true

module Gitlab
  module Ci
    module Pipeline
      module Quota
        class Deployments < ::Gitlab::Ci::Limit
          include ::Gitlab::Utils::StrongMemoize
          include ActionView::Helpers::TextHelper

          def initialize(namespace, pipeline, command)
            @namespace = namespace
            @pipeline = pipeline
            @command = command
          end

          def enabled?
            limit > 0
          end

          def exceeded?
            return false unless enabled?

            pipeline_deployment_count > limit
          end

          def message
            return unless exceeded?

            "Pipeline has too many deployments! Requested #{pipeline_deployment_count}, but the limit is #{limit}."
          end

          private

          def pipeline_deployment_count
            strong_memoize(:pipeline_deployment_count) do
              @command.pipeline_seed.deployments_count
            end
          end

          def limit
            strong_memoize(:limit) do
              @namespace.actual_limits.ci_pipeline_deployments
            end
          end
        end
      end
    end
  end
end