Commit 31968500 authored by Brett Walker's avatar Brett Walker

Use new service desk issue email address format

We now appended `-` and the project id in order
to support catch all email addresses
parent 3376aacd
# frozen_string_literal: true
# handles service desk issue creation emails with these formats:
# incoming+gitlab-org-gitlab-ce-20-@incoming.gitlab.com
# incoming+gitlab-org/gitlab-ce@incoming.gitlab.com (legacy)
module Gitlab
module Email
module Handler
......@@ -7,8 +10,21 @@ module Gitlab
class ServiceDeskHandler < BaseHandler
include ReplyProcessing
HANDLER_REGEX = /\A.+-(?<project_id>.+)-\z/.freeze
HANDLER_REGEX_LEGACY = /\A(?<project_path>[^\+]*)\z/.freeze
def initialize(mail, mail_key)
super(mail, mail_key)
if matched = HANDLER_REGEX.match(mail_key.to_s)
@project_id = matched[:project_id]
elsif matched = HANDLER_REGEX_LEGACY.match(mail_key.to_s)
@project_path = matched[:project_path]
end
end
def can_handle?
::EE::Gitlab::ServiceDesk.enabled? && service_desk_key.present?
::EE::Gitlab::ServiceDesk.enabled? && (project_id || can_handle_legacy_format?)
end
def execute
......@@ -24,33 +40,19 @@ module Gitlab
private
# mail key should end in `-` ie: `h5bp-html5-boilerplate-8-`
# but continue to support legacy style ie: `h5bp/html5-boilerplate`
def service_desk_key
return unless mail_key && (mail_key.end_with?('-') || mail_key.include?('/') && !mail_key.include?('+'))
mail_key
end
def extract_project_id(key)
key.split('-').last.to_i
end
attr_reader :project_id, :project_path
# rubocop: disable CodeReuse/ActiveRecord
def project
return @project if instance_variable_defined?(:@project)
found_project = Project.where(service_desk_enabled: true)
found_project = if mail_key.end_with?('-')
found_project.find_by_id(extract_project_id(service_desk_key))
found_project = if project_id
found_project.find_by_id(project_id)
else
found_project.find_by_full_path(service_desk_key)
found_project.find_by_full_path(project_path)
end
# found_project =
# Project.where(service_desk_enabled: true)
# .find_by_full_path(service_desk_key)
@project = found_project&.service_desk_enabled? ? found_project : nil
end
# rubocop: enable CodeReuse/ActiveRecord
......@@ -84,6 +86,10 @@ module Gitlab
"Service Desk #{from}: #{mail.subject}"
end
def can_handle_legacy_format?
project_path && project_path.include?('/') && !mail_key.include?('+')
end
end
end
end
......
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: from mail-ie0-x234.google.com (mail-ie0-x234.google.com [IPv6:2607:f8b0:4001:c03::234]) by iceking.adventuretime.ooo (8.14.3/8.14.3/Debian-9.4) with ESMTP id r5DL3nFJ016967 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=NOT) for <incoming+gitlabhq/gitlabhq@appmail.adventuretime.ooo>; Thu, 13 Jun 2013 17:03:50 -0400
Received: by mail-ie0-f180.google.com with SMTP id f4so21977375iea.25 for <incoming+gitlabhq/gitlabhq@appmail.adventuretime.ooo>; Thu, 13 Jun 2013 14:03:48 -0700
Received: by mail-ie0-f180.google.com with SMTP id f4so21977375iea.25 for <incoming+gitlabhq-gitlabhq-project_id-@appmail.adventuretime.ooo>; 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: incoming+email/test@appmail.adventuretime.ooo
To: incoming+email-test-project_id-@appmail.adventuretime.ooo
Message-ID: <CADkmRc+rNGAGGbV2iE5p918UVy4UyJqVcXRO2=otppgzduJSg@mail.gmail.com>
Subject: The message subject! @all
Mime-Version: 1.0
......
Delivered-To: incoming+email/test@appmail.adventuretime.ooo
Delivered-To: incoming+email-test-project_id-@appmail.adventuretime.ooo
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: from mail-ie0-x234.google.com (mail-ie0-x234.google.com [IPv6:2607:f8b0:4001:c03::234]) by iceking.adventuretime.ooo (8.14.3/8.14.3/Debian-9.4) with ESMTP id r5DL3nFJ016967 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=NOT) for <incoming+gitlabhq/gitlabhq@appmail.adventuretime.ooo>; Thu, 13 Jun 2013 17:03:50 -0400
Received: by mail-ie0-f180.google.com with SMTP id f4so21977375iea.25 for <incoming+gitlabhq/gitlabhq@appmail.adventuretime.ooo>; Thu, 13 Jun 2013 14:03:48 -0700
Received: by mail-ie0-f180.google.com with SMTP id f4so21977375iea.25 for <incoming+gitlabhq-gitlabhq-project_id-@appmail.adventuretime.ooo>; 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>
......
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: from mail-ie0-x234.google.com (mail-ie0-x234.google.com [IPv6:2607:f8b0:4001:c03::234]) by iceking.adventuretime.ooo (8.14.3/8.14.3/Debian-9.4) with ESMTP id r5DL3nFJ016967 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=NOT) for <incoming+gitlabhq/gitlabhq@appmail.adventuretime.ooo>; Thu, 13 Jun 2013 17:03:50 -0400
Received: by mail-ie0-f180.google.com with SMTP id f4so21977375iea.25 for <incoming+gitlabhq/gitlabhq@appmail.adventuretime.ooo>; 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: incoming+email/test@appmail.adventuretime.ooo
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
```
Delivered-To: incoming+email/test@appmail.adventuretime.ooo
Delivered-To: incoming+email-test-project_id-@appmail.adventuretime.ooo
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: from mail-ie0-x234.google.com (mail-ie0-x234.google.com [IPv6:2607:f8b0:4001:c03::234]) by iceking.adventuretime.ooo (8.14.3/8.14.3/Debian-9.4) with ESMTP id r5DL3nFJ016967 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=NOT) for <incoming+gitlabhq/gitlabhq@appmail.adventuretime.ooo>; Thu, 13 Jun 2013 17:03:50 -0400
Received: by mail-ie0-f180.google.com with SMTP id f4so21977375iea.25 for <incoming+gitlabhq/gitlabhq@appmail.adventuretime.ooo>; Thu, 13 Jun 2013 14:03:48 -0700
Received: by mail-ie0-f180.google.com with SMTP id f4so21977375iea.25 for <incoming+gitlabhq-gitlabhq-project_id-@appmail.adventuretime.ooo>; 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: Finn the Human <finn@adventuretime.ooo>
......
......@@ -9,7 +9,7 @@ describe Gitlab::Email::Handler::EE::ServiceDeskHandler do
stub_config_setting(host: 'localhost')
end
let(:email_raw) { fixture_file('emails/service_desk.eml', dir: 'ee') }
let(:email_raw) { email_fixture('emails/service_desk.eml', dir: 'ee') }
let(:namespace) { create(:namespace, name: "email") }
context 'service desk is enabled for the project' do
......@@ -23,20 +23,56 @@ describe Gitlab::Email::Handler::EE::ServiceDeskHandler do
allow(::EE::Gitlab::ServiceDesk).to receive(:enabled?).with(project: project).and_return(true)
end
it 'sends thank you the email and creates issue' do
setup_attachment
context 'when everything is fine' do
shared_examples 'a new issue request' do
it 'sends thank you the email and creates issue' do
setup_attachment
expect(Notify).to receive(:service_desk_thank_you_email).with(kind_of(Integer))
expect(Notify).to receive(:service_desk_thank_you_email).with(kind_of(Integer))
expect { receiver.execute }.to change { Issue.count }.by(1)
expect { receiver.execute }.to change { Issue.count }.by(1)
new_issue = Issue.last
new_issue = Issue.last
expect(new_issue.author).to eql(User.support_bot)
expect(new_issue.confidential?).to be true
expect(new_issue.all_references.all).to be_empty
expect(new_issue.title).to eq("Service Desk (from jake@adventuretime.ooo): The message subject! @all")
expect(new_issue.description).to eq("Service desk stuff!\n\n```\na = b\n```\n\n![image](uploads/image.png)")
expect(new_issue.author).to eql(User.support_bot)
expect(new_issue.confidential?).to be true
expect(new_issue.all_references.all).to be_empty
expect(new_issue.title).to eq("Service Desk (from jake@adventuretime.ooo): The message subject! @all")
expect(new_issue.description).to eq("Service desk stuff!\n\n```\na = b\n```\n\n![image](uploads/image.png)")
end
end
it_behaves_like 'a new issue request'
context "creates a new issue with legacy email address" do
let(:email_raw) { fixture_file('emails/service_desk_legacy.eml', dir: 'ee') }
it_behaves_like 'a new issue request'
end
end
context 'when email key' do
let(:mail) { Mail::Message.new(email_raw) }
it "matches the new format" do
handler = described_class.new(mail, "h5bp-html5-boilerplate-#{project.project_id}-")
expect(handler.instance_variable_get(:@project_id).to_i).to eq project.project_id
expect(handler.can_handle?).to be_truthy
end
it "matches the legacy format" do
handler = described_class.new(mail, "h5bp/html5-boilerplate")
expect(handler.instance_variable_get(:@project_path)).to eq 'h5bp/html5-boilerplate'
expect(handler.can_handle?).to be_truthy
end
it "doesn't match either format" do
handler = described_class.new(mail, "h5bp-html5-boilerplate-invalid")
expect(handler.can_handle?).to be_falsey
end
end
context 'when there is no from address' do
......@@ -53,7 +89,7 @@ describe Gitlab::Email::Handler::EE::ServiceDeskHandler do
end
context 'when there is a sender address and a from address' do
let(:email_raw) { fixture_file('emails/service_desk_sender_and_from.eml', dir: 'ee') }
let(:email_raw) { email_fixture('emails/service_desk_sender_and_from.eml', dir: 'ee') }
it 'prefers the from address' do
setup_attachment
......@@ -85,7 +121,7 @@ describe Gitlab::Email::Handler::EE::ServiceDeskHandler do
end
context 'when the email is forwarded through an alias' do
let(:email_raw) { fixture_file('emails/service_desk_forwarded.eml', dir: 'ee') }
let(:email_raw) { email_fixture('emails/service_desk_forwarded.eml', dir: 'ee') }
it 'sends thank you the email and creates issue' do
setup_attachment
......@@ -116,4 +152,8 @@ describe Gitlab::Email::Handler::EE::ServiceDeskHandler do
expect { receiver.execute rescue nil }.not_to change { Issue.count }
end
end
def email_fixture(path, dir:)
fixture_file(path, dir: dir).gsub('project_id', project.project_id.to_s)
end
end
......@@ -702,7 +702,7 @@ describe Project do
end
it 'uses project full path as service desk address key' do
expect(project.service_desk_address).to eq("test+#{project.full_path}@mail.com")
expect(project.service_desk_address).to eq("test+#{project.full_path_slug}-#{project.project_id}-@mail.com")
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