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:
```diff
image: appleboy/drone-telegram
settings:
token: xxxxxxxxxx
to: telegram_user_id
+ message_file: message_file.tpl
- name: send telegram notification
image: appleboy/drone-telegram
settings:
token: xxxxxxxxxx
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
@ -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

40
main.go
View File

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

View File

@ -2,6 +2,7 @@ package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
@ -59,23 +60,24 @@ type (
// Config for the plugin.
Config struct {
Token string
Debug bool
MatchEmail bool
WebPreview bool
To []string
Message []string
MessageFile string
Photo []string
Document []string
Sticker []string
Audio []string
Voice []string
Location []string
Video []string
Venue []string
Format string
GitHub bool
Token string
Debug bool
MatchEmail bool
WebPreview bool
To []string
Message []string
MessageFile string
TemplateVars string
Photo []string
Document []string
Sticker []string
Audio []string
Voice []string
Location []string
Video []string
Venue []string
Format string
GitHub bool
}
// Plugin values.
@ -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

View File

@ -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"}`,
},
}

View File

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