summaryrefslogtreecommitdiff
path: root/app/models/integrations/mattermost_slash_commands.rb
blob: f5079b9b9074dcb312f3cdbcf9154d72d723a825 (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
# frozen_string_literal: true

module Integrations
  class MattermostSlashCommands < BaseSlashCommands
    include Ci::TriggersHelper

    field :token,
      type: 'password',
      non_empty_password_title: -> { s_('ProjectService|Enter new token') },
      non_empty_password_help: -> { s_('ProjectService|Leave blank to use your current token.') },
      placeholder: ''

    def testable?
      false
    end

    def title
      'Mattermost slash commands'
    end

    def description
      "Perform common tasks with slash commands."
    end

    def self.to_param
      'mattermost_slash_commands'
    end

    def configure(user, params)
      token = ::Mattermost::Command.new(user)
        .create(command(params))

      update(active: true, token: token) if token
    rescue ::Mattermost::Error => e
      [false, e.message]
    end

    def list_teams(current_user)
      [::Mattermost::Team.new(current_user).all, nil]
    rescue ::Mattermost::Error => e
      [[], e.message]
    end

    private

    def command(params)
      pretty_project_name = project.full_name

      params.merge(
        auto_complete: true,
        auto_complete_desc: "Perform common operations on: #{pretty_project_name}",
        auto_complete_hint: '[help]',
        description: "Perform common operations on: #{pretty_project_name}",
        display_name: "GitLab / #{pretty_project_name}",
        method: 'P',
        username: 'GitLab')
    end
  end
end