3
3
# check_suite_finished.rb
4
4
# Part of NetDEF CI System
5
5
#
6
+ # This class handles the logic for determining if a CheckSuite has finished execution.
7
+ # It interacts with the Bamboo CI system to fetch the build status and updates the CheckSuite accordingly.
8
+ #
9
+ # Methods:
10
+ # - initialize(payload): Initializes the Finished class with the given payload.
11
+ # - finished: Main method to handle the completion logic for a CheckSuite.
12
+ # - fetch_build_status: Fetches the build status from Bamboo CI.
13
+ # - in_progress?(build_status): Checks if the CI build is still in progress.
14
+ #
15
+ # Example usage:
16
+ # Github::PlanExecution::Finished.new(payload).finished
17
+ #
6
18
# Copyright (c) 2024 by
7
19
# Network Device Education Foundation, Inc. ("NetDEF")
8
20
#
@@ -17,12 +29,22 @@ module PlanExecution
17
29
class Finished
18
30
include BambooCi ::Api
19
31
32
+ ##
33
+ # Initializes the Finished class with the given payload.
34
+ #
35
+ # @param [Hash] payload The payload containing information about the CheckSuite.
20
36
def initialize ( payload )
21
- @check_suite = CheckSuite . find_by ( bamboo_ci_ref : payload [ 'bamboo_ref' ] )
37
+ @check_suite = CheckSuite . find_by ( bamboo_ci_ref : payload [ 'bamboo_ref' ] ) if payload [ 'bamboo_ref' ]
38
+ @check_suite = CheckSuite . find ( payload [ 'check_suite_id' ] ) if payload [ 'check_suite_id' ]
22
39
@logger = GithubLogger . instance . create ( 'github_plan_execution_finished.log' , Logger ::INFO )
23
40
@hanged = payload [ 'hanged' ] || false
24
41
end
25
42
43
+ ##
44
+ # Main method to handle the completion logic for a CheckSuite.
45
+ # Fetches the CI execution status and updates the CheckSuite accordingly.
46
+ #
47
+ # @return [Array] An array containing the status code and message.
26
48
def finished
27
49
@logger . info ">>> Check Suite: #{ @check_suite . inspect } "
28
50
@@ -31,9 +53,9 @@ def finished
31
53
fetch_ci_execution
32
54
build_status = fetch_build_status
33
55
34
- @logger . info ">>> build_status: #{ build_status . inspect } "
56
+ @logger . info ">>> build_status: #{ build_status . inspect } . Hanged? #{ @hanged } "
35
57
36
- return [ 200 , 'Still running' ] if in_progress? ( build_status )
58
+ return [ 200 , 'Still running' ] if in_progress? ( build_status ) and ! @hanged
37
59
38
60
check_stages
39
61
clear_deleted_jobs
@@ -42,11 +64,19 @@ def finished
42
64
[ 200 , 'Finished' ]
43
65
end
44
66
67
+ ##
68
+ # Fetches the build status from Bamboo CI.
69
+ #
70
+ # @return [Hash] The build status.
45
71
def fetch_build_status
46
72
get_request ( URI ( "https://127.0.0.1/rest/api/latest/result/status/#{ @check_suite . bamboo_ci_ref } " ) )
47
73
end
48
74
49
- # Checks if CI still running
75
+ ##
76
+ # Checks if the CI build is still in progress.
77
+ #
78
+ # @param [Hash] build_status The build status.
79
+ # @return [Boolean] Returns true if the build is still in progress, false otherwise.
50
80
def in_progress? ( build_status )
51
81
@logger . info ">>> ci_stopped?: #{ ci_stopped? ( build_status ) } "
52
82
@logger . info ">>> ci_hanged?: #{ ci_hanged? ( build_status ) } "
@@ -59,6 +89,9 @@ def in_progress?(build_status)
59
89
60
90
private
61
91
92
+ ##
93
+ # Updates the status of all stages for the CheckSuite.
94
+ # Builds a summary for the last stage's last job.
62
95
def update_all_stages
63
96
last_stage =
64
97
Stage
@@ -71,8 +104,8 @@ def update_all_stages
71
104
build_summary ( last_stage . jobs . last )
72
105
end
73
106
74
- # This method will move all tests that no longer exist in BambooCI to the skipped state,
75
- # because there are no executions for them .
107
+ ##
108
+ # Moves all tests that no longer exist in BambooCI to the skipped state .
76
109
def clear_deleted_jobs
77
110
github_check = Github ::Check . new ( @check_suite )
78
111
@@ -81,22 +114,44 @@ def clear_deleted_jobs
81
114
end
82
115
end
83
116
117
+ ##
118
+ # Checks if the CI build has stopped.
119
+ #
120
+ # @param [Hash] build_status The build status.
121
+ # @return [Boolean] Returns true if the build has stopped, false otherwise.
84
122
def ci_stopped? ( build_status )
85
123
build_status . key? ( 'message' ) and !build_status . key? ( 'finished' )
86
124
end
87
125
126
+ ##
127
+ # Checks if the CI build has hanged.
128
+ #
129
+ # @param [Hash] build_status The build status.
130
+ # @return [Boolean] Returns true if the build has hanged, false otherwise.
88
131
def ci_hanged? ( build_status )
89
132
return true if ci_stopped? ( build_status )
90
133
91
134
build_status . dig ( 'progress' , 'percentageCompleted' ) . to_f >= 2.0
92
135
end
93
136
137
+ ##
138
+ # Updates the status of a stage based on the CI job result.
139
+ #
140
+ # @param [CiJob] ci_job The CI job to update.
141
+ # @param [Hash] result The result of the CI job.
142
+ # @param [Github::Check] github The Github check instance.
94
143
def update_stage_status ( ci_job , result , github )
95
144
return if ci_job . nil? || ( ci_job . finished? && !ci_job . job_ref . nil? )
96
145
97
146
update_ci_job_status ( github , ci_job , result [ 'state' ] )
98
147
end
99
148
149
+ ##
150
+ # Updates the status of a CI job based on the state.
151
+ #
152
+ # @param [Github::Check] github_check The Github check instance.
153
+ # @param [CiJob] ci_job The CI job to update.
154
+ # @param [String] state The state of the CI job.
100
155
def update_ci_job_status ( github_check , ci_job , state )
101
156
ci_job . enqueue ( github_check ) if ci_job . job_ref . nil?
102
157
@@ -119,6 +174,11 @@ def update_ci_job_status(github_check, ci_job, state)
119
174
build_summary ( ci_job )
120
175
end
121
176
177
+ ##
178
+ # Creates an output message for a CI job.
179
+ #
180
+ # @param [CiJob] ci_job The CI job to create the message for.
181
+ # @return [Hash] The output message.
122
182
def create_output_message ( ci_job )
123
183
url = "https://ci1.netdef.org/browse/#{ ci_job . job_ref } "
124
184
@@ -128,13 +188,22 @@ def create_output_message(ci_job)
128
188
}
129
189
end
130
190
191
+ ##
192
+ # Builds a summary for a CI job.
193
+ #
194
+ # @param [CiJob] ci_job The CI job to build the summary for.
131
195
def build_summary ( ci_job )
132
196
summary = Github ::Build ::Summary . new ( ci_job , agent : 'WatchDog' )
133
197
summary . build_summary
134
198
135
199
finished_execution? ( ci_job . check_suite )
136
200
end
137
201
202
+ ##
203
+ # Checks if the execution of the CheckSuite has finished.
204
+ #
205
+ # @param [CheckSuite] check_suite The CheckSuite to check.
206
+ # @return [Boolean] Returns true if the execution has finished, false otherwise.
138
207
def finished_execution? ( check_suite )
139
208
return false unless check_suite . pull_request . current_execution? ( check_suite )
140
209
return false unless check_suite . finished?
@@ -154,6 +223,8 @@ def slack_notify_cancelled(job)
154
223
SlackBot . instance . notify_cancelled ( job )
155
224
end
156
225
226
+ ##
227
+ # Checks the stages of the CheckSuite and updates their status.
157
228
def check_stages
158
229
github_check = Github ::Check . new ( @check_suite )
159
230
@logger . info ">>> @result: #{ @result . inspect } "
@@ -168,6 +239,8 @@ def check_stages
168
239
end
169
240
end
170
241
242
+ ##
243
+ # Fetches the CI execution status for the CheckSuite.
171
244
def fetch_ci_execution
172
245
@result = get_status ( @check_suite . bamboo_ci_ref )
173
246
end
0 commit comments