Skip to content

Commit faee8e5

Browse files
Merge pull request #119 from RodrigoMNardi/feature/gba-9/mulitple-plans
[GBA-9] Allow multiple plans
2 parents 766ac46 + 6eeb448 commit faee8e5

42 files changed

Lines changed: 1171 additions & 508 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
source 'https://rubygems.org'
1212

13-
ruby '3.1.2'
13+
ruby '>= 3.1.2'
1414

1515
# Token
1616
gem 'jwt'
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# SPDX-License-Identifier: BSD-2-Clause
2+
#
3+
# 20250812101554_add_pull_requests_plans.rb
4+
# Part of NetDEF CI System
5+
#
6+
# Copyright (c) 2025 by
7+
# Network Device Education Foundation, Inc. ("NetDEF")
8+
#
9+
# frozen_string_literal: true
10+
11+
class AddPullRequestsPlans < ActiveRecord::Migration[6.0]
12+
def change
13+
add_reference :plans, :pull_request, foreign_key: true
14+
add_column :plans, :name, :string, null: false, default: ''
15+
end
16+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# SPDX-License-Identifier: BSD-2-Clause
2+
#
3+
# 20250822071834_add_check_suite_plan.rb
4+
# Part of NetDEF CI System
5+
#
6+
# Copyright (c) 2025 by
7+
# Network Device Education Foundation, Inc. ("NetDEF")
8+
#
9+
# frozen_string_literal: true
10+
11+
class AddCheckSuitePlan < ActiveRecord::Migration[6.0]
12+
def change
13+
add_reference :check_suites, :plan, foreign_key: true
14+
end
15+
end

db/schema.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[7.2].define(version: 2025_04_16_153222) do
13+
ActiveRecord::Schema[7.2].define(version: 2025_08_22_071834) do
1414
# These are extensions that must be enabled in order to support this database
1515
enable_extension "plpgsql"
1616

@@ -60,8 +60,10 @@
6060
t.bigint "github_user_id"
6161
t.bigint "stopped_in_stage_id"
6262
t.bigint "cancelled_previous_check_suite_id"
63+
t.bigint "plan_id"
6364
t.index ["cancelled_previous_check_suite_id"], name: "index_check_suites_on_cancelled_previous_check_suite_id"
6465
t.index ["github_user_id"], name: "index_check_suites_on_github_user_id"
66+
t.index ["plan_id"], name: "index_check_suites_on_plan_id"
6567
t.index ["pull_request_id"], name: "index_check_suites_on_pull_request_id"
6668
t.index ["stopped_in_stage_id"], name: "index_check_suites_on_stopped_in_stage_id"
6769
end
@@ -130,7 +132,10 @@
130132
t.datetime "created_at", null: false
131133
t.datetime "updated_at", null: false
132134
t.bigint "check_suite_id"
135+
t.bigint "pull_request_id"
136+
t.string "name", default: "", null: false
133137
t.index ["check_suite_id"], name: "index_plans_on_check_suite_id"
138+
t.index ["pull_request_id"], name: "index_plans_on_pull_request_id"
134139
end
135140

136141
create_table "pull_request_subscriptions", force: :cascade do |t|
@@ -195,12 +200,14 @@
195200
add_foreign_key "audit_retries", "github_users"
196201
add_foreign_key "check_suites", "check_suites", column: "cancelled_previous_check_suite_id"
197202
add_foreign_key "check_suites", "github_users"
203+
add_foreign_key "check_suites", "plans"
198204
add_foreign_key "check_suites", "pull_requests"
199205
add_foreign_key "check_suites", "stages", column: "stopped_in_stage_id"
200206
add_foreign_key "ci_jobs", "check_suites"
201207
add_foreign_key "ci_jobs", "stages"
202208
add_foreign_key "github_users", "organizations"
203209
add_foreign_key "plans", "check_suites"
210+
add_foreign_key "plans", "pull_requests"
204211
add_foreign_key "pull_request_subscriptions", "pull_requests"
205212
add_foreign_key "pull_requests", "github_users"
206213
add_foreign_key "stages", "check_suites"

lib/bamboo_ci/api.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ def get_status(id)
2828
get_request(URI("https://127.0.0.1/rest/api/latest/result/#{id}?expand=stages.stage.results,artifacts"))
2929
end
3030

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

3434
url += custom_variables(check_suite)
3535

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

4242
# Fetch Request
43-
post_request(URI(url))
43+
post_request(URI(url.delete(' ')))
4444
end
4545

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

6060
# Fetch Request
61-
post_request(URI(url), body: "<comment><content>#{comment}</content></comment>")
61+
post_request(URI(url.delete(' ')), body: "<comment><content>#{comment}</content></comment>")
6262
end
6363

6464
def logger(severity, message)

lib/bamboo_ci/plan_run.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class PlanRun
2222
attr_reader :ci_key
2323
attr_accessor :checks_run, :ci_variables
2424

