help.rb 2.14 KB
Newer Older
1 2
# frozen_string_literal: true

Z.J. van de Weg's avatar
Z.J. van de Weg committed
3
module Gitlab
4
  module SlashCommands
Z.J. van de Weg's avatar
Z.J. van de Weg committed
5 6
    module Presenters
      class Help < Presenters::Base
7 8 9 10 11
        def initialize(project, commands)
          @project = project
          @commands = commands
        end

Z.J. van de Weg's avatar
Z.J. van de Weg committed
12 13
        def present(trigger, text)
          ephemeral_response(text: help_message(trigger, text))
Z.J. van de Weg's avatar
Z.J. van de Weg committed
14 15
        end

Z.J. van de Weg's avatar
Z.J. van de Weg committed
16
        private
Z.J. van de Weg's avatar
Z.J. van de Weg committed
17

Z.J. van de Weg's avatar
Z.J. van de Weg committed
18
        def help_message(trigger, text)
19 20 21 22 23 24 25 26
          unless @commands.present?
            return <<~MESSAGE
              This chatops integration does not have any commands that can be
              executed.

              #{footer}
            MESSAGE
          end
Z.J. van de Weg's avatar
Z.J. van de Weg committed
27 28

          if text.start_with?('help')
29 30 31 32 33
            <<~MESSAGE
              #{full_commands_message(trigger)}

              #{help_footer}
            MESSAGE
34
          else
35 36 37 38 39 40 41
            <<~MESSAGE
              The specified command is not valid.

              #{full_commands_message(trigger)}

              #{help_footer}
            MESSAGE
Z.J. van de Weg's avatar
Z.J. van de Weg committed
42 43
          end
        end
Z.J. van de Weg's avatar
Z.J. van de Weg committed
44

45
        def help_footer
46 47 48
          message = @project ? project_info : ''
          message += <<~MESSAGE
             *Documentation*
49

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

          message
55 56 57
        end

        def project_info
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
          <<~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
Z.J. van de Weg's avatar
Z.J. van de Weg committed
85 86
        end
      end
Z.J. van de Weg's avatar
Z.J. van de Weg committed
87 88 89
    end
  end
end