From d5661f17f1a278f44c89b095ea5511fd5f7608a6 Mon Sep 17 00:00:00 2001 From: Sami Alajrami Date: Fri, 23 Jan 2026 10:10:57 +0200 Subject: [PATCH 1/2] update list artifacts to use the new api endpoint --- cmd/kosli/listArtifacts.go | 164 +++++++++++++++----------------- cmd/kosli/listArtifacts_test.go | 6 -- 2 files changed, 79 insertions(+), 91 deletions(-) diff --git a/cmd/kosli/listArtifacts.go b/cmd/kosli/listArtifacts.go index cb898ce23..bddffa277 100644 --- a/cmd/kosli/listArtifacts.go +++ b/cmd/kosli/listArtifacts.go @@ -66,10 +66,6 @@ func newListArtifactsCmd(out io.Writer) *cobra.Command { if err != nil { return ErrorBeforePrintingUsage(cmd, err.Error()) } - err = MuXRequiredFlags(cmd, []string{"flow", "repo"}, true) - if err != nil { - return err - } return o.validate(cmd) }, RunE: func(cmd *cobra.Command, args []string) error { @@ -85,15 +81,13 @@ func newListArtifactsCmd(out io.Writer) *cobra.Command { } func (o *listArtifactsOptions) run(out io.Writer) error { - var url string + url := fmt.Sprintf("%s/api/v2/artifacts/%s?page=%d&per_page=%d", + global.Host, global.Org, o.pageNumber, o.pageLimit) + if o.flowName != "" { - filter = "flow" - url = fmt.Sprintf("%s/api/v2/artifacts/%s/%s?page=%d&per_page=%d", - global.Host, global.Org, o.flowName, o.pageNumber, o.pageLimit) - } else { - filter = "repo" - url = fmt.Sprintf("%s/api/v2/repos/%s/artifacts/%s?page=%d&per_page=%d", - global.Host, global.Org, o.repoName, o.pageNumber, o.pageLimit) + url = url + fmt.Sprintf("&flow_name=%s", o.flowName) + } else if o.repoName != "" { + url = url + fmt.Sprintf("&repo_name=%s", o.repoName) } reqParams := &requests.RequestParams{ @@ -113,15 +107,15 @@ func (o *listArtifactsOptions) run(out io.Writer) error { }) } -func printArtifactsListAsTable(raw string, out io.Writer, page int) error { - if filter == "flow" { - return printArtifactsListForFlow(raw, out, page) - } else { - return printArtifactsListForRepo(raw, out, page) - } -} +// func printArtifactsListAsTable(raw string, out io.Writer, page int) error { +// if filter == "flow" { +// return printArtifactsListForFlow(raw, out, page) +// } else { +// return printArtifactsListForRepo(raw, out, page) +// } +// } -func printArtifactsListForFlow(raw string, out io.Writer, page int) error { +func printArtifactsListAsTable(raw string, out io.Writer, page int) error { var artifacts []map[string]any err := json.Unmarshal([]byte(raw), &artifacts) if err != nil { @@ -162,68 +156,68 @@ func printArtifactsListForFlow(raw string, out io.Writer, page int) error { return nil } -func printArtifactsListForRepo(raw string, out io.Writer, page int) error { - var response map[string]any - err := json.Unmarshal([]byte(raw), &response) - if err != nil { - return err - } - - embedded, ok := response["_embedded"].(map[string]any) - if !ok { - return fmt.Errorf("artifacts not found in response") - } - artifactsRaw, ok := embedded["artifacts"] - if !ok { - return fmt.Errorf("artifacts not found in response") - } - artifactsSlice, ok := artifactsRaw.([]any) - if !ok { - return fmt.Errorf("artifacts not found in response") - } - artifacts := make([]map[string]any, len(artifactsSlice)) - for i, v := range artifactsSlice { - artifact, ok := v.(map[string]any) - if !ok { - return fmt.Errorf("invalid artifact format in response") - } - artifacts[i] = artifact - } - if len(artifacts) == 0 { - msg := "No artifacts were found" - if page != 1 { - msg = fmt.Sprintf("%s at page number %d", msg, page) - } - logger.Info(msg + ".") - return nil - } - - header := []string{"COMMIT", "ARTIFACT", "STATE", "CREATED_AT"} - rows := []string{} - for _, artifact := range artifacts { - gitCommit := artifact["commit"].(string)[:7] - artifactName := artifact["name"].(string) - - artifactDigest := artifact["fingerprint"].(string) - compliant := artifact["compliant_in_trail"].(bool) - artifactState := "COMPLIANT" - if !compliant { - artifactState = "NON-COMPLIANT" - } - createdAt, err := formattedTimestamp(artifact["created_at"], true) - if err != nil { - return err - } - - row := fmt.Sprintf("%s\tName: %s\t%s\t%s", gitCommit, artifactName, artifactState, createdAt) - rows = append(rows, row) - row = fmt.Sprintf("\tFingerprint: %s\t\t", artifactDigest) - rows = append(rows, row) - rows = append(rows, "\t\t\t") - - } - tabFormattedPrint(out, header, rows) - - return nil - -} +// func printArtifactsListForRepo(raw string, out io.Writer, page int) error { +// var response map[string]any +// err := json.Unmarshal([]byte(raw), &response) +// if err != nil { +// return err +// } + +// embedded, ok := response["_embedded"].(map[string]any) +// if !ok { +// return fmt.Errorf("artifacts not found in response") +// } +// artifactsRaw, ok := embedded["artifacts"] +// if !ok { +// return fmt.Errorf("artifacts not found in response") +// } +// artifactsSlice, ok := artifactsRaw.([]any) +// if !ok { +// return fmt.Errorf("artifacts not found in response") +// } +// artifacts := make([]map[string]any, len(artifactsSlice)) +// for i, v := range artifactsSlice { +// artifact, ok := v.(map[string]any) +// if !ok { +// return fmt.Errorf("invalid artifact format in response") +// } +// artifacts[i] = artifact +// } +// if len(artifacts) == 0 { +// msg := "No artifacts were found" +// if page != 1 { +// msg = fmt.Sprintf("%s at page number %d", msg, page) +// } +// logger.Info(msg + ".") +// return nil +// } + +// header := []string{"COMMIT", "ARTIFACT", "STATE", "CREATED_AT"} +// rows := []string{} +// for _, artifact := range artifacts { +// gitCommit := artifact["commit"].(string)[:7] +// artifactName := artifact["name"].(string) + +// artifactDigest := artifact["fingerprint"].(string) +// compliant := artifact["compliant_in_trail"].(bool) +// artifactState := "COMPLIANT" +// if !compliant { +// artifactState = "NON-COMPLIANT" +// } +// createdAt, err := formattedTimestamp(artifact["created_at"], true) +// if err != nil { +// return err +// } + +// row := fmt.Sprintf("%s\tName: %s\t%s\t%s", gitCommit, artifactName, artifactState, createdAt) +// rows = append(rows, row) +// row = fmt.Sprintf("\tFingerprint: %s\t\t", artifactDigest) +// rows = append(rows, row) +// rows = append(rows, "\t\t\t") + +// } +// tabFormattedPrint(out, header, rows) + +// return nil + +// } diff --git a/cmd/kosli/listArtifacts_test.go b/cmd/kosli/listArtifacts_test.go index 72ab0aaa8..8fba6fc70 100644 --- a/cmd/kosli/listArtifacts_test.go +++ b/cmd/kosli/listArtifacts_test.go @@ -62,12 +62,6 @@ func (suite *ListArtifactsCommandTestSuite) TearDownTest() { func (suite *ListArtifactsCommandTestSuite) TestListArtifactsCmd() { tests := []cmdTestCase{ - { - wantError: true, - name: "missing both flow and repo flags causes an error", - cmd: fmt.Sprintf(`list artifacts %s`, suite.defaultKosliArguments), - golden: "Error: at least one of --flow, --repo is required\n", - }, { wantError: true, name: "non-existing flow causes an error", From 689e3ebe6b7e6af0f20128af93fcb3329869f32d Mon Sep 17 00:00:00 2001 From: Sami Alajrami Date: Fri, 23 Jan 2026 10:12:13 +0200 Subject: [PATCH 2/2] remove dead code --- cmd/kosli/listArtifacts.go | 76 -------------------------------------- 1 file changed, 76 deletions(-) diff --git a/cmd/kosli/listArtifacts.go b/cmd/kosli/listArtifacts.go index bddffa277..3225c3b44 100644 --- a/cmd/kosli/listArtifacts.go +++ b/cmd/kosli/listArtifacts.go @@ -51,8 +51,6 @@ type listArtifactsOptions struct { repoName string } -var filter string - func newListArtifactsCmd(out io.Writer) *cobra.Command { o := new(listArtifactsOptions) cmd := &cobra.Command{ @@ -107,14 +105,6 @@ func (o *listArtifactsOptions) run(out io.Writer) error { }) } -// func printArtifactsListAsTable(raw string, out io.Writer, page int) error { -// if filter == "flow" { -// return printArtifactsListForFlow(raw, out, page) -// } else { -// return printArtifactsListForRepo(raw, out, page) -// } -// } - func printArtifactsListAsTable(raw string, out io.Writer, page int) error { var artifacts []map[string]any err := json.Unmarshal([]byte(raw), &artifacts) @@ -155,69 +145,3 @@ func printArtifactsListAsTable(raw string, out io.Writer, page int) error { return nil } - -// func printArtifactsListForRepo(raw string, out io.Writer, page int) error { -// var response map[string]any -// err := json.Unmarshal([]byte(raw), &response) -// if err != nil { -// return err -// } - -// embedded, ok := response["_embedded"].(map[string]any) -// if !ok { -// return fmt.Errorf("artifacts not found in response") -// } -// artifactsRaw, ok := embedded["artifacts"] -// if !ok { -// return fmt.Errorf("artifacts not found in response") -// } -// artifactsSlice, ok := artifactsRaw.([]any) -// if !ok { -// return fmt.Errorf("artifacts not found in response") -// } -// artifacts := make([]map[string]any, len(artifactsSlice)) -// for i, v := range artifactsSlice { -// artifact, ok := v.(map[string]any) -// if !ok { -// return fmt.Errorf("invalid artifact format in response") -// } -// artifacts[i] = artifact -// } -// if len(artifacts) == 0 { -// msg := "No artifacts were found" -// if page != 1 { -// msg = fmt.Sprintf("%s at page number %d", msg, page) -// } -// logger.Info(msg + ".") -// return nil -// } - -// header := []string{"COMMIT", "ARTIFACT", "STATE", "CREATED_AT"} -// rows := []string{} -// for _, artifact := range artifacts { -// gitCommit := artifact["commit"].(string)[:7] -// artifactName := artifact["name"].(string) - -// artifactDigest := artifact["fingerprint"].(string) -// compliant := artifact["compliant_in_trail"].(bool) -// artifactState := "COMPLIANT" -// if !compliant { -// artifactState = "NON-COMPLIANT" -// } -// createdAt, err := formattedTimestamp(artifact["created_at"], true) -// if err != nil { -// return err -// } - -// row := fmt.Sprintf("%s\tName: %s\t%s\t%s", gitCommit, artifactName, artifactState, createdAt) -// rows = append(rows, row) -// row = fmt.Sprintf("\tFingerprint: %s\t\t", artifactDigest) -// rows = append(rows, row) -// rows = append(rows, "\t\t\t") - -// } -// tabFormattedPrint(out, header, rows) - -// return nil - -// }