Repository: openshift-pipelines/pipelines-as-code
Pull request #258 (2021-10-05 09:47:32)

Introduce VCS Config
Each VCS can now provide their own config to query


Patch:
diff --git a/pkg/params/info/vcsconfig.go b/pkg/params/info/vcsconfig.go
new file mode 100644
index 00000000..811203f9
--- /dev/null
+++ b/pkg/params/info/vcsconfig.go
@@ -0,0 +1,6 @@
+package info
+
+type VCSConfig struct {
+	TaskStatusTMPL string
+	APIURL         string
+}
diff --git a/pkg/pipelineascode/pipelineascode.go b/pkg/pipelineascode/pipelineascode.go
index ddc216f1..fcad5cff 100644
--- a/pkg/pipelineascode/pipelineascode.go
+++ b/pkg/pipelineascode/pipelineascode.go
@@ -11,9 +11,9 @@ import (
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/kubeinteraction"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/matcher"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
+	"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/resolve"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/webvcs"
-	"github.com/openshift-pipelines/pipelines-as-code/pkg/webvcs/github"
 	tektonv1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 )
@@ -62,7 +62,7 @@ func Run(ctx context.Context, cs *params.Run, vcsintf webvcs.Interface, k8int ku
 	}
 
 	if repo.Spec.WebvcsSecret != nil {
-		err := secretFromRepository(ctx, cs, k8int, repo)
+		err := secretFromRepository(ctx, cs, k8int, vcsintf.GetConfig(), repo)
 		if err != nil {
 			return err
 		}
@@ -271,14 +271,13 @@ func fmtDuration(d time.Duration) string {
 	return fmt.Sprintf("%02d", m)
 }
 
-func secretFromRepository(ctx context.Context, cs *params.Run, k8int kubeinteraction.Interface, repo *apipac.Repository) error {
+func secretFromRepository(ctx context.Context, cs *params.Run, k8int kubeinteraction.Interface, config *info.VCSConfig, repo *apipac.Repository) error {
 	var err error
 
 	if repo.Spec.WebvcsAPIURL == "" {
-		if cs.Info.Pac.VCSType == "github" {
-			repo.Spec.WebvcsAPIURL = github.PublicURL
-		}
+		repo.Spec.WebvcsAPIURL = config.APIURL
 	}
+
 	cs.Info.Pac.VCSUser = repo.Spec.WebvcsAPIUser
 	cs.Info.Pac.VCSToken, err = k8int.GetSecret(
 		ctx,
@@ -293,7 +292,6 @@ func secretFromRepository(ctx context.Context, cs *params.Run, k8int kubeinterac
 		return err
 	}
 	cs.Info.Pac.VCSInfoFromRepo = true
-	cs.Info.Pac.VCSAPIURL = repo.Spec.WebvcsAPIURL
 
 	cs.Clients.Log.Infof("Using token from secret %s in key %s", repo.Spec.WebvcsSecret.Name, repo.Spec.WebvcsSecret.Key)
 	return nil
diff --git a/pkg/pipelineascode/status.go b/pkg/pipelineascode/status.go
index 47de9cf4..3818675f 100644
--- a/pkg/pipelineascode/status.go
+++ b/pkg/pipelineascode/status.go
@@ -18,21 +18,6 @@ import (
 
 const checkStatustmpl = `{{.taskStatus}}`
 
-const taskStatustmpl = `
-<table>
-  <tr><th>Status</th><th>Duration</th><th>Name</th></tr>
-
-{{- range $taskrun := .TaskRunList }}
-<tr>
-<td>{{ formatCondition $taskrun.Status.Conditions }}</td>
-<td>{{ formatDuration $taskrun.Status.StartTime $taskrun.Status.CompletionTime }}</td><td>
-
-{{ $taskrun.ConsoleLogURL }}
-
-</td></tr>
-{{- end }}
-</table>`
-
 const naStr = "---"
 
 type tkr struct {
@@ -104,7 +89,7 @@ func ConditionEmoji(c knative1.Conditions) string {
 	return status
 }
 
-func statusOfAllTaskListForCheckRun(pr *tektonv1beta1.PipelineRun, consoleURL string) (string, error) {
+func statusOfAllTaskListForCheckRun(pr *tektonv1beta1.PipelineRun, consoleURL, statusTemplate string) (string, error) {
 	var trl taskrunList
 	var outputBuffer bytes.Buffer
 
@@ -124,7 +109,7 @@ func statusOfAllTaskListForCheckRun(pr *tektonv1beta1.PipelineRun, consoleURL st
 		TaskRunList: trl,
 	}
 
-	t := template.Must(template.New("Task Status").Funcs(funcMap).Parse(taskStatustmpl))
+	t := template.Must(template.New("Task Status").Funcs(funcMap).Parse(statusTemplate))
 	if err := t.Execute(&outputBuffer, data); err != nil {
 		fmt.Fprintf(&outputBuffer, "failed to execute template: ")
 		return "", err
@@ -155,7 +140,7 @@ func postFinalStatus(ctx context.Context, cs *params.Run, k8int kubeinteraction.
 		consoleURL = "https://giphy.com/search/cat-reading"
 	}
 
-	taskStatus, err := statusOfAllTaskListForCheckRun(pr, consoleURL)
+	taskStatus, err := statusOfAllTaskListForCheckRun(pr, consoleURL, vcsintf.GetConfig().TaskStatusTMPL)
 	if err != nil {
 		return pr, err
 	}
@@ -170,10 +155,11 @@ func postFinalStatus(ctx context.Context, cs *params.Run, k8int kubeinteraction.
 		return pr, err
 	}
 
+	output := outputBuffer.String()
 	err = createStatus(ctx, vcsintf, cs, webvcs.StatusOpts{
 		Status:     "completed",
 		Conclusion: pipelineRunStatus(pr),
-		Text:       outputBuffer.String(),
+		Text:       output,
 		DetailsURL: consoleURL,
 	}, false)
 
diff --git a/pkg/webvcs/github/github.go b/pkg/webvcs/github/github.go
index 009bba78..97757816 100644
--- a/pkg/webvcs/github/github.go
+++ b/pkg/webvcs/github/github.go
@@ -12,13 +12,20 @@ import (
 	"golang.org/x/oauth2"
 )
 
-const PublicURL = "https://api.github.com/"
+const apiPublicURL = "https://api.github.com/"
 
 type VCS struct {
 	Client        *github.Client
 	Token, APIURL *string
 }
 
+func (v *VCS) GetConfig() *info.VCSConfig {
+	return &info.VCSConfig{
+		TaskStatusTMPL: taskStatusTemplate,
+		APIURL:         apiPublicURL,
+	}
+}
+
 func (v *VCS) SetClient(ctx context.Context, info info.PacOpts) {
 	ts := oauth2.StaticTokenSource(
 		&oauth2.Token{AccessToken: info.VCSToken},
@@ -32,7 +39,7 @@ func (v *VCS) SetClient(ctx context.Context, info info.PacOpts) {
 			apiURL = "https://" + apiURL
 		}
 	}
-	if apiURL != "" && apiURL != PublicURL {
+	if apiURL != "" && apiURL != apiPublicURL {
 		client, _ = github.NewEnterpriseClient(apiURL, apiURL, tc)
 	} else {
 		client = github.NewClient(tc)
diff --git a/pkg/webvcs/github/status.go b/pkg/webvcs/github/status.go
index 36247144..4793d14e 100644
--- a/pkg/webvcs/github/status.go
+++ b/pkg/webvcs/github/status.go
@@ -10,6 +10,21 @@ import (
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/webvcs"
 )
 
+const taskStatusTemplate = `
+<table>
+  <tr><th>Status</th><th>Duration</th><th>Name</th></tr>
+
+{{- range $taskrun := .TaskRunList }}
+<tr>
+<td>{{ formatCondition $taskrun.Status.Conditions }}</td>
+<td>{{ formatDuration $taskrun.Status.StartTime $taskrun.Status.CompletionTime }}</td><td>
+
+{{ $taskrun.ConsoleLogURL }}
+
+</td></tr>
+{{- end }}
+</table>`
+
 // createCheckRunStatus create a status via the checkRun API, which is only
 // available with Github apps tokens.
 func (v *VCS) createCheckRunStatus(ctx context.Context, runevent *info.Event, pacopts info.PacOpts, status webvcs.StatusOpts) error {
diff --git a/pkg/webvcs/interface.go b/pkg/webvcs/interface.go
index f9a0dd4c..e8398e0a 100644
--- a/pkg/webvcs/interface.go
+++ b/pkg/webvcs/interface.go
@@ -24,4 +24,5 @@ type Interface interface {
 	GetFileInsideRepo(context.Context, *info.Event, string, string) (string, error) // ctx, event, path, branch
 	SetClient(context.Context, info.PacOpts)
 	GetCommitInfo(context.Context, *info.Event) error
+	GetConfig() *info.VCSConfig
 }


Test patch:
diff --git a/pkg/test/webvcs/testwebvcs.go b/pkg/test/webvcs/testwebvcs.go
index 49d4cbd6..10089773 100644
--- a/pkg/test/webvcs/testwebvcs.go
+++ b/pkg/test/webvcs/testwebvcs.go
@@ -21,6 +21,10 @@ func (v *TestWebVCSImp) ParsePayload(ctx context.Context, run *params.Run, s str
 	return v.Event, nil
 }
 
+func (v *TestWebVCSImp) GetConfig() *info.VCSConfig {
+	return &info.VCSConfig{}
+}
+
 func (v *TestWebVCSImp) GetCommitInfo(ctx context.Context, runevent *info.Event) error {
 	return nil
 }