Skip to content

Commit

Permalink
[Branch] Add build failure concept
Browse files Browse the repository at this point in the history
Use the Branch record to keep track of build failures that we find when
polling the Travis API.  The main purpose is to avoid spamming gitter
with extra notifications, but also can be used to identify that a branch
has previously been broken and now has been fixed.

Makes sense to also notify on the passing cases so others aren't taking
the time to investigate a failure when this has already been
investigated/fixed by another.
  • Loading branch information
NickLaMuro committed Mar 18, 2020
1 parent 590d694 commit 69ceb27
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
33 changes: 33 additions & 0 deletions app/models/branch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,37 @@ def fq_branch_name
def git_service
GitService::Branch.new(self)
end

# Branch Failure

def notify_of_failure
if passing?
BuildFailureNotifier.new(self).report_passing
update(:last_build_failure_notified_at => nil, :travis_build_failure_id => nil)
elsif should_notify_of_failure?
update(:last_build_failure_notified_at => Time.zone.now)

BuildFailureNotifier.new(self).post_failure
end
end

def previously_failing?
!!travis_build_failure_id
end

def should_notify_of_failure?
last_build_failure_notified_at.nil? || last_build_failure_notified_at < 1.day.ago
end

# If we have reported a failure before and the branch is now green.
#
# The other advantage of checking `last_build_failure_notified_at.nil?` here
# is that we save a Travis API call, since we shouldn't be creating
# BuildFailure records without having found a build failure elsewhere (e.g.
# TravisBranchMonitor).
#
# New records will short circut before hitting `Travis::Repository.find`.
def passing?
!last_build_failure_notified_at.nil? && Travis::Repository.find(repo.name).branch(name).green?
end
end
6 changes: 6 additions & 0 deletions db/migrate/20200109223351_add_branch_build_failures.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddBranchBuildFailures < ActiveRecord::Migration[5.2]
def change
add_column :branches, :travis_build_failure_id, :integer
add_column :branches, :last_build_failure_notified_at, :datetime
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2017_10_06_050814) do
ActiveRecord::Schema.define(version: 2020_01_09_223351) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -44,6 +44,8 @@
t.string "merge_target"
t.string "pr_title"
t.integer "linter_offense_count"
t.integer "travis_build_failure_id"
t.datetime "last_build_failure_notified_at"
t.index ["repo_id"], name: "index_branches_on_repo_id"
end

Expand Down

0 comments on commit 69ceb27

Please sign in to comment.