BigW Consortium Gitlab

issue_message.rb 1.52 KB
Newer Older
1
module ChatMessage
2
  class IssueMessage < BaseMessage
3 4 5 6 7 8 9 10
    attr_reader :title
    attr_reader :issue_iid
    attr_reader :issue_url
    attr_reader :action
    attr_reader :state
    attr_reader :description

    def initialize(params)
Tiago Botelho committed
11
      super
12 13 14 15 16 17 18 19

      obj_attr = params[:object_attributes]
      obj_attr = HashWithIndifferentAccess.new(obj_attr)
      @title = obj_attr[:title]
      @issue_iid = obj_attr[:iid]
      @issue_url = obj_attr[:url]
      @action = obj_attr[:action]
      @state = obj_attr[:state]
20
      @description = obj_attr[:description] || ''
21 22 23 24
    end

    def attachments
      return [] unless opened_issue?
Tiago Botelho committed
25
      return description if markdown
26

Tiago Botelho committed
27
      description_message
28 29 30
    end

    def activity
Tiago Botelho committed
31 32 33 34 35 36
      {
        title: "Issue #{state} by #{user_name}",
        subtitle: "in #{project_link}",
        text: issue_link,
        image: user_avatar
      }
37 38 39 40 41
    end

    private

    def message
Tiago Botelho committed
42 43 44 45 46
      if state == 'opened'
        "[#{project_link}] Issue #{state} by #{user_name}"
      else
        "[#{project_link}] Issue #{issue_link} #{state} by #{user_name}"
      end
47 48 49 50 51 52 53
    end

    def opened_issue?
      action == "open"
    end

    def description_message
54 55 56 57
      [{
        title: issue_title,
        title_link: issue_url,
        text: format(description),
58
        color: "#C95823"
59
      }]
60 61 62
    end

    def project_link
63
      link(project_name, project_url)
64 65 66
    end

    def issue_link
67
      link(issue_title, issue_url)
68 69 70
    end

    def issue_title
Tiago Botelho committed
71
      "#{Issue.reference_prefix}#{issue_iid} #{title}"
72 73 74
    end
  end
end