summaryrefslogtreecommitdiff
path: root/lib/gitlab/email
diff options
context:
space:
mode:
authorBrett Walker <bwalker@gitlab.com>2018-12-12 18:35:01 -0600
committerBrett Walker <bwalker@gitlab.com>2019-01-03 14:37:35 -0600
commit34dd6196e31b248dc614edd531105ee6b6551060 (patch)
treea69b7cf94df25a5ca900a337625625e5c64bbd3a /lib/gitlab/email
parent23d5f4c99138a74cb4176bfca3fe3fdad1beecc4 (diff)
downloadgitlab-ce-34dd6196e31b248dc614edd531105ee6b6551060.tar.gz
Use new merge request email address format
We now use `-merge-request` instead of `+merge-request+` in order to support catch all email addresses
Diffstat (limited to 'lib/gitlab/email')
-rw-r--r--lib/gitlab/email/handler/create_merge_request_handler.rb25
-rw-r--r--lib/gitlab/email/handler/unsubscribe_handler.rb29
2 files changed, 34 insertions, 20 deletions
diff --git a/lib/gitlab/email/handler/create_merge_request_handler.rb b/lib/gitlab/email/handler/create_merge_request_handler.rb
index 5772727e855..bb62d76a091 100644
--- a/lib/gitlab/email/handler/create_merge_request_handler.rb
+++ b/lib/gitlab/email/handler/create_merge_request_handler.rb
@@ -3,23 +3,30 @@
require 'gitlab/email/handler/base_handler'
require 'gitlab/email/handler/reply_processing'
+# handles merge request creation emails with these forms:
+# incoming+gitlab-org-gitlab-ce-20-Author_Token12345678-merge-request@incoming.gitlab.com
+# incoming+gitlab-org/gitlab-ce+merge-request+Author_Token12345678@incoming.gitlab.com (legacy)
module Gitlab
module Email
module Handler
class CreateMergeRequestHandler < BaseHandler
include ReplyProcessing
- attr_reader :project_path, :incoming_email_token
+
+ HANDLER_REGEX = /\A.+-(?<project_id>.+)-(?<incoming_email_token>.+)-merge-request\z/.freeze
+ HANDLER_REGEX_LEGACY = /\A(?<project_path>[^\+]*)\+merge-request\+(?<incoming_email_token>.*)/.freeze
def initialize(mail, mail_key)
super(mail, mail_key)
- if m = /\A([^\+]*)\+merge-request\+(.*)/.match(mail_key.to_s)
- @project_path, @incoming_email_token = m.captures
+ if matched = HANDLER_REGEX.match(mail_key.to_s)
+ @project_id, @incoming_email_token = matched.captures
+ elsif matched = HANDLER_REGEX_LEGACY.match(mail_key.to_s)
+ @project_path, @incoming_email_token = matched.captures
end
end
def can_handle?
- @project_path && @incoming_email_token
+ incoming_email_token && (project_id || project_path)
end
def execute
@@ -41,7 +48,11 @@ module Gitlab
# rubocop: enable CodeReuse/ActiveRecord
def project
- @project ||= Project.find_by_full_path(project_path)
+ @project ||= if project_id
+ Project.find_by_id(project_id)
+ else
+ Project.find_by_full_path(project_path)
+ end
end
def metrics_params
@@ -50,6 +61,8 @@ module Gitlab
private
+ attr_reader :project_id, :project_path, :incoming_email_token
+
def build_merge_request
MergeRequests::BuildService.new(project, author, merge_request_params).execute
end
@@ -97,7 +110,7 @@ module Gitlab
def remove_patch_attachments
patch_attachments.each { |patch| mail.parts.delete(patch) }
- # reset the message, so it needs to be reporocessed when the attachments
+ # reset the message, so it needs to be reprocessed when the attachments
# have been modified
@message = nil
end
diff --git a/lib/gitlab/email/handler/unsubscribe_handler.rb b/lib/gitlab/email/handler/unsubscribe_handler.rb
index 77b6c9177de..2d679c676a5 100644
--- a/lib/gitlab/email/handler/unsubscribe_handler.rb
+++ b/lib/gitlab/email/handler/unsubscribe_handler.rb
@@ -2,14 +2,27 @@
require 'gitlab/email/handler/base_handler'
+# handles unsubscribe emails with these forms:
+# incoming+1234567890abcdef1234567890abcdef-unsubscribe@incoming.gitlab.com
+# incoming+1234567890abcdef1234567890abcdef+unsubscribe@incoming.gitlab.com (legacy)
module Gitlab
module Email
module Handler
class UnsubscribeHandler < BaseHandler
delegate :project, to: :sent_notification, allow_nil: true
+ HANDLER_REGEX = /\A(?<replytoken>\w+)#{Gitlab::IncomingEmail::UNSUBSCRIBE_SUFFIX}\z/.freeze
+ HANDLER_REGEX_LEGACY = /\A(?<replytoken>\w+)#{Regexp.escape(Gitlab::IncomingEmail::UNSUBSCRIBE_SUFFIX_OLD)}\z/.freeze
+
+ def initialize(mail, mail_key)
+ super(mail, mail_key)
+
+ matched = HANDLER_REGEX.match(mail_key.to_s) || HANDLER_REGEX_LEGACY.match(mail_key.to_s)
+ @reply_token = matched[:replytoken] if matched
+ end
+
def can_handle?
- mail_key =~ /\A\w+#{Regexp.escape(suffix)}\z/
+ @reply_token.present?
end
def execute
@@ -25,19 +38,7 @@ module Gitlab
private
def sent_notification
- @sent_notification ||= SentNotification.for(reply_key)
- end
-
- def suffix
- @suffix ||= if mail_key&.end_with?(Gitlab::IncomingEmail::UNSUBSCRIBE_SUFFIX)
- Gitlab::IncomingEmail::UNSUBSCRIBE_SUFFIX
- else
- Gitlab::IncomingEmail::UNSUBSCRIBE_SUFFIX_OLD
- end
- end
-
- def reply_key
- mail_key.sub(suffix, '')
+ @sent_notification ||= SentNotification.for(@reply_token)
end
end
end