Skip to content

Commit

Permalink
feat: pipelinerun list
Browse files Browse the repository at this point in the history
  • Loading branch information
xinnjie committed Dec 16, 2024
1 parent ff7ac6c commit e44529b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 19 deletions.
66 changes: 48 additions & 18 deletions pkg/cli/cmd/pipelinerun/list.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
package pipelinerun

import (
"context"
"encoding/json"
"fmt"
"io"
"text/tabwriter"
"text/template"

"github.com/jonboulle/clockwork"
"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/formatted"
pipelinev1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
pipelinev1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/results/pkg/cli/flags"
"github.com/tektoncd/results/pkg/cli/format"
pb "github.com/tektoncd/results/proto/v1alpha2/results_go_proto"
)

const listTemplate = `{{- $size := len .Results -}}{{- if eq $size 0 -}}
const listTemplate = `{{- $size := len .PipelineRuns -}}{{- if eq $size 0 -}}
No PipelineRuns found
{{ else -}}
NAMESPACE UID STARTED DURATION STATUS
{{- range $_, $result := .Results }}
{{ formatNamespace $result.Name }} {{ $result.Uid }} {{ formatAge $result.Summary.StartTime $.Time }} {{ formatDuration $result.Summary.StartTime $result.Summary.EndTime }} {{ formatStatus $result.Summary.Status }}
{{- range $_, $pr := .PipelineRuns }}
{{ $pr.ObjectMeta.Namespace }} {{ $pr.ObjectMeta.Name }} {{ formatAge $pr.Status.StartTime $.Time }} {{ formatDuration $pr.Status.StartTime $pr.Status.CompletionTime }} {{ formatCondition $pr.Status.Conditions }}
{{- end -}}
{{- end -}}`

Expand Down Expand Up @@ -50,39 +54,65 @@ List all PipelineRuns in 'default' namespace:
return fmt.Errorf("limit was %d, but must be greater than 0", opts.Limit)
}

resp, err := params.ResultsClient.ListResults(cmd.Context(), &pb.ListResultsRequest{
Parent: opts.Namespace,
resp, err := params.ResultsClient.ListRecords(cmd.Context(), &pb.ListRecordsRequest{
Parent: fmt.Sprintf("%s/results/-", opts.Namespace),
PageSize: int32(opts.Limit),
Filter: `summary.type==PIPELINE_RUN`,
Filter: `data_type==PIPELINE_RUN`,
})
if err != nil {
return fmt.Errorf("failed to list PipelineRuns from namespace %s: %v", opts.Namespace, err)
}
return printFormatted(cmd.OutOrStdout(), resp.Results, params.Clock)
return printFormatted(cmd.OutOrStdout(), resp.Records, params.Clock)
},
}
cmd.Flags().StringVarP(&opts.Namespace, "namespace", "n", "default", "Namespace to list PipelineRuns in")
cmd.Flags().IntVarP(&opts.Limit, "limit", "l", 0, "Limit the number of PipelineRuns to return")
return cmd
}

func printFormatted(out io.Writer, results []*pb.Result, c clockwork.Clock) error {
func pipelineRunFromRecord(record *pb.Record) (*pipelinev1.PipelineRun, error) {
pr := &pipelinev1.PipelineRun{}
if record.Data.GetType() == "tekton.dev/v1beta1.PipelineRun" {
prV1beta1 := &pipelinev1beta1.PipelineRun{}
if err := json.Unmarshal(record.Data.Value, prV1beta1); err != nil {
return nil, fmt.Errorf("failed to unmarshal PipelineRun data: %v", err)
}
if err := pr.ConvertFrom(context.TODO(), prV1beta1); err != nil {
return nil, fmt.Errorf("failed to convert v1beta1 PipelineRun to v1: %v", err)
}
} else {
if err := json.Unmarshal(record.Data.Value, pr); err != nil {
return nil, fmt.Errorf("failed to unmarshal PipelineRun data: %v", err)
}
}
return pr, nil
}

func printFormatted(out io.Writer, records []*pb.Record, c clockwork.Clock) error {
var data = struct {
Results []*pb.Result
Time clockwork.Clock
PipelineRuns []*pipelinev1.PipelineRun
Time clockwork.Clock
}{
Results: results,
Time: c,
PipelineRuns: []*pipelinev1.PipelineRun{},
Time: c,
}

for _, record := range records {
if pr, err := pipelineRunFromRecord(record); err != nil {
continue
} else {
data.PipelineRuns = append(data.PipelineRuns, pr)
}
}

funcMap := template.FuncMap{
"formatAge": format.Age,
"formatDuration": format.Duration,
"formatStatus": format.Status,
"formatNamespace": format.Namespace,
"formatAge": formatted.Age,
"formatDuration": formatted.Duration,
"formatCondition": formatted.Condition,
}

w := tabwriter.NewWriter(out, 0, 5, 3, ' ', tabwriter.TabIndent)
t := template.Must(template.New("List PipelineRuns").Funcs(funcMap).Parse(listTemplate))
t := template.Must(template.New("List TaskRuns").Funcs(funcMap).Parse(listTemplate))

err := t.Execute(w, data)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/cmd/records/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const tmpl = `Name: {{.Record.Name}}
UID: {{.Record.Uid}}
Status:
Created: {{ formatAge .Record.CreateTime .Time }} Duration: {{formatDuration .Record.CreateTime .Record.UpdateTime}}
{{if .Record.Data }}
{{- if .Record.Data }}
Type: {{.Record.Data.Type}}
Data:
{{formatJSON .Record.Data.Value}}
Expand Down

0 comments on commit e44529b

Please sign in to comment.