Skip to content

Commit b009fdc

Browse files
authored
feat(tibuild): enhance devbuild_poll command with templated markdown response (#256)
1 parent 12540cb commit b009fdc

File tree

3 files changed

+56
-9
lines changed

3 files changed

+56
-9
lines changed

chatops-lark/pkg/events/handler/devbuild_poll.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@ package handler
22

33
import (
44
"context"
5-
"encoding/json"
65
"flag"
76
"fmt"
7+
"html/template"
88
"net/url"
9+
"strings"
910

11+
"github.com/Masterminds/sprig/v3"
1012
"github.com/go-resty/resty/v2"
13+
"gopkg.in/yaml.v3"
14+
15+
_ "embed"
1116
)
1217

18+
//go:embed devbuild_poll.md.tmpl
19+
var devBuildPollResponseTmpl string
20+
1321
type pollParams struct {
1422
buildID string
1523
}
@@ -61,6 +69,23 @@ func runCommandDevbuildPoll(_ context.Context, args []string) (string, error) {
6169
}
6270
result := resp.Result().(*pollResult)
6371

64-
resultBytes, _ := json.Marshal(result.Status)
65-
return fmt.Sprintf("build status is %s", resultBytes), nil
72+
// Create a new template and add a custom function to format JSON
73+
t := template.Must(template.New("markdown").
74+
Funcs(sprig.FuncMap()).
75+
Funcs(template.FuncMap{"toYaml": func(v any) string {
76+
yamlBytes, err := yaml.Marshal(v)
77+
if err != nil {
78+
return fmt.Sprintf("failed to marshal to YAML: %v", err)
79+
}
80+
return strings.TrimSuffix(string(yamlBytes), "\n")
81+
}}).
82+
Parse(devBuildPollResponseTmpl))
83+
84+
// Execute the template with the result data
85+
var sb strings.Builder
86+
if err := t.Execute(&sb, result); err != nil {
87+
return "", fmt.Errorf("failed to execute template: %v", err)
88+
}
89+
90+
return sb.String(), nil
6691
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
**Build Status: {{.Status.Status}}**
2+
3+
{{- if .Status.PipelineViewURLs }}
4+
**Pipeline View Links:** {{ range $index, $url := .Status.PipelineViewURLs }}[Run {{$index | add 1}}]({{$url}}) {{ end }}
5+
{{- else if .Status.PipelineViewURL }}
6+
**Pipeline View Link:**
7+
[Run]({{.Status.PipelineViewURL}})
8+
{{- end }}
9+
10+
{{- if .Status.BuildReport }}
11+
**Build Report:**
12+
{{ range $key, $value := .Status.BuildReport }}
13+
- **{{ $key }}:**
14+
```yaml
15+
{{ toYaml $value }}
16+
```
17+
{{ end }}
18+
{{- end }}

chatops-lark/pkg/events/handler/root.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,6 @@ func (r *rootHandler) Handle(ctx context.Context, event *larkim.P2MessageReceive
178178
return nil
179179
}
180180

181-
hl.Info().
182-
Str("command", command.Name).
183-
Str("sender", *event.Event.Sender.SenderId.OpenId).
184-
Int("argsCount", len(command.Args)).
185-
Msg("Processing command")
186-
187181
refactionID, err := r.addReaction(*event.Event.Message.MessageId)
188182
if err != nil {
189183
hl.Err(err).Msg("send heartbeat failed")
@@ -209,21 +203,31 @@ func (r *rootHandler) Handle(ctx context.Context, event *larkim.P2MessageReceive
209203

210204
go func() {
211205
defer r.deleteReaction(*event.Event.Message.MessageId, refactionID)
206+
asyncLog := hl.With().Str("command", command.Name).
207+
Any("args", command.Args).
208+
Str("sender", *event.Event.Sender.SenderId.OpenId).
209+
Logger()
210+
211+
asyncLog.Info().Msg("Processing command")
212212
message, err := r.handleCommand(ctx, command)
213213
if err == nil {
214+
asyncLog.Info().Msg("Command processed successfully")
214215
r.sendResponse(*event.Event.Message.MessageId, StatusSuccess, message)
215216
return
216217
}
217218

218219
// send different level response for the error types.
219220
switch e := err.(type) {
220221
case SkipError:
222+
asyncLog.Info().Msg("Command was skipped")
221223
message = fmt.Sprintf("%s\n---\n**skip:**\n%v", message, e)
222224
r.sendResponse(*event.Event.Message.MessageId, StatusSkip, message)
223225
case InformationError:
226+
asyncLog.Info().Msg("Command was handled but just feedback information")
224227
message = fmt.Sprintf("%s\n---\n**information:**\n%v", message, e)
225228
r.sendResponse(*event.Event.Message.MessageId, StatusInfo, message)
226229
default:
230+
asyncLog.Err(err).Msg("Command processing failed")
227231
message = fmt.Sprintf("%s\n---\n**error:**\n%v", message, err)
228232
r.sendResponse(*event.Event.Message.MessageId, StatusFailure, message)
229233
}

0 commit comments

Comments
 (0)