summaryrefslogtreecommitdiff
path: root/app/models/project_services/ci/hip_chat_message.rb
blob: cbf325cc5255af0193bbb25155db43de7fe91767 (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
module Ci
  class HipChatMessage
    include Gitlab::Application.routes.url_helpers

    attr_reader :build

    def initialize(build)
      @build = build
    end

    def to_s
      lines = Array.new
      lines.push("<a href=\"#{ci_project_url(project)}\">#{project.name}</a> - ")
      lines.push("<a href=\"#{ci_namespace_project_commit_url(commit.gl_project.namespace, commit.gl_project, commit.sha)}\">Commit ##{commit.id}</a></br>")
      lines.push("#{commit.short_sha} #{commit.git_author_name} - #{commit.git_commit_message}</br>")
      lines.push("#{humanized_status(commit_status)} in #{commit.duration} second(s).")
      lines.join('')
    end

    def status_color(build_or_commit=nil)
      build_or_commit ||= commit_status
      case build_or_commit
      when :success
        'green'
      when :failed, :canceled
        'red'
      else # :pending, :running or unknown
        'yellow'
      end
    end

    def notify?
      [:failed, :canceled].include?(commit_status)
    end


    private

    def commit
      build.commit
    end

    def project
      commit.project
    end

    def build_status
      build.status.to_sym
    end

    def commit_status
      commit.status.to_sym
    end

    def humanized_status(build_or_commit=nil)
      build_or_commit ||= commit_status
      case build_or_commit
      when :pending
        "Pending"
      when :running
        "Running"
      when :failed
        "Failed"
      when :success
        "Successful"
      when :canceled
        "Canceled"
      else
        "Unknown"
      end
    end
  end
end