2018-08-03 03:15:25 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-08-25 21:42:46 -04:00
|
|
|
module Ci
|
2019-03-28 09:17:42 -04:00
|
|
|
class Trigger < ApplicationRecord
|
2017-09-06 07:13:52 -04:00
|
|
|
extend Gitlab::Ci::Model
|
2018-12-19 08:15:58 -05:00
|
|
|
include Presentable
|
2015-09-10 09:52:52 -04:00
|
|
|
|
2017-03-17 19:06:11 -04:00
|
|
|
belongs_to :project
|
2017-02-15 13:36:31 -05:00
|
|
|
belongs_to :owner, class_name: "User"
|
|
|
|
|
2017-04-07 09:56:08 -04:00
|
|
|
has_many :trigger_requests
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2017-02-15 13:36:31 -05:00
|
|
|
validates :token, presence: true, uniqueness: true
|
2020-01-14 07:07:41 -05:00
|
|
|
validates :owner, presence: true
|
2015-08-25 21:42:46 -04:00
|
|
|
|
|
|
|
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-13 06:16:27 -05:00
|
|
|
def last_used
|
|
|
|
last_trigger_request.try(:created_at)
|
|
|
|
end
|
|
|
|
|
2015-08-25 21:42:46 -04:00
|
|
|
def short_token
|
2018-12-19 09:51:02 -05:00
|
|
|
token[0...4] if token.present?
|
2017-02-15 13:36:31 -05:00
|
|
|
end
|
|
|
|
|
2017-03-07 08:02:56 -05:00
|
|
|
def can_access_project?
|
2020-01-14 07:07:41 -05:00
|
|
|
Ability.allowed?(self.owner, :create_build, project)
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-10-02 20:05:59 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Ci::Trigger.prepend_mod_with('Ci::Trigger')
|