Commit 4fb00f23 authored by Robert Speicher's avatar Robert Speicher

Merge branch 'sdesk-email-show1' into 'master'

Display custom service desk email

See merge request gitlab-org/gitlab!35840
parents fb9026a0 79ec946e
...@@ -38,6 +38,9 @@ class ProjectsController < Projects::ApplicationController ...@@ -38,6 +38,9 @@ class ProjectsController < Projects::ApplicationController
before_action only: [:new, :create] do before_action only: [:new, :create] do
frontend_experimentation_tracking_data(:new_create_project_ui, 'click_tab') 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) 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) push_frontend_feature_flag(:service_desk_custom_address, @project)
end end
......
...@@ -2468,6 +2468,10 @@ class Project < ApplicationRecord ...@@ -2468,6 +2468,10 @@ class Project < ApplicationRecord
alias_method :service_desk_enabled?, :service_desk_enabled alias_method :service_desk_enabled?, :service_desk_enabled
def service_desk_address def service_desk_address
service_desk_custom_address || service_desk_incoming_address
end
def service_desk_incoming_address
return unless service_desk_enabled? return unless service_desk_enabled?
config = Gitlab.config.incoming_email config = Gitlab.config.incoming_email
...@@ -2476,6 +2480,16 @@ class Project < ApplicationRecord ...@@ -2476,6 +2480,16 @@ class Project < ApplicationRecord
config.address&.gsub(wildcard, "#{full_path_slug}-#{id}-issue-") config.address&.gsub(wildcard, "#{full_path_slug}-#{id}-issue-")
end 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 def root_namespace
if namespace.has_parent? if namespace.has_parent?
namespace.root_ancestor namespace.root_ancestor
......
...@@ -9,6 +9,8 @@ module ServiceDeskSettings ...@@ -9,6 +9,8 @@ module ServiceDeskSettings
params.delete(:project_key) params.delete(:project_key)
end end
params[:project_key] = nil if params[:project_key].blank?
if settings.update(params) if settings.update(params)
success success
else else
......
...@@ -10,7 +10,8 @@ ...@@ -10,7 +10,8 @@
- if ::Gitlab::ServiceDesk.supported? - if ::Gitlab::ServiceDesk.supported?
.js-service-desk-setting-root{ data: { endpoint: project_service_desk_path(@project), .js-service-desk-setting-root{ data: { endpoint: project_service_desk_path(@project),
enabled: "#{@project.service_desk_enabled}", 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}", selected_template: "#{@project.service_desk_setting&.issue_template_key}",
outgoing_name: "#{@project.service_desk_setting&.outgoing_name}", outgoing_name: "#{@project.service_desk_setting&.outgoing_name}",
project_key: "#{@project.service_desk_setting&.project_key}", project_key: "#{@project.service_desk_setting&.project_key}",
......
...@@ -17,6 +17,12 @@ module Gitlab ...@@ -17,6 +17,12 @@ module Gitlab
def config def config
Gitlab.config.service_desk_email Gitlab.config.service_desk_email
end end
def address_for_key(key)
return if config.address.blank?
config.address.sub(Gitlab::IncomingEmail::WILDCARD_PLACEHOLDER, key)
end
end end
end end
end end
...@@ -56,4 +56,26 @@ RSpec.describe Gitlab::ServiceDeskEmail do ...@@ -56,4 +56,26 @@ RSpec.describe Gitlab::ServiceDeskEmail do
end end
end 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 end
...@@ -1450,16 +1450,69 @@ RSpec.describe Project do ...@@ -1450,16 +1450,69 @@ RSpec.describe Project do
end end
describe '#service_desk_address' do 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 subject { project.service_desk_address }
allow(Gitlab::ServiceDesk).to receive(:enabled?).and_return(true)
allow(Gitlab.config.incoming_email).to receive(:enabled).and_return(true) shared_examples 'with incoming email address' do
allow(Gitlab.config.incoming_email).to receive(:address).and_return("test+%{key}@mail.com") 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 end
it 'uses project full path as service desk address key' do context 'when service_desk_email is disabled' do
expect(project.service_desk_address).to eq("test+#{project.full_path_slug}-#{project.project_id}-issue-@mail.com") 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
end end
......
...@@ -31,6 +31,17 @@ RSpec.describe ServiceDeskSettings::UpdateService do ...@@ -31,6 +31,17 @@ RSpec.describe ServiceDeskSettings::UpdateService do
end end
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 context 'with invalid params' do
let(:params) { { outgoing_name: 'x' * 256 } } 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