Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make status message as templates #1455

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions pkg/formatting/starting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package formatting

import (
"bytes"
_ "embed"
"text/template"
)

//go:embed templates/starting.go.tmpl
var StartingPipelineRunText string

//go:embed templates/queuing.go.tmpl
var QueuingPipelineRunText string

type MessageTemplate struct {
PipelineRunName string
Namespace string
ConsoleName string
ConsoleURL string
TknBinary string
TknBinaryURL string
}

func (mt MessageTemplate) MakeTemplate(msg string) (string, error) {
outputBuffer := bytes.Buffer{}
t := template.Must(template.New("Message").Parse(msg))
data := struct{ Mt MessageTemplate }{Mt: mt}
if err := t.Execute(&outputBuffer, data); err != nil {
return "", err
}
return outputBuffer.String(), nil
}
49 changes: 49 additions & 0 deletions pkg/formatting/starting_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package formatting

import (
"testing"
)

func TestMessageTemplate_MakeTemplate(t *testing.T) {
mt := MessageTemplate{
PipelineRunName: "test-pipeline",
Namespace: "test-namespace",
ConsoleName: "test-console",
ConsoleURL: "https://test-console-url.com",
TknBinary: "test-tkn",
TknBinaryURL: "https://test-tkn-url.com",
}

tests := []struct {
name string
mt MessageTemplate
msg string
want string
wantErr bool
}{
{
name: "Test MakeTemplate",
mt: mt,
msg: "Starting Pipelinerun {{.Mt.PipelineRunName}} in namespace {{.Mt.Namespace}}",
want: "Starting Pipelinerun test-pipeline in namespace test-namespace",
},
{
name: "Error MakeTemplate",
mt: mt,
msg: "Starting Pipelinerun {{.Mt.PipelineRunName}} in namespace {{.FOOOBAR }}",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.mt.MakeTemplate(tt.msg)
if (err != nil) != tt.wantErr {
t.Errorf("MessageTemplate.MakeTemplate() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("MessageTemplate.MakeTemplate() = %v, want %v", got, tt.want)
}
})
}
}
1 change: 1 addition & 0 deletions pkg/formatting/templates/queuing.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PipelineRun <b>{{ .Mt.PipelineRunName }}</b> has been queued in namespace <b>{{ .Mt.Namespace }}</b><br><br>
5 changes: 5 additions & 0 deletions pkg/formatting/templates/starting.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Starting Pipelinerun <b>{{ .Mt.PipelineRunName }}</b> in namespace<b> {{ .Mt.Namespace }}</b><br>
You can monitor the execution using the [{{ .Mt.ConsoleName }}]({{ .Mt.ConsoleURL }}) PipelineRun viewer or through the command line by
using the [{{ .Mt.TknBinary }}]({{ .Mt.TknBinaryURL }}) CLI with the following command:
<br>
<code>{{ .Mt.TknBinary }} pr logs -n {{ .Mt.Namespace }} {{ .Mt.PipelineRunName }} -f</code>
7 changes: 1 addition & 6 deletions pkg/params/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ import (
)

const (
PACConfigmapName = "pipelines-as-code"
StartingPipelineRunText = `Starting Pipelinerun <b>%s</b> in namespace
<b>%s</b><br><br>You can monitor the execution using the [%s](%s) PipelineRun viewer or through the command line by using the following command:
<br><code>%s pr logs -n %s %s -f</code>`
QueuingPipelineRunText = `PipelineRun <b>%s</b> has been queued in namespace
<b>%s</b><br><br>`
PACConfigmapName = "pipelines-as-code"
)

