summaryrefslogtreecommitdiff
path: root/app/models/ci/trigger.rb
blob: 8792c5cf98b34a4f78c8fea91241a549f47b9037 (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
# frozen_string_literal: true

module Ci
  class Trigger < ApplicationRecord
    extend Gitlab::Ci::Model
    include Presentable

    belongs_to :project
    belongs_to :owner, class_name: "User"

    has_many :trigger_requests

    validates :token, presence: true, uniqueness: true
    validates :owner, presence: true, unless: :supports_legacy_tokens?

    before_validation :set_default_values

    def set_default_values
      self.token = SecureRandom.hex(15) if self.token.blank?
    end

    def last_trigger_request
      trigger_requests.last
    end

    def last_used
      last_trigger_request.try(:created_at)
    end

    def short_token
      token[0...4] if token.present?
    end

    def legacy?
      self.owner_id.blank?
    end

    def supports_legacy_tokens?
      Feature.enabled?(:use_legacy_pipeline_triggers, project)
    end

    def can_access_project?
      supports_legacy_tokens? && legacy? ||
        Ability.allowed?(self.owner, :create_build, project)
    end
  end
end