From e17c338fe427197326f9fe5c4b80381259eb6c09 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Tue, 24 Nov 2015 15:35:01 -0800 Subject: [PATCH] use new webhook package --- DOCS.md | 13 ++++--------- main.go | 18 +++++++++--------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/DOCS.md b/DOCS.md index 091990a..fff72d8 100644 --- a/DOCS.md +++ b/DOCS.md @@ -67,7 +67,7 @@ The following is an example Webhook payload (whitespace added): In some cases you may want to submit a custom payload in the body of your hook. For the use case we expose the following additional parameters: -* `template` - Go template to create a custom payload body. See [docs](https://golang.org/pkg/text/template/) +* `template` - Handlebars template to create a custom payload body. See [docs](http://handlebarsjs.com/) * `content_type` - HTTP request content type Example configuration that generate a custom Yaml payload: @@ -80,9 +80,9 @@ notify: - https://your.other.webhook/... content_type: application/yaml template: > - repo: {{.Repo.FullName}} - build: {{.Build.Number}} - commit: {{.Build.Commit}} + repo: {{repo.full_name}} + build: {{build.number}} + commit: {{build.commit}} ``` ## Basic Authentication @@ -106,11 +106,6 @@ notify: password: $$PASSWORD urls: - https://tower.example.com/... - content_type: application/yaml - template: > - repo: {{.Repo.FullName}} - build: {{.Build.Number}} - commit: {{.Build.Commit}} ``` ## Debugging Webhooks diff --git a/main.go b/main.go index 070a3aa..72deb45 100644 --- a/main.go +++ b/main.go @@ -8,20 +8,22 @@ import ( "net/http" "net/url" "os" - "text/template" "github.com/drone/drone-go/drone" "github.com/drone/drone-go/plugin" + "github.com/drone/drone-go/template" ) func main() { // plugin settings + var sys = drone.System{} var repo = drone.Repo{} var build = drone.Build{} var vargs = Webhook{} // set plugin parameters + plugin.Param("system", &sys) plugin.Param("repo", &repo) plugin.Param("build", &build) plugin.Param("vargs", &vargs) @@ -52,18 +54,16 @@ func main() { var buf bytes.Buffer if len(vargs.Template) == 0 { if err := json.NewEncoder(&buf).Encode(&data); err != nil { - fmt.Printf("Error encoding content template. %s\n", err) + fmt.Printf("Error encoding json payload. %s\n", err) os.Exit(1) } } else { - - t, err := template.New("_").Parse(vargs.Template) + err := template.Write(&buf, vargs.Template, &drone.Payload{ + Build: &build, + Repo: &repo, + System: &sys, + }) if err != nil { - fmt.Printf("Error parsing content template. %s\n", err) - os.Exit(1) - } - - if err := t.Execute(&buf, &data); err != nil { fmt.Printf("Error executing content template. %s\n", err) os.Exit(1) }