-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(publisher): pretty the notify lark message
Signed-off-by: wuhuizuo <[email protected]>
- Loading branch information
Showing
5 changed files
with
277 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} |
39 changes: 39 additions & 0 deletions
39
publisher/pkg/impl/lark_templates/tiup-publish-failure-notify.yaml.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package impl | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-redis/redis/v8" | ||
"github.com/rs/zerolog" | ||
) | ||
|
||
func Test_tiupWorker_notifyLark(t *testing.T) { | ||
t.Skipf("It is manually test case") | ||
const testWebhookURL = "https://open.feishu.cn/open-apis/bot/v2/hook/<please-replace-it>" | ||
|
||
type fields struct { | ||
logger zerolog.Logger | ||
redisClient redis.Cmdable | ||
options struct { | ||
LarkWebhookURL string | ||
MirrorURL string | ||
PublicServiceURL string | ||
NightlyInterval time.Duration | ||
} | ||
} | ||
type args struct { | ||
req *PublishRequest | ||
err error | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
}{ | ||
{ | ||
name: "Empty webhook URL", | ||
fields: fields{ | ||
logger: zerolog.New(nil), | ||
options: struct { | ||
LarkWebhookURL string | ||
MirrorURL string | ||
PublicServiceURL string | ||
NightlyInterval time.Duration | ||
}{ | ||
LarkWebhookURL: "", | ||
}, | ||
}, | ||
args: args{ | ||
req: &PublishRequest{ | ||
Publish: PublishInfo{ | ||
Name: "test-package", | ||
Version: "v1.0.0", | ||
}, | ||
}, | ||
err: errors.New("test error"), | ||
}, | ||
}, | ||
{ | ||
name: "Valid notification with all fields", | ||
fields: fields{ | ||
logger: zerolog.New(nil), | ||
options: struct { | ||
LarkWebhookURL string | ||
MirrorURL string | ||
PublicServiceURL string | ||
NightlyInterval time.Duration | ||
}{ | ||
LarkWebhookURL: testWebhookURL, | ||
MirrorURL: "https://test-mirror.com", | ||
PublicServiceURL: "https://test-service.com", | ||
}, | ||
}, | ||
args: args{ | ||
req: &PublishRequest{ | ||
From: From{ | ||
Type: FromTypeOci, | ||
Oci: &FromOci{ | ||
Repo: "test-repo", | ||
Tag: "test-tag", | ||
}, | ||
}, | ||
Publish: PublishInfo{ | ||
Name: "test-package", | ||
Version: "v1.0.0", | ||
OS: "linux", | ||
Arch: "amd64", | ||
}, | ||
}, | ||
err: errors.New("publish failed"), | ||
}, | ||
}, | ||
{ | ||
name: "Invalid webhook URL", | ||
fields: fields{ | ||
logger: zerolog.New(nil), | ||
options: struct { | ||
LarkWebhookURL string | ||
MirrorURL string | ||
PublicServiceURL string | ||
NightlyInterval time.Duration | ||
}{ | ||
LarkWebhookURL: "invalid-url", | ||
}, | ||
}, | ||
args: args{ | ||
req: &PublishRequest{ | ||
Publish: PublishInfo{ | ||
Name: "test-package", | ||
Version: "v1.0.0", | ||
}, | ||
}, | ||
err: errors.New("test error"), | ||
}, | ||
}, | ||
{ | ||
name: "Missing publish info", | ||
fields: fields{ | ||
logger: zerolog.New(nil), | ||
options: struct { | ||
LarkWebhookURL string | ||
MirrorURL string | ||
PublicServiceURL string | ||
NightlyInterval time.Duration | ||
}{ | ||
LarkWebhookURL: testWebhookURL, | ||
}, | ||
}, | ||
args: args{ | ||
req: &PublishRequest{}, | ||
err: errors.New("missing publish info"), | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
p := &tiupWorker{ | ||
logger: tt.fields.logger, | ||
redisClient: tt.fields.redisClient, | ||
options: tt.fields.options, | ||
} | ||
p.notifyLark(tt.args.req, tt.args.err) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters