2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
module Ci
|
2019-07-07 11:18:12 +05:30
|
|
|
class Trigger < ApplicationRecord
|
2018-03-17 18:26:18 +05:30
|
|
|
extend Gitlab::Ci::Model
|
2019-02-02 18:00:53 +05:30
|
|
|
include Presentable
|
2015-09-25 12:07:36 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
belongs_to :project
|
|
|
|
belongs_to :owner, class_name: "User"
|
2015-09-25 12:07:36 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
has_many :trigger_requests
|
|
|
|
|
|
|
|
validates :token, presence: true, uniqueness: true
|
2019-09-30 21:07:59 +05:30
|
|
|
validates :owner, presence: true, unless: :supports_legacy_tokens?
|
2015-09-25 12:07:36 +05:30
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-01-19 16:12:03 +05:30
|
|
|
def last_used
|
|
|
|
last_trigger_request.try(:created_at)
|
|
|
|
end
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
def short_token
|
2019-02-02 18:00:53 +05:30
|
|
|
token[0...4] if token.present?
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def legacy?
|
|
|
|
self.owner_id.blank?
|
|
|
|
end
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
def supports_legacy_tokens?
|
|
|
|
Feature.enabled?(:use_legacy_pipeline_triggers, project)
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def can_access_project?
|
2019-09-30 21:07:59 +05:30
|
|
|
supports_legacy_tokens? && legacy? ||
|
|
|
|
Ability.allowed?(self.owner, :create_build, project)
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|