From b7f97df1019cfcdd98786091b32ab6c647618332 Mon Sep 17 00:00:00 2001 From: "Daniel M. Lambea" Date: Tue, 29 Oct 2019 02:57:00 +0000 Subject: [PATCH] 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 --- DOCS.md | 39 ++++++++++++++++++++--- main.go | 40 ++++++++++++++---------- plugin.go | 46 +++++++++++++++++----------- plugin_test.go | 33 +++++++++++++++++++- tests/{.test.message => message.txt} | 0 tests/message_template.txt | 7 +++++ 6 files changed, 124 insertions(+), 41 deletions(-) rename tests/{.test.message => message.txt} (100%) create mode 100644 tests/message_template.txt diff --git a/DOCS.md b/DOCS.md index c0c6afa..1b97fe6 100644 --- a/DOCS.md +++ b/DOCS.md @@ -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 diff --git a/main.go b/main.go index 3be25c2..ca09b9e 100644 --- a/main.go +++ b/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", @@ -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"), }, } diff --git a/plugin.go b/plugin.go index ad966af..f43977e 100644 --- a/plugin.go +++ b/plugin.go @@ -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 diff --git a/plugin_test.go b/plugin_test.go index 23aeb3c..e474c93 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -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"}`, }, } diff --git a/tests/.test.message b/tests/message.txt similarity index 100% rename from tests/.test.message rename to tests/message.txt diff --git a/tests/message_template.txt b/tests/message_template.txt new file mode 100644 index 0000000..798b078 --- /dev/null +++ b/tests/message_template.txt @@ -0,0 +1,7 @@ +Sample message template loaded from file. + +*Environ:* {{tpl.env}} +*Version:* {{tpl.version}} + +Commit msg: {{commit.message}} +