push_message.rb 2.5 KB
Newer Older
F
Felipe Artur 已提交
1
module ChatMessage
D
Douwe Maan 已提交
2
  class PushMessage < BaseMessage
3 4 5 6
    attr_reader :after
    attr_reader :before
    attr_reader :commits
    attr_reader :ref
7
    attr_reader :ref_type
8 9

    def initialize(params)
T
Tiago Botelho 已提交
10
      super
11

12 13 14
      @after = params[:after]
      @before = params[:before]
      @commits = params.fetch(:commits, [])
15 16
      @ref_type = Gitlab::Git.tag_ref?(params[:ref]) ? 'tag' : 'branch'
      @ref = Gitlab::Git.ref_name(params[:ref])
17 18
    end

T
Tiago Botelho 已提交
19 20 21
    def attachments
      return [] if new_branch? || removed_branch?
      return commit_messages if markdown
22

T
Tiago Botelho 已提交
23
      commit_message_attachments
24 25
    end

T
Tiago Botelho 已提交
26 27 28 29 30 31 32 33
    def activity
      action = if new_branch?
                 "created"
               elsif removed_branch?
                 "removed"
               else
                 "pushed to"
               end
34

T
Tiago Botelho 已提交
35 36 37 38 39 40
      {
        title: "#{user_name} #{action} #{ref_type}",
        subtitle: "in #{project_link}",
        text: compare_link,
        image: user_avatar
      }
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    end

    private

    def message
      if new_branch?
        new_branch_message
      elsif removed_branch?
        removed_branch_message
      else
        push_message
      end
    end

    def format(string)
      Slack::Notifier::LinkFormatter.format(string)
    end

    def new_branch_message
60
      "#{user_name} pushed new #{ref_type} #{branch_link} to #{project_link}"
61 62 63
    end

    def removed_branch_message
64
      "#{user_name} removed #{ref_type} #{ref} from #{project_link}"
65 66 67
    end

    def push_message
68
      "#{user_name} pushed to #{ref_type} #{branch_link} of #{project_link} (#{compare_link})"
69 70 71
    end

    def commit_messages
72
      commits.map { |commit| compose_commit_message(commit) }.join("\n\n")
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    end

    def commit_message_attachments
      [{ text: format(commit_messages), color: attachment_color }]
    end

    def compose_commit_message(commit)
      author = commit[:author][:name]
      id = Commit.truncate_sha(commit[:id])
      message = commit[:message]
      url = commit[:url]

      "[#{id}](#{url}): #{message} - #{author}"
    end

    def new_branch?
89
      Gitlab::Git.blank_ref?(before)
90 91 92
    end

    def removed_branch?
93
      Gitlab::Git.blank_ref?(after)
94 95 96 97 98 99 100 101 102 103 104
    end

    def branch_url
      "#{project_url}/commits/#{ref}"
    end

    def compare_url
      "#{project_url}/compare/#{before}...#{after}"
    end

    def branch_link
105
      "[#{ref}](#{branch_url})"
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    end

    def project_link
      "[#{project_name}](#{project_url})"
    end

    def compare_link
      "[Compare changes](#{compare_url})"
    end

    def attachment_color
      '#345'
    end
  end
end