Commit a538eac7 authored by Mikołaj Wawrzyniak's avatar Mikołaj Wawrzyniak

Merge branch '333916-service-desk-no-suffix' into 'master'

Allow custom support email without configuring custom project suffix

See merge request gitlab-org/gitlab!71940
parents ff79b67f 6bcf1b17
...@@ -2582,18 +2582,21 @@ class Project < ApplicationRecord ...@@ -2582,18 +2582,21 @@ class Project < ApplicationRecord
config = Gitlab.config.incoming_email config = Gitlab.config.incoming_email
wildcard = Gitlab::IncomingEmail::WILDCARD_PLACEHOLDER wildcard = Gitlab::IncomingEmail::WILDCARD_PLACEHOLDER
config.address&.gsub(wildcard, "#{full_path_slug}-#{id}-issue-") config.address&.gsub(wildcard, "#{full_path_slug}-#{default_service_desk_suffix}")
end end
def service_desk_custom_address def service_desk_custom_address
return unless Gitlab::ServiceDeskEmail.enabled? return unless Gitlab::ServiceDeskEmail.enabled?
key = service_desk_setting&.project_key key = service_desk_setting&.project_key || default_service_desk_suffix
return unless key.present?
Gitlab::ServiceDeskEmail.address_for_key("#{full_path_slug}-#{key}") Gitlab::ServiceDeskEmail.address_for_key("#{full_path_slug}-#{key}")
end end
def default_service_desk_suffix
"#{id}-issue-"
end
def root_namespace def root_namespace
if namespace.has_parent? if namespace.has_parent?
namespace.root_ancestor namespace.root_ancestor
......
...@@ -166,13 +166,13 @@ To edit the custom email display name: ...@@ -166,13 +166,13 @@ To edit the custom email display name:
> - [Feature flag removed](https://gitlab.com/gitlab-org/gitlab/-/issues/284656) in GitLab 13.8. > - [Feature flag removed](https://gitlab.com/gitlab-org/gitlab/-/issues/284656) in GitLab 13.8.
It is possible to customize the email address used by Service Desk. To do this, you must configure It is possible to customize the email address used by Service Desk. To do this, you must configure
both a [custom mailbox](#configuring-a-custom-mailbox) and a a [custom mailbox](#configuring-a-custom-mailbox). If you want you can also configure a
[custom suffix](#configuring-a-custom-email-address-suffix). [custom suffix](#configuring-a-custom-email-address-suffix).
#### Configuring a custom mailbox #### Configuring a custom mailbox
NOTE: NOTE:
On GitLab.com a custom mailbox is already configured with `contact-project+%{key}@incoming.gitlab.com` as the email address, so you only have to configure the On GitLab.com a custom mailbox is already configured with `contact-project+%{key}@incoming.gitlab.com` as the email address, you can still configure the
[custom suffix](#configuring-a-custom-email-address-suffix) in project settings. [custom suffix](#configuring-a-custom-email-address-suffix) in project settings.
Using the `service_desk_email` configuration, you can customize the mailbox Using the `service_desk_email` configuration, you can customize the mailbox
...@@ -271,6 +271,8 @@ For example, suppose the `mygroup/myproject` project Service Desk settings has t ...@@ -271,6 +271,8 @@ For example, suppose the `mygroup/myproject` project Service Desk settings has t
The Service Desk email address for this project is: `contact+mygroup-myproject-support@example.com`. The Service Desk email address for this project is: `contact+mygroup-myproject-support@example.com`.
The [incoming email](../../administration/incoming_email.md) address still works. The [incoming email](../../administration/incoming_email.md) address still works.
If you don't configure the custom suffix, the default project identification will be used for identifying the project. You can see that email address in the project settings.
## Using Service Desk ## Using Service Desk
You can use Service Desk to [create an issue](#as-an-end-user-issue-creator) or [respond to one](#as-a-responder-to-the-issue). You can use Service Desk to [create an issue](#as-an-end-user-issue-creator) or [respond to one](#as-a-responder-to-the-issue).
......
...@@ -15,16 +15,14 @@ module Gitlab ...@@ -15,16 +15,14 @@ module Gitlab
PROJECT_KEY_PATTERN = /\A(?<slug>.+)-(?<key>[a-z0-9_]+)\z/.freeze PROJECT_KEY_PATTERN = /\A(?<slug>.+)-(?<key>[a-z0-9_]+)\z/.freeze
def initialize(mail, mail_key, service_desk_key: nil) def initialize(mail, mail_key, service_desk_key: nil)
super(mail, mail_key) if service_desk_key
mail_key ||= service_desk_key
if service_desk_key.present?
@service_desk_key = service_desk_key @service_desk_key = service_desk_key
elsif !mail_key&.include?('/') && (matched = HANDLER_REGEX.match(mail_key.to_s))
@project_slug = matched[:project_slug]
@project_id = matched[:project_id]&.to_i
elsif matched = HANDLER_REGEX_LEGACY.match(mail_key.to_s)
@project_path = matched[:project_path]
end end
super(mail, mail_key)
match_project_slug || match_legacy_project_slug
end end
def can_handle? def can_handle?
...@@ -42,15 +40,29 @@ module Gitlab ...@@ -42,15 +40,29 @@ module Gitlab
end end
end end
def match_project_slug
return if mail_key&.include?('/')
return unless matched = HANDLER_REGEX.match(mail_key.to_s)
@project_slug = matched[:project_slug]
@project_id = matched[:project_id]&.to_i
end
def match_legacy_project_slug
return unless matched = HANDLER_REGEX_LEGACY.match(mail_key.to_s)
@project_path = matched[:project_path]
end
def metrics_event def metrics_event
:receive_email_service_desk :receive_email_service_desk
end end
def project def project
strong_memoize(:project) do strong_memoize(:project) do
@project = service_desk_key ? project_from_key : super project_record = super
@project = nil unless @project&.service_desk_enabled? project_record ||= project_from_key if service_desk_key
@project project_record&.service_desk_enabled? ? project_record : nil
end end
end end
......
...@@ -54,7 +54,7 @@ RSpec.describe 'Service Desk Setting', :js, :clean_gitlab_redis_cache do ...@@ -54,7 +54,7 @@ RSpec.describe 'Service Desk Setting', :js, :clean_gitlab_redis_cache do
wait_for_requests wait_for_requests
project.reload project.reload
expect(find('[data-testid="incoming-email"]').value).to eq(project.service_desk_incoming_address) expect(find('[data-testid="incoming-email"]').value).to eq(project.service_desk_custom_address)
page.within '#js-service-desk' do page.within '#js-service-desk' do
fill_in('service-desk-project-suffix', with: 'foo') fill_in('service-desk-project-suffix', with: 'foo')
......
Return-Path: <jake@adventuretime.ooo>
Received: from iceking.adventuretime.ooo ([unix socket]) by iceking (Cyrus v2.2.13-Debian-2.2.13-19+squeeze3) with LMTPA; Thu, 13 Jun 2013 17:03:50 -0400
Received: by mail-ie0-f180.google.com with SMTP id f4so21977375iea.25 for <support+project_slug-project_key@example.com>; Thu, 13 Jun 2013 14:03:48 -0700
Received: by 10.0.0.1 with HTTP; Thu, 13 Jun 2013 14:03:48 -0700
Date: Thu, 13 Jun 2013 17:03:48 -0400
From: Jake the Dog <jake@adventuretime.ooo>
To: support+email-test-project_id-issue-@example.com
Message-ID: <CADkmRc+rNGAGGbV2iE5p918UVy4UyJqVcXRO2=otppgzduJSg@mail.gmail.com>
Subject: The message subject! @all
Mime-Version: 1.0
Content-Type: text/plain;
charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
X-Sieve: CMU Sieve 2.2
X-Received: by 10.0.0.1 with SMTP id n7mr11234144ipb.85.1371157428600; Thu,
13 Jun 2013 14:03:48 -0700 (PDT)
X-Scanned-By: MIMEDefang 2.69 on IPv6:2001:470:1d:165::1
Service desk stuff!
```
a = b
```
/label ~label1
/assign @user1
/close
...@@ -196,16 +196,18 @@ RSpec.describe Gitlab::Email::Handler::ServiceDeskHandler do ...@@ -196,16 +196,18 @@ RSpec.describe Gitlab::Email::Handler::ServiceDeskHandler do
end end
end end
context 'when using service desk key' do context 'when using custom service desk address' do
let_it_be(:service_desk_key) { 'mykey' }
let(:email_raw) { service_desk_fixture('emails/service_desk_custom_address.eml') }
let(:receiver) { Gitlab::Email::ServiceDeskReceiver.new(email_raw) } let(:receiver) { Gitlab::Email::ServiceDeskReceiver.new(email_raw) }
before do before do
stub_service_desk_email_setting(enabled: true, address: 'support+%{key}@example.com') stub_service_desk_email_setting(enabled: true, address: 'support+%{key}@example.com')
end end
context 'when using project key' do
let_it_be(:service_desk_key) { 'mykey' }
let(:email_raw) { service_desk_fixture('emails/service_desk_custom_address.eml') }
before_all do before_all do
create(:service_desk_setting, project: project, project_key: service_desk_key) create(:service_desk_setting, project: project, project_key: service_desk_key)
end end
...@@ -244,6 +246,17 @@ RSpec.describe Gitlab::Email::Handler::ServiceDeskHandler do ...@@ -244,6 +246,17 @@ RSpec.describe Gitlab::Email::Handler::ServiceDeskHandler do
end end
end end
context 'when project key is not set' do
let(:email_raw) { email_fixture('emails/service_desk_custom_address_no_key.eml') }
before do
stub_service_desk_email_setting(enabled: true, address: 'support+%{key}@example.com')
end
it_behaves_like 'a new issue request'
end
end
context 'when rate limiting is in effect' do context 'when rate limiting is in effect' do
it 'allows unlimited new issue creation' do it 'allows unlimited new issue creation' do
stub_application_setting(issues_create_limit: 1) stub_application_setting(issues_create_limit: 1)
......
...@@ -1715,13 +1715,19 @@ RSpec.describe Project, factory_default: :keep do ...@@ -1715,13 +1715,19 @@ RSpec.describe Project, factory_default: :keep do
allow(::Gitlab::ServiceDeskEmail).to receive(:config).and_return(config) allow(::Gitlab::ServiceDeskEmail).to receive(:config).and_return(config)
end end
it 'returns custom address when project_key is set' do context 'when project_key is set' do
it 'returns custom address including the project_key' do
create(:service_desk_setting, project: project, project_key: 'key1') create(:service_desk_setting, project: project, project_key: 'key1')
expect(subject).to eq("foo+#{project.full_path_slug}-key1@bar.com") expect(subject).to eq("foo+#{project.full_path_slug}-key1@bar.com")
end end
end
it_behaves_like 'with incoming email address' context 'when project_key is not set' do
it 'returns custom address including the project full path' do
expect(subject).to eq("foo+#{project.full_path_slug}-#{project.project_id}-issue-@bar.com")
end
end
end end
end end
......
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