type Run struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/params/settings/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const (

var (
TknBinaryName = `tkn`
TknBinaryURL = `https://tekton.dev/docs/cli/#installation`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we would need to set this to the opc url in downstream @piyush-garg

hubCatalogNameRegex = regexp.MustCompile(`^catalog-(\d+)-`)
)

Expand Down
32 changes: 21 additions & 11 deletions pkg/pipelineascode/pipelineascode.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ import (
"fmt"
"sync"

tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"go.uber.org/zap"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/openshift-pipelines/pipelines-as-code/pkg/action"
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys"
"github.com/openshift-pipelines/pipelines-as-code/pkg/customparams"
"github.com/openshift-pipelines/pipelines-as-code/pkg/events"
"github.com/openshift-pipelines/pipelines-as-code/pkg/formatting"
"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"
Expand All @@ -17,9 +22,6 @@ import (
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/settings"
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider"
"github.com/openshift-pipelines/pipelines-as-code/pkg/secrets"
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"go.uber.org/zap"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
Expand Down Expand Up @@ -168,14 +170,20 @@ func (p *PacRun) startPR(ctx context.Context, match matcher.Match) (*tektonv1.Pi
// Create status with the log url
p.logger.Infof("pipelinerun %s has been created in namespace %s for SHA: %s Target Branch: %s",
pr.GetName(), match.Repo.GetNamespace(), p.event.SHA, p.event.BaseBranch)

consoleURL := p.run.Clients.ConsoleUI.DetailURL(pr)
// Create status with the log url
msg := fmt.Sprintf(params.StartingPipelineRunText,
pr.GetName(), match.Repo.GetNamespace(),
p.run.Clients.ConsoleUI.GetName(), consoleURL,
settings.TknBinaryName,
pr.GetNamespace(),
pr.GetName())
mt := formatting.MessageTemplate{
PipelineRunName: pr.GetName(),
Namespace: match.Repo.GetNamespace(),
ConsoleName: p.run.Clients.ConsoleUI.GetName(),
ConsoleURL: consoleURL,
TknBinary: settings.TknBinaryName,
TknBinaryURL: settings.TknBinaryURL,
}
msg, err := mt.MakeTemplate(formatting.StartingPipelineRunText)
if err != nil {
return nil, fmt.Errorf("cannot create message template: %w", err)
}
status := provider.StatusOpts{
Status: "in_progress",
Conclusion: "pending",
Expand All @@ -189,7 +197,9 @@ func (p *PacRun) startPR(ctx context.Context, match matcher.Match) (*tektonv1.Pi
// if pipelineRun is in pending state then report status as queued
if pr.Spec.Status == tektonv1.PipelineRunSpecStatusPending {
status.Status = "queued"
status.Text = fmt.Sprintf(params.QueuingPipelineRunText, pr.GetName(), match.Repo.GetNamespace())
if status.Text, err = mt.MakeTemplate(formatting.QueuingPipelineRunText); err != nil {
return nil, fmt.Errorf("cannot create message template: %w", err)
}
}

if err := p.vcx.CreateStatus(ctx, p.run.Clients.Tekton, p.event, p.run.Info.Pac, status); err != nil {
Expand Down
20 changes: 14 additions & 6 deletions pkg/reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
"github.com/openshift-pipelines/pipelines-as-code/pkg/customparams"
"github.com/openshift-pipelines/pipelines-as-code/pkg/events"
"github.com/openshift-pipelines/pipelines-as-code/pkg/formatting"
pipelinesascode "github.com/openshift-pipelines/pipelines-as-code/pkg/generated/listers/pipelinesascode/v1alpha1"
"github.com/openshift-pipelines/pipelines-as-code/pkg/kubeinteraction"
"github.com/openshift-pipelines/pipelines-as-code/pkg/metrics"
Expand Down Expand Up @@ -196,12 +197,19 @@ func (r *Reconciler) updatePipelineRunToInProgress(ctx context.Context, logger *
}

consoleURL := r.run.Clients.ConsoleUI.DetailURL(pr)
msg := fmt.Sprintf(params.StartingPipelineRunText,
pr.GetName(), repo.GetNamespace(),
r.run.Clients.ConsoleUI.GetName(), consoleURL,
settings.TknBinaryName,
pr.GetNamespace(),
pr.GetName())

mt := formatting.MessageTemplate{
PipelineRunName: pr.GetName(),
Namespace: repo.GetNamespace(),
ConsoleName: r.run.Clients.ConsoleUI.GetName(),
ConsoleURL: consoleURL,
TknBinary: settings.TknBinaryName,
TknBinaryURL: settings.TknBinaryURL,
}
msg, err := mt.MakeTemplate(formatting.StartingPipelineRunText)
if err != nil {
return fmt.Errorf("cannot create message template: %w", err)
}
status := provider.StatusOpts{
Status: "in_progress",
Conclusion: "pending",
Expand Down