Skip to content

Commit 1e0b09c

Browse files
committed
Add BuildFailureNotifier lib
This class is a service class in charge of the logistics of sending a message to a configured gitter channel for a given repo, notifying there has been a failure.
1 parent 61e28b1 commit 1e0b09c

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

lib/build_failure_notifier.rb

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
require 'travis'
2+
require 'gitter/api'
3+
4+
class BuildFailureNotifier
5+
def self.gitter
6+
@gitter ||= begin
7+
api_url = Settings.gitter.api_url
8+
client_settings = {
9+
:token => Settings.gitter_credentials.token,
10+
:api_prefix => Settings.gitter.api_prefix,
11+
:api_url => api_url && URI(api_url)
12+
}
13+
14+
Gitter::API::Client.new(client_settings)
15+
end
16+
end
17+
18+
attr_reader :build, :build_failure, :repo, :room
19+
20+
alias repo_path room
21+
22+
def initialize(build_failure)
23+
@build_failure = build_failure
24+
25+
@room = build_failure.repo.name
26+
@repo = Travis::Repository.find(room)
27+
@build = v3_client.repo_build(build_failure.travis_build_id)
28+
end
29+
30+
def post_failure
31+
notification_msg = <<~MSG
32+
> ### :red_circle: Build Failure in #{repo_branches_markdown_url}!
33+
>
34+
> **Travis Build**: #{travis_build_url}
35+
MSG
36+
notification_msg << "> **Failure PR**: #{offending_pr}\n" if offending_pr
37+
notification_msg << "> **Commit**: #{commit_url}\n" if commit_url
38+
notification_msg << "> **Compare**: #{compare_url}\n" if compare_url
39+
40+
gitter_room.send_message(notification_msg)
41+
end
42+
43+
private
44+
45+
def gitter
46+
self.class.gitter
47+
end
48+
49+
# join room if needed, otherwise returns room
50+
def gitter_room
51+
@gitter_room ||= gitter.join_room(room)
52+
end
53+
54+
def v3_client
55+
@v3_client ||= TravisV3Client.new(:repo => @repo)
56+
end
57+
58+
def travis_build_url
59+
"#{Travis::Client::ORG_URI}#{repo_path}/builds/#{build.id}"
60+
end
61+
62+
# find the PR that caused this mess...
63+
def offending_pr
64+
if build.commit && build.commit.message =~ /^Merge pull request #(\d+)/
65+
"https://github.com/#{repo_path}/issues/#{$1}"
66+
end
67+
end
68+
69+
def commit_url
70+
if build.commit
71+
"https://github.com/ManageIQ/manageiq/commit/#{build.commit.sha[0, 8]}"
72+
end
73+
end
74+
75+
def compare_url
76+
build.commit.compare_url if build.commit && build.commit.compare_url
77+
end
78+
79+
def repo_branches_markdown_url
80+
"[`#{repo_path}`](https://travis-ci.org/#{repo_path}/branches)"
81+
end
82+
end

0 commit comments

Comments
 (0)