2018-11-20 20:47:30 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
require 'gitlab/email/handler/base_handler'
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
# 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-09-13 17:45:13 +05:30
|
|
|
module Gitlab
|
|
|
|
module Email
|
|
|
|
module Handler
|
|
|
|
class CreateIssueHandler < BaseHandler
|
2017-08-17 22:00:37 +05:30
|
|
|
include ReplyProcessing
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
HANDLER_REGEX = /\A#{HANDLER_ACTION_BASE_REGEX}-(?<incoming_email_token>.+)-issue\z/.freeze
|
|
|
|
HANDLER_REGEX_LEGACY = /\A(?<project_path>[^\+]*)\+(?<incoming_email_token>.*)\z/.freeze
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
def initialize(mail, mail_key)
|
|
|
|
super(mail, mail_key)
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
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 = matched[:project_path]
|
|
|
|
@incoming_email_token = matched[:incoming_email_token]
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def can_handle?
|
2019-02-15 15:39:39 +05:30
|
|
|
incoming_email_token && (project_id || can_handle_legacy_format?)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
raise ProjectNotFound unless project
|
|
|
|
|
|
|
|
validate_permission!(:create_issue)
|
|
|
|
|
|
|
|
verify_record!(
|
|
|
|
record: create_issue,
|
|
|
|
invalid_exception: InvalidIssueError,
|
|
|
|
record_name: 'issue')
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-09-13 17:45:13 +05:30
|
|
|
def author
|
2017-08-17 22:00:37 +05:30
|
|
|
@author ||= User.find_by(incoming_email_token: incoming_email_token)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def metrics_event
|
|
|
|
:receive_email_create_issue
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def create_issue
|
2021-11-11 11:23:49 +05:30
|
|
|
::Issues::CreateService.new(
|
2021-06-08 01:23:25 +05:30
|
|
|
project: project,
|
|
|
|
current_user: author,
|
|
|
|
params: {
|
|
|
|
title: mail.subject,
|
|
|
|
description: message_including_reply
|
2021-09-30 23:02:18 +05:30
|
|
|
},
|
|
|
|
spam_params: nil
|
2016-09-13 17:45:13 +05:30
|
|
|
).execute
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
def can_handle_legacy_format?
|
|
|
|
project_path && !incoming_email_token.include?('+') && !mail_key.include?(Gitlab::IncomingEmail::UNSUBSCRIBE_SUFFIX_LEGACY)
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|