summaryrefslogtreecommitdiff
path: root/app/services/mattermost/commands/issue_service.rb
blob: 17407355547bedb8f576e945ead03cfb8581b5ca (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
51
52
53
54
55
56
module Mattermost
  module Commands
    class IssueService < Mattermost::Commands::BaseService
      class << self
        def triggered_by?(command)
          command == 'issue'
        end

        def available?(project)
          project.issues_enabled? && project.default_issues_tracker?
        end

        def help_message(project)
          return nil unless available?(project)

          message = "issue show <issue_id>"
          message << "issue search <query>"
        end
      end

      private

      def create(_)
        return nil unless can?(current_user, :create_issue, project)

        # We parse again as the previous split splits on continues whitespace
        # per the ruby spec, but we loose information on where the new lines were
        match = command.match(/\Aissue create (?<title>.*)\n*/)
        title = match[:title]
        description = match.post_match

        Issues::CreateService.new(project, current_user, title: title, description: description).execute
      end

      def subcommands
        %w[create search show]
      end

      def collection
        project.issues
      end

      def readable?(issue)
        can?(current_user, :read_issue, issue)
      end

      def parse_command
        split = command.split
        subcommand = split[1]
        args = split[2..-1]

        [subcommand, args]
      end
    end
  end
end