2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
module Ci
|
2021-10-27 15:23:28 +05:30
|
|
|
class Trigger < Ci::ApplicationRecord
|
2019-02-02 18:00:53 +05:30
|
|
|
include Presentable
|
2021-12-11 22:18:48 +05:30
|
|
|
include Limitable
|
2022-08-13 15:12:31 +05:30
|
|
|
include IgnorableColumns
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
TRIGGER_TOKEN_PREFIX = 'glptt-'
|
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
ignore_column :ref, remove_with: '16.1', remove_after: '2023-05-22'
|
2021-12-11 22:18:48 +05:30
|
|
|
|
|
|
|
self.limit_name = 'pipeline_triggers'
|
|
|
|
self.limit_scope = :project
|
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
|
2020-03-13 15:44:24 +05:30
|
|
|
validates :owner, presence: true
|
2015-09-25 12:07:36 +05:30
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
attr_encrypted :encrypted_token_tmp,
|
|
|
|
attribute: :encrypted_token,
|
|
|
|
mode: :per_attribute_iv,
|
|
|
|
algorithm: 'aes-256-gcm',
|
|
|
|
key: Settings.attr_encrypted_db_key_base_32,
|
2023-06-20 00:43:36 +05:30
|
|
|
encode: false
|
2023-04-23 21:23:45 +05:30
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
before_validation :set_default_values
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
before_save :copy_token_to_encrypted_token
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
def set_default_values
|
2022-10-11 01:57:18 +05:30
|
|
|
self.token = "#{TRIGGER_TOKEN_PREFIX}#{SecureRandom.hex(20)}" if self.token.blank?
|
2015-09-25 12:07:36 +05:30
|
|
|
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
|
2022-10-11 01:57:18 +05:30
|
|
|
token.delete_prefix(TRIGGER_TOKEN_PREFIX)[0...4] if token.present?
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def can_access_project?
|
2020-03-13 15:44:24 +05:30
|
|
|
Ability.allowed?(self.owner, :create_build, project)
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
2023-04-23 21:23:45 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def copy_token_to_encrypted_token
|
|
|
|
self.encrypted_token_tmp = token
|
|
|
|
end
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
Ci::Trigger.prepend_mod_with('Ci::Trigger')
|