25-
def initialize(check_suite, logger_level: Logger::INFO)
25+
def initialize(check_suite, plan, logger_level: Logger::INFO)
2626
@logger_manager = []
2727
@logger_level = logger_level
2828

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

3434
@check_suite = check_suite
35+
@plan = plan
3536
@ci_variables = []
3637
end
3738

3839
def start_plan
39-
@response = submit_pr_to_ci(@check_suite, @ci_variables)
40+
@refs = []
41+
@response = submit_pr_to_ci(@check_suite, @plan, @ci_variables)
4042

4143
case @response&.code.to_i
4244
when 200, 201
45+
@check_suite.update(bamboo_ci_ref: JSON.parse(@response.body)['buildResultKey'])
46+
4347
success(@response)
4448
when 400..500
4549
failed(@response)
@@ -58,6 +62,10 @@ def bamboo_reference
5862
JSON.parse(@response.body)['buildResultKey']
5963
end
6064

65+
def bamboo_references
66+
@refs
67+
end
68+
6169
private
6270

6371
def success(response)

lib/github/build/action.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,17 @@ class Action
2626
# Initializes the Action class with the given parameters.
2727
#
2828
# @param [CheckSuite] check_suite The CheckSuite to handle.
29-
# @param [Github] github The Github instance to use.
29+
# @param [Github::Check] github The Github::Check instance to use.
3030
# @param [Array] jobs The jobs to create for the CheckSuite.
31+
# @param [String] Stage Plan name.
3132
# @param [Integer] logger_level The logging level to use (default: Logger::INFO).
32-
def initialize(check_suite, github, jobs, logger_level: Logger::INFO)
33+
def initialize(check_suite, github, jobs, name, logger_level: Logger::INFO)
3334
@check_suite = check_suite
3435
@github = github
3536
@jobs = jobs
3637
@loggers = []
37-
@stages = StageConfiguration.all
38+
@stages_config = StageConfiguration.all
39+
@name = name
3840

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

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

56-
@stages.each do |stage_config|
58+
@stages_config.each do |stage_config|
5759
create_check_run_stage(stage_config)
5860
end
5961

@@ -118,7 +120,7 @@ def create_ci_job(job)
118120

119121
return if stage_config.nil?
120122

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

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

@@ -130,9 +132,8 @@ def create_ci_job(job)
130132
#
131133
# @param [StageConfiguration] stage_config The stage configuration.
132134
def create_check_run_stage(stage_config)
133-
stage = Stage.find_by(name: stage_config.github_check_run_name, check_suite_id: @check_suite.id)
134-
135-
logger(Logger::INFO, "STAGE #{stage_config.github_check_run_name} #{stage.inspect} - @#{@check_suite.inspect}")
135+
logger(Logger::INFO, "create_check_run_stage - #{stage_config.github_check_run_name} - #{@name}")
136+
stage = Stage.find_by(name: "#{stage_config.github_check_run_name} - #{@name}", check_suite_id: @check_suite.id)
136137

137138
return create_stage(stage_config) if stage.nil?
138139
return unless stage.configuration.can_retry?
@@ -148,7 +149,7 @@ def create_check_run_stage(stage_config)
148149
# @param [StageConfiguration] stage_config The stage configuration.
149150
# @return [Stage] The created stage.
150151
def create_stage(stage_config)
151-
name = stage_config.github_check_run_name
152+
name = "#{stage_config.github_check_run_name} - #{@name}"
152153

153154
stage =
154155
Stage.create(check_suite: @check_suite,

lib/github/build/plan_run.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# SPDX-License-Identifier: BSD-2-Clause
2+
#
3+
# plan_run.rb
4+
# Part of NetDEF CI System
5+
#
6+
# Copyright (c) 2025 by
7+
# Network Device Education Foundation, Inc. ("NetDEF")
8+
#
9+
# frozen_string_literal: true
10+
11+
module Github
12+
module Build
13+
class PlanRun
14+
TIMER = 5 # seconds
15+
def initialize(pull_request, payload)
16+
@pull_request = pull_request
17+
@payload = payload
18+
end
19+
20+
def build
21+
return [422, 'No Plans associated with this Pull Request'] if @pull_request.plans.empty?
22+
23+
@pull_request.plans.each do |plan|
24+
CreateExecutionByPlan
25+
.delay(run_at: TIMER.seconds.from_now.utc, queue: 'create_execution_by_plan')
26+
.create(@pull_request.id, @payload, plan)
27+
end
28+
29+
[200, 'Scheduled Plan Runs']
30+
end
31+
end
32+
end
33+
end

lib/github/build/skip_old_tests.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ def skipping_old_test(check_run)
3535
return if check_run[:app][:name] != 'NetDEF CI Hook' or @stages.include?(check_run[:name])
3636

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

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

0 commit comments

Comments
 (0)