Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

source 'https://rubygems.org'

ruby '3.1.2'
ruby '>= 3.1.2'

# Token
gem 'jwt'
Expand Down
16 changes: 16 additions & 0 deletions db/migrate/20250812101554_add_pull_requests_plans.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# SPDX-License-Identifier: BSD-2-Clause
#
# 20250812101554_add_pull_requests_plans.rb
# Part of NetDEF CI System
#
# Copyright (c) 2025 by
# Network Device Education Foundation, Inc. ("NetDEF")
#
# frozen_string_literal: true

class AddPullRequestsPlans < ActiveRecord::Migration[6.0]
def change
add_reference :plans, :pull_request, foreign_key: true
add_column :plans, :name, :string, null: false, default: ''
end
end
15 changes: 15 additions & 0 deletions db/migrate/20250822071834_add_check_suite_plan.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# SPDX-License-Identifier: BSD-2-Clause
#
# 20250822071834_add_check_suite_plan.rb
# Part of NetDEF CI System
#
# Copyright (c) 2025 by
# Network Device Education Foundation, Inc. ("NetDEF")
#
# frozen_string_literal: true

class AddCheckSuitePlan < ActiveRecord::Migration[6.0]
def change
add_reference :check_suites, :plan, foreign_key: true
end
end
9 changes: 8 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.2].define(version: 2025_04_16_153222) do
ActiveRecord::Schema[7.2].define(version: 2025_08_22_071834) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -60,8 +60,10 @@
t.bigint "github_user_id"
t.bigint "stopped_in_stage_id"
t.bigint "cancelled_previous_check_suite_id"
t.bigint "plan_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 ["plan_id"], name: "index_check_suites_on_plan_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
Expand Down Expand Up @@ -130,7 +132,10 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "check_suite_id"
t.bigint "pull_request_id"
t.string "name", default: "", null: false
t.index ["check_suite_id"], name: "index_plans_on_check_suite_id"
t.index ["pull_request_id"], name: "index_plans_on_pull_request_id"
end

create_table "pull_request_subscriptions", force: :cascade do |t|
Expand Down Expand Up @@ -195,12 +200,14 @@
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", "plans"
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 "github_users", "organizations"
add_foreign_key "plans", "check_suites"
add_foreign_key "plans", "pull_requests"
add_foreign_key "pull_request_subscriptions", "pull_requests"
add_foreign_key "pull_requests", "github_users"
add_foreign_key "stages", "check_suites"
Expand Down
8 changes: 4 additions & 4 deletions lib/bamboo_ci/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def get_status(id)
get_request(URI("https://127.0.0.1/rest/api/latest/result/#{id}?expand=stages.stage.results,artifacts"))
end

def submit_pr_to_ci(check_suite, ci_variables)
url = "https://127.0.0.1/rest/api/latest/queue/#{check_suite.pull_request.plan}"
def submit_pr_to_ci(check_suite, plan, ci_variables)
url = "https://127.0.0.1/rest/api/latest/queue/#{plan.bamboo_ci_plan_name}"

url += custom_variables(check_suite)

Expand All @@ -40,7 +40,7 @@ def submit_pr_to_ci(check_suite, ci_variables)
logger(Logger::DEBUG, "Submission URL:\n #{url}")

# Fetch Request
post_request(URI(url))
post_request(URI(url.delete(' ')))
end

def custom_variables(check_suite)
Expand All @@ -58,7 +58,7 @@ def add_comment_to_ci(key, comment)
logger(Logger::DEBUG, "Comment Submission URL:\n #{url}")

# Fetch Request
post_request(URI(url), body: "<comment><content>#{comment}</content></comment>")
post_request(URI(url.delete(' ')), body: "<comment><content>#{comment}</content></comment>")
end

def logger(severity, message)
Expand Down
12 changes: 10 additions & 2 deletions lib/bamboo_ci/plan_run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class PlanRun
attr_reader :ci_key
attr_accessor :checks_run, :ci_variables

def initialize(check_suite, logger_level: Logger::INFO)
def initialize(check_suite, plan, logger_level: Logger::INFO)
@logger_manager = []
@logger_level = logger_level

Expand All @@ -32,14 +32,18 @@ def initialize(check_suite, logger_level: Logger::INFO)
logger(Logger::INFO, "BambooCi::PlanRun - CheckSuite: #{check_suite.inspect}")

@check_suite = check_suite
@plan = plan
@ci_variables = []
end

def start_plan
@response = submit_pr_to_ci(@check_suite, @ci_variables)
@refs = []
@response = submit_pr_to_ci(@check_suite, @plan, @ci_variables)

case @response&.code.to_i
when 200, 201
@check_suite.update(bamboo_ci_ref: JSON.parse(@response.body)['buildResultKey'])

