Skip to content

Commit

Permalink
Merge pull request #66 from FRRouting/master
Browse files Browse the repository at this point in the history
Release 2.3.0
  • Loading branch information
RodrigoMNardi authored May 18, 2024
2 parents 9909617 + 13204c6 commit f88441d
Show file tree
Hide file tree
Showing 33 changed files with 828 additions and 184 deletions.
16 changes: 12 additions & 4 deletions app/github_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,23 @@ def sinatra_logger_level

halt 200, 'OK' unless %w[rerequested].include? payload['action'].downcase

re_run = Github::Retry.new(payload, logger_level: GithubApp.sinatra_logger_level)
re_run = Github::Retry::Command.new(payload, logger_level: GithubApp.sinatra_logger_level)
halt re_run.start
when 'installation'
logger.debug '>>> Received a new installation policy'
halt 202, 'Updated'
when 'issue_comment'
logger.debug '>>> Received a new issue comment'

halt Github::ReRun::Comment.new(payload, logger_level: GithubApp.sinatra_logger_level).start
halt 404, 'Action not found' if payload.nil? or payload['comment'].nil?

case payload.dig('comment', 'body')
when /ci:retry/
halt Github::Retry::Comment.new(payload, logger_level: GithubApp.sinatra_logger_level).start
when /ci:rerun/
halt Github::ReRun::Comment.new(payload, logger_level: GithubApp.sinatra_logger_level).start
else
logger.debug '>>> Just a comment'
halt 200, 'Just a comment'
end
when 'check_suite'
logger.debug '>>> Received a new check_suite command'
halt 200, 'OK' unless payload['action'].downcase.match?('rerequested')
Expand Down
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
1 change: 1 addition & 0 deletions lib/github/build/retry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def enqueued_stages

stage = Stage.find_by(check_suite: @check_suite, name: bamboo_stage.github_check_run_name)

next if stage.nil?
next if stage.success?

url = "https://ci1.netdef.org/browse/#{stage.check_suite.bamboo_ci_ref}"
Expand Down
11 changes: 10 additions & 1 deletion lib/github/build/summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def initialize(job, logger_level: Logger::INFO, agent: 'Github')
@loggers << GithubLogger.instance.create(filename, logger_level)
end

@loggers << GithubLogger.instance.create("pr#{@check_suite.pull_request.github_pr_id}.log", logger_level)
@pr_log = GithubLogger.instance.create("pr#{@check_suite.pull_request.github_pr_id}.log", logger_level)
end

def build_summary
Expand All @@ -36,6 +36,8 @@ def build_summary
check_and_update_github_ref(current_stage)

logger(Logger::INFO, "build_summary: #{current_stage.inspect}")
msg = "Github::Build::Summary - #{@job.inspect}, #{current_stage.inspect}, bamboo info: #{bamboo_info}"
@pr_log.info(msg)

# Update current stage
update_summary(current_stage)
Expand All @@ -52,6 +54,11 @@ def build_summary

private

def bamboo_info
finish = Github::PlanExecution::Finished.new({ 'bamboo_ref' => @check_suite.bamboo_ci_ref })
finish.fetch_build_status
end

def check_and_update_github_ref(current_stage)
current_refs = @github.fetch_check_runs

Expand All @@ -73,6 +80,8 @@ def must_update_previous_stage(current_stage)

return if previous_stage.nil? or !(previous_stage.in_progress? or previous_stage.queued?)

logger(Logger::INFO, "must_update_previous_stage: #{previous_stage.inspect}")

finished_stage_summary(previous_stage)
end

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
4 changes: 4 additions & 0 deletions lib/github/check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def comment_reaction_thumb_up(repo, comment_id)
@app.create_issue_comment_reaction(repo, comment_id, '+1')
end

def comment_reaction_thumb_down(repo, comment_id)
@app.create_issue_comment_reaction(repo, comment_id, '-1')
end

def create(name)
@app.create_check_run(
@check_suite.pull_request.repository,
Expand Down
37 changes: 15 additions & 22 deletions lib/github/plan_execution/finished.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,8 @@ def finished
[200, 'Finished']
end

private

# This method will move all tests that no longer exist in BambooCI to the skipped state,
# because there are no executions for them.
def clear_deleted_jobs
github_check = Github::Check.new(@check_suite)

@check_suite.ci_jobs.where(status: %w[queued in_progress]).each do |ci_job|
ci_job.skipped(github_check)
end
def fetch_build_status
get_request(URI("https://127.0.0.1/rest/api/latest/result/status/#{@check_suite.bamboo_ci_ref}"))
end

# Checks if CI still running
Expand All @@ -63,6 +55,18 @@ def in_progress?(build_status)
true
end

private

# This method will move all tests that no longer exist in BambooCI to the skipped state,
# because there are no executions for them.
def clear_deleted_jobs
github_check = Github::Check.new(@check_suite)

@check_suite.ci_jobs.where(status: %w[queued in_progress]).each do |ci_job|
ci_job.skipped(github_check)
end
end

def ci_stopped?(build_status)
build_status.key?('message') and !build_status.key?('finished')
end
Expand Down Expand Up @@ -118,19 +122,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 Expand Up @@ -158,10 +155,6 @@ def check_stages
def fetch_ci_execution
@result = get_status(@check_suite.bamboo_ci_ref)
end

def fetch_build_status
get_request(URI("https://127.0.0.1/rest/api/latest/result/status/#{@check_suite.bamboo_ci_ref}"))
end
end
end
end
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: 1 addition & 3 deletions lib/github/re_run/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,7 @@ def sha256
end

def action?
return false if action.nil?

action.downcase.match? 'ci:rerun' and @payload['action'] == 'created'
action.to_s.downcase.match? 'ci:rerun' and @payload['action'] == 'created'
end
end
end
Expand Down
Loading

0 comments on commit f88441d

Please sign in to comment.