chore: remove trim \n from message (#105)

This commit is contained in:
Bo-Yi Wu 2021-07-12 15:17:02 +08:00 committed by GitHub
parent 2dfbc98fdd
commit ab44454733
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 8 deletions

View File

@ -122,7 +122,7 @@ Example configuration with video message:
+ - tests/video2.mp4
```
Example configuration with message format:
Example configuration with message format (`Markdown` or `HTML`), default as `Markdown`:
```diff
- name: send telegram notification
@ -130,7 +130,7 @@ Example configuration with message format:
settings:
token: xxxxxxxxxx
to: telegram_user_id
+ format: markdown
+ format: Markdown
```
Example configuration with a custom message template:

View File

@ -122,7 +122,7 @@ func main() {
cli.StringFlag{
Name: "format",
Value: formatMarkdown,
Usage: "telegram message format",
Usage: "telegram message format (Markdown or HTML)",
EnvVar: "PLUGIN_FORMAT,FORMAT,INPUT_FORMAT",
},
cli.StringFlag{

View File

@ -20,7 +20,8 @@ import (
)
const (
formatMarkdown = "markdown"
formatMarkdown = "Markdown"
formatHTML = "HTML"
)
type (
@ -259,6 +260,10 @@ func parseTo(to []string, authorEmail string, matchEmail bool) []int64 {
return ids
}
func templateMessage(t string, plugin Plugin) (string, error) {
return template.RenderTrim(t, plugin)
}
// Exec executes the plugin.
func (p Plugin) Exec() (err error) {
if len(p.Config.Token) == 0 || len(p.Config.To) == 0 {
@ -356,7 +361,7 @@ func (p Plugin) Exec() (err error) {
// send message.
for _, user := range ids {
for _, value := range message {
txt, err := template.RenderTrim(value, p)
txt, err := templateMessage(value, p)
if err != nil {
return err
}

View File

@ -353,7 +353,7 @@ func TestHTMLMessage(t *testing.T) {
Sha: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2",
Author: "Bo-Yi Wu",
Branch: "master",
Message: "Freakin' macOS isn't fully case-sensitive..",
Message: "test",
},
Build: Build{
Number: 101,
@ -364,11 +364,20 @@ func TestHTMLMessage(t *testing.T) {
Config: Config{
Token: os.Getenv("TELEGRAM_TOKEN"),
To: []string{os.Getenv("TELEGRAM_TO")},
Message: `
Test HTML Format
<a href='https://google.com'>Google .com 1</a>
<a href='https://google.com'>Google .com 2</a>
<a href='https://google.com'>Google .com 3</a>
`,
Format: formatHTML,
},
}
err := plugin.Exec()
assert.Nil(t, err)
assert.Nil(t, plugin.Exec())
plugin.Config.MessageFile = "tests/message_html.txt"
assert.Nil(t, plugin.Exec())
}
func TestMessageFile(t *testing.T) {

5
tests/message_html.txt Normal file
View File

@ -0,0 +1,5 @@
Test HTML Format from file
<a href='https://google.com'>Google .com 1</a>
<a href='https://google.com'>Google .com 2</a>
<a href='https://google.com'>Google .com 3</a>
<a href='https://google.com'>Google .com 4</a>