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:
Daniel M. Lambea 2019-10-29 02:57:00 +00:00 committed by Bo-Yi Wu
parent 866e25b433
commit b7f97df101
6 changed files with 124 additions and 41 deletions

39
DOCS.md
View File

@ -152,11 +152,37 @@ Example configuration with a custom message template:
Example configuration with a custom message template loaded from file: Example configuration with a custom message template loaded from file:
```diff ```diff
image: appleboy/drone-telegram - name: send telegram notification
settings: image: appleboy/drone-telegram
token: xxxxxxxxxx settings:
to: telegram_user_id token: xxxxxxxxxx
+ message_file: message_file.tpl to: telegram_user_id
+ 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 ## Parameter Reference
@ -173,6 +199,9 @@ message
message_file message_file
: overwrite the default message template with the contents of the specified 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 photo
: local file path : local file path

40
main.go
View File

@ -41,6 +41,11 @@ func main() {
Usage: "send telegram message from file", Usage: "send telegram message from file",
EnvVar: "PLUGIN_MESSAGE_FILE,TELEGRAM_MESSAGE_FILE,INPUT_MESSAGE_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{ cli.StringSliceFlag{
Name: "photo", Name: "photo",
Usage: "send photo message", Usage: "send photo message",
@ -287,23 +292,24 @@ func run(c *cli.Context) error {
DeployTo: c.String("deploy.to"), DeployTo: c.String("deploy.to"),
}, },
Config: Config{ Config: Config{
Token: c.String("token"), Token: c.String("token"),
Debug: c.Bool("debug"), Debug: c.Bool("debug"),
MatchEmail: c.Bool("match.email"), MatchEmail: c.Bool("match.email"),
WebPreview: c.Bool("webpage.preview"), WebPreview: c.Bool("webpage.preview"),
To: c.StringSlice("to"), To: c.StringSlice("to"),
Message: c.StringSlice("message"), Message: c.StringSlice("message"),
MessageFile: c.String("message.file"), MessageFile: c.String("message.file"),
Photo: c.StringSlice("photo"), TemplateVars: c.String("template.vars"),
Document: c.StringSlice("document"), Photo: c.StringSlice("photo"),
Sticker: c.StringSlice("sticker"), Document: c.StringSlice("document"),
Audio: c.StringSlice("audio"), Sticker: c.StringSlice("sticker"),
Voice: c.StringSlice("voice"), Audio: c.StringSlice("audio"),
Location: c.StringSlice("location"), Voice: c.StringSlice("voice"),
Video: c.StringSlice("video"), Location: c.StringSlice("location"),
Venue: c.StringSlice("venue"), Video: c.StringSlice("video"),
Format: c.String("format"), Venue: c.StringSlice("venue"),
GitHub: c.Bool("github"), Format: c.String("format"),
GitHub: c.Bool("github"),
}, },
} }

View File

@ -2,6 +2,7 @@ package main
import ( import (
"bufio" "bufio"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@ -59,23 +60,24 @@ type (
// Config for the plugin. // Config for the plugin.
Config struct { Config struct {
Token string Token string
Debug bool Debug bool
MatchEmail bool MatchEmail bool
WebPreview bool WebPreview bool
To []string To []string
Message []string Message []string
MessageFile string MessageFile string
Photo []string TemplateVars string
Document []string Photo []string
Sticker []string Document []string
Audio []string Sticker []string
Voice []string Audio []string
Location []string Voice []string
Video []string Location []string
Venue []string Video []string
Format string Venue []string
GitHub bool Format string
GitHub bool
} }
// Plugin values. // Plugin values.
@ -85,6 +87,7 @@ type (
Commit Commit Commit Commit
Build Build Build Build
Config Config Config Config
Tpl map[string]string
} }
// Location format // Location format
@ -258,9 +261,16 @@ func (p Plugin) Exec() (err error) {
message = p.Message() 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 var bot *tgbotapi.BotAPI
if bot, err = tgbotapi.NewBotAPI(p.Config.Token); err != nil { if bot, err = tgbotapi.NewBotAPI(p.Config.Token); err != nil {
return err return
} }
bot.Debug = p.Config.Debug bot.Debug = p.Config.Debug

View File

@ -347,7 +347,38 @@ func TestMessageFile(t *testing.T) {
Config: Config{ Config: Config{
Token: os.Getenv("TELEGRAM_TOKEN"), Token: os.Getenv("TELEGRAM_TOKEN"),
To: []string{os.Getenv("TELEGRAM_TO")}, 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"}`,
}, },
} }

View File

@ -0,0 +1,7 @@
Sample message template loaded from file.
*Environ:* {{tpl.env}}
*Version:* {{tpl.version}}
Commit msg: {{commit.message}}