summaryrefslogtreecommitdiff
path: root/lib/gitlab/slash_commands/presenters/help.rb
blob: 342dae456a824b90321744f2ca367ac8251bf97e (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
85
86
87
88
89
# frozen_string_literal: true

module Gitlab
  module SlashCommands
    module Presenters
      class Help < Presenters::Base
        def initialize(project, commands)
          @project = project
          @commands = commands
        end

        def present(trigger, text)
          ephemeral_response(text: help_message(trigger, text))
        end

        private

        def help_message(trigger, text)
          unless @commands.present?
            return <<~MESSAGE
              This chatops integration does not have any commands that can be
              executed.

              #{footer}
            MESSAGE
          end

          if text.start_with?('help')
            <<~MESSAGE
              #{full_commands_message(trigger)}

              #{help_footer}
            MESSAGE
          else
            <<~MESSAGE
              The specified command is not valid.

              #{full_commands_message(trigger)}

              #{help_footer}
            MESSAGE
          end
        end

        def help_footer
          message = @project ? project_info : ''
          message += <<~MESSAGE
             *Documentation*

             For more information about GitLab chatops, refer to its
             documentation: https://docs.gitlab.com/ce/ci/chatops/README.html.
          MESSAGE

          message
        end

        def project_info
          <<~MESSAGE
            *Project*

            The GitLab project for this chatops integration can be found at
            #{url_for(@project)}.

          MESSAGE
        end

        def full_commands_message(trigger)
          list = @commands
            .map { |command| "#{trigger} #{command.help_message}" }
            .join("\n")

          <<~MESSAGE
            *Available commands*

            The following commands are available for this chatops integration:

            #{list}

            If available, the `run` command is used for running GitLab CI jobs
            defined in this project's `.gitlab-ci.yml` file. For example, if a
            job called "help" is defined you can run it like so:

            `#{trigger} run help`
          MESSAGE
        end
      end
    end
  end
end