Skip to content

Commit

Permalink
Merge pull request #60 from RodrigoMNardi/feature/github/cancelled_info
Browse files Browse the repository at this point in the history
Detailed execution info
  • Loading branch information
RodrigoMNardi authored Apr 29, 2024
2 parents f4257d6 + 51e3551 commit ef38182
Show file tree
Hide file tree
Showing 17 changed files with 330 additions and 25 deletions.
17 changes: 17 additions & 0 deletions db/migrate/20240408141736_add_check_suite_cancelled_ref.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# SPDX-License-Identifier: BSD-2-Clause
#
# 20240408141736_add_check_suite_cancelled_ref.rb
# Part of NetDEF CI System
#
# Copyright (c) 2024 by
# Network Device Education Foundation, Inc. ("NetDEF")
#
# frozen_string_literal: true

class AddCheckSuiteCancelledRef < ActiveRecord::Migration[6.0]
def change
add_column :check_suites, :cancelled_by_id, :bigint
add_index :check_suites, :cancelled_by_id
add_foreign_key :check_suites, :stages, column: :cancelled_by_id
end
end
15 changes: 15 additions & 0 deletions db/migrate/20240408164410_add_check_suite_cancelled_in_stage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# SPDX-License-Identifier: BSD-2-Clause
#
# 20240408152636_add_check_suite_cancelled_in_stage.rb
# Part of NetDEF CI System
#
# Copyright (c) 2024 by
# Network Device Education Foundation, Inc. ("NetDEF")
#
# frozen_string_literal: true

class AddCheckSuiteCancelledInStage < ActiveRecord::Migration[6.0]
def change
add_reference :stages, :cancelled_at_stage, foreign_key: { to_table: :check_suites }
end
end
17 changes: 17 additions & 0 deletions db/migrate/20240417094603_change_check_suite_cancelled_by_id.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# SPDX-License-Identifier: BSD-2-Clause
#
# 20240417094603_change_check_suite_cancelled_by_id.rb
# Part of NetDEF CI System
#
# Copyright (c) 2024 by
# Network Device Education Foundation, Inc. ("NetDEF")
#
# frozen_string_literal: true

class ChangeCheckSuiteCancelledById < ActiveRecord::Migration[6.0]
def change
change_table :check_suites do |t|
t.references :cancelled_previous_check_suite, foreign_key: { to_table: :check_suites }
end
end
end
19 changes: 19 additions & 0 deletions db/migrate/20240417102854_remove_check_suite_columns.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# SPDX-License-Identifier: BSD-2-Clause
#
# 20240417102854_remove_check_suite_columns.rb
# Part of NetDEF CI System
#
# Copyright (c) 2024 by
# Network Device Education Foundation, Inc. ("NetDEF")
#
# frozen_string_literal: true

class RemoveCheckSuiteColumns < ActiveRecord::Migration[6.0]
def change
if ActiveRecord::Base.connection.column_exists?(:check_suites, :cancelled_by_id)
remove_column :check_suites, :cancelled_by_id
end

remove_column :check_suites, :id_id if ActiveRecord::Base.connection.column_exists?(:check_suites, :id_id)
end
end
30 changes: 30 additions & 0 deletions db/migrate/20240417130601_change_check_suite_stopped_in_stage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# SPDX-License-Identifier: BSD-2-Clause
#
# 202404130601_change_check_suite_stopped_in_stage.rb
# Part of NetDEF CI System
#
# Copyright (c) 2024 by
# Network Device Education Foundation, Inc. ("NetDEF")
#
# frozen_string_literal: true

class ChangeCheckSuiteStoppedInStage < ActiveRecord::Migration[6.0]
def change
if ActiveRecord::Base.connection.column_exists?(:stages, :cancelled_at_stage_id)
remove_column :stages, :cancelled_at_stage_id
end

if ActiveRecord::Base.connection.column_exists?(:check_suites, :cancelled_in_stage_id)
remove_column :check_suites, :cancelled_in_stage_id
end

if ActiveRecord::Base.connection.column_exists?(:check_suites, :cancelled_previous_check_suite_id)
remove_column :check_suites, :cancelled_previous_check_suite_id
end

