Repository: openshift-pipelines/pipelines-as-code
Pull request #166 (2021-06-29 12:31:36)

Add age to repo ls

![image](https://user-images.githubusercontent.com/98980/123797645-b3781080-d8e6-11eb-81db-1c4bb9f493be.png)

Signed-off-by: Chmouel Boudjnah <chmouel@redhat.com>



Patch:
diff --git a/pkg/cli/ui/formating.go b/pkg/cli/ui/formating.go
index ce214845..c7d16d72 100644
--- a/pkg/cli/ui/formating.go
+++ b/pkg/cli/ui/formating.go
@@ -5,7 +5,9 @@ import (
 	"net/url"
 	"strings"
 
+	"github.com/jonboulle/clockwork"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
+	"github.com/openshift-pipelines/pipelines-as-code/pkg/pipelineascode"
 )
 
 var shortShaLength = 7
@@ -54,3 +56,10 @@ func ShowLastSHA(repository v1alpha1.Repository) string {
 	}
 	return ShortSHA(*repository.Status[len(repository.Status)-1].SHA)
 }
+
+func ShowLastAge(repository v1alpha1.Repository, cw clockwork.Clock) string {
+	if len(repository.Status) == 0 {
+		return "---"
+	}
+	return pipelineascode.Age(repository.Status[len(repository.Status)-1].CompletionTime, cw)
+}
diff --git a/pkg/cmd/repository/list.go b/pkg/cmd/repository/list.go
index 8c4cbe7e..a8ace677 100644
--- a/pkg/cmd/repository/list.go
+++ b/pkg/cmd/repository/list.go
@@ -5,6 +5,7 @@ import (
 	"fmt"
 	"text/tabwriter"
 
+	"github.com/jonboulle/clockwork"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/cli"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/cli/ui"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/cmd/completion"
@@ -14,8 +15,8 @@ import (
 )
 
 var (
-	header            = "NAME\tOWNER/REPOSITORY\tSHA\tEVENT-TYPE"
-	body              = "%s\t%s\t%s\t%s"
+	header            = "NAME\tAGE\tOWNER/REPOSITORY\tSHA\tEVENT-TYPE"
+	body              = "%s\t%s\t%s\t%s\t%s"
 	allNamespacesFlag = "all-namespaces"
 	namespaceFlag     = "namespace"
 )
@@ -50,7 +51,8 @@ func ListCommand(p cli.Params) *cobra.Command {
 				return err
 			}
 			ctx := context.Background()
-			return list(ctx, cs, opts, ioStreams, p.GetNamespace(), selectors, noheaders)
+			cw := clockwork.NewRealClock()
+			return list(ctx, cs, opts, ioStreams, cw, p.GetNamespace(), selectors, noheaders)
 		},
 	}
 
@@ -79,7 +81,9 @@ func ListCommand(p cli.Params) *cobra.Command {
 }
 
 func list(ctx context.Context, cs *cli.Clients, opts *flags.CliOpts, ioStreams *ui.IOStreams,
-	currentNamespace, selectors string, noheaders bool) error {
+	cw clockwork.Clock,
+	currentNamespace, selectors string,
+	noheaders bool) error {
 	if opts.Namespace != "" {
 		currentNamespace = opts.Namespace
 	}
@@ -110,7 +114,10 @@ func list(ctx context.Context, cs *cli.Clients, opts *flags.CliOpts, ioStreams *
 			return err
 		}
 
-		fmt.Fprintf(w, body, repository.GetName(), repoOwner,
+		fmt.Fprintf(w, body,
+			repository.GetName(),
+			ui.ShowLastAge(repository, cw),
+			repoOwner,
 			ui.ShowLastSHA(repository),
 			repository.Spec.EventType)
 


Test patch:
diff --git a/pkg/cmd/repository/list_test.go b/pkg/cmd/repository/list_test.go
index d168039d..ceb0d0ee 100644
--- a/pkg/cmd/repository/list_test.go
+++ b/pkg/cmd/repository/list_test.go
@@ -144,7 +144,8 @@ func TestList(t *testing.T) {
 				PipelineAsCode: stdata.PipelineAsCode,
 			}
 			io, out := newIOStream()
-			if err := list(ctx, cs, tt.args.opts, io, tt.args.currentNamespace,
+			if err := list(ctx, cs, tt.args.opts, io,
+				cw, tt.args.currentNamespace,
 				tt.args.selectors, tt.args.noheaders); (err != nil) != tt.wantErr {
 				t.Errorf("describe() error = %v, wantErr %v", err, tt.wantErr)
 			} else {
diff --git a/pkg/cmd/repository/testdata/TestList-Test_list_repositories.golden b/pkg/cmd/repository/testdata/TestList-Test_list_repositories.golden
index c7996f99..79984539 100644
--- a/pkg/cmd/repository/testdata/TestList-Test_list_repositories.golden
+++ b/pkg/cmd/repository/testdata/TestList-Test_list_repositories.golden
@@ -1,2 +1,2 @@
-NAME    OWNER/REPOSITORY   SHA   EVENT-TYPE     STATUS
-repo1   owner/repo         SHA   pull_request   Success
+NAME    AGE              OWNER/REPOSITORY   SHA   EVENT-TYPE     STATUS
+repo1   15 minutes ago   owner/repo         SHA   pull_request   Success
diff --git a/pkg/cmd/repository/testdata/TestList-Test_list_repositories_all_namespaces.golden b/pkg/cmd/repository/testdata/TestList-Test_list_repositories_all_namespaces.golden
index c5b025d1..cc474d7c 100644
--- a/pkg/cmd/repository/testdata/TestList-Test_list_repositories_all_namespaces.golden
+++ b/pkg/cmd/repository/testdata/TestList-Test_list_repositories_all_namespaces.golden
@@ -1,3 +1,3 @@
-NAME    OWNER/REPOSITORY   SHA   EVENT-TYPE     NAMESPACE    STATUS
-repo1   owner/repo         SHA   pull_request   namespace1   Success
-repo2   owner/repo         SHA   pull_request   namespace2   Success
+NAME    AGE              OWNER/REPOSITORY   SHA   EVENT-TYPE     NAMESPACE    STATUS
+repo1   15 minutes ago   owner/repo         SHA   pull_request   namespace1   Success
+repo2   15 minutes ago   owner/repo         SHA   pull_request   namespace2   Success
diff --git a/pkg/cmd/repository/testdata/TestList-Test_list_repositories_specific_namespaces.golden b/pkg/cmd/repository/testdata/TestList-Test_list_repositories_specific_namespaces.golden
index efa8f652..d69a6353 100644
--- a/pkg/cmd/repository/testdata/TestList-Test_list_repositories_specific_namespaces.golden
+++ b/pkg/cmd/repository/testdata/TestList-Test_list_repositories_specific_namespaces.golden
@@ -1,2 +1,2 @@
-NAME    OWNER/REPOSITORY   SHA   EVENT-TYPE     STATUS
-repo2   owner/repo         SHA   pull_request   Success
+NAME    AGE              OWNER/REPOSITORY   SHA   EVENT-TYPE     STATUS
+repo2   15 minutes ago   owner/repo         SHA   pull_request   Success