summaryrefslogtreecommitdiff
path: root/app/services/mattermost/issue_service.rb
blob: e1429c23438e5efc61420c5ea0d70f9e669374a7 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
module Mattermost
  class IssueService < BaseService
    SUBCOMMANDS = ['create', 'search'].freeze

    def execute
      resource  = if resource_id
                    find_resource
                  elsif subcommand
                    send(subcommand)
                  else
                    nil
                  end

      generate_response(resource)
    end

    private

    def find_resource
      return nil unless can?(current_user, :read_issue, project)

      collection.find_by(iid: resource_id)
    end

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

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

    def search
      return nil unless can?(current_user, :read_issue, project)

      query = params[:text].gsub(/\Asearch /, '')
      collection.search(query).limit(5)
    end

    def issue_create_error(errors)
      {
        response_type: :ephemeral,
        text: "An error occured creating your issue: #{errors}" #TODO; this displays an array
      }
    end

    def single_resource(issue)
      return issue_create_error(issue) if issue.errors.any?

      message = "### [#{issue.to_reference} #{issue.title}](#{link(issue)})"
      message << "\n\n#{issue.description}" if issue.description

      {
        response_type: :in_channel,
        text: message
      }
    end

    def collection
      project.issues
    end

    def link(issue)
      Gitlab::Routing.
        url_helpers.
        namespace_project_issue_url(project.namespace, project, issue)
    end

    def issue_params
      match = params[:text].match(/\Acreate (?<title>.+$)/)

      return issue_create_error("No title given") unless match

      {
        title: match[:title],
        description: params[:text].gsub(/\Acreate .+$\s*/, '')
      }
    end

    def subcommand
      match = params[:text].match(/\A(?<subcommand>(#{SUBCOMMANDS.join('|')}))/)

      match ? match[:subcommand] : nil
    end
  end
end