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
|
|
|
|
|
|
|
HANDLER_REGEX = /\A.+-(?<project_id>.+)-(?<incoming_email_token>.+)-issue\z/.freeze
|
|
|
|
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
|
|
|
|
|
|
|
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
|
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
|
|
|
|
|
|
|
def project
|
2018-12-14 13:37:03 -05:00
|
|
|
@project ||= if project_id
|
|
|
|
Project.find_by_id(project_id)
|
|
|
|
else
|
|
|
|
Project.find_by_full_path(project_path)
|
|
|
|
end
|
2016-05-18 18:19:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2016-06-15 07:41:31 -04:00
|
|
|
|
2018-12-14 13:37:03 -05:00
|
|
|
attr_reader :project_id, :project_path, :incoming_email_token
|
|
|
|
|
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
|