summaryrefslogtreecommitdiff
path: root/app/models/project_services/chat_message/build_message.rb
blob: c776e0a20c411fb964786fddf00390d6e08cc4d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
module ChatMessage
  class BuildMessage < BaseMessage
    attr_reader :sha
    attr_reader :ref_type
    attr_reader :ref
    attr_reader :status
    attr_reader :project_name
    attr_reader :project_url
    attr_reader :user_name
    attr_reader :user_url
    attr_reader :duration
    attr_reader :stage
    attr_reader :build_id
    attr_reader :build_name

    def initialize(params)
      @sha = params[:sha]
      @ref_type = params[:tag] ? 'tag' : 'branch'
      @ref = params[:ref]
      @project_name = params[:project_name]
      @project_url = params[:project_url]
      @status = params[:commit][:status]
      @user_name = params[:commit][:author_name]
      @user_url = params[:commit][:author_url]
      @duration = params[:commit][:duration]
      @stage = params[:build_stage]
      @build_name = params[:build_name]
      @build_id = params[:build_id]
    end

    def pretext
      ''
    end

    def fallback
      format(message)
    end

    def attachments
      [{ text: format(message), color: attachment_color }]
    end

    private

    def message
      "#{project_link}: Commit #{commit_link} of #{branch_link} #{ref_type} by #{user_link} #{humanized_status} on build #{build_link} of stage #{stage} in #{duration} #{'second'.pluralize(duration)}"
    end

    def build_url
      "#{project_url}/builds/#{build_id}"
    end

    def build_link
      link(build_name, build_url)
    end

    def user_link
      link(user_name, user_url)
    end

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

    def humanized_status
      case status
      when 'success'
        'passed'
      else
        status
      end
    end

    def attachment_color
      if status == 'success'
        'good'
      else
        'danger'
      end
    end

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

    def branch_link
      link(ref, branch_url)
    end

    def project_link
      link(project_name, project_url)
    end

    def commit_url
      "#{project_url}/commit/#{sha}/builds"
    end

    def commit_link
      link(Commit.truncate_sha(sha), commit_url)
    end
  end
end