change_table :check_suites do |t|
t.references :stopped_in_stage, foreign_key: { to_table: :stages }
t.references :cancelled_previous_check_suite, foreign_key: { to_table: :check_suites }
end
end
end
8 changes: 7 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[7.0].define(version: 2024_03_27_112035) do
ActiveRecord::Schema[7.0].define(version: 2024_04_17_130601) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -58,8 +58,12 @@
t.integer "retry", default: 0
t.boolean "sync", default: false
t.bigint "github_user_id"
t.bigint "stopped_in_stage_id"
t.bigint "cancelled_previous_check_suite_id"
t.index ["cancelled_previous_check_suite_id"], name: "index_check_suites_on_cancelled_previous_check_suite_id"
t.index ["github_user_id"], name: "index_check_suites_on_github_user_id"
t.index ["pull_request_id"], name: "index_check_suites_on_pull_request_id"
t.index ["stopped_in_stage_id"], name: "index_check_suites_on_stopped_in_stage_id"
end

create_table "ci_jobs", force: :cascade do |t|
Expand Down Expand Up @@ -159,8 +163,10 @@

add_foreign_key "audit_retries", "check_suites"
add_foreign_key "audit_retries", "github_users"
add_foreign_key "check_suites", "check_suites", column: "cancelled_previous_check_suite_id"
add_foreign_key "check_suites", "github_users"
add_foreign_key "check_suites", "pull_requests"
add_foreign_key "check_suites", "stages", column: "stopped_in_stage_id"
add_foreign_key "ci_jobs", "check_suites"
add_foreign_key "ci_jobs", "stages"
add_foreign_key "plans", "check_suites"
Expand Down
3 changes: 3 additions & 0 deletions lib/github/build_plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ def cancel_previous_ci_jobs
ci_job.cancelled(@github_check)
end

@last_check_suite.update(stopped_in_stage: @last_check_suite.stages.where(status: :in_progress).last)

@last_check_suite.stages.where(status: %w[queued in_progress]).each do |stage|
stage.cancelled(@github_check)
end
Expand Down Expand Up @@ -169,6 +171,7 @@ def ci_jobs
end

def stop_execution_message
@check_suite.update(cancelled_previous_check_suite_id: @last_check_suite.id)
BambooCi::StopPlan.comment(@last_check_suite, @check_suite)
end

Expand Down
9 changes: 1 addition & 8 deletions lib/github/plan_execution/finished.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,12 @@ def build_summary(ci_job)
end

def finished_execution?(check_suite)
return false unless current_execution?(check_suite)
return false unless check_suite.pull_request.current_execution?(check_suite)
return false unless check_suite.finished?

SlackBot.instance.execution_finished_notification(check_suite)
end

def current_execution?(check_suite)
pull_request = check_suite.pull_request
last_check_suite = pull_request.check_suites.reload.all.order(:created_at).last

check_suite.id == last_check_suite.id
end

def slack_notify_success(job)
SlackBot.instance.notify_success(job)
end
Expand Down
29 changes: 25 additions & 4 deletions lib/github/re_run/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,31 @@ def stop_previous_execution
logger(Logger::INFO, 'Stopping previous execution')
logger(Logger::INFO, fetch_run_ci_by_pr.inspect)

@last_check_suite = nil

fetch_run_ci_by_pr.each do |check_suite|
check_suite.ci_jobs.not_skipped.each do |ci_job|
ci_job.cancelled(@github_check)
end
stop_and_update_previous_execution(check_suite)
end
end

def stop_and_update_previous_execution(check_suite)
if @last_check_suite.nil?
check_suite.update(stopped_in_stage: check_suite.stages.where(status: :in_progress).last)
else
check_suite.update(cancelled_previous_check_suite_id: @last_check_suite.id)
@last_check_suite.update(stopped_in_stage: check_suite.stages.where(status: :in_progress).last)
end

BambooCi::StopPlan.build(check_suite.bamboo_ci_ref)
cancel_previous_jobs(check_suite)

@last_check_suite = check_suite

BambooCi::StopPlan.build(check_suite.bamboo_ci_ref)
end

def cancel_previous_jobs(check_suite)
check_suite.ci_jobs.not_skipped.each do |ci_job|
ci_job.cancelled(@github_check)
end
end

Expand Down Expand Up @@ -106,6 +125,8 @@ def ci_jobs(check_suite, bamboo_plan)

check_suite.update(bamboo_ci_ref: bamboo_plan.bamboo_reference, re_run: true)

check_suite.update(cancelled_previous_check_suite: @last_check_suite)

create_ci_jobs(bamboo_plan, check_suite)

CheckSuite.where(commit_sha_ref: check_suite.commit_sha_ref).each do |cs|
Expand Down
4 changes: 4 additions & 0 deletions lib/models/check_suite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class CheckSuite < ActiveRecord::Base
validates :commit_sha_ref, presence: true

belongs_to :pull_request

belongs_to :stopped_in_stage, class_name: 'Stage', optional: true
belongs_to :cancelled_previous_check_suite, class_name: 'CheckSuite', optional: true

