debian-mirror-gitlab/app/models/service_desk_setting.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

112 lines
2.9 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
class ServiceDeskSetting < ApplicationRecord
include Gitlab::Utils::StrongMemoize
2023-06-20 00:43:36 +05:30
include IgnorableColumns
2020-07-28 23:09:34 +05:30
2023-05-27 22:25:52 +05:30
CUSTOM_EMAIL_VERIFICATION_SUBADDRESS = '+verify'
2023-06-20 00:43:36 +05:30
ignore_columns %i[
custom_email_smtp_address
custom_email_smtp_port
custom_email_smtp_username
encrypted_custom_email_smtp_password
encrypted_custom_email_smtp_password_iv
], remove_with: '16.1', remove_after: '2023-05-22'
2023-04-23 21:23:45 +05:30
attribute :custom_email_enabled, default: false
2020-07-28 23:09:34 +05:30
belongs_to :project
2023-05-27 22:25:52 +05:30
2020-07-28 23:09:34 +05:30
validates :project_id, presence: true
validate :valid_issue_template
2021-09-04 01:27:46 +05:30
validate :valid_project_key
2020-07-28 23:09:34 +05:30
validates :outgoing_name, length: { maximum: 255 }, allow_blank: true
2021-09-30 23:02:18 +05:30
validates :project_key,
2023-06-20 00:43:36 +05:30
length: { maximum: 255 },
allow_blank: true,
format: { with: /\A[a-z0-9_]+\z/, message: -> (setting, data) { _("can contain only lowercase letters, digits, and '_'.") } }
2020-07-28 23:09:34 +05:30
2023-04-23 21:23:45 +05:30
validates :custom_email,
2023-06-20 00:43:36 +05:30
length: { maximum: 255 },
uniqueness: true,
allow_nil: true,
format: /\A[\w\-._]+@[\w\-.]+\.{1}[a-zA-Z]{2,}\z/
validates :custom_email_credential,
presence: true,
if: :needs_custom_email_credentials?
2023-04-23 21:23:45 +05:30
validates :custom_email,
2023-06-20 00:43:36 +05:30
presence: true,
devise_email: true,
if: :needs_custom_email_credentials?
2023-04-23 21:23:45 +05:30
2021-09-04 01:27:46 +05:30
scope :with_project_key, ->(key) { where(project_key: key) }
2023-06-20 00:43:36 +05:30
def custom_email_credential
project&.service_desk_custom_email_credential
2023-05-27 22:25:52 +05:30
end
2023-06-20 00:43:36 +05:30
def custom_email_verification
project&.service_desk_custom_email_verification
2023-04-23 21:23:45 +05:30
end
2023-05-27 22:25:52 +05:30
def custom_email_address_for_verification
return unless custom_email.present?
custom_email.sub("@", "#{CUSTOM_EMAIL_VERIFICATION_SUBADDRESS}@")
end
2020-07-28 23:09:34 +05:30
def issue_template_content
strong_memoize(:issue_template_content) do
next unless issue_template_key.present?
2021-11-11 11:23:49 +05:30
TemplateFinder.new(
:issues, project,
name: issue_template_key,
source_template_project: source_template_project
).execute.content
2020-07-28 23:09:34 +05:30
rescue ::Gitlab::Template::Finders::RepoTemplateFinder::FileNotFoundError
end
end
def issue_template_missing?
issue_template_key.present? && !issue_template_content.present?
end
def valid_issue_template
if issue_template_missing?
errors.add(:issue_template_key, 'is empty or does not exist')
end
end
2021-09-04 01:27:46 +05:30
def valid_project_key
if projects_with_same_slug_and_key_exists?
errors.add(:project_key, 'already in use for another service desk address.')
end
end
private
2021-11-11 11:23:49 +05:30
def source_template_project
nil
end
2021-09-04 01:27:46 +05:30
def projects_with_same_slug_and_key_exists?
return false unless project_key
2023-03-04 22:38:38 +05:30
settings = self.class.with_project_key(project_key).where.not(project_id: project_id).preload(:project)
2021-09-04 01:27:46 +05:30
project_slug = self.project.full_path_slug
settings.any? do |setting|
setting.project.full_path_slug == project_slug
end
end
2023-05-27 22:25:52 +05:30
2023-06-20 00:43:36 +05:30
def needs_custom_email_credentials?
2023-05-27 22:25:52 +05:30
custom_email_enabled? || custom_email_verification.present?
end
2020-07-28 23:09:34 +05:30
end
2021-11-11 11:23:49 +05:30
ServiceDeskSetting.prepend_mod