Skip to content

Commit

Permalink
feat(publisher): pretty the notify lark message (#200)
Browse files Browse the repository at this point in the history
Signed-off-by: wuhuizuo <[email protected]>

Signed-off-by: wuhuizuo <[email protected]>
  • Loading branch information
wuhuizuo authored Nov 12, 2024
1 parent 430c66d commit 0c2a438
Show file tree
Hide file tree
Showing 7 changed files with 309 additions and 21 deletions.
10 changes: 10 additions & 0 deletions publisher/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/PingCAP-QE/ee-apps/publisher
go 1.23

require (
github.com/Masterminds/sprig/v3 v3.3.0
github.com/PingCAP-QE/ee-apps/dl v0.0.0-20241104081507-4ebce35328e3
github.com/cloudevents/sdk-go/v2 v2.15.2
github.com/go-redis/redis/v8 v8.11.5
Expand All @@ -17,6 +18,9 @@ require (
)

require (
dario.cat/mergo v1.0.1 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.3.0 // indirect
github.com/aws/smithy-go v1.22.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
Expand All @@ -26,23 +30,29 @@ require (
github.com/go-chi/chi/v5 v5.1.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/huandu/xstrings v1.5.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/ks3sdklib/aws-sdk-go v1.3.0
github.com/manveru/faker v0.0.0-20171103152722-9fbc68a78c4d // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/spf13/cast v1.7.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
go.opentelemetry.io/otel v1.31.0 // indirect
go.opentelemetry.io/otel/trace v1.31.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sync v0.8.0 // indirect
Expand Down
22 changes: 22 additions & 0 deletions publisher/go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver/v3 v3.3.0 h1:B8LGeaivUe71a5qox1ICM/JLl0NqZSW5CHyL+hmvYS0=
github.com/Masterminds/semver/v3 v3.3.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
github.com/Masterminds/sprig/v3 v3.3.0 h1:mQh0Yrg1XPo6vjYXgtf5OtijNAKJRNcTdOOGZe3tPhs=
github.com/Masterminds/sprig/v3 v3.3.0/go.mod h1:Zy1iXRYNqNLUolqCpL4uhk6SHUMAOSCzdgBfDb35Lz0=
github.com/PingCAP-QE/ee-apps/dl v0.0.0-20241104081507-4ebce35328e3 h1:lB5LmXyMZObiuqS7Q0nFMva3Y/LKuOFX+3MHSWWJqLo=
github.com/PingCAP-QE/ee-apps/dl v0.0.0-20241104081507-4ebce35328e3/go.mod h1:4Q9Ns8T4rU7dlZng4fLe9qwSWlB4rjFD7Egpip1kI38=
github.com/aws/smithy-go v1.22.0 h1:uunKnWlcoL3zO7q+gG2Pk53joueEOsnNB28QdMsmiMM=
Expand All @@ -15,6 +23,8 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/r
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dimfeld/httppath v0.0.0-20170720192232-ee938bf73598 h1:MGKhKyiYrvMDZsmLR/+RGffQSXwEkXgfLSA08qDn9AI=
github.com/dimfeld/httppath v0.0.0-20170720192232-ee938bf73598/go.mod h1:0FpDmbrt36utu8jEmeU05dPC9AB5tsLYVVi+ZHfyuwI=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
Expand All @@ -33,6 +43,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/huandu/xstrings v1.5.0 h1:2ag3IFq9ZDANvthTwTiqSSZLjDc+BedvHPAp5tJy2TI=
github.com/huandu/xstrings v1.5.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
Expand All @@ -54,6 +66,10 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down Expand Up @@ -83,6 +99,10 @@ github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/segmentio/kafka-go v0.4.47 h1:IqziR4pA3vrZq7YdRxaT3w1/5fvIH5qpCwstUanQQB0=
github.com/segmentio/kafka-go v0.4.47/go.mod h1:HjF6XbOKh0Pjlkr5GVZxt6CsjjwnmhVOfURM5KMd8qg=
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
github.com/spf13/cast v1.7.0 h1:ntdiHjuueXFgm5nzDRdOS4yfT43P5Fnud6DH50rz/7w=
github.com/spf13/cast v1.7.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand Down Expand Up @@ -126,6 +146,8 @@ goa.design/plugins/v3 v3.19.1/go.mod h1:IjmiAqbpTowpBXDyNboLaGdOZMlclb99dGE4f8qa
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
Expand Down
46 changes: 46 additions & 0 deletions publisher/pkg/impl/lark.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package impl

import (
"bytes"
"encoding/json"
"text/template"

"github.com/Masterminds/sprig/v3"
"gopkg.in/yaml.v3"

_ "embed"
)

//go:embed lark_templates/tiup-publish-failure-notify.yaml.tmpl
var larkTemplateBytes string

func newLarkCardWithGoTemplate(infos any) (string, error) {
tmpl, err := template.New("lark").Funcs(sprig.FuncMap()).Parse(larkTemplateBytes)
if err != nil {
return "", err
}

tmplResult := new(bytes.Buffer)
if err := tmpl.Execute(tmplResult, infos); err != nil {
return "", err
}

values := make(map[string]interface{})
if err := yaml.Unmarshal(tmplResult.Bytes(), &values); err != nil {
return "", err
}

jsonBytes, err := json.Marshal(values)
if err != nil {
return "", err
}

return string(jsonBytes), nil
}

type failureNotifyInfo struct {
Title string
RerunCommands string
FailedMessage string
Params [][2]string // key-value pairs.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
msg_type: interactive
card:
config:
wide_screen_mode: true
elements:
{{- with .FailedMessage }}
- tag: markdown
content: |-
**Failed summary:**
```text
{{ indent 8 . }}
```
- tag: hr
{{- end }}
{{- with .Params }}
- tag: markdown
content: |-
**Params:**
{{- range . }}
- **{{ index . 0 }}:** {{ index . 1 }}
{{- end }}
{{- end }}

{{- with .RerunCommands }}
- tag: hr
- tag: markdown
content: |-
🔧 **Rerun:**

```BASH
{{ . }}
```
{{- end }}

header:
template: red # blue | wathet | turquoise | green | yellow | orange | red | carmine | violet | purple | indigo | grey
title:
content: {{ .Title }}
tag: plain_text
54 changes: 33 additions & 21 deletions publisher/pkg/impl/tiup_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package impl
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"os"
Expand All @@ -20,9 +19,10 @@ type tiupWorker struct {
logger zerolog.Logger
redisClient redis.Cmdable
options struct {
LarkWebhookURL string
MirrorURL string
NightlyInterval time.Duration
LarkWebhookURL string
MirrorURL string
PublicServiceURL string
NightlyInterval time.Duration
}
}

Expand All @@ -41,6 +41,11 @@ func NewTiupWorker(logger *zerolog.Logger, redisClient redis.Cmdable, options ma
handler.options.NightlyInterval = nigthlyInterval
handler.options.MirrorURL = options["mirror_url"]
handler.options.LarkWebhookURL = options["lark_webhook_url"]
if options["public_service_url"] != "" {
handler.options.PublicServiceURL = options["public_service_url"]
} else {
handler.options.PublicServiceURL = "http://publisher-<env>-mirror.<namespace>.svc"
}

return &handler, nil
}
Expand All @@ -67,7 +72,7 @@ func (p *tiupWorker) Handle(event cloudevents.Event) cloudevents.Result {
p.redisClient.SetXX(context.Background(), event.ID(), PublishStateSuccess, redis.KeepTTL)
case cloudevents.IsNACK(result):
p.redisClient.SetXX(context.Background(), event.ID(), PublishStateFailed, redis.KeepTTL)
p.notifyLark(&data.Publish, result)
p.notifyLark(data, result)
default:
p.redisClient.SetXX(context.Background(), event.ID(), PublishStateCanceled, redis.KeepTTL)
}
Expand Down Expand Up @@ -136,34 +141,41 @@ func (p *tiupWorker) handle(data *PublishRequest) cloudevents.Result {
return cloudevents.ResultACK
}

func (p *tiupWorker) notifyLark(publishInfo *PublishInfo, err error) {
func (p *tiupWorker) notifyLark(req *PublishRequest, err error) {
if p.options.LarkWebhookURL == "" {
return
}

message := fmt.Sprintf("Failed to publish %s-%s @%s/%s platform to mirror %s: %v",
publishInfo.Name,
publishInfo.Version,
publishInfo.OS,
publishInfo.Arch,
p.options.MirrorURL,
err)

payload := map[string]interface{}{
"msg_type": "text",
"content": map[string]string{
"text": message,
rerunCmd := fmt.Sprintf(`go run %s --url %s tiup request-to-publish --body '{"artifact_url": "%s"}'`,
"github.com/PingCAP-QE/ee-apps/publisher/cmd/publisher-cli@main",
p.options.PublicServiceURL,
req.From.String(),
)

info := failureNotifyInfo{
Title: "TiUP Publish Failed",
FailedMessage: err.Error(),
RerunCommands: rerunCmd,
Params: [][2]string{
{"package", req.Publish.Name},
{"version", req.Publish.Version},
{"os", req.Publish.OS},
{"arch", req.Publish.Arch},
{"to-mirror", p.options.MirrorURL},
{"from", req.From.String()},
},
}

jsonPayload, err := json.Marshal(payload)
jsonPayload, err := newLarkCardWithGoTemplate(info)
if err != nil {
p.logger.Err(err).Msg("failed to marshal JSON payload")
p.logger.Err(err).Msg("failed to gen message payload")
return
}

resp, err := http.Post(p.options.LarkWebhookURL, "application/json", bytes.NewBuffer(jsonPayload))
resp, err := http.Post(p.options.LarkWebhookURL, "application/json", bytes.NewBufferString(jsonPayload))
if err != nil {
p.logger.Err(err).Msg("failed to send notification to Lark")
return
}
defer resp.Body.Close()

Expand Down
Loading

0 comments on commit 0c2a438

Please sign in to comment.