Repository: appleboy/drone-telegram
Pull request #105 (2021-07-12 01:53:39)

chore: remove trim \n from message
related #100 

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>


Patch:
diff --git a/DOCS.md b/DOCS.md
index 6265de8..76805ba 100644
--- a/DOCS.md
+++ b/DOCS.md
@@ -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:
diff --git a/main.go b/main.go
index 3c5e1d8..455e68a 100644
--- a/main.go
+++ b/main.go
@@ -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{
diff --git a/plugin.go b/plugin.go
index 8f1c5d2..19c71a3 100644
--- a/plugin.go
+++ b/plugin.go
@@ -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
 			}


Test patch:
diff --git a/plugin_test.go b/plugin_test.go
index 19d34df..56207fd 100644
--- a/plugin_test.go
+++ b/plugin_test.go
@@ -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) {
diff --git a/tests/message_html.txt b/tests/message_html.txt
new file mode 100644
index 0000000..e33e77e
--- /dev/null
+++ b/tests/message_html.txt
@@ -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>