success(@response)
when 400..500
failed(@response)
Expand All @@ -58,6 +62,10 @@ def bamboo_reference
JSON.parse(@response.body)['buildResultKey']
end

def bamboo_references
@refs
end

private

def success(response)
Expand Down
21 changes: 11 additions & 10 deletions lib/github/build/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ class Action
# Initializes the Action class with the given parameters.
#
# @param [CheckSuite] check_suite The CheckSuite to handle.
# @param [Github] github The Github instance to use.
# @param [Github::Check] github The Github::Check instance to use.
# @param [Array] jobs The jobs to create for the CheckSuite.
# @param [String] Stage Plan name.
# @param [Integer] logger_level The logging level to use (default: Logger::INFO).
def initialize(check_suite, github, jobs, logger_level: Logger::INFO)
def initialize(check_suite, github, jobs, name, logger_level: Logger::INFO)
@check_suite = check_suite
@github = github
@jobs = jobs
@loggers = []
@stages = StageConfiguration.all
@stages_config = StageConfiguration.all
@name = name

%w[github_app.log github_build_action.log].each do |filename|
@loggers << GithubLogger.instance.create(filename, logger_level)
Expand All @@ -49,11 +51,11 @@ def initialize(check_suite, github, jobs, logger_level: Logger::INFO)
#
# @param [Boolean] rerun Indicates if the jobs should be rerun (default: false).
def create_summary(rerun: false)
logger(Logger::INFO, "SUMMARY #{@stages.inspect}")
logger(Logger::INFO, "SUMMARY #{@stages_config.inspect}")

Github::Build::SkipOldTests.new(@check_suite).skip_old_tests

@stages.each do |stage_config|
@stages_config.each do |stage_config|
create_check_run_stage(stage_config)
end

Expand Down Expand Up @@ -118,7 +120,7 @@ def create_ci_job(job)

return if stage_config.nil?

stage = Stage.find_by(check_suite: @check_suite, name: stage_config.github_check_run_name)
stage = Stage.find_by(check_suite: @check_suite, name: "#{stage_config.github_check_run_name} - #{@name}")

logger(Logger::INFO, "create_jobs - #{job.inspect} -> #{stage.inspect}")

Expand All @@ -130,9 +132,8 @@ def create_ci_job(job)
#
# @param [StageConfiguration] stage_config The stage configuration.
def create_check_run_stage(stage_config)
stage = Stage.find_by(name: stage_config.github_check_run_name, check_suite_id: @check_suite.id)

logger(Logger::INFO, "STAGE #{stage_config.github_check_run_name} #{stage.inspect} - @#{@check_suite.inspect}")
logger(Logger::INFO, "create_check_run_stage - #{stage_config.github_check_run_name} - #{@name}")
stage = Stage.find_by(name: "#{stage_config.github_check_run_name} - #{@name}", check_suite_id: @check_suite.id)

return create_stage(stage_config) if stage.nil?
return unless stage.configuration.can_retry?
Expand All @@ -148,7 +149,7 @@ def create_check_run_stage(stage_config)
# @param [StageConfiguration] stage_config The stage configuration.
# @return [Stage] The created stage.
def create_stage(stage_config)
name = stage_config.github_check_run_name
name = "#{stage_config.github_check_run_name} - #{@name}"

stage =
Stage.create(check_suite: @check_suite,
Expand Down
33 changes: 33 additions & 0 deletions lib/github/build/plan_run.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# SPDX-License-Identifier: BSD-2-Clause
#
# plan_run.rb
# Part of NetDEF CI System
#
# Copyright (c) 2025 by
# Network Device Education Foundation, Inc. ("NetDEF")
#
# frozen_string_literal: true

module Github
module Build
class PlanRun
TIMER = 5 # seconds
def initialize(pull_request, payload)
@pull_request = pull_request
@payload = payload
end

def build
return [422, 'No Plans associated with this Pull Request'] if @pull_request.plans.empty?

@pull_request.plans.each do |plan|
CreateExecutionByPlan
.delay(run_at: TIMER.seconds.from_now.utc, queue: 'create_execution_by_plan')
.create(@pull_request.id, @payload, plan)
end

[200, 'Scheduled Plan Runs']
end
end
end
end
1 change: 0 additions & 1 deletion lib/github/build/skip_old_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def skipping_old_test(check_run)
return if check_run[:app][:name] != 'NetDEF CI Hook' or @stages.include?(check_run[:name])

@logger.info("Skipping old test suite: #{check_run[:name]}")
puts("Skipping old test suite: #{check_run[:name]}")

message = 'Old test suite, skipping...'
@github.skipped(check_run[:id], { title: "#{check_run[:name]} summary", summary: message })
Expand Down
Loading