Add support for generic templates by using custom extra vars (#84)
* Fix DOCS.md example configuration with custom message from file * Move .test.message to a better named file * Add support for additional, custom extra vars to be used in the message template * Add cmdline/envvars to support the extra vars * Enhance DOCS.md with example configuration using generic template file with custom vars * Add testcase for custom template vars
This commit is contained in:
parent
866e25b433
commit
b7f97df101
29
DOCS.md
29
DOCS.md
@ -152,6 +152,7 @@ Example configuration with a custom message template:
|
||||
Example configuration with a custom message template loaded from file:
|
||||
|
||||
```diff
|
||||
- name: send telegram notification
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
token: xxxxxxxxxx
|
||||
@ -159,6 +160,31 @@ Example configuration with a custom message template loaded from file:
|
||||
+ message_file: message_file.tpl
|
||||
```
|
||||
|
||||
Example configuration with a generic message template loaded from file, with additional extra vars:
|
||||
|
||||
```diff
|
||||
- name: send telegram notification
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
token: xxxxxxxxxx
|
||||
to: telegram_user_id
|
||||
+ message_file: message_file.tpl
|
||||
+ template_vars:
|
||||
+ env: testing
|
||||
+ app: MyApp
|
||||
```
|
||||
|
||||
Where `message_file.tpl` is:
|
||||
```
|
||||
Build finished for *{{tpl.app}}* - *{{tpl.env}}*
|
||||
|
||||
{{#success build.status}}
|
||||
build {{build.number}} succeeded. Good job.
|
||||
{{else}}
|
||||
build {{build.number}} failed. Fix me please.
|
||||
{{/success}}
|
||||
```
|
||||
|
||||
## Parameter Reference
|
||||
|
||||
token
|
||||
@ -173,6 +199,9 @@ message
|
||||
message_file
|
||||
: overwrite the default message template with the contents of the specified file
|
||||
|
||||
template_vars
|
||||
: define additional template vars. Example: `var1: hello` can be used within the template as `tpl.var1`
|
||||
|
||||
photo
|
||||
: local file path
|
||||
|
||||
|
6
main.go
6
main.go
@ -41,6 +41,11 @@ func main() {
|
||||
Usage: "send telegram message from file",
|
||||
EnvVar: "PLUGIN_MESSAGE_FILE,TELEGRAM_MESSAGE_FILE,INPUT_MESSAGE_FILE",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "template.vars",
|
||||
Usage: "additional template vars to be used in message, as JSON string",
|
||||
EnvVar: "PLUGIN_TEMPLATE_VARS,TELEGRAM_TEMPLATE_VARS,INPUT_TEMPLATE_VARS",
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
Name: "photo",
|
||||
Usage: "send photo message",
|
||||
@ -294,6 +299,7 @@ func run(c *cli.Context) error {
|
||||
To: c.StringSlice("to"),
|
||||
Message: c.StringSlice("message"),
|
||||
MessageFile: c.String("message.file"),
|
||||
TemplateVars: c.String("template.vars"),
|
||||
Photo: c.StringSlice("photo"),
|
||||
Document: c.StringSlice("document"),
|
||||
Sticker: c.StringSlice("sticker"),
|
||||
|
12
plugin.go
12
plugin.go
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
@ -66,6 +67,7 @@ type (
|
||||
To []string
|
||||
Message []string
|
||||
MessageFile string
|
||||
TemplateVars string
|
||||
Photo []string
|
||||
Document []string
|
||||
Sticker []string
|
||||
@ -85,6 +87,7 @@ type (
|
||||
Commit Commit
|
||||
Build Build
|
||||
Config Config
|
||||
Tpl map[string]string
|
||||
}
|
||||
|
||||
// Location format
|
||||
@ -258,9 +261,16 @@ func (p Plugin) Exec() (err error) {
|
||||
message = p.Message()
|
||||
}
|
||||
|
||||
if p.Config.TemplateVars != "" {
|
||||
p.Tpl = make(map[string]string)
|
||||
if err = json.Unmarshal([]byte(p.Config.TemplateVars), &p.Tpl); err != nil {
|
||||
return fmt.Errorf("unable to unmarshall template vars from JSON string '%s': %v", p.Config.TemplateVars, err)
|
||||
}
|
||||
}
|
||||
|
||||
var bot *tgbotapi.BotAPI
|
||||
if bot, err = tgbotapi.NewBotAPI(p.Config.Token); err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
|
||||
bot.Debug = p.Config.Debug
|
||||
|
@ -347,7 +347,38 @@ func TestMessageFile(t *testing.T) {
|
||||
Config: Config{
|
||||
Token: os.Getenv("TELEGRAM_TOKEN"),
|
||||
To: []string{os.Getenv("TELEGRAM_TO")},
|
||||
MessageFile: "tests/.test.message",
|
||||
MessageFile: "tests/message.txt",
|
||||
},
|
||||
}
|
||||
|
||||
err := plugin.Exec()
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestTemplateVars(t *testing.T) {
|
||||
plugin := Plugin{
|
||||
Repo: Repo{
|
||||
Name: "go-hello",
|
||||
Namespace: "appleboy",
|
||||
},
|
||||
Commit: Commit{
|
||||
Sha: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2",
|
||||
Author: "Bo-Yi Wu",
|
||||
Branch: "master",
|
||||
Message: "This is a test commit msg",
|
||||
},
|
||||
Build: Build{
|
||||
Number: 101,
|
||||
Status: "success",
|
||||
Link: "https://github.com/appleboy/go-hello",
|
||||
},
|
||||
|
||||
Config: Config{
|
||||
Token: os.Getenv("TELEGRAM_TOKEN"),
|
||||
To: []string{os.Getenv("TELEGRAM_TO")},
|
||||
Format: "markdown",
|
||||
MessageFile: "tests/message_template.txt",
|
||||
TemplateVars: `{"env":"testing","version":"1.2.0-SNAPSHOT"}`,
|
||||
},
|
||||
}
|
||||
|
||||
|
7
tests/message_template.txt
Normal file
7
tests/message_template.txt
Normal file
@ -0,0 +1,7 @@
|
||||
Sample message template loaded from file.
|
||||
|
||||
*Environ:* {{tpl.env}}
|
||||
*Version:* {{tpl.version}}
|
||||
|
||||
Commit msg: {{commit.message}}
|
||||
|
Loading…
Reference in New Issue
Block a user