diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-06-16 21:25:58 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-06-16 21:25:58 +0300 |
commit | a5f4bba440d7f9ea47046a0a561d49adf0a1e6d4 (patch) | |
tree | fb69158581673816a8cd895f9d352dcb3c678b1e /app/models/service_desk_setting.rb | |
parent | d16b2e8639e99961de6ddc93909f3bb5c1445ba1 (diff) |
Add latest changes from gitlab-org/gitlab@14-0-stable-eev14.0.0-rc42
Diffstat (limited to 'app/models/service_desk_setting.rb')
-rw-r--r-- | app/models/service_desk_setting.rb | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/app/models/service_desk_setting.rb b/app/models/service_desk_setting.rb index bcc17d32272..c5203354b9d 100644 --- a/app/models/service_desk_setting.rb +++ b/app/models/service_desk_setting.rb @@ -6,9 +6,12 @@ class ServiceDeskSetting < ApplicationRecord belongs_to :project validates :project_id, presence: true validate :valid_issue_template + validate :valid_project_key validates :outgoing_name, length: { maximum: 255 }, allow_blank: true validates :project_key, length: { maximum: 255 }, allow_blank: true, format: { with: /\A[a-z0-9_]+\z/ } + scope :with_project_key, ->(key) { where(project_key: key) } + def issue_template_content strong_memoize(:issue_template_content) do next unless issue_template_key.present? @@ -27,4 +30,23 @@ class ServiceDeskSetting < ApplicationRecord errors.add(:issue_template_key, 'is empty or does not exist') end end + + 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 + + def projects_with_same_slug_and_key_exists? + return false unless project_key + + settings = self.class.with_project_key(project_key).preload(:project) + project_slug = self.project.full_path_slug + + settings.any? do |setting| + setting.project.full_path_slug == project_slug + end + end end |