BigW Consortium Gitlab

issue_search.rb 1.17 KB
Newer Older
Z.J. van de Weg committed
1 2 3
module Gitlab
  module ChatCommands
    module Presenters
4
      class IssueSearch < Presenters::Base
5
        include Presenters::IssueBase
6

Z.J. van de Weg committed
7 8 9
        def present
          text = if @resource.count >= 5
                   "Here are the first 5 issues I found:"
Z.J. van de Weg committed
10 11
                 elsif @resource.one?
                   "Here is the only issue I found:"
Z.J. van de Weg committed
12 13 14
                 else
                   "Here are the #{@resource.count} issues I found:"
                 end
15

Z.J. van de Weg committed
16 17
          ephemeral_response(text: text, attachments: attachments)
        end
18

Z.J. van de Weg committed
19
        private
20

Z.J. van de Weg committed
21 22 23
        def attachments
          @resource.map do |issue|
            url = "[#{issue.to_reference}](#{url_for([namespace, project, issue])})"
24

Z.J. van de Weg committed
25 26 27 28
            {
              color: color(issue),
              fallback: "#{issue.to_reference} #{issue.title}",
              text: "#{url} · #{issue.title} (#{status_text(issue)})",
29

Z.J. van de Weg committed
30
              mrkdwn_in: [
Z.J. van de Weg committed
31
                :text
Z.J. van de Weg committed
32 33 34 35 36 37 38 39 40 41 42 43 44
              ]
            }
          end
        end

        def project
          @project ||= @resource.first.project
        end

        def namespace
          @namespace ||= project.namespace.becomes(Namespace)
        end
      end
45 46 47
    end
  end
end