diff --git a/app/models/branch.rb b/app/models/branch.rb index 976ab852..577e6b26 100644 --- a/app/models/branch.rb +++ b/app/models/branch.rb @@ -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 diff --git a/db/migrate/20200109223351_add_branch_build_failures.rb b/db/migrate/20200109223351_add_branch_build_failures.rb new file mode 100644 index 00000000..1d1d5927 --- /dev/null +++ b/db/migrate/20200109223351_add_branch_build_failures.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 1294151d..78ae5a11 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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