Commit 79ec946e authored by Jan Provaznik's avatar Jan Provaznik

Display custom service desk email

When a custom service desk email is set, prefer this address
on the service desk issues page.
parent 82678ecc
......@@ -38,6 +38,9 @@ class ProjectsController < Projects::ApplicationController
before_action only: [:new, :create] do
frontend_experimentation_tracking_data(:new_create_project_ui, 'click_tab')
push_frontend_feature_flag(:new_create_project_ui) if experiment_enabled?(:new_create_project_ui)
end
before_action only: [:edit] do
push_frontend_feature_flag(:service_desk_custom_address, @project)
end
......
......@@ -2468,6 +2468,10 @@ class Project < ApplicationRecord
alias_method :service_desk_enabled?, :service_desk_enabled
def service_desk_address
service_desk_custom_address || service_desk_incoming_address
end
def service_desk_incoming_address
return unless service_desk_enabled?
config = Gitlab.config.incoming_email
......@@ -2476,6 +2480,16 @@ class Project < ApplicationRecord
config.address&.gsub(wildcard, "#{full_path_slug}-#{id}-issue-")
end
def service_desk_custom_address
return unless ::Gitlab::ServiceDeskEmail.enabled?
return unless ::Feature.enabled?(:service_desk_custom_address, self)
key = service_desk_setting&.project_key
return unless key.present?
::Gitlab::ServiceDeskEmail.address_for_key("#{full_path_slug}-#{key}")
end
def root_namespace
if namespace.has_parent?
namespace.root_ancestor
......
......@@ -9,6 +9,8 @@ module ServiceDeskSettings
params.delete(:project_key)
end
params[:project_key] = nil if params[:project_key].blank?
if settings.update(params)
success
else
......
......@@ -10,7 +10,8 @@
- if ::Gitlab::ServiceDesk.supported?
.js-service-desk-setting-root{ data: { endpoint: project_service_desk_path(@project),
enabled: "#{@project.service_desk_enabled}",
incoming_email: (@project.service_desk_address if @project.service_desk_enabled),
incoming_email: (@project.service_desk_incoming_address if @project.service_desk_enabled),
custom_email: (@project.service_desk_custom_address if @project.service_desk_enabled),
selected_template: "#{@project.service_desk_setting&.issue_template_key}",
outgoing_name: "#{@project.service_desk_setting&.outgoing_name}",
project_key: "#{@project.service_desk_setting&.project_key}",
......
......@@ -17,6 +17,12 @@ module Gitlab
def config
Gitlab.config.service_desk_email
end
def address_for_key(key)
return if config.address.blank?
config.address.sub(Gitlab::IncomingEmail::WILDCARD_PLACEHOLDER, key)
end
end
end
end
......@@ -56,4 +56,26 @@ RSpec.describe Gitlab::ServiceDeskEmail do
end
end
end
describe '.address_for_key' do
context 'when service desk address is set' do
before do
stub_service_desk_email_setting(address: 'address+%{key}@example.com')
end
it 'returns address' do
expect(described_class.address_for_key('foo')).to eq('address+foo@example.com')
end
end
context 'when service desk address is not set' do
before do
stub_service_desk_email_setting(address: nil)
end
it 'returns nil' do
expect(described_class.key_from_address('foo')).to be_nil
end
end
end
end
......@@ -1449,16 +1449,69 @@ RSpec.describe Project do
end
describe '#service_desk_address' do
let_it_be(:project) { create(:project, service_desk_enabled: true) }
let_it_be(:project, reload: true) { create(:project, service_desk_enabled: true) }
before do
allow(Gitlab::ServiceDesk).to receive(:enabled?).and_return(true)
allow(Gitlab.config.incoming_email).to receive(:enabled).and_return(true)
allow(Gitlab.config.incoming_email).to receive(:address).and_return("test+%{key}@mail.com")
subject { project.service_desk_address }
shared_examples 'with incoming email address' do
context 'when incoming email is enabled' do
before do
config = double(enabled: true, address: 'test+%{key}@mail.com')
allow(::Gitlab.config).to receive(:incoming_email).and_return(config)
end
it 'uses project full path as service desk address key' do
expect(project.service_desk_address).to eq("test+#{project.full_path_slug}-#{project.project_id}-issue-@mail.com")
end
end
context 'when incoming email is disabled' do
before do
config = double(enabled: false)
allow(::Gitlab.config).to receive(:incoming_email).and_return(config)
end
it 'uses project full path as service desk address key' do
expect(project.service_desk_address).to be_nil
end
end
end
it 'uses project full path as service desk address key' do
expect(project.service_desk_address).to eq("test+#{project.full_path_slug}-#{project.project_id}-issue-@mail.com")
context 'when service_desk_email is disabled' do
before do
allow(::Gitlab::ServiceDeskEmail).to receive(:enabled?).and_return(false)
end
it_behaves_like 'with incoming email address'
end
context 'when service_desk_email is enabled' do
before do
config = double(enabled: true, address: 'foo+%{key}@bar.com')
allow(::Gitlab::ServiceDeskEmail).to receive(:config).and_return(config)
end
context 'when service_desk_custom_address flag is enabled' do
before do
stub_feature_flags(service_desk_custom_address: true)
end
it 'returns custom address when project_key is set' do
create(:service_desk_setting, project: project, project_key: 'key1')
expect(subject).to eq("foo+#{project.full_path_slug}-key1@bar.com")
end
it_behaves_like 'with incoming email address'
end
context 'when service_desk_custom_address flag is disabled' do
before do
stub_feature_flags(service_desk_custom_address: false)
end
it_behaves_like 'with incoming email address'
end
end
end
......
......@@ -31,6 +31,17 @@ RSpec.describe ServiceDeskSettings::UpdateService do
end
end
context 'when project_key is an empty string' do
let(:params) { { project_key: '' } }
it 'sets nil project_key' do
result = described_class.new(settings.project, user, params).execute
expect(result[:status]).to eq :success
expect(settings.reload.project_key).to be_nil
end
end
context 'with invalid params' do
let(:params) { { outgoing_name: 'x' * 256 } }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment