Skip to content

Commit

Permalink
Add BuildFailureNotifier lib
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
NickLaMuro committed Apr 7, 2020
1 parent 84eb3e9 commit 21cbf41
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 0 deletions.
98 changes: 98 additions & 0 deletions lib/build_failure_notifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
require 'travis'
require 'gitter/api'

class BuildFailureNotifier
def self.gitter
@gitter ||= begin
api_url = Settings.gitter.api_url
client_settings = {
:token => Settings.gitter_credentials.token,
:api_prefix => Settings.gitter.api_prefix,
:api_uri => api_url && URI(api_url)
}

Gitter::API::Client.new(client_settings)
end
end

def self.repo_room_map
settings = Settings.travis_branch_monitor
repo_map = settings.included_repos.to_h.merge(settings.excluded_repos.to_h)

repo_map.stringify_keys!
repo_map.each do |repo, room_uri|
repo_map[repo] = repo if room_uri.nil?
end
end

attr_reader :branch, :build, :repo, :repo_path, :room

def initialize(branch)
@branch = branch
@repo_path = branch.repo.name
@repo = Travis::Repository.find(repo_path)
@room = repo_room_map[repo_path]
@build = repo.session.find_one(Travis::Client::Build, branch.travis_build_failure_id)
end

def post_failure
notification_msg = <<~MSG
> ### :red_circle: Build Failure in #{repo_branches_markdown_url}!
>
> **Travis Build**: #{travis_build_url}
MSG
notification_msg << "> **Failure PR**: #{offending_pr}\n" if offending_pr
notification_msg << "> **Commit**: #{commit_url}\n" if commit_url
notification_msg << "> **Compare**: #{compare_url}\n" if compare_url

gitter_room.send_message(notification_msg)
end

def report_passing
notification_msg = <<~MSG
> ### :green_heart: #{repo_branches_markdown_url} now passing!
MSG

gitter_room.send_message(notification_msg)
end

private

def gitter
self.class.gitter
end

def repo_room_map
self.class.repo_room_map
end

# join room if needed, otherwise returns room
def gitter_room
@gitter_room ||= gitter.join_room(room)
end

def travis_build_url
"https://travis-ci.org/#{repo_path}/builds/#{build.id}"
end

# find the PR that caused this mess...
def offending_pr
if build.commit && build.commit.message =~ /^Merge pull request #(\d+)/
"https://github.com/#{repo_path}/issues/#{$1}"
end
end

def commit_url
if build.commit
"https://github.com/#{repo_path}/commit/#{build.commit.sha[0, 8]}"
end
end

def compare_url
build.commit.compare_url if build.commit && build.commit.compare_url
end

def repo_branches_markdown_url
"[`#{repo_path}`](https://travis-ci.org/#{repo_path}/branches)"
end
end
37 changes: 37 additions & 0 deletions spec/lib/build_failure_notifier_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
describe BuildFailureNotifier do
include IncludedReposConfigMethods

describe "#repo_room_map (private)" do
it "builds a map from a hash of only keys" do
stub_settings included_repos_keys_only
expected_map = {
"ManageIQ/manageiq" => "ManageIQ/manageiq",
"ManageIQ/miq_bot" => "ManageIQ/miq_bot"
}

expect(described_class.repo_room_map).to eq(expected_map)
end

it "builds a map from a hash of keys with values" do
stub_settings included_repos_keys_and_values
expected_map = {
"ManageIQ/manageiq-ui-classic" => "ManageIQ/ui",
"ManageIQ/manageiq-gems-pending" => "ManageIQ/core"
}

expect(described_class.repo_room_map).to eq(expected_map)
end

it "builds a map from a mixed hash with keys and some values" do
stub_settings included_repos_mixed_keys_with_some_values
expected_map = {
"ManageIQ/manageiq-ui-classic" => "ManageIQ/ui",
"ManageIQ/manageiq-gems-pending" => "ManageIQ/core",
"ManageIQ/manageiq" => "ManageIQ/manageiq",
"ManageIQ/miq_bot" => "ManageIQ/miq_bot"
}

expect(described_class.repo_room_map).to eq(expected_map)
end
end
end
33 changes: 33 additions & 0 deletions spec/support/travis_branch_monitor_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'yaml'

module IncludedReposConfigMethods
def included_repos_keys_only
YAML.safe_load <<~YAML
---
travis_branch_monitor:
included_repos:
ManageIQ/manageiq:
ManageIQ/miq_bot:
YAML
end

def included_repos_keys_and_values
YAML.safe_load <<~YAML
travis_branch_monitor:
included_repos:
ManageIQ/manageiq-ui-classic: ManageIQ/ui
ManageIQ/manageiq-gems-pending: ManageIQ/core
YAML
end

def included_repos_mixed_keys_with_some_values
YAML.safe_load <<~YAML
travis_branch_monitor:
included_repos:
ManageIQ/manageiq-ui-classic: ManageIQ/ui
ManageIQ/manageiq-gems-pending: ManageIQ/core
ManageIQ/manageiq:
ManageIQ/miq_bot:
YAML
end
end

0 comments on commit 21cbf41

Please sign in to comment.