create_issue_handler.rb 1.26 KB
Newer Older
1
require 'gitlab/email/handler/base_handler'
2 3 4

module Gitlab
  module Email
5 6
    module Handler
      class CreateIssueHandler < BaseHandler
7
        include ReplyProcessing
8
        attr_reader :project_path, :incoming_email_token
9 10 11

        def initialize(mail, mail_key)
          super(mail, mail_key)
12
          @project_path, @incoming_email_token =
13 14 15
            mail_key && mail_key.split('+', 2)
        end

16
        def can_handle?
17
          !incoming_email_token.nil?
18 19 20
        end

        def execute
21
          raise ProjectNotFound unless project
22

23 24
          validate_permission!(:create_issue)

25 26 27 28
          verify_record!(
            record: create_issue,
            invalid_exception: InvalidIssueError,
            record_name: 'issue')
29 30 31
        end

        def author
32
          @author ||= User.find_by(incoming_email_token: incoming_email_token)
33 34 35
        end

        def project
36
          @project ||= Project.find_by_full_path(project_path)
37 38
        end

39 40 41 42
        def metrics_params
          super.merge(project: project)
        end

43
        private
44

45 46 47 48 49 50 51 52 53 54 55 56
        def create_issue
          Issues::CreateService.new(
            project,
            author,
            title:       mail.subject,
            description: message
          ).execute
        end
      end
    end
  end
end