adding auth and some updates
This commit is contained in:
parent
26546268e8
commit
53f8e310e3
71
main.go
71
main.go
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
@ -14,26 +15,22 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
|
// plugin settings
|
||||||
var repo = drone.Repo{}
|
var repo = drone.Repo{}
|
||||||
var build = drone.Build{}
|
var build = drone.Build{}
|
||||||
var vargs = struct {
|
var vargs = Webhook{}
|
||||||
Urls []string `json:"urls"`
|
|
||||||
Headers map[string]string `json:"header"`
|
|
||||||
Method string `json:"method"`
|
|
||||||
Template string `json:"template"`
|
|
||||||
ContentType string `json:"content_type"`
|
|
||||||
}{}
|
|
||||||
|
|
||||||
|
// set plugin parameters
|
||||||
plugin.Param("repo", &repo)
|
plugin.Param("repo", &repo)
|
||||||
plugin.Param("build", &build)
|
plugin.Param("build", &build)
|
||||||
plugin.Param("vargs", &vargs)
|
plugin.Param("vargs", &vargs)
|
||||||
plugin.Parse()
|
|
||||||
|
|
||||||
// data structure
|
// parse the parameters
|
||||||
data := struct {
|
if err := plugin.Parse(); err != nil {
|
||||||
Repo drone.Repo `json:"repo"`
|
fmt.Println(err)
|
||||||
Build drone.Build `json:"build"`
|
os.Exit(1)
|
||||||
}{repo, build}
|
}
|
||||||
|
|
||||||
// set default values
|
// set default values
|
||||||
if len(vargs.Method) == 0 {
|
if len(vargs.Method) == 0 {
|
||||||
|
@ -43,19 +40,26 @@ func main() {
|
||||||
vargs.ContentType = "application/json"
|
vargs.ContentType = "application/json"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// data structure
|
||||||
|
data := struct {
|
||||||
|
Repo drone.Repo `json:"repo"`
|
||||||
|
Build drone.Build `json:"build"`
|
||||||
|
}{repo, build}
|
||||||
|
|
||||||
// creates the payload. by default the payload
|
// creates the payload. by default the payload
|
||||||
// is the build details in json format, but a custom
|
// is the build details in json format, but a custom
|
||||||
// template may also be used.
|
// template may also be used.
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
if len(vargs.Template) == 0 {
|
if len(vargs.Template) == 0 {
|
||||||
json.NewEncoder(&buf).Encode(&data)
|
json.NewEncoder(&buf).Encode(&data)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
t, err := template.New("_").Parse(vargs.Template)
|
t, err := template.New("_").Parse(vargs.Template)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error parsing content template. %s\n", err)
|
fmt.Printf("Error parsing content template. %s\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Execute(&buf, &data)
|
t.Execute(&buf, &data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error executing content template. %s\n", err)
|
fmt.Printf("Error executing content template. %s\n", err)
|
||||||
|
@ -63,8 +67,11 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// post payload to each url
|
// build and execute a request for each url.
|
||||||
for _, rawurl := range vargs.Urls {
|
// all auth, headers, method, template (payload),
|
||||||
|
// and content_type values will be applied to
|
||||||
|
// every webhook request.
|
||||||
|
for i, rawurl := range vargs.Urls {
|
||||||
|
|
||||||
uri, err := url.Parse(rawurl)
|
uri, err := url.Parse(rawurl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -72,21 +79,51 @@ func main() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// vargs.Method defaults to POST, no need to check
|
||||||
req, err := http.NewRequest(vargs.Method, uri.String(), &buf)
|
req, err := http.NewRequest(vargs.Method, uri.String(), &buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error creating http request. %s\n", err)
|
fmt.Printf("Error creating http request. %s\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// vargs.ContentType defaults to application/json, no need to check
|
||||||
req.Header.Set("Content-Type", vargs.ContentType)
|
req.Header.Set("Content-Type", vargs.ContentType)
|
||||||
for key, value := range vargs.Headers {
|
for key, value := range vargs.Headers {
|
||||||
req.Header.Set(key, value)
|
req.Header.Set(key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set basic auth if a user or user and pass is provided
|
||||||
|
if len(vargs.Auth.Username) > 0 {
|
||||||
|
if len(vargs.Auth.Password) > 0 {
|
||||||
|
req.SetBasicAuth(vargs.Auth.Username, vargs.Auth.Password)
|
||||||
|
} else {
|
||||||
|
req.SetBasicAuth(vargs.Auth.Username, "")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error executing http request. %s\n", err)
|
fmt.Printf("Error executing http request. %s\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if vargs.Verbose {
|
||||||
|
|
||||||
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
// I do not think we need to os.Exit(1) if we are
|
||||||
|
// unable to read a http response body.
|
||||||
|
fmt.Printf("Error reading http response body. %s\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// scrub out basic auth pass
|
||||||
|
if len(vargs.Auth.Password) > 0 {
|
||||||
|
req.SetBasicAuth(vargs.Auth.Username, "XXXXX")
|
||||||
|
}
|
||||||
|
|
||||||
|
// print out
|
||||||
|
fmt.Printf("%d\n URL: %s\n METHOD: %s\n HEADERS: %s\n BODY: %s\n RESPONSE STATUS: %s\n RESPONSE: %s", i+1, req.URL, req.Method, req.Header, req.Body, resp.Status, string(body))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
16
webhook.go
Normal file
16
webhook.go
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Webhook struct {
|
||||||
|
Urls []string `json:"urls"`
|
||||||
|
Verbose bool `json:"verbose"`
|
||||||
|
Auth BasicAuth `json:"auth"`
|
||||||
|
Headers map[string]string `json:"header"`
|
||||||
|
Method string `json:"method"`
|
||||||
|
Template string `json:"template"`
|
||||||
|
ContentType string `json:"content_type"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type BasicAuth struct {
|
||||||
|
Username string `json:"username"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user