Skip to content

Commit

Permalink
Add support for timestamps in jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
Taucher2003 committed Oct 2, 2024
1 parent 5a272e8 commit 82c3b85
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
18 changes: 15 additions & 3 deletions lib/action/step/create_summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module GitlabPipelineAction
module Step
class CreateSummary < Base
TIMESTAMP_REGEX = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}Z/

def execute
File.write context.gh_step_summary_path, create_summary
end
Expand Down Expand Up @@ -59,9 +61,11 @@ def job_summaries
def extract_summary(trace)
return if trace.nil?

lines_after_summary_start = trace.lines.map(&:strip).drop_while do |line|
line !~ /^\e\[0Ksection_start:\d+:glpa_summary/
end.drop(1)
lines_after_summary_start = trace.lines
.map(&:strip)
.map(&method(:remove_timestamp))
.drop_while { |line| line !~ /^\e\[0Ksection_start:\d+:glpa_summary/ }
.drop(1)
summary_lines = lines_after_summary_start.take_while { |line| line !~ /^\e\[0Ksection_end:\d+:glpa_summary/ }

if summary_lines.empty?
Expand All @@ -76,6 +80,14 @@ def job_traces
.pipeline_jobs(context.gl_project_id, context.gl_pipeline.id)
.map { |job| { job: job, trace: context.gitlab_client.job_trace(context.gl_project_id, job.id) } }
end

def remove_timestamp(line)
if line =~ TIMESTAMP_REGEX
line[32..]
else
line
end
end
end
end
end
4 changes: 3 additions & 1 deletion spec/action/full_run_pipeline_action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@
'## Job summaries',
'job-with-summary',
"This line should be part of the summary\n" \
'this one as well'
'this one as well',
'job-with-timestamps-and-summary',
'Summary line in timestamp job'
)
end

Expand Down
24 changes: 24 additions & 0 deletions spec/action/step/create_summary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,30 @@
end
end

context 'when jobs have a summary and timestamps are enabled' do
before do
allow(gitlab_client).to receive(:job_trace).and_return(
<<~TRACE
2024-09-27T22:55:05.708980Z 00O \e[0Ksection_start:1560896352:glpa_summary\r\e[0KTitle of the GLPA Summary
2024-09-27T22:55:05.708980Z 00O Content of timestamped summary
2024-09-27T22:55:05.708980Z 00O \e[0Ksection_end:1560896353:glpa_summary\r\e[0K
TRACE
)
end

it 'includes the job summary' do
expect(create_summary).to include(
<<~DESC
## Job summaries
### build
Content of timestamped summary
DESC
)
end
end

context 'when no jobs have a trace' do
before do
allow(gitlab_client).to receive(:job_trace).and_return(nil)
Expand Down
9 changes: 9 additions & 0 deletions test/.gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,12 @@ job-with-summary:
echo 'this one as well'
echo -e "\e[0Ksection_end:`date +%s`:glpa_summary\r\e[0K"
echo 'this one not'
job-with-timestamps-and-summary:
variables:
FF_TIMESTAMPS: 'true'
script:
- |
echo -e "\e[0Ksection_start:`date +%s`:glpa_summary\r\e[0KHeader of the section"
echo 'Summary line in timestamp job'
echo -e "\e[0Ksection_end:`date +%s`:glpa_summary\r\e[0K"

0 comments on commit 82c3b85

Please sign in to comment.