diff options
author | Brett Walker <bwalker@gitlab.com> | 2018-12-28 11:53:39 -0700 |
---|---|---|
committer | Brett Walker <bwalker@gitlab.com> | 2019-01-03 14:37:35 -0600 |
commit | a4f2de796411236bfda81b7fa281cfa8199c5acf (patch) | |
tree | d1af8211ccae7ea525ca01f06c4993d4710c2771 /lib/gitlab/email | |
parent | 54eb6260e75690ec45802618a7e1b9807f0e7e08 (diff) | |
download | gitlab-ce-a4f2de796411236bfda81b7fa281cfa8199c5acf.tar.gz |
Refactoring and review comments
including verifying the project_slug
Diffstat (limited to 'lib/gitlab/email')
-rw-r--r-- | lib/gitlab/email/handler/create_issue_handler.rb | 21 | ||||
-rw-r--r-- | lib/gitlab/email/handler/create_merge_request_handler.rb | 21 | ||||
-rw-r--r-- | lib/gitlab/email/handler/reply_processing.rb | 21 | ||||
-rw-r--r-- | lib/gitlab/email/handler/unsubscribe_handler.rb | 2 |
4 files changed, 35 insertions, 30 deletions
diff --git a/lib/gitlab/email/handler/create_issue_handler.rb b/lib/gitlab/email/handler/create_issue_handler.rb index 7e5fa5f5cd2..50928f0d59e 100644 --- a/lib/gitlab/email/handler/create_issue_handler.rb +++ b/lib/gitlab/email/handler/create_issue_handler.rb @@ -11,16 +11,19 @@ module Gitlab class CreateIssueHandler < BaseHandler include ReplyProcessing - HANDLER_REGEX = /\A.+-(?<project_id>.+)-(?<incoming_email_token>.+)-issue\z/.freeze + HANDLER_REGEX = /\A#{HANDLER_ACTION_BASE_REGEX}-issue\z/.freeze HANDLER_REGEX_LEGACY = /\A(?<project_path>[^\+]*)\+(?<incoming_email_token>.*)\z/.freeze def initialize(mail, mail_key) super(mail, mail_key) - if matched = HANDLER_REGEX.match(mail_key.to_s) - @project_id, @incoming_email_token = matched.captures + if !mail_key&.include?('/') && (matched = HANDLER_REGEX.match(mail_key.to_s)) + @project_slug = matched[:project_slug] + @project_id = matched[:project_id]&.to_i + @incoming_email_token = matched[:incoming_email_token] elsif matched = HANDLER_REGEX_LEGACY.match(mail_key.to_s) - @project_path, @incoming_email_token = matched.captures + @project_path = matched[:project_path] + @incoming_email_token = matched[:incoming_email_token] end end @@ -45,18 +48,8 @@ module Gitlab end # rubocop: enable CodeReuse/ActiveRecord - def project - @project ||= if project_id - Project.find_by_id(project_id) - else - Project.find_by_full_path(project_path) - end - end - private - attr_reader :project_id, :project_path, :incoming_email_token - def create_issue Issues::CreateService.new( project, diff --git a/lib/gitlab/email/handler/create_merge_request_handler.rb b/lib/gitlab/email/handler/create_merge_request_handler.rb index d065da70931..21bb09fa4a1 100644 --- a/lib/gitlab/email/handler/create_merge_request_handler.rb +++ b/lib/gitlab/email/handler/create_merge_request_handler.rb @@ -12,16 +12,19 @@ module Gitlab class CreateMergeRequestHandler < BaseHandler include ReplyProcessing - HANDLER_REGEX = /\A.+-(?<project_id>.+)-(?<incoming_email_token>.+)-merge-request\z/.freeze + HANDLER_REGEX = /\A#{HANDLER_ACTION_BASE_REGEX}-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 matched = HANDLER_REGEX.match(mail_key.to_s) - @project_id, @incoming_email_token = matched.captures + if !mail_key&.include?('/') && (matched = HANDLER_REGEX.match(mail_key.to_s)) + @project_slug = matched[:project_slug] + @project_id = matched[:project_id]&.to_i + @incoming_email_token = matched[:incoming_email_token] elsif matched = HANDLER_REGEX_LEGACY.match(mail_key.to_s) - @project_path, @incoming_email_token = matched.captures + @project_path = matched[:project_path] + @incoming_email_token = matched[:incoming_email_token] end end @@ -47,22 +50,12 @@ module Gitlab end # rubocop: enable CodeReuse/ActiveRecord - def project - @project ||= if project_id - Project.find_by_id(project_id) - else - Project.find_by_full_path(project_path) - end - end - def metrics_params super.merge(includes_patches: patch_attachments.any?) end private - attr_reader :project_id, :project_path, :incoming_email_token - def build_merge_request MergeRequests::BuildService.new(project, author, merge_request_params).execute end diff --git a/lib/gitlab/email/handler/reply_processing.rb b/lib/gitlab/email/handler/reply_processing.rb index ff6b2c729b2..76b393f3259 100644 --- a/lib/gitlab/email/handler/reply_processing.rb +++ b/lib/gitlab/email/handler/reply_processing.rb @@ -6,13 +6,28 @@ module Gitlab module ReplyProcessing private + HANDLER_ACTION_BASE_REGEX = /(?<project_slug>.+)-(?<project_id>\d+)-(?<incoming_email_token>.+)/.freeze + + attr_reader :project_id, :project_slug, :project_path, :incoming_email_token + def author raise NotImplementedError end + # rubocop:disable Gitlab/ModuleWithInstanceVariables def project - raise NotImplementedError + return @project if instance_variable_defined?(:@project) + + if project_id + @project = Project.find_by_id(project_id) + @project = nil unless valid_project_slug?(@project) + else + @project = Project.find_by_full_path(project_path) + end + + @project end + # rubocop:enable Gitlab/ModuleWithInstanceVariables def message @message ||= process_message @@ -58,6 +73,10 @@ module Gitlab raise invalid_exception, msg end + + def valid_project_slug?(found_project) + project_slug == found_project.full_path_slug + end end end end diff --git a/lib/gitlab/email/handler/unsubscribe_handler.rb b/lib/gitlab/email/handler/unsubscribe_handler.rb index 7589658d2fe..20e4c125626 100644 --- a/lib/gitlab/email/handler/unsubscribe_handler.rb +++ b/lib/gitlab/email/handler/unsubscribe_handler.rb @@ -23,7 +23,7 @@ module Gitlab end def can_handle? - @reply_token.present? + reply_token.present? end def execute |