diff --git a/cmd/relacs/konflux/list_conforma_runs.go b/cmd/relacs/konflux/list_conforma_runs.go index 5e18245..bbcaf59 100644 --- a/cmd/relacs/konflux/list_conforma_runs.go +++ b/cmd/relacs/konflux/list_conforma_runs.go @@ -60,16 +60,19 @@ var listConformaRunsCmd = &cobra.Command{ Short: "List the conforma pipeline runs triggered by a given Git tag", Long: `List the conforma pipeline run results for a specific Git tag. -Only conforma runs triggered directly on the tag's commit (via -pipelines-as-code) are considered; the conforma run for the custom -snapshot is not included.`, - RunE: common.RunCommand(runListConformaRuns), +By default (scenario acs-conforma-dev), only conforma runs triggered +directly on the tag's commit (via pipelines-as-code) are considered. +With --scenario acs-conforma-prod-like (only supported for the +stackrox repository), the conforma runs attached to the tag's custom +snapshot are listed instead.`, + RunE: common.RunCommand(runListConformaRuns), } func init() { listConformaRunsCmd.Flags().String("org", config.GitHubOrganization, "GitHub organization (optional)") listConformaRunsCmd.Flags().String("repo", config.MainGitHubRepository, "GitHub repository (optional)") listConformaRunsCmd.Flags().String("tag", "", "Git tag (required)") + listConformaRunsCmd.Flags().String("scenario", konflux.ConformaScenarioDev, "Conforma scenario: "+konflux.ConformaScenarioDev+" or "+konflux.ConformaScenarioProdLike+" (the latter is only supported for the stackrox repository)") //nolint:errcheck listConformaRunsCmd.MarkFlagRequired("tag") @@ -80,8 +83,9 @@ func runListConformaRuns(cmd *cobra.Command, args []string, c *client.Client) (a org, _ := cmd.Flags().GetString("org") repo, _ := cmd.Flags().GetString("repo") tag, _ := cmd.Flags().GetString("tag") + scenario, _ := cmd.Flags().GetString("scenario") - entries, err := konflux.ListConformaRuns(ctx, c, org, repo, tag) + entries, err := konflux.ListConformaRuns(ctx, c, org, repo, tag, scenario) if err != nil { return nil, fmt.Errorf("failed to list conforma runs: %w", err) } diff --git a/cmd/relacs/konflux/list_conforma_violations.go b/cmd/relacs/konflux/list_conforma_violations.go index e017232..0a46843 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 @@ -51,16 +51,19 @@ var listConformaViolationsCmd = &cobra.Command{ Short: "List the Conforma violations for a given Git tag", Long: `Retrieve the Conforma violations reported by the conforma pipeline runs for a given Git tag. -Only conforma runs triggered directly on the tag's commit (via -pipelines-as-code) are considered; the conforma run for the custom -snapshot is not included.`, - RunE: common.RunCommand(runListConformaViolations), +By default (scenario acs-conforma-dev), only conforma runs triggered +directly on the tag's commit (via pipelines-as-code) are considered. +With --scenario acs-conforma-prod-like (only supported for the +stackrox repository), the violations of the conforma runs attached +to the tag's custom snapshot are listed instead.`, + RunE: common.RunCommand(runListConformaViolations), } func init() { listConformaViolationsCmd.Flags().String("org", config.GitHubOrganization, "GitHub organization (optional)") listConformaViolationsCmd.Flags().String("repo", config.MainGitHubRepository, "GitHub repository (optional)") listConformaViolationsCmd.Flags().String("tag", "", "Git tag (required)") + listConformaViolationsCmd.Flags().String("scenario", konflux.ConformaScenarioDev, "Conforma scenario: "+konflux.ConformaScenarioDev+" or "+konflux.ConformaScenarioProdLike+" (the latter is only supported for the stackrox repository)") //nolint:errcheck listConformaViolationsCmd.MarkFlagRequired("tag") @@ -72,8 +75,9 @@ func runListConformaViolations(cmd *cobra.Command, args []string, c *client.Clie org, _ := cmd.Flags().GetString("org") repo, _ := cmd.Flags().GetString("repo") tag, _ := cmd.Flags().GetString("tag") + scenario, _ := cmd.Flags().GetString("scenario") - result, err := konflux.ListConformaViolations(ctx, c, org, repo, tag) + result, err := konflux.ListConformaViolations(ctx, c, org, repo, tag, scenario) if err != nil { return nil, fmt.Errorf("failed to list conforma violations: %w", err) } @@ -90,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, "; ")), @@ -101,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/cmd/relacs/main.go b/cmd/relacs/main.go index 5c9763a..5986e0c 100644 --- a/cmd/relacs/main.go +++ b/cmd/relacs/main.go @@ -10,6 +10,7 @@ import ( "github.com/spf13/cobra" "github.com/stackrox/relacs/cmd/relacs/github" "github.com/stackrox/relacs/cmd/relacs/konflux" + "github.com/stackrox/relacs/cmd/relacs/serve" "github.com/stackrox/relacs/cmd/relacs/version" "github.com/stackrox/relacs/pkg/versioncheck" ) @@ -46,6 +47,7 @@ func main() { rootCmd.AddCommand(version.VersionCmd) rootCmd.AddCommand(github.GitHubCmd) rootCmd.AddCommand(konflux.KonfluxCmd) + rootCmd.AddCommand(serve.ServeCmd) // Execute root command ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) diff --git a/cmd/relacs/serve/cmd.go b/cmd/relacs/serve/cmd.go new file mode 100644 index 0000000..5537595 --- /dev/null +++ b/cmd/relacs/serve/cmd.go @@ -0,0 +1,95 @@ +package serve + +import ( + "context" + "embed" + "fmt" + "html/template" + "io/fs" + "log" + "net" + "net/http" + "time" + + "github.com/spf13/cobra" + "github.com/stackrox/relacs/pkg/client" +) + +//go:embed templates/* +var templateFS embed.FS + +var ( + homeTmpl *template.Template + tagTmpl *template.Template +) + +var port string + +var ServeCmd = &cobra.Command{ + Use: "serve", + Short: "Start a local web server showing release information", + Long: `Starts a local HTTP server that serves release information as web pages`, + RunE: runServe, +} + +func init() { + ServeCmd.Flags().StringVar(&port, "port", "8080", "Port to listen on") +} + +func parsePageTemplate(files ...string) (*template.Template, error) { + return template.ParseFS(templateFS, files...) +} + +func runServe(cmd *cobra.Command, args []string) error { + var err error + homeTmpl, err = parsePageTemplate("templates/base.html", "templates/home.html") + if err != nil { + return fmt.Errorf("failed to parse home template: %w", err) + } + tagTmpl, err = parsePageTemplate("templates/base.html", "templates/tag.html") + if err != nil { + return fmt.Errorf("failed to parse tag template: %w", err) + } + + c, err := client.NewClient() + if err != nil { + return fmt.Errorf("failed to create client: %w", err) + } + + staticFS, err := fs.Sub(templateFS, "templates/static") + if err != nil { + return fmt.Errorf("failed to create static file system: %w", err) + } + + mux := http.NewServeMux() + mux.Handle("GET /static/", http.StripPrefix("/static/", http.FileServer(http.FS(staticFS)))) + mux.HandleFunc("GET /{$}", handleHome()) + mux.HandleFunc("GET /tag", handleTag(c)) + + addr := net.JoinHostPort("localhost", port) + server := &http.Server{ + Addr: addr, + Handler: mux, + ReadHeaderTimeout: 10 * time.Second, + } + + ctx := cmd.Context() + shutdownDone := make(chan struct{}) + go func() { + defer close(shutdownDone) + <-ctx.Done() + shutdownCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + if err := server.Shutdown(shutdownCtx); err != nil { + log.Printf("Server shutdown error: %v", err) + } + }() + + log.Printf("Server listening on http://%s", addr) + err = server.ListenAndServe() + if err == http.ErrServerClosed { + <-shutdownDone + return nil + } + return err +} diff --git a/cmd/relacs/serve/handlers.go b/cmd/relacs/serve/handlers.go new file mode 100644 index 0000000..56e074e --- /dev/null +++ b/cmd/relacs/serve/handlers.go @@ -0,0 +1,48 @@ +package serve + +import ( + "bytes" + "html/template" + "log" + "net/http" + + "github.com/stackrox/relacs/pkg/client" + "github.com/stackrox/relacs/pkg/config" + "github.com/stackrox/relacs/pkg/serve" +) + +func renderTemplate(w http.ResponseWriter, tmpl *template.Template, data any) { + var buf bytes.Buffer + // Start rendering from the outermost template; it pulls in child-defined blocks. + if err := tmpl.ExecuteTemplate(&buf, "base.html", data); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + _, err := buf.WriteTo(w) + if err != nil { + log.Printf("Failed to write template to response: %v", err) + } +} + +func handleHome() http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + renderTemplate(w, homeTmpl, nil) + } +} + +func handleTag(c *client.Client) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + tag := r.URL.Query().Get("tag") + if tag == "" { + http.Error(w, "Missing required query parameter: tag", http.StatusBadRequest) + return + } + repo := r.URL.Query().Get("repo") + if repo == "" { + repo = config.MainGitHubRepository + } + + data := serve.FetchTagData(r.Context(), c, repo, tag) + renderTemplate(w, tagTmpl, data) + } +} diff --git a/cmd/relacs/serve/templates/base.html b/cmd/relacs/serve/templates/base.html new file mode 100644 index 0000000..0274a7c --- /dev/null +++ b/cmd/relacs/serve/templates/base.html @@ -0,0 +1,17 @@ + + + + + + relacs{{block "title" .}}{{end}} + + + + +
+ {{block "content" .}}{{end}} +
+ + diff --git a/cmd/relacs/serve/templates/home.html b/cmd/relacs/serve/templates/home.html new file mode 100644 index 0000000..f99d1f2 --- /dev/null +++ b/cmd/relacs/serve/templates/home.html @@ -0,0 +1,35 @@ +{{template "base.html" .}} + +{{define "title"}} - Home{{end}} + +{{define "content"}} +

Tags

+ +
+

Tag

+

+ View pipeline status, artifacts, and Conforma results for a specific tag. +

+
+
+
+ + +
+
+ + +
+
+ + +
+
+
+
+{{end}} diff --git a/cmd/relacs/serve/templates/static/style.css b/cmd/relacs/serve/templates/static/style.css new file mode 100644 index 0000000..776b947 --- /dev/null +++ b/cmd/relacs/serve/templates/static/style.css @@ -0,0 +1,123 @@ +:root { + --color-bg: #f5f5f5; + --color-surface: #ffffff; + --color-text: #1a1a1a; + --color-text-secondary: #555; + --color-border: #ddd; + --color-passed: #16a34a; + --color-passed-bg: #dcfce7; + --color-failed: #dc2626; + --color-failed-bg: #fee2e2; + --color-warning: #ca8a04; + --color-warning-bg: #fef9c3; + --color-link: #2563eb; + --color-nav: #1e293b; +} +* { margin: 0; padding: 0; box-sizing: border-box; } +body { + font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; + background: var(--color-bg); + color: var(--color-text); + line-height: 1.5; +} +nav { + background: var(--color-nav); + color: #fff; + padding: 0.75rem 1.5rem; + display: flex; + align-items: center; + gap: 1.5rem; +} +nav a { color: #fff; text-decoration: none; font-weight: 600; font-size: 1.1rem; } +nav a:hover { opacity: 0.85; } +.container { max-width: 1200px; margin: 0 auto; padding: 1.5rem; } +a { color: var(--color-link); text-decoration: none; } +a:hover { text-decoration: underline; } +.card { + background: var(--color-surface); + border: 1px solid var(--color-border); + border-radius: 8px; + padding: 1.25rem; + margin-bottom: 1rem; +} +.card h2 { margin-bottom: 0.75rem; font-size: 1.1rem; } +.badge { + display: inline-block; + padding: 0.25rem 0.75rem; + border-radius: 9999px; + font-size: 0.85rem; + font-weight: 600; +} +.badge-passed { background: var(--color-passed-bg); color: var(--color-passed); } +.badge-failed { background: var(--color-failed-bg); color: var(--color-failed); } +.badge-warning { background: var(--color-warning-bg); color: var(--color-warning); } +.badge-mock { background: #e0e7ff; color: #4338ca; font-style: italic; } +.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; } +.kv { margin-bottom: 0.5rem; } +.kv dt { font-size: 0.8rem; color: var(--color-text-secondary); text-transform: uppercase; letter-spacing: 0.05em; } +.kv dd { font-size: 0.95rem; word-break: break-all; } +table { width: 100%; border-collapse: collapse; font-size: 0.9rem; } +th, td { text-align: left; padding: 0.5rem 0.75rem; border-bottom: 1px solid var(--color-border); } +th { background: var(--color-bg); font-weight: 600; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--color-text-secondary); } +.group-header { + display: flex; + align-items: center; + gap: 0.5rem; + padding: 0.75rem 0; + font-weight: 600; + border-bottom: 1px solid var(--color-border); + cursor: pointer; +} +.group-header:hover { opacity: 0.8; } +.section-header { + display: flex; + align-items: center; + gap: 0.5rem; + font-size: 1.1rem; + font-weight: 600; + cursor: pointer; +} +.section-header:hover { opacity: 0.8; } +details summary { list-style: none; } +details summary::-webkit-details-marker { display: none; } +details[open] > summary .chevron { transform: rotate(90deg); } +.chevron { transition: transform 0.15s; display: inline-block; } +.errors { background: var(--color-failed-bg); border: 1px solid var(--color-failed); border-radius: 8px; padding: 1rem; margin-bottom: 1rem; } +.note { background: var(--color-bg); border: 1px solid var(--color-border); border-radius: 8px; padding: 0.75rem 1rem; margin: 0.5rem 0; font-size: 0.85rem; color: var(--color-text-secondary); } +.tabs { display: flex; gap: 0.25rem; border-bottom: 1px solid var(--color-border); margin: 0.75rem 0; } +.tab { background: none; border: none; border-bottom: 2px solid transparent; padding: 0.5rem 0.75rem; font-size: 0.9rem; font-weight: 500; color: var(--color-text-secondary); cursor: pointer; } +.tab:hover { color: var(--color-text); } +.tab.active { color: var(--color-text); border-bottom-color: var(--color-link); } +.retest-button { background: none; border: none; cursor: pointer; font-size: 1rem; padding: 0 0.25rem; margin-left: auto; } +.retest-button.disabled { opacity: 0.4; cursor: not-allowed; } +.modal { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0, 0, 0, 0.5); display: flex; align-items: center; justify-content: center; z-index: 100; } +.modal-content { max-width: 640px; width: 90%; } +.modal-content pre { white-space: pre-wrap; word-break: break-all; background: var(--color-bg); border: 1px solid var(--color-border); border-radius: 8px; padding: 0.75rem 1rem; } +.copy-wrapper { position: relative; margin-bottom: 0.75rem; } +.copy-button { position: absolute; top: 0.5rem; right: 0.5rem; background: var(--color-bg); border: 1px solid var(--color-border); border-radius: 6px; cursor: pointer; padding: 0.25rem 0.5rem; font-size: 0.85rem; } +.copy-button:hover { opacity: 0.8; } +.errors li { margin-left: 1.25rem; color: var(--color-failed); } +input[type="text"], input[type="number"], select { + padding: 0.5rem 0.75rem; + border: 1px solid var(--color-border); + border-radius: 6px; + font-size: 0.95rem; + width: 100%; + font-family: inherit; + background: var(--color-surface); +} +button { + padding: 0.5rem 1.25rem; + background: var(--color-link); + color: #fff; + border: none; + border-radius: 6px; + font-size: 0.95rem; + cursor: pointer; + font-weight: 500; +} +button:hover { opacity: 0.9; } +.form-row { display: flex; gap: 0.75rem; align-items: end; } +.form-group { display: flex; flex-direction: column; gap: 0.25rem; } +.form-group label { font-size: 0.85rem; font-weight: 500; color: var(--color-text-secondary); } +@media (max-width: 768px) { .grid-2 { grid-template-columns: 1fr; } } diff --git a/cmd/relacs/serve/templates/tag.html b/cmd/relacs/serve/templates/tag.html new file mode 100644 index 0000000..6f10b11 --- /dev/null +++ b/cmd/relacs/serve/templates/tag.html @@ -0,0 +1,427 @@ +{{template "base.html" .}} + +{{define "title"}} - {{.Repo}}: {{.Tag}}{{end}} + +{{define "conforma-policy-note"}} +
+ Conforma violations here are for the acs-conforma-dev policy only and only for components defined in + this repository (no results from retag-* pipelines).
+ For a preview of the release like acs-conforma-prod-like policy results, review the Conforma Release + Preview tab for the tag in the stackrox repository. +
+{{end}} + +{{define "conforma-tabs"}} +{{if and (not .Results.Violations) (not .Results.InProgressRuns) (not .Results.SkippedRuns)}} +

No violations found.

+{{else}} +
+ + + +
+
+ {{if not .Results.Groups}} +

No violations found.

+ {{else}} + {{range $i, $group := .Results.Groups}} +
+ + ▶ + {{$group.Name}} + {{len $group.Violations}} violation(s) + ({{len + $group.Archs}} architecture(s)) + +
+ + +
+ + + + + + + + + {{range $group.Violations}} + + + + + {{end}} + +
ImageViolation
{{.Image}}{{.Violation}}
+
+ {{end}} + {{end}} +
+ + +{{end}} +{{end}} + +{{define "content"}} +

{{.Repo}}: {{.Tag}}

+ +{{if .Errors}} +
+ Errors occurred while fetching data: + +
+{{end}} + +
+
+

Status

+
+ {{if eq .OverallStatus "green"}} + Green + {{else if eq .OverallStatus "waiting"}} + Waiting + {{else if eq .OverallStatus "error"}} + Error + {{else}} + Failed + {{end}} +
+
+
Pipelines
+
+ {{if .PipelinesError}} + Error fetching data + {{else if .AllPipelinesPassed}} + All passed + {{else if .PipelinesRunning}} + Running + {{else}} + Some failed + {{end}} +
+
+ {{if eq .Repo "stackrox"}} +
+
Release Preview violations
+
+ {{if .ConformaReleasePreview.Error}} + Error fetching data + {{else if .ConformaReleasePreview.Violations}} + {{len .ConformaReleasePreview.Violations}} violation(s) + {{else}} + No violations + {{end}} +
+
+ {{end}} +
+
Conforma Dev violations
+
+ {{if .ConformaDev.Error}} + Error fetching data + {{else if .HasViolations}} + {{len .ConformaDev.Violations}} violation(s) + {{else if or .ConformaDev.Waiting .PipelinesRunning}} + Waiting + {{else}} + No violations + {{end}} +
+
+
+ +
+

Artifacts

+
+
GitHub Commit
+
{{if .CommitSHA}}{{slice .CommitSHA 0 12}}{{else}}unavailable{{end}}
+
+ {{if eq .Repo "stackrox"}} +
+
Custom Snapshot
+
{{if .SnapshotName}}{{.SnapshotName}}{{else}}unavailable{{end}}
+
+
+
Operator Bundle Image
+
{{if .OperatorBundleImage}}{{.OperatorBundleImage}}{{else}}unavailable{{end}}
+
+
+
Operator Index Image
+
{{if .OperatorIndexImage}}{{.OperatorIndexImage}}{{else}}unavailable{{end}}
+
+ {{end}} +
+
+ +
+ + ▶ + Pipeline Runs + {{if .PipelinesError}} + error + {{else if .AllPipelinesPassed}} + passed + {{else if .PipelinesRunning}} + running + {{else}} + failed + {{end}} + + {{if not .PipelineGroups}} +

No pipeline runs found.

+ {{else}} + {{range $i, $m := .PipelineMilestones}} +
+ + ▶ + {{$m.Name}} + {{if eq $m.Status "passed"}} + passed + {{else if eq $m.Status "running"}} + running + {{else}} + failed + {{end}} + ({{len + $m.Groups}} group(s)) + {{if $m.FailedPipelines}} + {{if $m.Retestable}} + + {{else}} + 🔁 + {{end}} + {{end}} + + {{range $j, $g := $m.Groups}} +
+ + ▶ + {{$g.Component}} + {{if $g.AllPassed}} + passed + {{else if $g.Running}} + running + {{else}} + failed + {{end}} + ({{len + $g.PipelineRuns}}) + {{if and (not $g.AllPassed) (not $g.Running)}} + {{if $m.Retestable}} + + {{else}} + 🔁 + {{end}} + {{end}} + + + + + + + + + + + + + {{range .PipelineRuns}} + + + + + + + + {{end}} + +
NameCreatedEvent TypeStatusReason
{{.Name}}{{.CreationTimestamp.Format "2006-01-02 15:04:05"}}{{.EventType}} + {{if .Succeeded}} + Succeeded + {{else if .Running}} + Running + {{else}} + Failed + {{end}} + {{.Reason}}
+ {{if and (not $g.AllPassed) (not $g.Running)}} + + {{end}} +
+ {{end}} + {{if $m.FailedPipelines}} + + {{end}} +
+ {{end}} + {{end}} +
+ +{{if eq .Repo "stackrox"}} +
+ + ▶ + Conforma Release Preview + {{if .ConformaReleasePreview.Error}} + error + {{else if .ConformaReleasePreview.Violations}} + {{len .ConformaReleasePreview.Violations}} violation(s) + {{else if .ConformaReleasePreview.Waiting}} + running + {{else}} + no violations + {{end}} + + {{if .ConformaReleasePreview.Error}} +

Failed to fetch conforma release preview violations. See errors above.

+ {{else}} + {{template "conforma-tabs" .PreviewTabs}} + {{end}} +
+{{end}} + +
+ + ▶ + Conforma Dev + {{if .ConformaDev.Error}} + error + {{else if .HasViolations}} + {{len .ConformaDev.Violations}} violation(s) + {{else if or .ConformaDev.Waiting .PipelinesRunning}} + waiting + {{else}} + no violations + {{end}} + + {{template "conforma-policy-note" .}} + {{if .ConformaDev.Error}} +

Failed to fetch conforma violations. See errors above. If no pipeline runs + are finished yet, revisit this page later.

+ {{else}} + {{template "conforma-tabs" .DevTabs}} + {{end}} +
+ + +{{end}} diff --git a/pkg/config/config.go b/pkg/config/config.go index 453c643..698e41f 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -6,8 +6,10 @@ const ( KonfluxTenant = "rh-acs-tenant" KonfluxApplicationPrefix = "acs" KonfluxHost = "konflux-ui.apps.stone-prd-rh01.pg1f.p1.openshiftapps.com" - KubearchiveAPIURL = "https://kubearchive-api-server-product-kubearchive.apps.stone-prd-rh01.pg1f.p1.openshiftapps.com" - ImageTagSuffix = "fast" + KubearchiveAPIURL = "https://kubearchive-api-server-product-kubearchive.apps.stone-prd-rh01.pg1f.p1.openshiftapps.com" + ConformaDevScenarioPrefix = "acs-conforma-dev" + ConformaProdLikeScenarioName = "acs-conforma-prod-like" + ImageTagSuffix = "fast" IssueSource = "redhat.atlassian.net" JiraBaseURL = "https://redhat.atlassian.net" JiraProjectKey = "ROX" diff --git a/pkg/konflux/conforma.go b/pkg/konflux/conforma.go index 959874b..8204d8b 100644 --- a/pkg/konflux/conforma.go +++ b/pkg/konflux/conforma.go @@ -32,24 +32,42 @@ 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. 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. -func ListConformaViolations(ctx context.Context, c *client.Client, org, repo, tag string) (ConformaViolations, error) { - pipelineRuns, err := getConformaPipelineRuns(ctx, c, org, repo, tag) +// 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. 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 { return ConformaViolations{}, fmt.Errorf("failed to get conforma pipeline runs: %w", err) } + return violationsFromPipelineRuns(ctx, c, pipelineRuns, tag) +} + +// violationsFromPipelineRuns lists the violations reported by the given +// conforma pipeline runs, reading the report of the most recent run per +// component and scenario. +func violationsFromPipelineRuns(ctx context.Context, c *client.Client, pipelineRuns []kflx.PipelineRun, tag string) (ConformaViolations, error) { conformaRuns := latestPipelineRunPerComponentAndScenario(pipelineRuns) if len(conformaRuns) == 0 { return ConformaViolations{}, fmt.Errorf("no conforma pipeline runs found for tag %s", tag) @@ -59,7 +77,10 @@ func ListConformaViolations(ctx context.Context, c *client.Client, org, repo, ta 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 } @@ -67,25 +88,32 @@ func ListConformaViolations(ctx context.Context, c *client.Client, org, repo, ta 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 c05ee2b..7359200 100644 --- a/pkg/konflux/conforma_runs.go +++ b/pkg/konflux/conforma_runs.go @@ -8,10 +8,17 @@ import ( "github.com/stackrox/relacs/pkg/client" kflx "github.com/stackrox/relacs/pkg/client/konflux" + "github.com/stackrox/relacs/pkg/config" "github.com/stackrox/relacs/pkg/github" "github.com/stackrox/relacs/pkg/utils" ) +// Conforma scenarios selectable via the --scenario flag. +const ( + ConformaScenarioDev = config.ConformaDevScenarioPrefix + ConformaScenarioProdLike = config.ConformaProdLikeScenarioName +) + type ConformaRunEntry struct { Name string `json:"name" yaml:"name"` Component string `json:"component" yaml:"component"` @@ -20,8 +27,8 @@ type ConformaRunEntry struct { URL string `json:"url" yaml:"url"` } -func ListConformaRuns(ctx context.Context, c *client.Client, org, repo, tag string) ([]ConformaRunEntry, error) { - pipelineRuns, err := getConformaPipelineRuns(ctx, c, org, repo, tag) +func ListConformaRuns(ctx context.Context, c *client.Client, org, repo, tag, scenario string) ([]ConformaRunEntry, error) { + pipelineRuns, err := conformaPipelineRunsForScenario(ctx, c, org, repo, tag, scenario) if err != nil { return nil, err } @@ -38,6 +45,50 @@ func ListConformaRuns(ctx context.Context, c *client.Client, org, repo, tag stri return entries, nil } +// conformaPipelineRunsForScenario returns the conforma pipeline runs for the +// given tag and scenario. The dev scenario discovers runs triggered by +// pipelines-as-code on the tag's commit; the prod-like scenario returns the +// runs attached to the tag's custom snapshot and is only supported for the +// main repository. +func conformaPipelineRunsForScenario(ctx context.Context, c *client.Client, org, repo, tag, scenario string) ([]kflx.PipelineRun, error) { + switch scenario { + case ConformaScenarioDev: + return getConformaPipelineRuns(ctx, c, org, repo, tag) + case ConformaScenarioProdLike: + if repo != config.MainGitHubRepository { + return nil, fmt.Errorf("the %s scenario is only supported for the %s repository", ConformaScenarioProdLike, config.MainGitHubRepository) + } + return getProdLikeConformaRuns(ctx, c, org, repo, tag) + default: + return nil, fmt.Errorf("unknown conforma scenario %q, must be %s or %s", scenario, ConformaScenarioDev, ConformaScenarioProdLike) + } +} + +// getProdLikeConformaRuns returns the acs-conforma-prod-like pipeline runs +// attached to the custom snapshot for the given tag. +func getProdLikeConformaRuns(ctx context.Context, c *client.Client, org, repo, tag string) ([]kflx.PipelineRun, error) { + snapshot, err := GetCustomSnapshot(ctx, c, config.KonfluxApplicationPrefix, org, repo, tag) + if err != nil { + return nil, fmt.Errorf("failed to get custom snapshot: %w", err) + } + + application := snapshot.Metadata.Labels["appstudio.openshift.io/application"] + scenarioName, err := utils.Application{Name: application}.GetResourceWithVersion(config.ConformaProdLikeScenarioName) + if err != nil { + return nil, fmt.Errorf("failed to get conforma scenario name: %w", err) + } + + labels := []string{ + "appstudio.openshift.io/snapshot=" + snapshot.Metadata.Name, + "test.appstudio.openshift.io/scenario=" + scenarioName, + } + pipelineRuns, err := c.GetKonflux().GetPipelineRuns(ctx, labels) + if err != nil { + return nil, fmt.Errorf("failed to get pipeline runs: %w", err) + } + return pipelineRuns, nil +} + func getConformaPipelineRuns(ctx context.Context, c *client.Client, org, repo, tag string) ([]kflx.PipelineRun, error) { commitObj, err := github.GetCommitForTag(ctx, c, org, repo, tag) if err != nil { @@ -72,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/pkg/serve/tag.go b/pkg/serve/tag.go new file mode 100644 index 0000000..5487de0 --- /dev/null +++ b/pkg/serve/tag.go @@ -0,0 +1,601 @@ +package serve + +import ( + "context" + "fmt" + "sort" + "strconv" + "strings" + "sync" + "time" + + "github.com/stackrox/relacs/pkg/client" + kflx "github.com/stackrox/relacs/pkg/client/konflux" + "github.com/stackrox/relacs/pkg/config" + "github.com/stackrox/relacs/pkg/github" + "github.com/stackrox/relacs/pkg/konflux" + "github.com/stackrox/relacs/pkg/utils" +) + +type PipelineGroup struct { + // Name is the pipeline name (the pipelinesascode.tekton.dev/original-prname + // label); it is the grouping key and drives the milestone membership. + Name string + // Component is the group's display name: the + // appstudio.openshift.io/component label, falling back to the pipeline + // name when the runs carry no component label. + Component string + PipelineRuns []PipelineRunInfo + AllPassed bool + // Running reports whether the group's latest run is still running. + Running bool +} + +// PipelineMilestone sorts the pipeline run groups into the release +// milestones, in order: everything else, the operator bundle, and the +// operator index and custom snapshot. +type PipelineMilestone struct { + Name string + Status string // "passed", "running", "failed" + Groups []PipelineGroup + // FailedPipelines lists the pipeline names of the groups whose latest + // run failed; they are the candidates for a /konflux-retest. + FailedPipelines []string + // Retestable reports whether the milestone's retest button is enabled: + // all groups of the previous milestone succeeded. + Retestable bool +} + +// pipelineMilestoneOrder lists the milestones in display order. A milestone +// with no group names collects all groups that belong to no other milestone. +var pipelineMilestoneOrder = []struct { + Name string + Names []string +}{ + {Name: "Operands & Operator"}, + {Name: "Operator Bundle", Names: []string{"operator-bundle-on-push"}}, + {Name: "Custom Snapshot & Index Image", Names: []string{"operator-index-on-push", "create-custom-snapshot"}}, +} + +// groupPipelineMilestones sorts the groups into the release milestones and +// computes each milestone's combined status from the latest run of each of +// its groups. Empty milestones are dropped. +func groupPipelineMilestones(groups []PipelineGroup) []PipelineMilestone { + membership := map[string]int{} + for i, milestone := range pipelineMilestoneOrder { + for _, name := range milestone.Names { + membership[name] = i + } + } + + byMilestone := make([][]PipelineGroup, len(pipelineMilestoneOrder)) + for _, g := range groups { + idx, ok := membership[g.Name] + if !ok { + idx = 0 + } + byMilestone[idx] = append(byMilestone[idx], g) + } + + var milestones []PipelineMilestone + for i, milestone := range pipelineMilestoneOrder { + if len(byMilestone[i]) == 0 { + continue + } + // A milestone is retestable when all groups of the previous + // milestone succeeded; an empty previous milestone counts as passed. + retestable := i == 0 || milestoneStatus(byMilestone[i-1]) == "passed" + + var failedPipelines []string + for _, g := range byMilestone[i] { + if len(g.PipelineRuns) > 0 && !g.PipelineRuns[0].Succeeded && !g.PipelineRuns[0].Running { + failedPipelines = append(failedPipelines, g.Name) + } + } + + milestones = append(milestones, PipelineMilestone{ + Name: milestone.Name, + Status: milestoneStatus(byMilestone[i]), + Groups: byMilestone[i], + FailedPipelines: failedPipelines, + Retestable: retestable, + }) + } + return milestones +} + +// milestoneStatus is the combined status of a milestone's groups: "failed" +// when the latest run of any group did not succeed and is not running, +// "running" while any latest run is still running, and "passed" otherwise. +func milestoneStatus(groups []PipelineGroup) string { + failed, running := false, false + for _, g := range groups { + if len(g.PipelineRuns) == 0 { + continue + } + latest := g.PipelineRuns[0] + switch { + case latest.Succeeded: + case latest.Running: + running = true + default: + failed = true + } + } + switch { + case failed: + return "failed" + case running: + return "running" + default: + return "passed" + } +} + +type PipelineRunInfo struct { + Name string + CreationTimestamp time.Time + EventType string + Succeeded bool + Running bool + Reason string + URL string +} + +type Violation struct { + Image string + Arch string + Violation string +} + +// ViolationGroup groups the conforma violations for one component version, +// e.g. all violations reported for images matching "scanner-4-10*". +type ViolationGroup struct { + Name string + Archs []string + Violations []Violation +} + +// ConformaRun is 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 ConformaRun struct { + Name string + URL string + Reason string +} + +func convertConformaRuns(runs []konflux.ConformaRunInfo) []ConformaRun { + out := make([]ConformaRun, len(runs)) + for i, run := range runs { + out[i] = ConformaRun{Name: run.Name, URL: run.URL, Reason: run.Reason} + } + return out +} + +// ConformaResults holds the results of one conforma scenario for a tag. +type ConformaResults struct { + Violations []Violation + Groups []ViolationGroup + InProgressRuns []ConformaRun + SkippedRuns []ConformaRun + Error bool + // Waiting reports that the checks are still running and nothing has been + // verified yet. + Waiting bool +} + +func convertConformaResult(result konflux.ConformaViolations) ConformaResults { + violations := convertViolations(result.Violations) + return ConformaResults{ + Violations: violations, + Groups: groupViolations(violations), + InProgressRuns: convertConformaRuns(result.InProgressRuns), + SkippedRuns: convertConformaRuns(result.SkippedRuns), + Waiting: len(result.InProgressRuns) > 0 && len(result.Violations.Violations) == 0 && len(result.SkippedRuns) == 0, + } +} + +// ConformaTabData is the data for one conforma widget's tab content, +// identified by a prefix that namespaces its element ids. +type ConformaTabData struct { + Prefix string + Results ConformaResults +} + +func (t TagData) DevTabs() ConformaTabData { + return ConformaTabData{Prefix: "dev", Results: t.ConformaDev} +} + +func (t TagData) PreviewTabs() ConformaTabData { + return ConformaTabData{Prefix: "preview", Results: t.ConformaReleasePreview} +} + +type TagData struct { + Tag string + Repo string + + OverallStatus string // "passed", "failed", "warning" + AllPipelinesPassed bool + // PipelinesRunning reports whether build pipeline runs are still running + // and none failed; it drives the Pipeline Runs widget's running status. + PipelinesRunning bool + HasViolations bool + + CommitSHA string + CommitURL string + SnapshotName string + SnapshotURL string + OperatorBundleImage string + OperatorIndexImage string + + PipelineGroups []PipelineGroup + PipelineMilestones []PipelineMilestone + PipelinesError bool + + // ConformaDev holds the acs-conforma-dev results, ConformaReleasePreview + // the acs-conforma-prod-like results (main repository only). + ConformaDev ConformaResults + ConformaReleasePreview ConformaResults + + Errors []string +} + +func computeTagOverallStatus(data *TagData, isMainRepo bool) { + data.AllPipelinesPassed = len(data.PipelineGroups) > 0 + for _, g := range data.PipelineGroups { + if !g.AllPassed { + data.AllPipelinesPassed = false + break + } + } + data.HasViolations = len(data.ConformaDev.Violations) > 0 + + // The pipeline widget's running status is independent of the overall + // status below, so compute it before the error early-return. + buildFailed, buildRunning := false, false + for _, g := range data.PipelineGroups { + if len(g.PipelineRuns) == 0 { + continue + } + latest := g.PipelineRuns[0] + if latest.Succeeded { + continue + } + if latest.Running { + buildRunning = true + } else { + buildFailed = true + } + } + data.PipelinesRunning = buildRunning && !buildFailed + + // The overall status is "error" when any data could not be retrieved, + // "failed" when prod-like violations were found, prod-like runs were + // skipped or the latest run of a build pipeline run group did not + // succeed, "waiting" while conforma or build pipeline runs are still in + // progress, and "green" only when every build pipeline run group's latest + // run succeeded and, for the main repository, the custom snapshot and the + // operator bundle and index images are present and there are no prod-like + // violations. For other repositories, green build pipeline runs are + // sufficient. + if data.PipelinesError || data.ConformaDev.Error || + (isMainRepo && (data.ConformaReleasePreview.Error || data.SnapshotName == "" || + data.OperatorBundleImage == "" || data.OperatorIndexImage == "")) { + data.OverallStatus = "error" + return + } + + failed := buildFailed || (isMainRepo && (len(data.ConformaReleasePreview.Violations) > 0 || len(data.ConformaReleasePreview.SkippedRuns) > 0)) + waiting := buildRunning || (isMainRepo && len(data.ConformaReleasePreview.InProgressRuns) > 0) + + switch { + case failed: + data.OverallStatus = "failed" + case waiting: + data.OverallStatus = "waiting" + default: + data.OverallStatus = "green" + } +} + +// violationImageGroup extracts the "--" prefix of an +// image name, e.g. "scanner-4-10" from "scanner-4-10-s390x". Components may +// themselves contain hyphens ("scanner-db-4-10"). Images without a numeric +// major-minor version form their own group. +func violationImageGroup(image string) string { + parts := strings.Split(image, "-") + for i := 1; i < len(parts)-1; i++ { + if isNumeric(parts[i]) && isNumeric(parts[i+1]) { + return strings.Join(parts[:i+2], "-") + } + } + return image +} + +// violationImageArch extracts the architecture of an image relative to its +// group prefix. Image names look like +// "operator-5-0-sha256:-ppc64le", so the remainder after the group is +// "[-]": the digest contains no hyphens, and the arch may +// ("x86-64"). Images without an arch suffix are the multi-arch image. +func violationImageArch(image, group string) string { + remainder := strings.TrimPrefix(strings.TrimPrefix(image, group), "-") + _, arch, found := strings.Cut(remainder, "-") + if !found || arch == "" { + return "multi-arch" + } + return arch +} + +func isNumeric(s string) bool { + _, err := strconv.Atoi(s) + return err == nil +} + +// groupViolations groups violations by their image's component version prefix +// and annotates each violation with its image's architecture. Groups are +// sorted by name, and each group lists its distinct architectures sorted. +func groupViolations(violations []Violation) []ViolationGroup { + groupMap := map[string][]Violation{} + var groupOrder []string + for _, v := range violations { + name := violationImageGroup(v.Image) + if _, exists := groupMap[name]; !exists { + groupOrder = append(groupOrder, name) + } + groupMap[name] = append(groupMap[name], v) + } + sort.Strings(groupOrder) + + groups := make([]ViolationGroup, 0, len(groupOrder)) + for _, name := range groupOrder { + seen := map[string]bool{} + var archs []string + for i := range groupMap[name] { + groupMap[name][i].Arch = violationImageArch(groupMap[name][i].Image, name) + if !seen[groupMap[name][i].Arch] { + seen[groupMap[name][i].Arch] = true + archs = append(archs, groupMap[name][i].Arch) + } + } + sort.Strings(archs) + groups = append(groups, ViolationGroup{ + Name: name, + Archs: archs, + Violations: groupMap[name], + }) + } + return groups +} + +// ComputeGroupStatus sets AllPassed on each group based on the most recently +// created PipelineRun. Runs within each group are sorted newest-first. +func ComputeGroupStatus(groups []PipelineGroup) { + for i := range groups { + runs := groups[i].PipelineRuns + sort.Slice(runs, func(a, b int) bool { + return runs[a].CreationTimestamp.After(runs[b].CreationTimestamp) + }) + groups[i].PipelineRuns = runs + if len(runs) > 0 { + groups[i].AllPassed = runs[0].Succeeded + groups[i].Running = !runs[0].Succeeded && runs[0].Running + } + } +} + +func convertPipelineRun(plr kflx.PipelineRun) (PipelineRunInfo, error) { + var condition kflx.PipelineRunCondition + for _, c := range plr.Status.Conditions { + if c.Type == "Succeeded" { + condition = c + break + } + } + if condition.Type == "" { + return PipelineRunInfo{}, fmt.Errorf("no Succeeded condition found for pipeline run %s", plr.Metadata.Name) + } + return PipelineRunInfo{ + Name: plr.Metadata.Name, + CreationTimestamp: *plr.Metadata.CreationTimestamp, + EventType: plr.Metadata.Labels["pipelinesascode.tekton.dev/event-type"], + Succeeded: condition.Status == "True", + Running: condition.Status == "Unknown", + Reason: condition.Reason, + URL: utils.Application{Name: plr.Metadata.Labels["appstudio.openshift.io/application"]}.GetPipelineRunURL(plr.Metadata.Name), + }, nil +} + +func groupPipelineRuns(plrs []kflx.PipelineRun) ([]PipelineGroup, error) { + groupMap := map[string][]PipelineRunInfo{} + components := map[string]string{} + var groupOrder []string + + for _, plr := range plrs { + info, err := convertPipelineRun(plr) + if err != nil { + return nil, err + } + name := plr.Metadata.Labels["pipelinesascode.tekton.dev/original-prname"] + if name == "" { + name = plr.Metadata.Name + } + if _, exists := groupMap[name]; !exists { + groupOrder = append(groupOrder, name) + components[name] = plr.Metadata.Labels["appstudio.openshift.io/component"] + } + groupMap[name] = append(groupMap[name], info) + } + + sort.Strings(groupOrder) + groups := make([]PipelineGroup, 0, len(groupOrder)) + for _, name := range groupOrder { + group := PipelineGroup{ + Name: name, + Component: name, + PipelineRuns: groupMap[name], + } + if component := components[name]; component != "" { + group.Component = component + } + groups = append(groups, group) + } + return groups, nil +} + +func convertViolations(v konflux.Violations) []Violation { + out := make([]Violation, len(v.Violations)) + for i, viol := range v.Violations { + out[i] = Violation{Image: viol.Image, Violation: viol.Violation} + } + return out +} + +func FetchTagData(ctx context.Context, c *client.Client, repo, tag string) TagData { + org := config.GitHubOrganization + appPrefix := config.KonfluxApplicationPrefix + isMainRepo := repo == config.MainGitHubRepository + + data := TagData{ + Tag: tag, + Repo: repo, + } + + var ( + mu sync.Mutex + wg sync.WaitGroup + commit github.CommitInfo + snap kflx.Snapshot + plrs []kflx.PipelineRun + indexImg konflux.OperatorIndexImage + ) + + addError := func(msg string) { + mu.Lock() + data.Errors = append(data.Errors, msg) + mu.Unlock() + } + + wg.Add(2) + + go func() { + defer wg.Done() + var err error + commit, err = github.GetCommitForTag(ctx, c, org, repo, tag) + if err != nil { + addError(fmt.Sprintf("Failed to get commit for tag: %v", err)) + } + }() + + go func() { + defer wg.Done() + var err error + plrs, err = konflux.ListPipelineRunsForTag(ctx, c, org, repo, tag) + if err != nil { + addError(fmt.Sprintf("Failed to list pipeline runs: %v", err)) + mu.Lock() + data.PipelinesError = true + mu.Unlock() + } + }() + + wg.Add(1) + go func() { + defer wg.Done() + result, violErr := konflux.ListConformaViolations(ctx, c, org, repo, tag, konflux.ConformaScenarioDev) + if violErr != nil { + addError(fmt.Sprintf("Failed to list conforma violations: %v", violErr)) + mu.Lock() + data.ConformaDev.Error = true + mu.Unlock() + return + } + mu.Lock() + data.ConformaDev = convertConformaResult(result) + mu.Unlock() + }() + + if isMainRepo { + wg.Add(3) + + go func() { + defer wg.Done() + result, violErr := konflux.ListConformaViolations(ctx, c, org, repo, tag, konflux.ConformaScenarioProdLike) + if violErr != nil { + addError(fmt.Sprintf("Failed to list conforma release preview violations: %v", violErr)) + mu.Lock() + data.ConformaReleasePreview.Error = true + mu.Unlock() + return + } + mu.Lock() + data.ConformaReleasePreview = convertConformaResult(result) + mu.Unlock() + }() + + go func() { + defer wg.Done() + var err error + indexImg, err = konflux.GetOperatorIndexImage(ctx, c, org, repo, tag) + if err != nil { + addError(fmt.Sprintf("Failed to get operator index image: %v", err)) + } + }() + + go func() { + defer wg.Done() + var err error + snap, err = konflux.GetCustomSnapshot(ctx, c, appPrefix, org, repo, tag) + if err != nil { + addError(fmt.Sprintf("Failed to get custom snapshot: %v", err)) + } + }() + } + + wg.Wait() + + if indexImg.ImageURL != "" { + data.OperatorIndexImage = indexImg.ImageURL + "@" + indexImg.ImageDigest + } + + if commit.SHA != "" { + data.CommitSHA = commit.SHA + data.CommitURL = fmt.Sprintf("https://github.com/%s/%s/commit/%s", org, repo, commit.SHA) + } + + if snap.Metadata.Name != "" { + data.SnapshotName = snap.Metadata.Name + snapshotURL, err := snap.GetURL() + if err == nil { + data.SnapshotURL = snapshotURL + } + + bundleImage, err := snap.GetOperatorBundleImage(tag) + if err != nil { + addError(fmt.Sprintf("Failed to get operator bundle image: %v", err)) + } else { + data.OperatorBundleImage = bundleImage + } + } + + if plrs != nil { + if len(plrs) == 0 { + addError("No pipeline runs found for tag") + data.PipelinesError = true + } else { + groups, err := groupPipelineRuns(plrs) + if err != nil { + addError(fmt.Sprintf("Failed to process pipeline runs: %v", err)) + data.PipelinesError = true + } else { + ComputeGroupStatus(groups) + data.PipelineGroups = groups + data.PipelineMilestones = groupPipelineMilestones(groups) + } + } + } + + computeTagOverallStatus(&data, isMainRepo) + + return data +} diff --git a/pkg/serve/tag_test.go b/pkg/serve/tag_test.go new file mode 100644 index 0000000..2e57f57 --- /dev/null +++ b/pkg/serve/tag_test.go @@ -0,0 +1,561 @@ +package serve + +import ( + "reflect" + "testing" + "time" + + kflx "github.com/stackrox/relacs/pkg/client/konflux" + "github.com/stackrox/relacs/pkg/konflux" +) + +func TestViolationImageGroup(t *testing.T) { + tests := []struct { + image string + expected string + }{ + // Image names look like + // "---sha256:-". + {image: "operator-5-0-sha256:f0c5ee9a4de4d26e02d84154e2cd2856f133661e44c5e9dfb1867c75cd8069c5-ppc64le", expected: "operator-5-0"}, + {image: "main-4-10-sha256:f0c5ee9a4de4d26e02d84154e2cd2856f133661e44c5e9dfb1867c75cd8069c5-x86-64", expected: "main-4-10"}, + // Components may themselves contain hyphens. + {image: "scanner-db-4-10-sha256:f0c5ee9a4de4d26e02d84154e2cd2856f133661e44c5e9dfb1867c75cd8069c5-s390x", expected: "scanner-db-4-10"}, + // Images without a numeric major-minor version form their own group. + {image: "main", expected: "main"}, + } + + for _, test := range tests { + t.Run(test.expected, func(t *testing.T) { + if got := violationImageGroup(test.image); got != test.expected { + t.Errorf("violationImageGroup(%q) = %q, expected %q", test.image, got, test.expected) + } + }) + } +} + +func TestViolationImageArch(t *testing.T) { + tests := []struct { + image string + group string + expected string + }{ + {image: "operator-5-0-sha256:f0c5ee9a4de4d26e02d84154e2cd2856f133661e44c5e9dfb1867c75cd8069c5-ppc64le", group: "operator-5-0", expected: "ppc64le"}, + // Arch names may contain hyphens themselves. + {image: "operator-5-0-sha256:f0c5ee9a4de4d26e02d84154e2cd2856f133661e44c5e9dfb1867c75cd8069c5-x86-64", group: "operator-5-0", expected: "x86-64"}, + // Images without an arch suffix are the multi-arch image. + {image: "operator-5-0-sha256:f0c5ee9a4de4d26e02d84154e2cd2856f133661e44c5e9dfb1867c75cd8069c5", group: "operator-5-0", expected: "multi-arch"}, + } + + for _, test := range tests { + t.Run(test.expected, func(t *testing.T) { + if got := violationImageArch(test.image, test.group); got != test.expected { + t.Errorf("violationImageArch(%q, %q) = %q, expected %q", test.image, test.group, got, test.expected) + } + }) + } +} + +func TestGroupViolations(t *testing.T) { + violations := []Violation{ + {Image: "scanner-4-10-sha256:f0c5ee9a4de4d26e02d84154e2cd2856f133661e44c5e9dfb1867c75cd8069c5-s390x", Violation: "violation one"}, + {Image: "main-4-10-sha256:f0c5ee9a4de4d26e02d84154e2cd2856f133661e44c5e9dfb1867c75cd8069c5", Violation: "violation two"}, + {Image: "scanner-4-10-sha256:f0c5ee9a4de4d26e02d84154e2cd2856f133661e44c5e9dfb1867c75cd8069c5", Violation: "violation three"}, + {Image: "main-4-10-sha256:f0c5ee9a4de4d26e02d84154e2cd2856f133661e44c5e9dfb1867c75cd8069c5", Violation: "violation four"}, + } + + groups := groupViolations(violations) + + expected := []ViolationGroup{ + { + Name: "main-4-10", + Archs: []string{"multi-arch"}, + Violations: []Violation{ + {Image: "main-4-10-sha256:f0c5ee9a4de4d26e02d84154e2cd2856f133661e44c5e9dfb1867c75cd8069c5", Arch: "multi-arch", Violation: "violation two"}, + {Image: "main-4-10-sha256:f0c5ee9a4de4d26e02d84154e2cd2856f133661e44c5e9dfb1867c75cd8069c5", Arch: "multi-arch", Violation: "violation four"}, + }, + }, + { + Name: "scanner-4-10", + Archs: []string{"multi-arch", "s390x"}, + Violations: []Violation{ + {Image: "scanner-4-10-sha256:f0c5ee9a4de4d26e02d84154e2cd2856f133661e44c5e9dfb1867c75cd8069c5-s390x", Arch: "s390x", Violation: "violation one"}, + {Image: "scanner-4-10-sha256:f0c5ee9a4de4d26e02d84154e2cd2856f133661e44c5e9dfb1867c75cd8069c5", Arch: "multi-arch", Violation: "violation three"}, + }, + }, + } + + if !reflect.DeepEqual(groups, expected) { + t.Errorf("groupViolations() = %+v, expected %+v", groups, expected) + } +} + +func TestGroupViolations_Empty(t *testing.T) { + if groups := groupViolations(nil); len(groups) != 0 { + t.Errorf("groupViolations(nil) = %+v, expected no groups", groups) + } +} + +func TestGroupPipelineRuns_ComponentNames(t *testing.T) { + newPipelineRun := func(name, pipeline, component string) kflx.PipelineRun { + labels := map[string]string{ + "pipelinesascode.tekton.dev/original-prname": pipeline, + } + if component != "" { + labels["appstudio.openshift.io/component"] = component + } + created := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC) + return kflx.PipelineRun{ + Metadata: kflx.Metadata{ + Name: name, + CreationTimestamp: &created, + Labels: labels, + }, + Status: kflx.PipelineRunStatus{ + Conditions: []kflx.PipelineRunCondition{ + {Type: "Succeeded", Status: "True", Reason: "Succeeded"}, + }, + }, + } + } + + plrs := []kflx.PipelineRun{ + newPipelineRun("plr-1", "operator-bundle-on-push", "operator-bundle-5-0"), + newPipelineRun("plr-2", "operator-bundle-on-push", "operator-bundle-5-0"), + newPipelineRun("plr-3", "create-custom-snapshot", ""), + } + + groups, err := groupPipelineRuns(plrs) + + if err != nil { + t.Fatalf("groupPipelineRuns() unexpected error: %v", err) + } + if len(groups) != 2 { + t.Fatalf("groupPipelineRuns() returned %d groups, expected 2", len(groups)) + } + byName := map[string]PipelineGroup{} + for _, g := range groups { + byName[g.Name] = g + } + bundle := byName["operator-bundle-on-push"] + if bundle.Component != "operator-bundle-5-0" { + t.Errorf("operator-bundle-on-push component = %q, expected operator-bundle-5-0", bundle.Component) + } + if len(bundle.PipelineRuns) != 2 { + t.Errorf("operator-bundle-on-push group has %d runs, expected 2", len(bundle.PipelineRuns)) + } + snapshot := byName["create-custom-snapshot"] + if snapshot.Component != "create-custom-snapshot" { + t.Errorf("create-custom-snapshot component = %q, expected fallback to the pipeline name", snapshot.Component) + } +} + +func TestGroupPipelineMilestones(t *testing.T) { + group := func(name string, succeeded, running bool) PipelineGroup { + return PipelineGroup{Name: name, PipelineRuns: []PipelineRunInfo{{Succeeded: succeeded, Running: running}}} + } + + groups := []PipelineGroup{ + group("operator-index-on-push", true, false), + group("main-on-push", false, true), + group("create-custom-snapshot", true, false), + group("operator-bundle-on-push", false, false), + group("retag-main", true, false), + } + + milestones := groupPipelineMilestones(groups) + + expected := []PipelineMilestone{ + { + Name: "Operands & Operator", + Status: "running", // main-on-push is running, retag-main passed + Groups: []PipelineGroup{group("main-on-push", false, true), group("retag-main", true, false)}, + FailedPipelines: nil, // the running group is not failed + Retestable: true, + }, + { + Name: "Operator Bundle", + Status: "failed", + Groups: []PipelineGroup{group("operator-bundle-on-push", false, false)}, + FailedPipelines: []string{"operator-bundle-on-push"}, + Retestable: false, // the previous milestone is still running + }, + { + Name: "Custom Snapshot & Index Image", + Status: "passed", + Groups: []PipelineGroup{group("operator-index-on-push", true, false), group("create-custom-snapshot", true, false)}, + Retestable: false, // the previous milestone failed + }, + } + + if !reflect.DeepEqual(milestones, expected) { + t.Errorf("groupPipelineMilestones() = %+v, expected %+v", milestones, expected) + } +} + +func TestGroupPipelineMilestones_Retestable(t *testing.T) { + group := func(name string, succeeded, running bool) PipelineGroup { + return PipelineGroup{Name: name, PipelineRuns: []PipelineRunInfo{{Succeeded: succeeded, Running: running}}} + } + + groups := []PipelineGroup{ + group("main-on-push", true, false), + group("operator-bundle-on-push", false, false), + group("create-custom-snapshot", true, false), + } + + milestones := groupPipelineMilestones(groups) + + // The first milestone passed, so the failed Operator Bundle milestone is + // retestable; it failed, so the last milestone is not. + if !milestones[1].Retestable { + t.Error("Operator Bundle milestone expected to be retestable") + } + if !reflect.DeepEqual(milestones[1].FailedPipelines, []string{"operator-bundle-on-push"}) { + t.Errorf("Operator Bundle FailedPipelines = %v, expected [operator-bundle-on-push]", milestones[1].FailedPipelines) + } + if milestones[2].Retestable { + t.Error("Custom Snapshot & Index Image milestone expected not to be retestable") + } +} + +func TestGroupPipelineMilestones_DropsEmptyMilestones(t *testing.T) { + // A non-main repository has no operator bundle/index pipelines. + groups := []PipelineGroup{{Name: "main-on-push", PipelineRuns: []PipelineRunInfo{{Succeeded: true}}}} + + milestones := groupPipelineMilestones(groups) + + if len(milestones) != 1 || milestones[0].Name != "Operands & Operator" { + t.Errorf("groupPipelineMilestones() = %+v, expected only the Operands & Operator milestone", milestones) + } +} + +func TestMilestoneStatus(t *testing.T) { + tests := []struct { + name string + groups []PipelineGroup + expected string + }{ + { + name: "passed when all latest runs succeeded", + groups: []PipelineGroup{{PipelineRuns: []PipelineRunInfo{{Succeeded: true}}}}, + expected: "passed", + }, + { + name: "running while a latest run is running", + groups: []PipelineGroup{{PipelineRuns: []PipelineRunInfo{{Running: true}}}}, + expected: "running", + }, + { + name: "failed when a latest run failed", + groups: []PipelineGroup{{PipelineRuns: []PipelineRunInfo{{}}}}, + expected: "failed", + }, + { + name: "failed takes precedence over running", + groups: []PipelineGroup{ + {PipelineRuns: []PipelineRunInfo{{Running: true}}}, + {PipelineRuns: []PipelineRunInfo{{}}}, + }, + expected: "failed", + }, + { + name: "passed for groups without runs", + groups: []PipelineGroup{{}}, + expected: "passed", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + if got := milestoneStatus(test.groups); got != test.expected { + t.Errorf("milestoneStatus() = %q, expected %q", got, test.expected) + } + }) + } +} + +func TestConvertConformaResult_Waiting(t *testing.T) { + tests := []struct { + name string + result konflux.ConformaViolations + expected bool + }{ + { + name: "waiting when only in-progress runs", + result: konflux.ConformaViolations{InProgressRuns: []konflux.ConformaRunInfo{{Name: "run"}}}, + expected: true, + }, + { + name: "not waiting when violations were found", + result: konflux.ConformaViolations{ + Violations: konflux.Violations{Violations: []konflux.Violation{{Image: "main", Violation: "v"}}}, + InProgressRuns: []konflux.ConformaRunInfo{{Name: "run"}}, + }, + expected: false, + }, + { + name: "not waiting when runs were skipped", + result: konflux.ConformaViolations{ + SkippedRuns: []konflux.ConformaRunInfo{{Name: "run"}}, + InProgressRuns: []konflux.ConformaRunInfo{{Name: "run"}}, + }, + expected: false, + }, + { + name: "not waiting when everything was read", + result: konflux.ConformaViolations{}, + expected: false, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + if got := convertConformaResult(test.result).Waiting; got != test.expected { + t.Errorf("Waiting = %v, expected %v", got, test.expected) + } + }) + } +} + +func TestComputeTagOverallStatus(t *testing.T) { + buildGroup := func(name string, succeeded, running bool) PipelineGroup { + return PipelineGroup{Name: name, PipelineRuns: []PipelineRunInfo{{Succeeded: succeeded, Running: running}}} + } + mainRepoGreen := TagData{ + PipelineGroups: []PipelineGroup{buildGroup("build", true, false)}, + SnapshotName: "snap", + OperatorBundleImage: "bundle", + OperatorIndexImage: "index", + ConformaReleasePreview: ConformaResults{}, + } + + tests := []struct { + name string + isMainRep bool + data TagData + expected string + }{ + { + name: "green when everything is satisfied", + isMainRep: true, + data: mainRepoGreen, + expected: "green", + }, + { + name: "error when pipeline runs could not be retrieved", + isMainRep: true, + data: TagData{PipelinesError: true}, + expected: "error", + }, + { + name: "error when dev conforma results could not be retrieved", + isMainRep: true, + data: TagData{ConformaDev: ConformaResults{Error: true}}, + expected: "error", + }, + { + name: "error when release preview results could not be retrieved", + isMainRep: true, + data: TagData{ConformaReleasePreview: ConformaResults{Error: true}}, + expected: "error", + }, + { + name: "error when the custom snapshot is missing", + isMainRep: true, + data: func() TagData { + d := mainRepoGreen + d.SnapshotName = "" + return d + }(), + expected: "error", + }, + { + name: "error when the operator bundle image is missing", + isMainRep: true, + data: func() TagData { + d := mainRepoGreen + d.OperatorBundleImage = "" + return d + }(), + expected: "error", + }, + { + name: "error when the operator index image is missing", + isMainRep: true, + data: func() TagData { + d := mainRepoGreen + d.OperatorIndexImage = "" + return d + }(), + expected: "error", + }, + { + name: "failed when there are release preview violations", + isMainRep: true, + data: func() TagData { + d := mainRepoGreen + d.ConformaReleasePreview.Violations = []Violation{{Image: "main-4-10", Violation: "v"}} + return d + }(), + expected: "failed", + }, + { + name: "failed when release preview runs were skipped", + isMainRep: true, + data: func() TagData { + d := mainRepoGreen + d.ConformaReleasePreview.SkippedRuns = []ConformaRun{{Name: "run"}} + return d + }(), + expected: "failed", + }, + { + name: "failed when the latest build pipeline run failed", + isMainRep: true, + data: func() TagData { + d := mainRepoGreen + d.PipelineGroups = []PipelineGroup{buildGroup("build", false, false)} + return d + }(), + expected: "failed", + }, + { + name: "waiting when release preview runs are in progress", + isMainRep: true, + data: func() TagData { + d := mainRepoGreen + d.ConformaReleasePreview.InProgressRuns = []ConformaRun{{Name: "run"}} + return d + }(), + expected: "waiting", + }, + { + name: "waiting when the latest build pipeline run is running", + isMainRep: true, + data: func() TagData { + d := mainRepoGreen + d.PipelineGroups = []PipelineGroup{buildGroup("build", false, true)} + return d + }(), + expected: "waiting", + }, + { + name: "failed takes precedence over waiting", + isMainRep: true, + data: func() TagData { + d := mainRepoGreen + d.PipelineGroups = []PipelineGroup{buildGroup("a", false, true), buildGroup("b", false, false)} + return d + }(), + expected: "failed", + }, + { + name: "green for other repositories with only green builds", + isMainRep: false, + data: TagData{PipelineGroups: []PipelineGroup{buildGroup("build", true, false)}}, + expected: "green", + }, + { + name: "waiting for other repositories with a running build", + isMainRep: false, + data: TagData{PipelineGroups: []PipelineGroup{buildGroup("build", false, true)}}, + expected: "waiting", + }, + { + name: "failed for other repositories with a failed build", + isMainRep: false, + data: TagData{PipelineGroups: []PipelineGroup{buildGroup("build", false, false)}}, + expected: "failed", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + computeTagOverallStatus(&test.data, test.isMainRep) + if test.data.OverallStatus != test.expected { + t.Errorf("computeTagOverallStatus() = %q, expected %q", test.data.OverallStatus, test.expected) + } + }) + } +} + +func TestComputeTagOverallStatus_PipelinesRunning(t *testing.T) { + buildGroup := func(name string, succeeded, running bool) PipelineGroup { + return PipelineGroup{Name: name, PipelineRuns: []PipelineRunInfo{{Succeeded: succeeded, Running: running}}} + } + + tests := []struct { + name string + groups []PipelineGroup + expectRunning bool + }{ + { + name: "running when all latest runs are running", + groups: []PipelineGroup{buildGroup("a", false, true), buildGroup("b", false, true)}, + expectRunning: true, + }, + { + name: "not running when a latest run failed", + groups: []PipelineGroup{buildGroup("a", false, true), buildGroup("b", false, false)}, + expectRunning: false, + }, + { + name: "not running when all latest runs succeeded", + groups: []PipelineGroup{buildGroup("a", true, false)}, + expectRunning: false, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + data := TagData{PipelineGroups: test.groups} + computeTagOverallStatus(&data, false) + if data.PipelinesRunning != test.expectRunning { + t.Errorf("PipelinesRunning = %v, expected %v", data.PipelinesRunning, test.expectRunning) + } + }) + } +} + +func TestComputeTagOverallStatus_PipelinesRunningDespiteError(t *testing.T) { + // The overall status errors out (e.g. the conforma fetch failed), but the + // pipeline widget still needs its running status. + data := TagData{ + ConformaDev: ConformaResults{Error: true}, + PipelineGroups: []PipelineGroup{{Name: "a", PipelineRuns: []PipelineRunInfo{{Running: true}}}}, + } + + computeTagOverallStatus(&data, false) + + if data.OverallStatus != "error" { + t.Errorf("OverallStatus = %q, expected error", data.OverallStatus) + } + if !data.PipelinesRunning { + t.Error("PipelinesRunning = false, expected true despite the overall error") + } +} + +func TestComputeGroupStatus_Running(t *testing.T) { + newer := time.Date(2025, 2, 1, 0, 0, 0, 0, time.UTC) + older := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC) + + groups := []PipelineGroup{{ + Name: "scanner-on-push", + PipelineRuns: []PipelineRunInfo{ + {Name: "old", CreationTimestamp: older, Succeeded: true}, + {Name: "new", CreationTimestamp: newer, Running: true}, + }, + }} + + ComputeGroupStatus(groups) + + if groups[0].AllPassed { + t.Error("AllPassed = true, expected false for a running latest run") + } + if !groups[0].Running { + t.Error("Running = false, expected true for a running latest run") + } + if groups[0].PipelineRuns[0].Name != "new" { + t.Errorf("latest run = %q, expected the newest run to be sorted first", groups[0].PipelineRuns[0].Name) + } +} diff --git a/tests/konflux/conforma_violations_test.go b/tests/konflux/conforma_violations_test.go index b707e6d..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" ) @@ -104,24 +105,154 @@ func (s *ConformaViolationsTestSuite) mockConformaReportUnavailable(runName stri func (s *ConformaViolationsTestSuite) listConformaViolations() (relacskonflux.ConformaViolations, error) { testClient, err := helpers.SetupTestClient() s.Require().NoError(err) - return relacskonflux.ListConformaViolations(s.ctx, testClient, config.GitHubOrganization, config.MainGitHubRepository, conformaTestTag) + return relacskonflux.ListConformaViolations(s.ctx, testClient, config.GitHubOrganization, config.MainGitHubRepository, conformaTestTag, relacskonflux.ConformaScenarioDev) +} + +// mockProdLikeCustomSnapshot mocks the custom snapshot for the test tag and +// returns its name. The snapshot's application label is versioned, from which +// the versioned prod-like scenario name is derived. +func (s *ConformaViolationsTestSuite) mockProdLikeCustomSnapshot() string { + helpers.MockLightweightTag(config.GitHubOrganization, config.MainGitHubRepository, conformaTestTag, conformaTestSHA) + + labels := strings.Join([]string{ + "pac.test.appstudio.openshift.io/url-org=" + config.GitHubOrganization, + "pac.test.appstudio.openshift.io/url-repository=" + config.MainGitHubRepository, + "pac.test.appstudio.openshift.io/sha=" + conformaTestSHA, + "acs-style-snapshot=true", + "acs-product-version=" + conformaTestTag, + "appstudio.openshift.io/application=acs-4-10", + }, ",") + + helpers.MockK8sSnapshots(labels, map[string]interface{}{ + "items": []map[string]interface{}{ + { + "metadata": map[string]interface{}{ + "name": "acs-4-10-custom-snapshot", + "uid": "uid-snapshot", + "creationTimestamp": "2025-01-01T00:00:00Z", + "labels": map[string]interface{}{ + "appstudio.openshift.io/application": "acs-4-10", + }, + }, + }, + }, + }) + helpers.MockQueryResourcesEmpty(config.KubearchiveAPIURL, "appstudio.redhat.com", "v1alpha1", "snapshots", config.KonfluxTenant) + return "acs-4-10-custom-snapshot" +} + +// mockProdLikeConformaRuns mocks the prod-like conforma pipeline runs attached +// to the given custom snapshot. +func (s *ConformaViolationsTestSuite) mockProdLikeConformaRuns(snapshotName string, runs ...map[string]interface{}) { + labels := strings.Join([]string{ + "appstudio.openshift.io/snapshot=" + snapshotName, + "test.appstudio.openshift.io/scenario=acs-conforma-prod-like-4-10", + }, ",") + + helpers.MockK8sPipelineRuns(labels, map[string]interface{}{ + "items": runs, + }) + helpers.MockQueryResourcesEmpty(config.KubearchiveAPIURL, "tekton.dev", "v1", "pipelineruns", config.KonfluxTenant) +} + +func (s *ConformaViolationsTestSuite) prodLikeConformaPipelineRun(name, uid, component string, conditions []map[string]interface{}) map[string]interface{} { + return map[string]interface{}{ + "metadata": map[string]interface{}{ + "name": name, + "uid": uid, + "creationTimestamp": "2025-01-01T00:00:00Z", + "labels": map[string]interface{}{ + "appstudio.openshift.io/component": component, + "test.appstudio.openshift.io/scenario": "acs-conforma-prod-like-4-10", + }, + }, + "status": map[string]interface{}{ + "conditions": conditions, + }, + } +} + +func (s *ConformaViolationsTestSuite) listConformaViolationsForScenario(repo, scenario string) (relacskonflux.ConformaViolations, error) { + testClient, err := helpers.SetupTestClient() + s.Require().NoError(err) + return relacskonflux.ListConformaViolations(s.ctx, testClient, config.GitHubOrganization, repo, conformaTestTag, scenario) +} + +func (s *ConformaViolationsTestSuite) TestListConformaViolations_ProdLike() { + snapshotName := s.mockProdLikeCustomSnapshot() + run := s.prodLikeConformaPipelineRun("acs-conforma-prod-like-4-10-abc12", "uid-prod-like", "main-4-10", s.succeededConditions()) + s.mockProdLikeConformaRuns(snapshotName, run) + s.mockConformaReport("acs-conforma-prod-like-4-10-abc12") + + result, err := s.listConformaViolationsForScenario(config.MainGitHubRepository, relacskonflux.ConformaScenarioProdLike) + + s.Require().NoError(err) + s.Len(result.Violations.Violations, 1) + s.Equal("violation one", result.Violations.Violations[0].Violation) + s.Empty(result.SkippedRuns) + s.Empty(result.InProgressRuns) + s.True(gock.IsDone()) +} + +func (s *ConformaViolationsTestSuite) TestListConformaViolations_ProdLikeNonMainRepo() { + _, err := s.listConformaViolationsForScenario("collector", relacskonflux.ConformaScenarioProdLike) + + s.Require().Error(err) + s.Contains(err.Error(), "only supported for the stackrox repository") + s.True(gock.IsDone()) +} + +func (s *ConformaViolationsTestSuite) TestListConformaViolations_UnknownScenario() { + _, err := s.listConformaViolationsForScenario(config.MainGitHubRepository, "acs-conforma-other") + + s.Require().Error(err) + s.Contains(err.Error(), "unknown conforma scenario") + s.True(gock.IsDone()) +} + +func (s *ConformaViolationsTestSuite) TestListConformaRuns_ProdLike() { + snapshotName := s.mockProdLikeCustomSnapshot() + run := s.prodLikeConformaPipelineRun("acs-conforma-prod-like-4-10-abc12", "uid-prod-like", "main-4-10", s.succeededConditions()) + s.mockProdLikeConformaRuns(snapshotName, run) + + testClient, err := helpers.SetupTestClient() + s.Require().NoError(err) + entries, err := relacskonflux.ListConformaRuns(s.ctx, testClient, config.GitHubOrganization, config.MainGitHubRepository, conformaTestTag, relacskonflux.ConformaScenarioProdLike) + + s.Require().NoError(err) + s.Require().Len(entries, 1) + s.Equal("acs-conforma-prod-like-4-10-abc12", entries[0].Name) + s.Equal("main-4-10", entries[0].Component) + s.Equal("acs-conforma-prod-like-4-10", entries[0].Scenario) + s.Equal("Succeeded", entries[0].Status) + s.True(gock.IsDone()) } 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) @@ -134,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()) } @@ -150,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()) } @@ -181,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()) }