Repository: openshift-pipelines/pipelines-as-code
Pull request #353 (2021-11-05 11:02:52)

Add log url in tkn pac describe
* Add an option for kubernetes/tekton dahsboard user to TEKTON_DASHBOARD_URL for at least show the live url.
* Fix getting mixed live running pipelinerun and non live
* Improve testing
* Generally improve the way we show things, when there is only one or multiple runs or no runs.


Patch:
diff --git a/pkg/cmd/tknpac/repository/describe.go b/pkg/cmd/tknpac/repository/describe.go
index 98e616ff..c4cb1cbe 100644
--- a/pkg/cmd/tknpac/repository/describe.go
+++ b/pkg/cmd/tknpac/repository/describe.go
@@ -3,6 +3,7 @@ package repository
 import (
 	"context"
 	"fmt"
+	"os"
 	"strings"
 	"text/tabwriter"
 	"text/template"
@@ -15,6 +16,7 @@ import (
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/cli"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/cli/prompt"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/cmd/tknpac/completion"
+	"github.com/openshift-pipelines/pipelines-as-code/pkg/consoleui"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/formatting"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
 	sortrepostatus "github.com/openshift-pipelines/pipelines-as-code/pkg/sort"
@@ -27,30 +29,34 @@ const (
 	describeTemplate = `{{ $.ColorScheme.Bold "Name" }}:	{{.Repository.Name}}
 {{ $.ColorScheme.Bold "Namespace" }}:	{{.Repository.Namespace}}
 {{ $.ColorScheme.Bold "URL" }}:	{{.Repository.Spec.URL}}
-{{- if eq (len .Repository.Status) 0 }}
+{{- if eq (len .Statuses) 0 }}
+
 {{ $.ColorScheme.Dimmed "No runs has started."}}
 {{- else }}
 {{- $status := (index .Statuses 0) }}
 
-{{ $.ColorScheme.Bold "Last Run:" }} 
-
-Status:	{{ $.ColorScheme.ColorStatus (index $status.Status.Conditions 0).Reason  }}
-PipelineRun:	{{ $.ColorScheme.HyperLink $status.PipelineRunName $status.LogURL }}
-Event:	{{ $status.EventType }}
-Branch:	{{ sanitizeBranch $status.TargetBranch }}
-Commit URL:	{{ $status.SHAURL }}
-Commit Title:	{{ $status.Title }}
-StartTime:	{{ formatTime $status.StartTime $.Clock }}
+{{- if (gt (len .Statuses) 1) }}
+
+{{ $.ColorScheme.Underline "Last Run:" }} 
+{{ end }}
+{{ $.ColorScheme.Bold "Status:" }}	{{ $.ColorScheme.ColorStatus (index $status.Status.Conditions 0).Reason  }}
+{{ $.ColorScheme.Bold "Log:"  }}	{{ $status.LogURL}}
+{{ $.ColorScheme.Bold "PipelineRun:" }}	{{ $.ColorScheme.HyperLink $status.PipelineRunName $status.LogURL }}
+{{ $.ColorScheme.Bold "Event:" }}	{{ $status.EventType }}
+{{ $.ColorScheme.Bold "Branch:" }}	{{ sanitizeBranch $status.TargetBranch }}
+{{ $.ColorScheme.Bold "Commit URL:" }}	{{ $status.SHAURL }}
+{{ $.ColorScheme.Bold "Commit Title:" }}	{{ $status.Title }}
+{{ $.ColorScheme.Bold "StartTime:" }}	{{ formatTime $status.StartTime $.Clock }}
 {{- if $status.CompletionTime }}
-Duration:	{{ formatDuration $status.StartTime $status.CompletionTime }}
+{{ $.ColorScheme.Bold "Duration:" }}	{{ formatDuration $status.StartTime $status.CompletionTime }}
 {{- end }}
-{{- if gt (len .Repository.Status) 1 }}
+{{- if (gt (len .Statuses) 1) }}
 
-{{ $.ColorScheme.Bold "Other Runs:" }}
+{{ $.ColorScheme.Underline "Other Runs:" }}
 
 STATUS	Event	Branch	 SHA	 STARTED TIME	DURATION	PIPELINERUN
 ――――――	―――――	――――――	 ―――	 ――――――――――――	――――――――	―――――――――――
-{{- range $i, $st := (slice .Statuses 1 (len .Repository.Status)) }}
+{{- range $i, $st := (slice .Statuses 1 (len .Statuses)) }}
 {{ formatStatus $st $.ColorScheme $.Clock }}
 {{- end }}
 {{- end }}
@@ -145,6 +151,13 @@ func DescribeCommand(run *params.Run, ioStreams *cli.IOStreams) *cobra.Command {
 			if err != nil {
 				return err
 			}
+
+			// The only way to know the tekton dashboard url is if the user specify it because we are not supposed to have access to the configmap.
+			// so let the user specify a env variable to implicitly set tekton dashboard
+			if os.Getenv("TEKTON_DASHBOARD_URL") != "" {
+				run.Clients.ConsoleUI = &consoleui.TektonDashboard{BaseURL: os.Getenv("TEKTON_DASHBOARD_URL")}
+			}
+
 			return describe(ctx, run, clock, opts, ioStreams, repoName)
 		},
 	}
@@ -226,19 +239,21 @@ func getLivePRAndRepostatus(ctx context.Context, cs *params.Run, repository *v1a
 	}
 
 	for _, pr := range prs.Items {
+		logurl := cs.Clients.ConsoleUI.DetailURL(pr.GetNamespace(), pr.GetName())
 		if pr.Status.Conditions == nil || len(pr.Status.Conditions) == 0 {
-			repositorystatus = convertPrStatusToRepositoryStatus(repositorystatus, pr)
+			repositorystatus = convertPrStatusToRepositoryStatus(repositorystatus, pr, logurl)
 		} else if pr.Status.Conditions[0].Reason == tektonv1beta1.PipelineRunReasonRunning.String() {
-			repositorystatus = convertPrStatusToRepositoryStatus(repositorystatus, pr)
+			repositorystatus = convertPrStatusToRepositoryStatus(repositorystatus, pr, logurl)
 		}
 	}
+
 	return sortrepostatus.RepositorySortRunStatus(repositorystatus)
 }
 
-func convertPrStatusToRepositoryStatus(repositorystatus []v1alpha1.RepositoryRunStatus, pr tektonv1beta1.PipelineRun) []v1alpha1.RepositoryRunStatus {
+func convertPrStatusToRepositoryStatus(repositorystatus []v1alpha1.RepositoryRunStatus, pr tektonv1beta1.PipelineRun, logurl string) []v1alpha1.RepositoryRunStatus {
 	return append(repositorystatus, v1alpha1.RepositoryRunStatus{
 		Status:          pr.Status.Status,
-		LogURL:          new(string),
+		LogURL:          &logurl,
 		PipelineRunName: pr.GetName(),
 		StartTime:       pr.Status.StartTime,
 		SHA:             github.String(pr.GetLabels()["pipelinesascode.tekton.dev/sha"]),


Test patch:
diff --git a/pkg/cmd/tknpac/repository/describe_test.go b/pkg/cmd/tknpac/repository/describe_test.go
index 6b551e01..dec5516a 100644
--- a/pkg/cmd/tknpac/repository/describe_test.go
+++ b/pkg/cmd/tknpac/repository/describe_test.go
@@ -12,6 +12,7 @@ import (
 	"github.com/jonboulle/clockwork"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/cli"
+	"github.com/openshift-pipelines/pipelines-as-code/pkg/consoleui"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/params/clients"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
@@ -54,7 +55,7 @@ func TestDescribe(t *testing.T) {
 		wantErr bool
 	}{
 		{
-			name: "Describe a Pipeline with a Live Run and a Repository Run",
+			name: "live run and repository run",
 			args: args{
 				repoName:         "test-run",
 				currentNamespace: ns,
@@ -64,6 +65,7 @@ func TestDescribe(t *testing.T) {
 						map[string]string{
 							"pipelinesascode.tekton.dev/repository": "test-run",
 							"pipelinesascode.tekton.dev/branch":     "tartanpion",
+							"pipelinesascode.tekton.dev/event-type": "papayolo",
 						}, 30),
 				},
 				statuses: []v1alpha1.RepositoryRunStatus{
@@ -83,13 +85,53 @@ func TestDescribe(t *testing.T) {
 						SHAURL:          github.String("https://anurl.com/commit/SHA"),
 						Title:           github.String("A title"),
 						TargetBranch:    github.String("TargetBranch"),
+						EventType:       github.String("propseryouplaboun"),
 					},
 				},
 			},
 			wantErr: false,
 		},
 		{
-			name: "Describe a Pipeline with a Single Run - optnamespace",
+			name: "one live run",
+			args: args{
+				repoName:         "test-run",
+				currentNamespace: ns,
+				opts:             &cli.PacCliOpts{},
+				pruns: []*tektonv1beta1.PipelineRun{
+					tektontest.MakePRCompletion(cw, "running", ns, running,
+						map[string]string{
+							"pipelinesascode.tekton.dev/repository": "test-run",
+							"pipelinesascode.tekton.dev/branch":     "tartanpion",
+						}, 30),
+				},
+				statuses: []v1alpha1.RepositoryRunStatus{},
+			},
+			wantErr: false,
+		},
+		{
+			name: "multiple live runs",
+			args: args{
+				repoName:         "test-run",
+				currentNamespace: ns,
+				opts:             &cli.PacCliOpts{},
+				pruns: []*tektonv1beta1.PipelineRun{
+					tektontest.MakePRCompletion(cw, "running", ns, running,
+						map[string]string{
+							"pipelinesascode.tekton.dev/repository": "test-run",
+							"pipelinesascode.tekton.dev/branch":     "tartanpion",
+						}, 30),
+					tektontest.MakePRCompletion(cw, "running2", ns, running,
+						map[string]string{
+							"pipelinesascode.tekton.dev/repository": "test-run",
+							"pipelinesascode.tekton.dev/branch":     "vavaroom",
+						}, 30),
+				},
+				statuses: []v1alpha1.RepositoryRunStatus{},
+			},
+			wantErr: false,
+		},
+		{
+			name: "one repository status and optnamespace",
 			args: args{
 				repoName:         "test-run",
 				currentNamespace: "namespace",
@@ -119,7 +161,7 @@ func TestDescribe(t *testing.T) {
 			wantErr: false,
 		},
 		{
-			name: "Describe a Pipeline with a Multiple Run",
+			name: "multiple repo status",
 			args: args{
 				opts:             &cli.PacCliOpts{},
 				repoName:         "test-run",
@@ -202,6 +244,7 @@ func TestDescribe(t *testing.T) {
 				Clients: clients.Clients{
 					PipelineAsCode: stdata.PipelineAsCode,
 					Tekton:         stdata.Pipeline,
+					ConsoleUI:      consoleui.FallBackConsole{},
 				},
 				Info: info.Info{Kube: info.KubeOpts{Namespace: tt.args.currentNamespace}},
 			}
diff --git a/pkg/cmd/tknpac/repository/testdata/TestCommands-Describe.golden b/pkg/cmd/tknpac/repository/testdata/TestCommands-Describe.golden
index 7b08e4c8..17c146b7 100644
--- a/pkg/cmd/tknpac/repository/testdata/TestCommands-Describe.golden
+++ b/pkg/cmd/tknpac/repository/testdata/TestCommands-Describe.golden
@@ -1,10 +1,8 @@
-Name:        repo1
-Namespace:   ns
-URL:         https://anurl.com/repo/owner
-
-Last Run: 
-
+Name:           repo1
+Namespace:      ns
+URL:            https://anurl.com/repo/owner
 Status:         Success
+Log:            https://everywhere.anwywhere
 PipelineRun:    pipelinerun1
 Event:          pull_request
 Branch:         TargetBranch
diff --git a/pkg/cmd/tknpac/repository/testdata/TestDescribe-live_run_and_repository_run.golden b/pkg/cmd/tknpac/repository/testdata/TestDescribe-live_run_and_repository_run.golden
new file mode 100644
index 00000000..d146dadf
--- /dev/null
+++ b/pkg/cmd/tknpac/repository/testdata/TestDescribe-live_run_and_repository_run.golden
@@ -0,0 +1,20 @@
+Name:        test-run
+Namespace:   ns
+URL:         https://anurl.com
+
+Last Run: 
+
+Status:         Running
+Log:            https://giphy.com/search/random-dogs
+PipelineRun:    running
+Event:          papayolo
+Branch:         tartanpion
+Commit URL:     
+Commit Title:   
+StartTime:      -35 minutes ago
+
+Other Runs:
+
+STATUS    Event               Branch          SHA    STARTED TIME    DURATION   PIPELINERUN
+――――――    ―――――               ――――――          ―――    ――――――――――――    ――――――――   ―――――――――――
+Success   propseryouplaboun   TargetBranch   SHA    16 minutes ago   1 minute   pipelinerun1
diff --git a/pkg/cmd/tknpac/repository/testdata/TestDescribe-multiple_live_runs.golden b/pkg/cmd/tknpac/repository/testdata/TestDescribe-multiple_live_runs.golden
new file mode 100644
index 00000000..41d73e05
--- /dev/null
+++ b/pkg/cmd/tknpac/repository/testdata/TestDescribe-multiple_live_runs.golden
@@ -0,0 +1,20 @@
+Name:        test-run
+Namespace:   ns
+URL:         https://anurl.com
+
+Last Run: 
+
+Status:         Running
+Log:            https://giphy.com/search/random-dogs
+PipelineRun:    running
+Event:          
+Branch:         tartanpion
+Commit URL:     
+Commit Title:   
+StartTime:      -35 minutes ago
+
+Other Runs:
+
+STATUS    Event   Branch      SHA    STARTED TIME     DURATION   PIPELINERUN
+――――――    ―――――   ――――――      ―――    ――――――――――――     ――――――――   ―――――――――――
+Running           vavaroom          -35 minutes ago   ---        running2
diff --git a/pkg/cmd/tknpac/repository/testdata/TestDescribe-Describe_a_Pipeline_with_a_Multiple_Run.golden b/pkg/cmd/tknpac/repository/testdata/TestDescribe-multiple_repo_status.golden
similarity index 93%
rename from pkg/cmd/tknpac/repository/testdata/TestDescribe-Describe_a_Pipeline_with_a_Multiple_Run.golden
rename to pkg/cmd/tknpac/repository/testdata/TestDescribe-multiple_repo_status.golden
index 43406bed..db85bd51 100644
--- a/pkg/cmd/tknpac/repository/testdata/TestDescribe-Describe_a_Pipeline_with_a_Multiple_Run.golden
+++ b/pkg/cmd/tknpac/repository/testdata/TestDescribe-multiple_repo_status.golden
@@ -5,6 +5,7 @@ URL:         https://anurl.com
 Last Run: 
 
 Status:         Success
+Log:            https://everywhere.anwywhere
 PipelineRun:    pipelinerun1
 Event:          pull_request
 Branch:         TargetBranch
diff --git a/pkg/cmd/tknpac/repository/testdata/TestDescribe-Describe_a_Pipeline_with_a_Live_Run_and_a_Repository_Run.golden b/pkg/cmd/tknpac/repository/testdata/TestDescribe-one_live_run.golden
similarity index 54%
rename from pkg/cmd/tknpac/repository/testdata/TestDescribe-Describe_a_Pipeline_with_a_Live_Run_and_a_Repository_Run.golden
rename to pkg/cmd/tknpac/repository/testdata/TestDescribe-one_live_run.golden
index 7be98bc9..92571bf5 100644
--- a/pkg/cmd/tknpac/repository/testdata/TestDescribe-Describe_a_Pipeline_with_a_Live_Run_and_a_Repository_Run.golden
+++ b/pkg/cmd/tknpac/repository/testdata/TestDescribe-one_live_run.golden
@@ -1,10 +1,8 @@
-Name:        test-run
-Namespace:   ns
-URL:         https://anurl.com
-
-Last Run: 
-
+Name:           test-run
+Namespace:      ns
+URL:            https://anurl.com
 Status:         Running
+Log:            https://giphy.com/search/random-dogs
 PipelineRun:    running
 Event:          
 Branch:         tartanpion
diff --git a/pkg/cmd/tknpac/repository/testdata/TestDescribe-Describe_a_Pipeline_with_a_Single_Run_-_optnamespace.golden b/pkg/cmd/tknpac/repository/testdata/TestDescribe-one_repository_status_and_optnamespace.golden
similarity index 63%
rename from pkg/cmd/tknpac/repository/testdata/TestDescribe-Describe_a_Pipeline_with_a_Single_Run_-_optnamespace.golden
rename to pkg/cmd/tknpac/repository/testdata/TestDescribe-one_repository_status_and_optnamespace.golden
index 988184f4..676b392f 100644
--- a/pkg/cmd/tknpac/repository/testdata/TestDescribe-Describe_a_Pipeline_with_a_Single_Run_-_optnamespace.golden
+++ b/pkg/cmd/tknpac/repository/testdata/TestDescribe-one_repository_status_and_optnamespace.golden
@@ -1,10 +1,8 @@
-Name:        test-run
-Namespace:   optnamespace
-URL:         https://anurl.com
-
-Last Run: 
-
+Name:           test-run
+Namespace:      optnamespace
+URL:            https://anurl.com
 Status:         Success
+Log:            https://everywhere.anwywhere
 PipelineRun:    pipelinerun1
 Event:          <nil>
 Branch:         TargetBranch