diff --git a/cmd/relacs/konflux/list_conforma_violations.go b/cmd/relacs/konflux/list_conforma_violations.go index c8d622a..bdbb890 100644 --- a/cmd/relacs/konflux/list_conforma_violations.go +++ b/cmd/relacs/konflux/list_conforma_violations.go @@ -12,12 +12,12 @@ import ( ) type ConformaViolationsOutput struct { - Organization string `json:"organization" yaml:"organization"` - Repository string `json:"repository" yaml:"repository"` - Tag string `json:"tag" yaml:"tag"` - Violations konflux.Violations `json:"violations" yaml:"violations"` - SkippedRuns []string `json:"skippedRuns" yaml:"skippedRuns"` - InProgressRuns []string `json:"inProgressRuns" yaml:"inProgressRuns"` + Organization string `json:"organization" yaml:"organization"` + Repository string `json:"repository" yaml:"repository"` + Tag string `json:"tag" yaml:"tag"` + Violations konflux.Violations `json:"violations" yaml:"violations"` + SkippedRuns []konflux.ConformaRunInfo `json:"skippedRuns" yaml:"skippedRuns"` + InProgressRuns []konflux.ConformaRunInfo `json:"inProgressRuns" yaml:"inProgressRuns"` } func (c ConformaViolationsOutput) String() string { @@ -34,13 +34,13 @@ func (c ConformaViolationsOutput) String() string { if len(c.InProgressRuns) > 0 { output += fmt.Sprintln("In-progress runs:") for _, run := range c.InProgressRuns { - output += fmt.Sprintf(" - %s\n", run) + output += fmt.Sprintf(" - %s (%s)\n", run.Name, run.URL) } } if len(c.SkippedRuns) > 0 { output += fmt.Sprintln("Skipped runs (no report available):") for _, run := range c.SkippedRuns { - output += fmt.Sprintf(" - %s\n", run) + output += fmt.Sprintf(" - %s: %s\n", run.Name, run.Reason) } } return output @@ -94,10 +94,10 @@ func runListConformaViolations(cmd *cobra.Command, args []string, c *client.Clie // The result is partial: still render the output, but exit non-zero. var reasons []string if len(result.SkippedRuns) > 0 { - reasons = append(reasons, fmt.Sprintf("no conforma report available for %d run(s): %s", len(result.SkippedRuns), strings.Join(result.SkippedRuns, ", "))) + reasons = append(reasons, fmt.Sprintf("no conforma report available for %d run(s): %s", len(result.SkippedRuns), runNames(result.SkippedRuns))) } if len(result.InProgressRuns) > 0 { - reasons = append(reasons, fmt.Sprintf("%d conforma run(s) still in progress: %s", len(result.InProgressRuns), strings.Join(result.InProgressRuns, ", "))) + reasons = append(reasons, fmt.Sprintf("%d conforma run(s) still in progress: %s", len(result.InProgressRuns), runNames(result.InProgressRuns))) } return output, &common.PartialResultError{ Err: fmt.Errorf("%s", strings.Join(reasons, "; ")), @@ -105,3 +105,11 @@ func runListConformaViolations(cmd *cobra.Command, args []string, c *client.Clie } return output, nil } + +func runNames(runs []konflux.ConformaRunInfo) string { + names := make([]string, len(runs)) + for i, run := range runs { + names[i] = run.Name + } + return strings.Join(names, ", ") +} diff --git a/pkg/konflux/conforma.go b/pkg/konflux/conforma.go index 9141b64..8204d8b 100644 --- a/pkg/konflux/conforma.go +++ b/pkg/konflux/conforma.go @@ -32,18 +32,29 @@ type Violation struct { Violation string `json:"violation"` } +// ConformaRunInfo describes a conforma pipeline run that did not contribute +// to the violations: it is either still in progress or its report could not +// be retrieved or parsed (Reason). +type ConformaRunInfo struct { + Name string `json:"name" yaml:"name"` + URL string `json:"url" yaml:"url"` + Reason string `json:"reason,omitempty" yaml:"reason,omitempty"` +} + // ConformaViolations is the result of listing the conforma violations for a // tag. The result is partial when SkippedRuns or InProgressRuns is non-empty. type ConformaViolations struct { - Violations Violations `json:"violations" yaml:"violations"` - SkippedRuns []string `json:"skippedRuns" yaml:"skippedRuns"` - InProgressRuns []string `json:"inProgressRuns" yaml:"inProgressRuns"` + Violations Violations `json:"violations" yaml:"violations"` + SkippedRuns []ConformaRunInfo `json:"skippedRuns" yaml:"skippedRuns"` + InProgressRuns []ConformaRunInfo `json:"inProgressRuns" yaml:"inProgressRuns"` } // ListConformaViolations returns the violations reported by the conforma // pipeline runs for the given tag and scenario. Runs that are still in // progress are listed in InProgressRuns, and runs whose report could not be -// retrieved (e.g. they failed before the report step ran) in SkippedRuns. +// retrieved (e.g. they failed before the report step ran) in SkippedRuns. It +// errors only when no report could be read at all because runs were skipped; +// while every run is still in progress, the partial result is returned. func ListConformaViolations(ctx context.Context, c *client.Client, org, repo, tag, scenario string) (ConformaViolations, error) { pipelineRuns, err := conformaPipelineRunsForScenario(ctx, c, org, repo, tag, scenario) if err != nil { @@ -66,7 +77,10 @@ func violationsFromPipelineRuns(ctx context.Context, c *client.Client, pipelineR reportsRead := 0 for _, conformaRun := range conformaRuns { if isPipelineRunInProgress(conformaRun.Status.Conditions) { - result.InProgressRuns = append(result.InProgressRuns, conformaRun.Metadata.Name) + result.InProgressRuns = append(result.InProgressRuns, ConformaRunInfo{ + Name: conformaRun.Metadata.Name, + URL: pipelineRunURL(conformaRun), + }) continue } @@ -74,25 +88,32 @@ func violationsFromPipelineRuns(ctx context.Context, c *client.Client, pipelineR report, err := c.GetKonflux().GetPodLogs(ctx, podName, "step-report-json") if err != nil { fmt.Fprintf(os.Stderr, "WARNING: failed to get report for conforma run %s: %v\n", conformaRun.Metadata.Name, err) - result.SkippedRuns = append(result.SkippedRuns, conformaRun.Metadata.Name) + result.SkippedRuns = append(result.SkippedRuns, ConformaRunInfo{ + Name: conformaRun.Metadata.Name, + URL: pipelineRunURL(conformaRun), + Reason: err.Error(), + }) continue } runViolations, err := parseConformaViolations(report) if err != nil { fmt.Fprintf(os.Stderr, "WARNING: failed to parse conforma report for run %s: %v\n", conformaRun.Metadata.Name, err) - result.SkippedRuns = append(result.SkippedRuns, conformaRun.Metadata.Name) + result.SkippedRuns = append(result.SkippedRuns, ConformaRunInfo{ + Name: conformaRun.Metadata.Name, + URL: pipelineRunURL(conformaRun), + Reason: err.Error(), + }) continue } result.Violations.Violations = append(result.Violations.Violations, runViolations.Violations...) reportsRead++ } - if reportsRead == 0 { - if len(result.SkippedRuns) == 0 { - return ConformaViolations{}, fmt.Errorf("conforma checks for tag %s are still in progress (%d run(s) running)", tag, len(result.InProgressRuns)) - } + if reportsRead == 0 && len(result.SkippedRuns) > 0 { return ConformaViolations{}, fmt.Errorf("failed to retrieve any conforma report for tag %s (%d run(s) still in progress)", tag, len(result.InProgressRuns)) } + // When every run is still in progress, the result is partial with the + // runs listed in InProgressRuns rather than an error. return result, nil } diff --git a/pkg/konflux/conforma_runs.go b/pkg/konflux/conforma_runs.go index 7b3c0fa..7359200 100644 --- a/pkg/konflux/conforma_runs.go +++ b/pkg/konflux/conforma_runs.go @@ -123,19 +123,21 @@ func getConformaPipelineRuns(ctx context.Context, c *client.Client, org, repo, t return conformaRuns, nil } -func convertToConformaRunEntry(plr kflx.PipelineRun) ConformaRunEntry { +func pipelineRunURL(plr kflx.PipelineRun) string { appName := plr.Metadata.Labels["appstudio.openshift.io/application"] + return utils.Application{Name: appName}.GetPipelineRunURL(plr.Metadata.Name) +} + +func convertToConformaRunEntry(plr kflx.PipelineRun) ConformaRunEntry { component := plr.Metadata.Labels["appstudio.openshift.io/component"] scenario := plr.Metadata.Labels["test.appstudio.openshift.io/scenario"] - plrURL := utils.Application{Name: appName}.GetPipelineRunURL(plr.Metadata.Name) - return ConformaRunEntry{ Name: plr.Metadata.Name, Component: component, Scenario: scenario, Status: resolveStatus(plr.Status.Conditions), - URL: plrURL, + URL: pipelineRunURL(plr), } } diff --git a/tests/konflux/conforma_violations_test.go b/tests/konflux/conforma_violations_test.go index 67a04d1..17b96ff 100644 --- a/tests/konflux/conforma_violations_test.go +++ b/tests/konflux/conforma_violations_test.go @@ -8,6 +8,7 @@ import ( "github.com/h2non/gock" "github.com/stackrox/relacs/pkg/config" relacskonflux "github.com/stackrox/relacs/pkg/konflux" + "github.com/stackrox/relacs/pkg/utils" "github.com/stackrox/relacs/tests/helpers" "github.com/stretchr/testify/suite" ) @@ -229,19 +230,29 @@ func (s *ConformaViolationsTestSuite) TestListConformaRuns_ProdLike() { func (s *ConformaViolationsTestSuite) TestListConformaViolations_InProgress() { // Tekton reports a started run as a Succeeded condition with unknown - // status, not as an absent condition. + // status, not as an absent condition. With every run still in progress, + // the partial result is returned instead of an error. started := s.conformaPipelineRun("acs-conforma-dev-4-10-started", "uid-started", "main-4-10", s.startedConditions()) s.mockConformaPipelineRuns(started) result, err := s.listConformaViolations() - s.Require().Error(err) - s.Contains(err.Error(), "still in progress") + s.Require().NoError(err) s.Empty(result.Violations.Violations) s.Empty(result.SkippedRuns) + s.Equal([]relacskonflux.ConformaRunInfo{s.expectedRunInfo("acs-conforma-dev-4-10-started")}, result.InProgressRuns) s.True(gock.IsDone()) } +// expectedRunInfo returns the expected ConformaRunInfo for a pipeline run +// built by the suite's fixtures, whose application label is "acs-4-10". +func (s *ConformaViolationsTestSuite) expectedRunInfo(name string) relacskonflux.ConformaRunInfo { + return relacskonflux.ConformaRunInfo{ + Name: name, + URL: utils.Application{Name: "acs-4-10"}.GetPipelineRunURL(name), + } +} + func (s *ConformaViolationsTestSuite) TestListConformaViolations_PartialInProgress() { succeeded := s.conformaPipelineRun("acs-conforma-dev-4-10-done", "uid-done", "main-4-10", s.succeededConditions()) running := s.conformaPipelineRun("acs-conforma-dev-4-10-running", "uid-running", "scanner-4-10", nil) @@ -254,7 +265,7 @@ func (s *ConformaViolationsTestSuite) TestListConformaViolations_PartialInProgre s.Len(result.Violations.Violations, 1) s.Equal("violation one", result.Violations.Violations[0].Violation) s.Empty(result.SkippedRuns) - s.Equal([]string{"acs-conforma-dev-4-10-running"}, result.InProgressRuns) + s.Equal([]relacskonflux.ConformaRunInfo{s.expectedRunInfo("acs-conforma-dev-4-10-running")}, result.InProgressRuns) s.True(gock.IsDone()) } @@ -270,8 +281,11 @@ func (s *ConformaViolationsTestSuite) TestListConformaViolations_PartialSkippedA s.Require().NoError(err) s.Len(result.Violations.Violations, 1) - s.Equal([]string{"acs-conforma-dev-4-10-failed"}, result.SkippedRuns) - s.Equal([]string{"acs-conforma-dev-4-10-running"}, result.InProgressRuns) + s.Require().Len(result.SkippedRuns, 1) + s.Equal(s.expectedRunInfo("acs-conforma-dev-4-10-failed").Name, result.SkippedRuns[0].Name) + s.Equal(s.expectedRunInfo("acs-conforma-dev-4-10-failed").URL, result.SkippedRuns[0].URL) + s.NotEmpty(result.SkippedRuns[0].Reason) + s.Equal([]relacskonflux.ConformaRunInfo{s.expectedRunInfo("acs-conforma-dev-4-10-running")}, result.InProgressRuns) s.True(gock.IsDone()) } @@ -301,7 +315,10 @@ func (s *ConformaViolationsTestSuite) TestListConformaViolations_UnparseableRepo s.Require().NoError(err) s.Len(result.Violations.Violations, 1) - s.Equal([]string{"acs-conforma-dev-4-10-empty"}, result.SkippedRuns) + s.Require().Len(result.SkippedRuns, 1) + s.Equal(s.expectedRunInfo("acs-conforma-dev-4-10-empty").Name, result.SkippedRuns[0].Name) + s.Equal(s.expectedRunInfo("acs-conforma-dev-4-10-empty").URL, result.SkippedRuns[0].URL) + s.NotEmpty(result.SkippedRuns[0].Reason) s.Empty(result.InProgressRuns) s.True(gock.IsDone()) }