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)
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
result = create_issue
|
|
|
|
issue = result[:issue]
|
|
|
|
|
|
|
|
# issue won't be present only on unrecoverable errors
|
|
|
|
raise InvalidIssueError, result.errors.join(', ') if result.error? && issue.blank?
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
verify_record!(
|
2022-11-25 23:54:43 +05:30
|
|
|
record: issue,
|
2016-09-13 17:45:13 +05:30
|
|
|
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(
|
2023-04-23 21:23:45 +05:30
|
|
|
container: project,
|
2021-06-08 01:23:25 +05:30
|
|
|
current_user: author,
|
|
|
|
params: {
|
|
|
|
title: mail.subject,
|
2022-01-26 12:08:38 +05:30
|
|
|
description: message_including_reply_or_only_quotes
|
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?
|
2023-01-13 00:05:48 +05:30
|
|
|
project_path && !incoming_email_token.include?('+') && !mail_key.include?(Gitlab::Email::Common::UNSUBSCRIBE_SUFFIX_LEGACY)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|