This commit is contained in:
Joachim Hill-Grannec 2017-09-01 10:49:41 -04:00
parent dbc5015833
commit cfb46f2f1e
2 changed files with 48 additions and 48 deletions

49
main.go
View File

@ -2,15 +2,16 @@ package main
import ( import (
"fmt" "fmt"
"os"
"log"
"github.com/urfave/cli" "github.com/urfave/cli"
"log"
"os"
) )
const ( const (
respFormat = "Webhook %d\n URL: %s\n RESPONSE STATUS: %s\n RESPONSE BODY: %s\n" respFormat = "Webhook %d\n URL: %s\n RESPONSE STATUS: %s\n RESPONSE BODY: %s\n"
debugRespFormat = "Webhook %d\n URL: %s\n METHOD: %s\n HEADERS: %s\n REQUEST BODY: %s\n RESPONSE STATUS: %s\n RESPONSE BODY: %s\n" debugRespFormat = "Webhook %d\n URL: %s\n METHOD: %s\n HEADERS: %s\n REQUEST BODY: %s\n RESPONSE STATUS: %s\n RESPONSE BODY: %s\n"
) )
var build string var build string
func main() { func main() {
@ -27,13 +28,13 @@ func main() {
Value: "POST", Value: "POST",
}, },
cli.StringFlag{ cli.StringFlag{
Name: "username", Name: "username",
Usage: "Username for basic auth", Usage: "Username for basic auth",
EnvVar: "PLUGIN_USERNAME,WEBHOOK_USERNAME", EnvVar: "PLUGIN_USERNAME,WEBHOOK_USERNAME",
}, },
cli.StringFlag{ cli.StringFlag{
Name: "password", Name: "password",
Usage: "Password for basic auth", Usage: "Password for basic auth",
EnvVar: "PLUGIN_PASSWORD,WEBHOOK_PASSWORD", EnvVar: "PLUGIN_PASSWORD,WEBHOOK_PASSWORD",
}, },
cli.StringFlag{ cli.StringFlag{
@ -43,28 +44,28 @@ func main() {
Value: "application/json", Value: "application/json",
}, },
cli.StringFlag{ cli.StringFlag{
Name: "template", Name: "template",
Usage: "Custom template for webhook", Usage: "Custom template for webhook",
EnvVar: "PLUGIN_TEMPLATE", EnvVar: "PLUGIN_TEMPLATE",
}, },
cli.StringSliceFlag{ cli.StringSliceFlag{
Name: "headers", Name: "headers",
Usage: "Custom headers key map", Usage: "Custom headers key map",
EnvVar: "PLUGIN_HEADERS", EnvVar: "PLUGIN_HEADERS",
}, },
cli.StringSliceFlag{ cli.StringSliceFlag{
Name: "urls", Name: "urls",
Usage: "List of urls to perform the action on", Usage: "List of urls to perform the action on",
EnvVar: "PLUGIN_URLS", EnvVar: "PLUGIN_URLS",
}, },
cli.BoolFlag{ cli.BoolFlag{
Name: "debug", Name: "debug",
Usage: "For debug information", Usage: "For debug information",
EnvVar: "PLUGIN_DEBUG", EnvVar: "PLUGIN_DEBUG",
}, },
cli.BoolFlag{ cli.BoolFlag{
Name: "skip-verify", Name: "skip-verify",
Usage: "Skip ssl verification", Usage: "Skip ssl verification",
EnvVar: "PLUGIN_SKIP_VERIFY", EnvVar: "PLUGIN_SKIP_VERIFY",
}, },
cli.StringFlag{ cli.StringFlag{
@ -182,15 +183,15 @@ func run(c *cli.Context) error {
Started: c.Int64("job.started"), Started: c.Int64("job.started"),
}, },
Config: Config{ Config: Config{
Method: c.String("method"), Method: c.String("method"),
Username: c.String("username"), Username: c.String("username"),
Password: c.String("password"), Password: c.String("password"),
ContentType: c.String("content-type"), ContentType: c.String("content-type"),
Template: c.String("template"), Template: c.String("template"),
Headers: c.StringSlice("headers"), Headers: c.StringSlice("headers"),
URLs: c.StringSlice("urls"), URLs: c.StringSlice("urls"),
Debug: c.Bool("debug"), Debug: c.Bool("debug"),
SkipVerify: c.Bool("skip-verify"), SkipVerify: c.Bool("skip-verify"),
}, },
} }
return plugin.Exec() return plugin.Exec()

View File

@ -1,16 +1,17 @@
package main package main
import ( import (
"fmt"
"os"
"io/ioutil"
"bytes" "bytes"
"net/url"
"crypto/tls" "crypto/tls"
"net/http"
"encoding/json" "encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings" "strings"
) )
type ( type (
Repo struct { Repo struct {
Owner string `json:"owner"` Owner string `json:"owner"`
@ -20,7 +21,7 @@ type (
Build struct { Build struct {
Tag string `json:"tag"` Tag string `json:"tag"`
Event string `json:"event"` Event string `json:"event"`
Number int `json:"number"` Number int `json:"number"`
Commit string `json:"commit"` Commit string `json:"commit"`
Ref string `json:"ref"` Ref string `json:"ref"`
Branch string `json:"branch"` Branch string `json:"branch"`
@ -28,20 +29,20 @@ type (
Message string `json:"message"` Message string `json:"message"`
Status string `json:"status"` Status string `json:"status"`
Link string `json:"link"` Link string `json:"link"`
Started int64 `json:"started"` Started int64 `json:"started"`
Created int64 `json:"created"` Created int64 `json:"created"`
} }
Config struct { Config struct {
Method string Method string
Username string Username string
Password string Password string
ContentType string ContentType string
Template string Template string
Headers []string Headers []string
URLs []string URLs []string
Debug bool Debug bool
SkipVerify bool SkipVerify bool
} }
Job struct { Job struct {
@ -63,7 +64,7 @@ func (p Plugin) Exec() error {
if p.Config.Template == "" { if p.Config.Template == "" {
data := struct { data := struct {
Repo Repo `json:"repo"` Repo Repo `json:"repo"`
Build Build `json:"build"` Build Build `json:"build"`
}{p.Repo, p.Build} }{p.Repo, p.Build}
@ -73,11 +74,11 @@ func (p Plugin) Exec() error {
} }
b = buf.Bytes() b = buf.Bytes()
} else { } else {
txt, err := RenderTrim(p.Config.Template, p) txt, err := RenderTrim(p.Config.Template, p)
if err != nil { if err != nil {
return err return err
} }
text := txt text := txt
b = []byte(text) b = []byte(text)
} }
@ -163,5 +164,3 @@ func (p Plugin) Exec() error {
} }
return nil return nil
} }