summaryrefslogtreecommitdiff
path: root/app/models/ci/pending_build.rb
blob: 0663052f51d4df5065b988e0fde322d047426e02 (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
51
52
53
54
55
56
57
58
# frozen_string_literal: true

module Ci
  class PendingBuild < ApplicationRecord
    extend Gitlab::Ci::Model

    belongs_to :project
    belongs_to :build, class_name: 'Ci::Build'

    scope :ref_protected, -> { where(protected: true) }
    scope :queued_before, ->(time) { where(arel_table[:created_at].lt(time)) }

    def self.upsert_from_build!(build)
      entry = self.new(args_from_build(build))

      entry.validate!

      self.upsert(entry.attributes.compact, returning: %w[build_id], unique_by: :build_id)
    end

    def self.args_from_build(build)
      args = {
        build: build,
        project: build.project,
        protected: build.protected?
      }

      if Feature.enabled?(:ci_pending_builds_maintain_shared_runners_data, type: :development, default_enabled: :yaml)
        args.merge(instance_runners_enabled: shareable?(build))
      else
        args
      end
    end
    private_class_method :args_from_build

    def self.shareable?(build)
      shared_runner_enabled?(build) &&
        builds_access_level?(build) &&
        project_not_removed?(build)
    end
    private_class_method :shareable?

    def self.shared_runner_enabled?(build)
      build.project.shared_runners.exists?
    end
    private_class_method :shared_runner_enabled?

    def self.project_not_removed?(build)
      !build.project.pending_delete?
    end
    private_class_method :project_not_removed?

    def self.builds_access_level?(build)
      build.project.project_feature.builds_access_level.nil? || build.project.project_feature.builds_access_level > 0
    end
    private_class_method :builds_access_level?
  end
end