From 62883db0fe9fe015c1fd6cc6cfb942b3c3970064 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Ma=C5=82achowski?= Date: Fri, 31 Jan 2025 09:44:45 +0100 Subject: [PATCH 1/2] Strip timestamps before searching report string --- pkg/imagebuilder/report.go | 5 +++ pkg/imagebuilder/report_test.go | 66 ++++++++++++++++----------------- 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/pkg/imagebuilder/report.go b/pkg/imagebuilder/report.go index c251e8dba591..7bcd35d09015 100644 --- a/pkg/imagebuilder/report.go +++ b/pkg/imagebuilder/report.go @@ -44,11 +44,16 @@ func (br *BuildReport) GetImages() []string { } func NewBuildReportFromLogs(log string) (*BuildReport, error) { + // Strip all timestamps from log + log = regexp.MustCompile(`\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z\s+`).ReplaceAllString(log, "") + + // Find the report in the log matches := reportRegex.FindStringSubmatch(log) if len(matches) < 2 { return nil, nil } + // Parse the report data var report BuildReport if err := json.Unmarshal([]byte(matches[1]), &report); err != nil { return nil, err diff --git a/pkg/imagebuilder/report_test.go b/pkg/imagebuilder/report_test.go index 7aaeefa36b69..be99455821d6 100644 --- a/pkg/imagebuilder/report_test.go +++ b/pkg/imagebuilder/report_test.go @@ -7,45 +7,41 @@ import ( var _ = Describe("Report", func() { Describe("NewReportFromLogs", func() { - logs := `Starting: prepare_image_build_report -============================================================================== -Task : Python script -Description : Run a Python file or inline script -Version : 0.248.1 -Author : Microsoft Corporation -Help : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/python-script -============================================================================== -/usr/bin/python /home/vsts/work/1/s/scripts/prepare_image_build_report.py --image-build-report-file /home/vsts/work/1/s/image-report.json --image-name ginkgo-test-image/ginkgo --sign-step-succeeded true --job-status Succeeded --image-build-report-file /home/vsts/work/_temp/generated-tags.json --images-to-sign=europe-docker.pkg.dev/kyma-project/prod/ginkgo-test-image/ginkgo:1.23.0-50049457 --images-to-sign=europe-docker.pkg.dev/kyma-project/prod/ginkgo-test-image/ginkgo:wartosc --images-to-sign=europe-docker.pkg.dev/kyma-project/prod/ginkgo-test-image/ginkgo:innytag --images-to-sign=europe-docker.pkg.dev/kyma-project/prod/ginkgo-test-image/ginkgo:v20250129-50049457 --images-to-sign=europe-docker.pkg.dev/kyma-project/prod/ginkgo-test-image/ginkgo:1.23.0 ----IMAGE BUILD REPORT--- -{ - "status": "Succeeded", - "pushed": true, - "signed": true, - "is_production": true, - "image_spec": { - "image_name": "ginkgo-test-image/ginkgo", - "tags": [ - "1.23.0-50049457", - "wartosc", - "innytag", - "v20250129-50049457", - "1.23.0" - ], - "repository_path": "europe-docker.pkg.dev/kyma-project/prod/" - } -} ----END OF IMAGE BUILD REPORT--- - -Finishing: prepare_image_build_report` + logs := `2025-01-31T08:32:23.5327056Z ##[section]Starting: prepare_image_build_report +2025-01-31T08:32:23.5434336Z ============================================================================== +2025-01-31T08:32:23.5434499Z Task : Python script +2025-01-31T08:32:23.5434594Z Description : Run a Python file or inline script +2025-01-31T08:32:23.5434703Z Version : 0.248.1 +2025-01-31T08:32:23.5434803Z Author : Microsoft Corporation +2025-01-31T08:32:23.5434910Z Help : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/python-script +2025-01-31T08:32:23.5435059Z ============================================================================== +2025-01-31T08:32:23.6965198Z [command]/opt/hostedtoolcache/Python/3.13.1/x64/bin/python /home/vsts/work/1/s/scripts/prepare_image_build_report.py --image-name github-tools-sap/conduit-cli --image-build-succeeded true --sign-step-succeeded $(sign_images.signing_success) --job-status Succeeded --images-to-sign=europe-docker.pkg.dev/kyma-project/dev/github-tools-sap/conduit-cli:PR-477 +2025-01-31T08:32:23.7344251Z ---IMAGE BUILD REPORT--- +2025-01-31T08:32:23.7345746Z { +2025-01-31T08:32:23.7346062Z "status": "Succeeded", +2025-01-31T08:32:23.7357582Z "pushed": true, +2025-01-31T08:32:23.7358184Z "signed": false, +2025-01-31T08:32:23.7358759Z "is_production": false, +2025-01-31T08:32:23.7359525Z "image_spec": { +2025-01-31T08:32:23.7360295Z "image_name": "github-tools-sap/conduit-cli", +2025-01-31T08:32:23.7360618Z "tags": [ +2025-01-31T08:32:23.7361207Z "PR-477" +2025-01-31T08:32:23.7361687Z ], +2025-01-31T08:32:23.7362370Z "repository_path": "europe-docker.pkg.dev/kyma-project/dev/" +2025-01-31T08:32:23.7362690Z } +2025-01-31T08:32:23.7363276Z } +2025-01-31T08:32:23.7363903Z ---END OF IMAGE BUILD REPORT--- +2025-01-31T08:32:23.7416532Z +2025-01-31T08:32:23.7530550Z ##[section]Finishing: prepare_image_build_report` expectedReport := &BuildReport{ Status: "Succeeded", IsPushed: true, - IsSigned: true, - IsProduction: true, + IsSigned: false, + IsProduction: false, ImageSpec: ImageSpec{ - Name: "ginkgo-test-image/ginkgo", - Tags: []string{"1.23.0-50049457", "wartosc", "innytag", "v20250129-50049457", "1.23.0"}, - RepositoryPath: "europe-docker.pkg.dev/kyma-project/prod/", + Name: "github-tools-sap/conduit-cli", + Tags: []string{"PR-477"}, + RepositoryPath: "europe-docker.pkg.dev/kyma-project/dev/", }, } From 1eccbbb554522c70e528c075a5e5cdbfe72fccd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Ma=C5=82achowski?= Date: Fri, 31 Jan 2025 09:52:55 +0100 Subject: [PATCH 2/2] Move regex up, to match reprotRegex --- pkg/imagebuilder/report.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/imagebuilder/report.go b/pkg/imagebuilder/report.go index 7bcd35d09015..8af1cac90442 100644 --- a/pkg/imagebuilder/report.go +++ b/pkg/imagebuilder/report.go @@ -8,7 +8,11 @@ import ( ) // reportRegex is a regular expression that matches the image build report -var reportRegex = regexp.MustCompile(`(?s)---IMAGE BUILD REPORT---\n(.*)\n---END OF IMAGE BUILD REPORT---`) +var ( + reportRegex = regexp.MustCompile(`(?s)---IMAGE BUILD REPORT---\n(.*)\n---END OF IMAGE BUILD REPORT---`) + + timestampRegex = regexp.MustCompile(`\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z\s+`) +) type BuildReport struct { // Status is the overall status of the build including signing and pushing @@ -45,7 +49,7 @@ func (br *BuildReport) GetImages() []string { func NewBuildReportFromLogs(log string) (*BuildReport, error) { // Strip all timestamps from log - log = regexp.MustCompile(`\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z\s+`).ReplaceAllString(log, "") + log = timestampRegex.ReplaceAllString(log, "") // Find the report in the log matches := reportRegex.FindStringSubmatch(log)