has_many :ci_jobs, dependent: :delete_all
has_many :stages, dependent: :delete_all
has_many :audit_retries, dependent: :delete_all
Expand Down
32 changes: 32 additions & 0 deletions reports/build_stage_failed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# SPDX-License-Identifier: BSD-2-Clause
#
# build_stage_failed.rb
# Part of NetDEF CI System
#
# Copyright (c) 2024 by
# Network Device Education Foundation, Inc. ("NetDEF")
#
# frozen_string_literal: true

require_relative '../database_loader'
require_relative '../lib/helpers/configuration'

begin_date = ARGV[0]
end_date = ARGV[1]
author = ARGV[2]

check_suites = []

Stage
.joins(:jobs, :check_suite)
.where(stages: { name: 'Build' })
.where(jobs: { created_at: [begin_date..end_date], status: %i[failure skipped] })
.where(check_suites: { author: author })
.each do |stage|
message = "Check Suite ID: https://ci1.netdef.org/browse/#{stage.check_suite.bamboo_ci_ref}"
check_suites << message unless check_suites.include? message
end

check_suites.each do |line|
puts line
end
73 changes: 73 additions & 0 deletions reports/chained_rerun_report.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# SPDX-License-Identifier: BSD-2-Clause
#
# rerun_report.rb
# Part of NetDEF CI System
#
# Copyright (c) 2024 by
# Network Device Education Foundation, Inc. ("NetDEF")
#
# frozen_string_literal: true

require 'json'
require 'csv'
require_relative '../database_loader'

module Reports
class ChainedRerun
def report(begin_date, end_date)
@result = {}
@offenders = []

CheckSuite
.left_joins(:audit_retries)
.where.not(check_suites: { cancelled_previous_check_suite_id: nil })
.where(created_at: [begin_date..end_date])
.order(:created_at)
.group_by(&:pull_request_id)
.each_pair do |pull_request_id, chained_check_suites|
generate_result(pull_request_id, chained_check_suites)
end
end

private

def generate_result(pull_request_id, chained_check_suites)
@local_chained = {}
@chain = 1

create_paths(chained_check_suites)

puts "Pull Request ID: #{pull_request_id}"
@local_chained.each_pair do |_c, path|
puts path.reverse.join(' -> ')
end
end

def create_paths(chained_check_suites)
chained_check_suites.reverse_each do |check_suite|
initialize_or_add(check_suite)

if chained_check_suites.map(&:id).include?(check_suite.cancelled_previous_check_suite_id)
@local_chained[@chain] << check_suite.cancelled_previous_check_suite_id
else
@local_chained[@chain] << CheckSuite.find(check_suite.cancelled_previous_check_suite_id).id
@chain += 1
end
end
end

def initialize_or_add(check_suite)
@local_chained[@chain] ||= []
@local_chained[@chain] << check_suite.id unless @local_chained[@chain].include? check_suite.id
end
end
end

return unless __FILE__ == $PROGRAM_NAME

ActiveRecord::Base.logger = Logger.new('/dev/null')

begin_date = ARGV[0]
end_date = ARGV[1]

Reports::ChainedRerun.new.report(begin_date, end_date)
42 changes: 42 additions & 0 deletions reports/github_user_usage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# SPDX-License-Identifier: BSD-2-Clause
#
# github_user_usage.rb
# Part of NetDEF CI System
#
# Copyright (c) 2024 by
# Network Device Education Foundation, Inc. ("NetDEF")
#
# frozen_string_literal: true

require_relative '../database_loader'

ActiveRecord::Base.logger = Logger.new('/dev/null')

user = GithubUser.find_by_github_login(ARGV[0])

if user.pull_requests.empty?
puts "No pull requests found for user: #{user.github_login}"
else
puts "Pull Requests for user: #{user.github_login}"
user.pull_requests.each do |pr|
puts "Pull Request: https://github.com/FRRouting/frr/pull/#{pr.github_pr_id}"
end
end

if user.check_suites.empty?
puts "No check suites found for user: #{user.github_login}"
else
puts "Check Suites for user: #{user.github_login}"
user.check_suites.each do |cs|
puts "Check Suite: https://ci1.netdef.org/browse/#{cs.bamboo_ci_ref}"
end
end

if user.audit_retries.empty?
puts "No audit retries found for user: #{user.github_login}"
else
puts "Audit Retries for user: #{user.github_login}"
user.audit_retries.each do |ar|
puts "Audit Retry: #{ar.retry_type} at #{ar.created_at}"
end
end
Loading

0 comments on commit ef38182

Please sign in to comment.