33# action.rb
44# Part of NetDEF CI System
55#
6+ # This class handles the build action for a given CheckSuite.
7+ # It creates summaries, jobs, and timeout workers for the CheckSuite.
8+ #
9+ # Methods:
10+ # - initialize(check_suite, github, jobs, logger_level: Logger::INFO): Initializes the Action class with the
11+ # given parameters.
12+ # - create_summary(rerun: false): Creates a summary for the CheckSuite, including jobs and timeout workers.
13+ #
14+ # Example usage:
15+ # Github::Build::Action.new(check_suite, github, jobs).create_summary
16+ #
617# Copyright (c) 2023 by
718# Network Device Education Foundation, Inc. ("NetDEF")
819#
1122module Github
1223 module Build
1324 class Action
25+ ##
26+ # Initializes the Action class with the given parameters.
27+ #
28+ # @param [CheckSuite] check_suite The CheckSuite to handle.
29+ # @param [Github] github The Github instance to use.
30+ # @param [Array] jobs The jobs to create for the CheckSuite.
31+ # @param [Integer] logger_level The logging level to use (default: Logger::INFO).
1432 def initialize ( check_suite , github , jobs , logger_level : Logger ::INFO )
1533 @check_suite = check_suite
1634 @github = github
@@ -26,6 +44,10 @@ def initialize(check_suite, github, jobs, logger_level: Logger::INFO)
2644 logger ( Logger ::WARN , ">>>> Building action to CheckSuite: #{ @check_suite . inspect } " )
2745 end
2846
47+ ##
48+ # Creates a summary for the CheckSuite, including jobs and timeout workers.
49+ #
50+ # @param [Boolean] rerun Indicates if the jobs should be rerun (default: false).
2951 def create_summary ( rerun : false )
3052 logger ( Logger ::INFO , "SUMMARY #{ @stages . inspect } " )
3153
@@ -37,10 +59,15 @@ def create_summary(rerun: false)
3759
3860 logger ( Logger ::INFO , "@jobs - #{ @jobs . inspect } " )
3961 create_jobs ( rerun )
62+ create_timeout_worker
4063 end
4164
4265 private
4366
67+ ##
68+ # Creates jobs for the CheckSuite.
69+ #
70+ # @param [Boolean] rerun Indicates if the jobs should be rerun.
4471 def create_jobs ( rerun )
4572 @jobs . each do |job |
4673 ci_job = create_ci_job ( job )
@@ -60,13 +87,32 @@ def create_jobs(rerun)
6087 end
6188 end
6289
90+ ##
91+ # Creates a timeout worker for the CheckSuite.
92+ def create_timeout_worker
93+ logger ( Logger ::INFO , "CiJobStatus::Update: TimeoutExecution for '#{ @check_suite . id } '" )
94+
95+ TimeoutExecution
96+ . delay ( run_at : 30 . minute . from_now . utc , queue : 'timeout_execution' )
97+ . timeout ( @check_suite . id )
98+ end
99+
100+ ##
101+ # Starts the stage in progress if configured to do so.
102+ #
103+ # @param [CiJob] ci_job The CI job to start in progress.
63104 def stage_with_start_in_progress ( ci_job )
64105 return unless !ci_job . stage . nil? and ci_job . stage . configuration . start_in_progress?
65106
66107 ci_job . in_progress ( @github )
67108 ci_job . stage . in_progress ( @github , output : { } )
68109 end
69110
111+ ##
112+ # Creates a CI job for the given job parameters.
113+ #
114+ # @param [Hash] job The job parameters.
115+ # @return [CiJob, nil] The created CI job or nil if the stage configuration is not found.
70116 def create_ci_job ( job )
71117 stage_config = StageConfiguration . find_by ( bamboo_stage_name : job [ :stage ] )
72118
@@ -79,6 +125,10 @@ def create_ci_job(job)
79125 CiJob . create ( check_suite : @check_suite , name : job [ :name ] , job_ref : job [ :job_ref ] , stage : stage )
80126 end
81127
128+ ##
129+ # Creates a check run stage for the given stage configuration.
130+ #
131+ # @param [StageConfiguration] stage_config The stage configuration.
82132 def create_check_run_stage ( stage_config )
83133 stage = Stage . find_by ( name : stage_config . github_check_run_name , check_suite_id : @check_suite . id )
84134
@@ -92,6 +142,11 @@ def create_check_run_stage(stage_config)
92142 stage . enqueue ( @github , output : initial_output ( stage ) )
93143 end
94144
145+ ##
146+ # Creates a new stage for the given stage configuration.
147+ #
148+ # @param [StageConfiguration] stage_config The stage configuration.
149+ # @return [Stage] The created stage.
95150 def create_stage ( stage_config )
96151 name = stage_config . github_check_run_name
97152
@@ -110,6 +165,11 @@ def create_stage(stage_config)
110165 stage
111166 end
112167
168+ ##
169+ # Generates the initial output for a CI job.
170+ #
171+ # @param [CiJob] ci_job The CI job.
172+ # @return [Hash] The initial output.
113173 def initial_output ( ci_job )
114174 output = { title : '' , summary : '' }
115175 url = "https://ci1.netdef.org/browse/#{ ci_job . check_suite . bamboo_ci_ref } "
@@ -120,6 +180,11 @@ def initial_output(ci_job)
120180 output
121181 end
122182
183+ ##
184+ # Logs a message with the given severity.
185+ #
186+ # @param [Integer] severity The severity level.
187+ # @param [String] message The message to log.
123188 def logger ( severity , message )
124189 @loggers . each do |logger_object |
125190 logger_object . add ( severity , message )
0 commit comments