2018-08-23 05:22:23 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-21 12:40:08 -04:00
|
|
|
require 'gitlab/email/handler/base_handler'
|
2016-05-18 18:19:25 -04:00
|
|
|
|
2018-12-14 13:37:03 -05:00
|
|
|
# handles issue creation emails with these formats:
|
|
|
|
# incoming+gitlab-org-gitlab-ce-20-Author_Token12345678-issue@incoming.gitlab.com
|
|
|
|
# incoming+gitlab-org/gitlab-ce+Author_Token12345678@incoming.gitlab.com (legacy)
|
2016-05-18 18:19:25 -04:00
|
|
|
module Gitlab
|
|
|
|
module Email
|
2016-05-21 12:40:08 -04:00
|
|
|
module Handler
|
|
|
|
class CreateIssueHandler < BaseHandler
|
2016-10-12 13:07:36 -04:00
|
|
|
include ReplyProcessing
|
2018-12-14 13:37:03 -05:00
|
|
|
|
2019-01-02 15:41:28 -05:00
|
|
|
HANDLER_REGEX = /\A#{HANDLER_ACTION_BASE_REGEX}-(?<incoming_email_token>.+)-issue\z/.freeze
|
2018-12-14 13:37:03 -05:00
|
|
|
HANDLER_REGEX_LEGACY = /\A(?<project_path>[^\+]*)\+(?<incoming_email_token>.*)\z/.freeze
|
2016-05-23 10:16:40 -04:00
|
|
|
|
|
|
|
def initialize(mail, mail_key)
|
|
|
|
super(mail, mail_key)
|
2018-12-14 13:37:03 -05:00
|
|
|
|
2018-12-28 13:53:39 -05:00
|
|
|
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]
|
2018-12-14 13:37:03 -05:00
|
|
|
elsif matched = HANDLER_REGEX_LEGACY.match(mail_key.to_s)
|
2018-12-28 13:53:39 -05:00
|
|
|
@project_path = matched[:project_path]
|
|
|
|
@incoming_email_token = matched[:incoming_email_token]
|
2018-12-14 13:37:03 -05:00
|
|
|
end
|
2016-05-23 10:16:40 -04:00
|
|
|
end
|
|
|
|
|
2016-05-18 18:19:25 -04:00
|
|
|
def can_handle?
|
2018-12-14 13:37:03 -05:00
|
|
|
incoming_email_token && (project_id || can_handle_legacy_format?)
|
2016-05-18 18:19:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2016-05-23 13:19:20 -04:00
|
|
|
raise ProjectNotFound unless project
|
2016-06-15 04:16:44 -04:00
|
|
|
|
2016-05-18 18:19:25 -04:00
|
|
|
validate_permission!(:create_issue)
|
|
|
|
|
2016-06-20 07:11:42 -04:00
|
|
|
verify_record!(
|
|
|
|
record: create_issue,
|
|
|
|
invalid_exception: InvalidIssueError,
|
|
|
|
record_name: 'issue')
|
2016-05-18 18:19:25 -04:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-05-18 18:19:25 -04:00
|
|
|
def author
|
2016-08-19 19:33:46 -04:00
|
|
|
@author ||= User.find_by(incoming_email_token: incoming_email_token)
|
2016-05-18 18:19:25 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-05-18 18:19:25 -04:00
|
|
|
|
|
|
|
private
|
2016-06-15 07:41:31 -04:00
|
|
|
|
2016-05-18 18:19:25 -04:00
|
|
|
def create_issue
|
|
|
|
Issues::CreateService.new(
|
|
|
|
project,
|
|
|
|
author,
|
|
|
|
title: mail.subject,
|
2018-05-09 16:19:16 -04:00
|
|
|
description: message_including_reply
|
2016-05-18 18:19:25 -04:00
|
|
|
).execute
|
|
|
|
end
|
2018-12-14 13:37:03 -05:00
|
|
|
|
|
|
|
def can_handle_legacy_format?
|
|
|
|
project_path && !incoming_email_token.include?('+') && !mail_key.include?(Gitlab::IncomingEmail::UNSUBSCRIBE_SUFFIX_LEGACY)
|
|
|
|
end
|
2016-05-18 18:19:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|