-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Worker that is in charge of monitoring Travis for build_failures, and will create BuildFailure records and send messages to gitter as needed.
- Loading branch information
1 parent
c656adc
commit fbdf197
Showing
2 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
require 'travis' | ||
|
||
class TravisBranchMonitor | ||
include Sidekiq::Worker | ||
sidekiq_options :queue => :miq_bot_glacial, :retry => false | ||
|
||
include Sidetiq::Schedulable | ||
recurrence { hourly.minute_of_hour(0, 15, 30, 45) } | ||
|
||
include SidekiqWorkerMixin | ||
|
||
class << self | ||
private | ||
|
||
# For this class, repos will sometimes the repo needs to be mapped to a | ||
# specific gitter room, so a hash is required. | ||
# | ||
# This override allows for doing this in the config | ||
# | ||
# travis_branch_monitor: | ||
# included_repos: | ||
# ManageIQ/manageiq-ui-classic: ManageIQ/ui | ||
# ManageIQ/manageiq-gems-pending: ManageIQ/core | ||
# ManageIQ/manageiq: | ||
# ManageIQ/miq_bot: | ||
# | ||
# Which you are allowed to leave the value empty, and the key will be used | ||
# where appropriate (not used in this class). | ||
# | ||
# The result from the above for this method will then be: | ||
# | ||
# [ | ||
# "ManageIQ/manageiq-ui-classic", | ||
# "ManageIQ/manageiq-gems-pending", | ||
# "ManageIQ/manageiq", | ||
# "ManageIQ/miq_bot" | ||
# ] | ||
# | ||
def included_and_excluded_repos | ||
super # just used for error handling... | ||
|
||
[ | ||
settings.included_repos.try(:to_h).try(:stringify_keys).try(:keys), | ||
settings.excluded_repos.try(:to_h).try(:stringify_keys).try(:keys) | ||
] | ||
end | ||
end | ||
|
||
def perform | ||
if !first_unique_worker? | ||
logger.info("#{self.class} is already running, skipping") | ||
else | ||
process_repos | ||
end | ||
end | ||
|
||
def process_repos | ||
enabled_repos.each do |repo| | ||
process_repo(repo) | ||
end | ||
end | ||
|
||
def process_repo(repo) | ||
# repo.regular_branch_names.each do |branch| | ||
# end | ||
|
||
# TODO: Handle this for all release branches | ||
process_branch(repo, 'master') | ||
end | ||
|
||
def process_branch(repo, branch) | ||
branch_record = repo.regular_branches.where(:name => branch).first | ||
|
||
# If we already have a failure record, call notify with that record | ||
return branch_record.notify_of_failure if branch_record.previously_failing? | ||
|
||
# otherwise, check if any builds exist with a failures, and if so, create a | ||
# new BuildFailure record. | ||
v3_client = TravisV3Client.new(:repo => Travis::Repository.find(repo.name)) | ||
branch_builds = v3_client.repo_branch_builds(branch) | ||
|
||
if branch_builds.first.failed? | ||
first_failure = find_first_recent_failure(branch_builds) | ||
branch_record.update(:travis_build_failure_id => first_failure.id) | ||
|
||
branch_record.notify_of_failure | ||
end | ||
end | ||
|
||
private | ||
|
||
def find_first_recent_failure(builds) | ||
first_recent_failure = nil | ||
builds.each do |build| | ||
if build.passed? | ||
break | ||
else | ||
first_recent_failure = build | ||
end | ||
end | ||
|
||
first_recent_failure | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
describe TravisBranchMonitor do | ||
include IncludedReposConfigMethods | ||
|
||
describe ".included_and_excluded_repos (private)" do | ||
it "builds the list from a hash of only keys" do | ||
stub_settings included_repos_keys_only | ||
expected = %w[ManageIQ/manageiq ManageIQ/miq_bot] | ||
|
||
expect(described_class.send(:included_and_excluded_repos)).to eq([expected, nil]) | ||
end | ||
|
||
it "builds the list from a hash of keys with values" do | ||
stub_settings included_repos_keys_and_values | ||
expected = %w[ManageIQ/manageiq-ui-classic ManageIQ/manageiq-gems-pending] | ||
|
||
expect(described_class.send(:included_and_excluded_repos)).to eq([expected, nil]) | ||
end | ||
|
||
it "builds the list from a mixed hash with keys and some values" do | ||
stub_settings included_repos_mixed_keys_with_some_values | ||
expected = %w[ | ||
ManageIQ/manageiq-ui-classic | ||
ManageIQ/manageiq-gems-pending | ||
ManageIQ/manageiq | ||
ManageIQ/miq_bot | ||
] | ||
|
||
expect(described_class.send(:included_and_excluded_repos)).to eq([expected, nil]) | ||
end | ||
end | ||
end |