From 82c3b85e31392f658fa178af96df7315071237c9 Mon Sep 17 00:00:00 2001 From: Niklas van Schrick Date: Wed, 2 Oct 2024 19:11:37 +0200 Subject: [PATCH] Add support for timestamps in jobs --- lib/action/step/create_summary.rb | 18 ++++++++++++--- spec/action/full_run_pipeline_action_spec.rb | 4 +++- spec/action/step/create_summary_spec.rb | 24 ++++++++++++++++++++ test/.gitlab-ci.yml | 9 ++++++++ 4 files changed, 51 insertions(+), 4 deletions(-) diff --git a/lib/action/step/create_summary.rb b/lib/action/step/create_summary.rb index 6c58650..4878abd 100644 --- a/lib/action/step/create_summary.rb +++ b/lib/action/step/create_summary.rb @@ -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 @@ -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? @@ -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 diff --git a/spec/action/full_run_pipeline_action_spec.rb b/spec/action/full_run_pipeline_action_spec.rb index c008496..58560e0 100644 --- a/spec/action/full_run_pipeline_action_spec.rb +++ b/spec/action/full_run_pipeline_action_spec.rb @@ -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 diff --git a/spec/action/step/create_summary_spec.rb b/spec/action/step/create_summary_spec.rb index b39a25f..e5c1a1d 100644 --- a/spec/action/step/create_summary_spec.rb +++ b/spec/action/step/create_summary_spec.rb @@ -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) diff --git a/test/.gitlab-ci.yml b/test/.gitlab-ci.yml index 006414c..7c3359c 100644 --- a/test/.gitlab-ci.yml +++ b/test/.gitlab-ci.yml @@ -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"