Skip to content

Commit bd8ef57

Browse files
committed
fix: Fixed Comments on Bitbucket Data Center & Cloud
when PipelineRun finishes PAC creates final status as comment on Bitbucket Data Center and Cloud, but as Bitbucket (both) doesn't support HTML comment they were not being rendered as HTML rather they were only raw HTML string. in this PR markdown is used as formatting template to prettify comment on Bitbucket. https://issues.redhat.com/browse/SRVKP-7331 Signed-off-by: Zaki Shaikh <[email protected]>
1 parent 8ce32f3 commit bd8ef57

File tree

10 files changed

+84
-14
lines changed

10 files changed

+84
-14
lines changed

pkg/formatting/starting.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,22 @@ import (
77
)
88

99
//go:embed templates/starting.go.tmpl
10-
var StartingPipelineRunText string
10+
var StartingPipelineRunHTML string
11+
12+
//go:embed templates/starting.markdown.go.tmpl
13+
var StartingPipelineRunMarkdown string
1114

1215
//go:embed templates/queuing.go.tmpl
13-
var QueuingPipelineRunText string
16+
var QueuingPipelineRunHTML string
17+
18+
//go:embed templates/queuing.markdown.go.tmpl
19+
var QueuingPipelineRunMarkdown string
1420

1521
//go:embed templates/pipelinerunstatus.tmpl
16-
var PipelineRunStatusText string
22+
var PipelineRunStatusHTML string
23+
24+
//go:embed templates/pipelinerunstatus_markdown.tmpl
25+
var PipelineRunStatusMarkDown string
1726

