From 34dd6196e31b248dc614edd531105ee6b6551060 Mon Sep 17 00:00:00 2001 From: Brett Walker Date: Wed, 12 Dec 2018 18:35:01 -0600 Subject: Use new merge request email address format We now use `-merge-request` instead of `+merge-request+` in order to support catch all email addresses --- .../email/handler/create_merge_request_handler.rb | 25 ++++++++++++++----- lib/gitlab/email/handler/unsubscribe_handler.rb | 29 +++++++++++----------- 2 files changed, 34 insertions(+), 20 deletions(-) (limited to 'lib') 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.+-(?.+)-(?.+)-merge-request\z/.freeze + HANDLER_REGEX_LEGACY = /\A(?[^\+]*)\+merge-request\+(?.*)/.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(?\w+)#{Gitlab::IncomingEmail::UNSUBSCRIBE_SUFFIX}\z/.freeze + HANDLER_REGEX_LEGACY = /\A(?\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 -- cgit v1.2.1