summaryrefslogtreecommitdiff
path: root/lib/gitlab/email/handler/create_issue.rb
blob: 72d49ec6c96a3dc3d4c3bbd698325f5c3aa496c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

require 'gitlab/email/handler'

module Gitlab
  module Email
    class Handler
      class CreateIssue < Handler
        def can_handle?
          !!project
        end

        def execute
          validate_permission!(:create_issue)

          verify_record(
            create_issue,
            InvalidIssueError,
            "The issue could not be created for the following reasons:"
          )
        end

        def author
          @author ||= User.find_by(authentication_token: authentication_token)
        end

        def project
          @project ||= Project.find_with_namespace(project_namespace)
        end

        private
        def authentication_token
          mail_key[/[^\+]+$/]
        end

        def project_namespace
          mail_key[/^[^\+]+/]
        end

        def create_issue
          Issues::CreateService.new(
            project,
            author,
            title:       mail.subject,
            description: message
          ).execute
        end
      end
    end
  end
end