BigW Consortium Gitlab

presenter.rb 3.26 KB
Newer Older
1 2 3
module Mattermost
  class Presenter
    class << self
4
      include Gitlab::Routing.url_helpers
5

6
      def authorize_chat_name(url)
7 8 9 10 11
        message = if url
                    ":wave: Hi there! Before I do anything for you, please [connect your GitLab account](#{url})."
                  else
                    ":sweat_smile: Couldn't identify you, nor can I autorize you!"
                  end
12

13
        ephemeral_response(message)
14 15
      end

16
      def help(commands, trigger)
Z.J. van de Weg committed
17
        if commands.none?
18
          ephemeral_response("No commands configured")
19
        else
Z.J. van de Weg committed
20
          commands.map! { |command| "#{trigger} #{command}" }
21
          message = header_with_list("Available commands", commands)
22

23
          ephemeral_response(message)
24 25 26
        end
      end

27 28 29 30 31 32
      def present(subject)
        return not_found unless subject

        if subject.is_a?(Gitlab::ChatCommands::Result)
          show_result(subject)
        elsif subject.respond_to?(:count)
33
          if subject.many?
34
            multiple_resources(subject)
35
          elsif subject.none?
36
            not_found
37
          else
38
            single_resource(subject)
39
          end
40 41
        else
          single_resource(subject)
42 43 44
        end
      end

45 46 47 48
      def access_denied
        ephemeral_response("Whoops! That action is not allowed. This incident will be [reported](https://xkcd.com/838/).")
      end

49
      private
50

51
      def show_result(result)
52 53 54 55 56 57
        case result.type
        when :success
          in_channel_response(result.message)
        else
          ephemeral_response(result.message)
        end
58 59
      end

60
      def not_found
Z.J. van de Weg committed
61
        ephemeral_response("404 not found! GitLab couldn't find what you were looking for! :boom:")
62 63
      end

64
      def single_resource(resource)
65
        return error(resource) if resource.errors.any? || !resource.persisted?
66 67

        message = "### #{title(resource)}"
68
        message << "\n\n#{resource.description}" if resource.try(:description)
69

70
        in_channel_response(message)
71
      end
72

73
      def multiple_resources(resources)
74 75 76
        resources.map! { |resource| title(resource) }

        message = header_with_list("Multiple results were found:", resources)
77

78 79 80 81
        ephemeral_response(message)
      end

      def error(resource)
Z.J. van de Weg committed
82
        message = header_with_list("The action was not successful, because:", resource.errors.messages)
83

84
        ephemeral_response(message)
85
      end
86

87
      def title(resource)
88 89 90 91
        reference = resource.try(:to_reference) || resource.try(:id)
        title = resource.try(:title) || resource.try(:name)

        "[#{reference} #{title}](#{url(resource)})"
92 93
      end

94 95 96 97 98 99 100 101 102 103
      def header_with_list(header, items)
        message = [header]

        items.each do |item|
          message << "- #{item}"
        end

        message.join("\n")
      end

104
      def url(resource)
105
        url_for(
106 107 108
          [
            resource.project.namespace.becomes(Namespace),
            resource.project,
109 110
            resource
          ]
111 112
        )
      end
113

114 115 116
      def ephemeral_response(message)
        {
          response_type: :ephemeral,
117 118
          text: message,
          status: 200
119 120 121 122 123 124
        }
      end

      def in_channel_response(message)
        {
          response_type: :in_channel,
125 126
          text: message,
          status: 200
127
        }
128
      end
129 130 131
    end
  end
end