1827
type MessageTemplate struct {
1928
PipelineRunName string
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
- **Namespace**: [{{ .Mt.Namespace }}]({{ .Mt.NamespaceURL }})
2+
- **PipelineRun**: [{{ .Mt.PipelineRunName }}]({{ .Mt.ConsoleURL }})
3+
4+
---
5+
6+
### Task Statuses:
7+
8+
| **Status** | **Name** | **Duration** |
9+
|------------|----------|--------------|
10+
{{ .Mt.TaskStatus }}
11+
12+
{{- if not (eq .Mt.FailureSnippet "")}}
13+
---
14+
15+
### Failure snippet:
16+
{{ .Mt.FailureSnippet }}
17+
{{- end }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PipelineRun **{{ .Mt.PipelineRunName }}** has been queued in namespace **{{ .Mt.Namespace }}**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Starting PipelineRun **{{ .Mt.PipelineRunName }}** in namespace **{{ .Mt.Namespace }}**
2+
3+
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:
4+
5+
`{{ .Mt.TknBinary }} pr logs -n {{ .Mt.Namespace }} {{ .Mt.PipelineRunName }} -f`

pkg/pipelineascode/pipelineascode.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ func (p *PacRun) startPR(ctx context.Context, match matcher.Match) (*tektonv1.Pi
226226
TknBinary: settings.TknBinaryName,
227227
TknBinaryURL: settings.TknBinaryURL,
228228
}
229-
msg, err := mt.MakeTemplate(formatting.StartingPipelineRunText)
229+
230+
template := provider.GetCommentTemplate(pr.GetAnnotations()[keys.GitProvider], provider.StartingPipelineType)
231+
msg, err := mt.MakeTemplate(template)
230232
if err != nil {
231233
return nil, fmt.Errorf("cannot create message template: %w", err)
232234
}
@@ -243,7 +245,8 @@ func (p *PacRun) startPR(ctx context.Context, match matcher.Match) (*tektonv1.Pi
243245
// if pipelineRun is in pending state then report status as queued
244246
if pr.Spec.Status == tektonv1.PipelineRunSpecStatusPending {
245247
status.Status = queuedStatus
246-
if status.Text, err = mt.MakeTemplate(formatting.QueuingPipelineRunText); err != nil {
248+
template := provider.GetCommentTemplate(pr.GetAnnotations()[keys.GitProvider], provider.QueueingPipelineType)
249+
if status.Text, err = mt.MakeTemplate(template); err != nil {
247250
return nil, fmt.Errorf("cannot create message template: %w", err)
248251
}
249252
}

pkg/provider/bitbucketcloud/bitbucket.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ type Provider struct {
2929
Token, APIURL *string
3030
Username *string
3131
provenance string
32+
eventEmitter *events.EventEmitter
33+
repo *v1alpha1.Repository
3234
}
3335

3436
// CheckPolicyAllowing TODO: Implement ME.
@@ -45,9 +47,7 @@ func (v *Provider) SetPacInfo(pacInfo *info.PacOpts) {
4547
v.pacInfo = pacInfo
4648
}
4749

48-
const taskStatusTemplate = `| **Status** | **Duration** | **Name** |
49-
| --- | --- | --- |
50-
{{range $taskrun := .TaskRunList }}|{{ formatCondition $taskrun.PipelineRunTaskRunStatus.Status.Conditions }}|{{ formatDuration $taskrun.PipelineRunTaskRunStatus.Status.StartTime $taskrun.PipelineRunTaskRunStatus.Status.CompletionTime }}|{{ $taskrun.ConsoleLogURL }}|
50+
const taskStatusTemplate = `{{range $taskrun := .TaskRunList }} | **{{ formatCondition $taskrun.PipelineRunTaskRunStatus.Status.Conditions }}** | {{ $taskrun.ConsoleLogURL }} | *{{ formatDuration $taskrun.PipelineRunTaskRunStatus.Status.StartTime $taskrun.PipelineRunTaskRunStatus.Status.CompletionTime }}* |
5151
{{ end }}`
5252

5353
func (v *Provider) Validate(_ context.Context, _ *params.Run, _ *info.Event) error {
@@ -110,7 +110,10 @@ func (v *Provider) CreateStatus(_ context.Context, event *info.Event, statusopts
110110

111111
_, err := v.Client.Repositories.Commits.CreateCommitStatus(cmo, cso)
112112
if err != nil {
113-
return err
113+
// Only emit an event to notify the user that something went wrong with the commit status API,
114+
// and proceed with creating the comment (if applicable).
115+
v.eventEmitter.EmitMessage(v.repo, zap.ErrorLevel, "FailedToSetCommitStatus",
116+
"cannot set status with the Bitbucket Cloud token because of: "+err.Error())
114117
}
115118

116119
eventType := triggertype.IsPullRequestType(event.EventType)
@@ -176,7 +179,7 @@ func (v *Provider) GetFileInsideRepo(_ context.Context, event *info.Event, path,
176179
return v.getBlob(event, revision, path)
177180
}
178181

179-
func (v *Provider) SetClient(_ context.Context, run *params.Run, event *info.Event, _ *v1alpha1.Repository, _ *events.EventEmitter) error {
182+
func (v *Provider) SetClient(_ context.Context, run *params.Run, event *info.Event, repo *v1alpha1.Repository, eventEmitter *events.EventEmitter) error {
180183
if event.Provider.Token == "" {
181184
return fmt.Errorf("no git_provider.secret has been set in the repo crd")
182185
}
@@ -187,6 +190,8 @@ func (v *Provider) SetClient(_ context.Context, run *params.Run, event *info.Eve
187190
v.Token = &event.Provider.Token
188191
v.Username = &event.Provider.User
189192
v.run = run
193+
v.eventEmitter = eventEmitter
194+
v.repo = repo
190195
return nil
191196
}
192197

pkg/provider/bitbucketdatacenter/bitbucketdatacenter.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ import (
2323
"go.uber.org/zap"
2424
)
2525

26-
const taskStatusTemplate = `
27-
{{range $taskrun := .TaskRunList }}* **{{ formatCondition $taskrun.PipelineRunTaskRunStatus.Status.Conditions }}** {{ $taskrun.ConsoleLogURL }} *{{ formatDuration $taskrun.Status.StartTime $taskrun.Status.CompletionTime }}*
26+
const taskStatusTemplate = `{{range $taskrun := .TaskRunList }}| **{{ formatCondition $taskrun.PipelineRunTaskRunStatus.Status.Conditions }}** | {{ $taskrun.ConsoleLogURL }} | *{{ formatDuration $taskrun.Status.StartTime $taskrun.Status.CompletionTime }}* |
2827
{{ end }}`
2928
const apiResponseLimit = 100
3029

pkg/provider/provider.go

+29
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package provider
22

33
import (
44
"fmt"
5+
"github.com/openshift-pipelines/pipelines-as-code/pkg/formatting"
56
"net/url"
67
"regexp"
78
"strings"
@@ -28,6 +29,34 @@ const (
2829
GitHubApp = "GitHubApp"
2930
)
3031

32+
type CommentType int
33+
34+
const (
35+
StartingPipelineType CommentType = iota
36+
PipelineRunStatusType
37+
QueueingPipelineType
38+
)
39+
40+
var htmlCommentTemplates = map[CommentType]string{
41+
StartingPipelineType: formatting.StartingPipelineRunHTML,
42+
PipelineRunStatusType: formatting.PipelineRunStatusHTML,
43+
QueueingPipelineType: formatting.QueuingPipelineRunHTML,
44+
}
45+
46+
var markdownCommentTemplates = map[CommentType]string{
47+
StartingPipelineType: formatting.StartingPipelineRunMarkdown,
48+
PipelineRunStatusType: formatting.PipelineRunStatusHTML,
49+
QueueingPipelineType: formatting.QueuingPipelineRunMarkdown,
50+
}
51+
52+
func GetCommentTemplate(gitProvider string, commentType CommentType) string {
53+
templates := htmlCommentTemplates
54+
if strings.Contains(gitProvider, "bitbucket") {
55+
templates = markdownCommentTemplates
56+
}
57+
return templates[commentType]
58+
}
59+
3160
func Valid(value string, validValues []string) bool {
3261
for _, v := range validValues {
3362
if v == value {

pkg/reconciler/reconciler.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ func (r *Reconciler) updatePipelineRunToInProgress(ctx context.Context, logger *
276276
TknBinary: settings.TknBinaryName,
277277
TknBinaryURL: settings.TknBinaryURL,
278278
}
279-
msg, err := mt.MakeTemplate(formatting.StartingPipelineRunText)
279+
template := provider.GetCommentTemplate(pr.GetAnnotations()[keys.GitProvider], provider.StartingPipelineType)
280+
msg, err := mt.MakeTemplate(template)
280281
if err != nil {
281282
return fmt.Errorf("cannot create message template: %w", err)
282283
}

pkg/reconciler/status.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ func (r *Reconciler) postFinalStatus(ctx context.Context, logger *zap.SugaredLog
138138
}
139139
}
140140
var tmplStatusText string
141-
if tmplStatusText, err = mt.MakeTemplate(formatting.PipelineRunStatusText); err != nil {
141+
template := provider.GetCommentTemplate(pr.GetAnnotations()[apipac.GitProvider], provider.PipelineRunStatusType)
142+
if tmplStatusText, err = mt.MakeTemplate(template); err != nil {
142143
return nil, fmt.Errorf("cannot create message template: %w", err)
143144
}
144145

0 commit comments

Comments
 (0)