Skip to content

feat(tibuild): enhance devbuild_poll command with templated markdown response #256

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

Merged
merged 1 commit into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
31 changes: 28 additions & 3 deletions chatops-lark/pkg/events/handler/devbuild_poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@ package handler

import (
"context"
"encoding/json"
"flag"
"fmt"
"html/template"
"net/url"
"strings"

"github.com/Masterminds/sprig/v3"
"github.com/go-resty/resty/v2"
"gopkg.in/yaml.v3"

_ "embed"
)

//go:embed devbuild_poll.md.tmpl
var devBuildPollResponseTmpl string

type pollParams struct {
buildID string
}
Expand Down Expand Up @@ -61,6 +69,23 @@ func runCommandDevbuildPoll(_ context.Context, args []string) (string, error) {
}
result := resp.Result().(*pollResult)

resultBytes, _ := json.Marshal(result.Status)
return fmt.Sprintf("build status is %s", resultBytes), nil
// Create a new template and add a custom function to format JSON
t := template.Must(template.New("markdown").
Funcs(sprig.FuncMap()).
Funcs(template.FuncMap{"toYaml": func(v any) string {
yamlBytes, err := yaml.Marshal(v)
if err != nil {
return fmt.Sprintf("failed to marshal to YAML: %v", err)
}
return strings.TrimSuffix(string(yamlBytes), "\n")
}}).
Parse(devBuildPollResponseTmpl))

// Execute the template with the result data
var sb strings.Builder
if err := t.Execute(&sb, result); err != nil {
return "", fmt.Errorf("failed to execute template: %v", err)
}

return sb.String(), nil
}
18 changes: 18 additions & 0 deletions chatops-lark/pkg/events/handler/devbuild_poll.md.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
**Build Status: {{.Status.Status}}**

{{- if .Status.PipelineViewURLs }}
**Pipeline View Links:** {{ range $index, $url := .Status.PipelineViewURLs }}[Run {{$index | add 1}}]({{$url}}) {{ end }}
{{- else if .Status.PipelineViewURL }}
**Pipeline View Link:**
[Run]({{.Status.PipelineViewURL}})
{{- end }}

{{- if .Status.BuildReport }}
**Build Report:**
{{ range $key, $value := .Status.BuildReport }}
- **{{ $key }}:**
```yaml
{{ toYaml $value }}
```
{{ end }}
{{- end }}
16 changes: 10 additions & 6 deletions chatops-lark/pkg/events/handler/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,6 @@ func (r *rootHandler) Handle(ctx context.Context, event *larkim.P2MessageReceive
return nil
}

hl.Info().
Str("command", command.Name).
Str("sender", *event.Event.Sender.SenderId.OpenId).
Int("argsCount", len(command.Args)).
Msg("Processing command")

refactionID, err := r.addReaction(*event.Event.Message.MessageId)
if err != nil {
hl.Err(err).Msg("send heartbeat failed")
Expand All @@ -209,21 +203,31 @@ func (r *rootHandler) Handle(ctx context.Context, event *larkim.P2MessageReceive

go func() {
defer r.deleteReaction(*event.Event.Message.MessageId, refactionID)
asyncLog := hl.With().Str("command", command.Name).
Any("args", command.Args).
Str("sender", *event.Event.Sender.SenderId.OpenId).
Logger()

asyncLog.Info().Msg("Processing command")
message, err := r.handleCommand(ctx, command)
if err == nil {
asyncLog.Info().Msg("Command processed successfully")
r.sendResponse(*event.Event.Message.MessageId, StatusSuccess, message)
return
}

// send different level response for the error types.
switch e := err.(type) {
case SkipError:
asyncLog.Info().Msg("Command was skipped")
message = fmt.Sprintf("%s\n---\n**skip:**\n%v", message, e)
r.sendResponse(*event.Event.Message.MessageId, StatusSkip, message)
case InformationError:
asyncLog.Info().Msg("Command was handled but just feedback information")
message = fmt.Sprintf("%s\n---\n**information:**\n%v", message, e)
r.sendResponse(*event.Event.Message.MessageId, StatusInfo, message)
default:
asyncLog.Err(err).Msg("Command processing failed")
message = fmt.Sprintf("%s\n---\n**error:**\n%v", message, err)
r.sendResponse(*event.Event.Message.MessageId, StatusFailure, message)
}
Expand Down
Loading