mirror of
https://github.com/drone-plugins/drone-webhook.git
synced 2026-06-04 18:24:05 +08:00
Use new template lib
This commit is contained in:
Generated
+15
-3
@@ -9,8 +9,20 @@
|
|||||||
"lexer",
|
"lexer",
|
||||||
"parser"
|
"parser"
|
||||||
]
|
]
|
||||||
revision = "a2232af10b53ef1ae5a767f5178db3a6c1dab655"
|
revision = "2eebd0f5dd9564c0c6e439df11d8a3c7b9b9ab11"
|
||||||
version = "v2.0.1"
|
version = "v2.0.2"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
branch = "master"
|
||||||
|
name = "github.com/drone/drone-template-lib"
|
||||||
|
packages = ["template"]
|
||||||
|
revision = "5748d3149f4859c6d6cf43edb4fa35bba379c918"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
name = "github.com/pkg/errors"
|
||||||
|
packages = ["."]
|
||||||
|
revision = "645ef00459ed84a119197bfb8d8205042c6df63d"
|
||||||
|
version = "v0.8.0"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
name = "github.com/urfave/cli"
|
name = "github.com/urfave/cli"
|
||||||
@@ -21,6 +33,6 @@
|
|||||||
[solve-meta]
|
[solve-meta]
|
||||||
analyzer-name = "dep"
|
analyzer-name = "dep"
|
||||||
analyzer-version = 1
|
analyzer-version = 1
|
||||||
inputs-digest = "f0f9f5cbe5ccddecfff2301c3783e001bde86bec1082c3026fda1ec005c18af5"
|
inputs-digest = "2f1e60cee9e7125c15754093974fd20cd1b6e9ac4d8b6a33a0364477f96461f7"
|
||||||
solver-name = "gps-cdcl"
|
solver-name = "gps-cdcl"
|
||||||
solver-version = 1
|
solver-version = 1
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
[[constraint]]
|
[[constraint]]
|
||||||
name = "github.com/aymerick/raymond"
|
branch = "master"
|
||||||
version = "2.0.1"
|
name = "github.com/drone/drone-template-lib"
|
||||||
|
|
||||||
[[constraint]]
|
[[constraint]]
|
||||||
name = "github.com/urfave/cli"
|
name = "github.com/urfave/cli"
|
||||||
|
|||||||
+16
@@ -0,0 +1,16 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
const respFormat = `Webhook %d
|
||||||
|
URL: %s
|
||||||
|
RESPONSE STATUS: %s
|
||||||
|
RESPONSE BODY: %s
|
||||||
|
`
|
||||||
|
|
||||||
|
const debugFormat = `Webhook %d
|
||||||
|
URL: %s
|
||||||
|
METHOD: %s
|
||||||
|
HEADERS: %s
|
||||||
|
REQUEST BODY: %s
|
||||||
|
RESPONSE STATUS: %s
|
||||||
|
RESPONSE BODY: %s
|
||||||
|
`
|
||||||
@@ -151,6 +151,7 @@ func main() {
|
|||||||
Usage: "source env file",
|
Usage: "source env file",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := app.Run(os.Args); err != nil {
|
if err := app.Run(os.Args); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -191,5 +192,6 @@ func run(c *cli.Context) error {
|
|||||||
SkipVerify: c.Bool("skip-verify"),
|
SkipVerify: c.Bool("skip-verify"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return plugin.Exec()
|
return plugin.Exec()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,11 +10,8 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
"github.com/drone/drone-template-lib/template"
|
||||||
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"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@@ -63,8 +60,10 @@ type (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (p Plugin) Exec() error {
|
func (p Plugin) Exec() error {
|
||||||
var buf bytes.Buffer
|
var (
|
||||||
var b []byte
|
buf bytes.Buffer
|
||||||
|
b []byte
|
||||||
|
)
|
||||||
|
|
||||||
if p.Config.Template == "" {
|
if p.Config.Template == "" {
|
||||||
data := struct {
|
data := struct {
|
||||||
@@ -76,15 +75,17 @@ func (p Plugin) Exec() error {
|
|||||||
fmt.Printf("Error: Failed to encode JSON payload. %s\n", err)
|
fmt.Printf("Error: Failed to encode JSON payload. %s\n", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
b = buf.Bytes()
|
b = buf.Bytes()
|
||||||
} else {
|
} else {
|
||||||
txt, err := RenderTrim(p.Config.Template, p)
|
txt, err := template.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)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// build and execute a request for each url.
|
// build and execute a request for each url.
|
||||||
@@ -101,7 +102,6 @@ func (p Plugin) Exec() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
r := bytes.NewReader(b)
|
r := bytes.NewReader(b)
|
||||||
|
|
||||||
req, err := http.NewRequest(p.Config.Method, uri.String(), r)
|
req, err := http.NewRequest(p.Config.Method, uri.String(), r)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -121,13 +121,17 @@ func (p Plugin) Exec() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
client := http.DefaultClient
|
client := http.DefaultClient
|
||||||
|
|
||||||
if p.Config.SkipVerify {
|
if p.Config.SkipVerify {
|
||||||
client = &http.Client{
|
client = &http.Client{
|
||||||
Transport: &http.Transport{
|
Transport: &http.Transport{
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
TLSClientConfig: &tls.Config{
|
||||||
|
InsecureSkipVerify: true,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -146,7 +150,7 @@ func (p Plugin) Exec() error {
|
|||||||
|
|
||||||
if p.Config.Debug {
|
if p.Config.Debug {
|
||||||
fmt.Printf(
|
fmt.Printf(
|
||||||
debugRespFormat,
|
debugFormat,
|
||||||
i+1,
|
i+1,
|
||||||
req.URL,
|
req.URL,
|
||||||
req.Method,
|
req.Method,
|
||||||
@@ -166,5 +170,6 @@ func (p Plugin) Exec() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
-137
@@ -1,137 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
"unicode"
|
|
||||||
"unicode/utf8"
|
|
||||||
|
|
||||||
"github.com/aymerick/raymond"
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
raymond.RegisterHelpers(funcs)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Render parses and executes a template, returning the results in string format.
|
|
||||||
func Render(template string, payload interface{}) (s string, err error) {
|
|
||||||
u, err := url.Parse(template)
|
|
||||||
if err == nil {
|
|
||||||
switch u.Scheme {
|
|
||||||
case "http", "https":
|
|
||||||
res, err := http.Get(template)
|
|
||||||
if err != nil {
|
|
||||||
return s, err
|
|
||||||
}
|
|
||||||
defer res.Body.Close()
|
|
||||||
out, err := ioutil.ReadAll(res.Body)
|
|
||||||
if err != nil {
|
|
||||||
return s, err
|
|
||||||
}
|
|
||||||
template = string(out)
|
|
||||||
|
|
||||||
case "file":
|
|
||||||
out, err := ioutil.ReadFile(u.Path)
|
|
||||||
if err != nil {
|
|
||||||
return s, err
|
|
||||||
}
|
|
||||||
template = string(out)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return raymond.Render(template, payload)
|
|
||||||
}
|
|
||||||
|
|
||||||
// RenderTrim parses and executes a template, returning the results in string
|
|
||||||
// format. The result is trimmed to remove left and right padding and newlines
|
|
||||||
// that may be added unintentially in the template markup.
|
|
||||||
func RenderTrim(template string, playload interface{}) (string, error) {
|
|
||||||
out, err := Render(template, playload)
|
|
||||||
return strings.Trim(out, " \n"), err
|
|
||||||
}
|
|
||||||
|
|
||||||
var funcs = map[string]interface{}{
|
|
||||||
"uppercasefirst": uppercaseFirst,
|
|
||||||
"uppercase": strings.ToUpper,
|
|
||||||
"lowercase": strings.ToLower,
|
|
||||||
"duration": toDuration,
|
|
||||||
"datetime": toDatetime,
|
|
||||||
"success": isSuccess,
|
|
||||||
"failure": isFailure,
|
|
||||||
"truncate": truncate,
|
|
||||||
"urlencode": urlencode,
|
|
||||||
"since": since,
|
|
||||||
}
|
|
||||||
|
|
||||||
func truncate(s string, len int) string {
|
|
||||||
if utf8.RuneCountInString(s) <= len {
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
runes := []rune(s)
|
|
||||||
return string(runes[:len])
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func uppercaseFirst(s string) string {
|
|
||||||
a := []rune(s)
|
|
||||||
a[0] = unicode.ToUpper(a[0])
|
|
||||||
s = string(a)
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func toDuration(started, finished float64) string {
|
|
||||||
return fmt.Sprint(time.Duration(finished-started) * time.Second)
|
|
||||||
}
|
|
||||||
|
|
||||||
func toDatetime(timestamp float64, layout, zone string) string {
|
|
||||||
if len(zone) == 0 {
|
|
||||||
return time.Unix(int64(timestamp), 0).Format(layout)
|
|
||||||
}
|
|
||||||
loc, err := time.LoadLocation(zone)
|
|
||||||
if err != nil {
|
|
||||||
return time.Unix(int64(timestamp), 0).Local().Format(layout)
|
|
||||||
}
|
|
||||||
return time.Unix(int64(timestamp), 0).In(loc).Format(layout)
|
|
||||||
}
|
|
||||||
|
|
||||||
func isSuccess(conditional bool, options *raymond.Options) string {
|
|
||||||
if !conditional {
|
|
||||||
return options.Inverse()
|
|
||||||
}
|
|
||||||
|
|
||||||
switch options.ParamStr(0) {
|
|
||||||
case "success":
|
|
||||||
return options.Fn()
|
|
||||||
default:
|
|
||||||
return options.Inverse()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func isFailure(conditional bool, options *raymond.Options) string {
|
|
||||||
if !conditional {
|
|
||||||
return options.Inverse()
|
|
||||||
}
|
|
||||||
|
|
||||||
switch options.ParamStr(0) {
|
|
||||||
case "failure", "error", "killed":
|
|
||||||
return options.Fn()
|
|
||||||
default:
|
|
||||||
return options.Inverse()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func urlencode(options *raymond.Options) string {
|
|
||||||
return url.QueryEscape(options.Fn())
|
|
||||||
}
|
|
||||||
|
|
||||||
func since(start int64) string {
|
|
||||||
// NOTE: not using `time.Since()` because the fractional second component
|
|
||||||
// will give us something like "40m12.917523438s" vs "40m12s". We lose
|
|
||||||
// some precision, but the format is much more readable.
|
|
||||||
now := time.Unix(time.Now().Unix(), 0)
|
|
||||||
return fmt.Sprint(now.Sub(time.Unix(start, 0)))
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user