drone-webhook/main.go

136 lines
3.4 KiB
Go
Raw Normal View History

2015-11-03 19:31:25 +00:00
package main
import (
"bytes"
"encoding/json"
2015-11-18 21:47:08 +00:00
"fmt"
2015-11-20 23:51:50 +00:00
"io/ioutil"
2015-11-03 19:31:25 +00:00
"net/http"
2015-11-18 21:47:08 +00:00
"net/url"
2015-11-03 19:31:25 +00:00
"os"
"github.com/drone/drone-go/drone"
2015-11-18 21:47:08 +00:00
"github.com/drone/drone-go/plugin"
2015-11-24 23:35:01 +00:00
"github.com/drone/drone-go/template"
2015-11-03 19:31:25 +00:00
)
func main() {
2015-11-20 23:51:50 +00:00
// plugin settings
2015-11-24 23:35:01 +00:00
var sys = drone.System{}
2015-11-03 19:31:25 +00:00
var repo = drone.Repo{}
var build = drone.Build{}
2015-11-20 23:51:50 +00:00
var vargs = Webhook{}
2015-11-03 19:31:25 +00:00
2015-11-20 23:51:50 +00:00
// set plugin parameters
2015-11-24 23:35:01 +00:00
plugin.Param("system", &sys)
2015-11-03 19:31:25 +00:00
plugin.Param("repo", &repo)
plugin.Param("build", &build)
plugin.Param("vargs", &vargs)
2015-11-20 23:51:50 +00:00
// parse the parameters
if err := plugin.Parse(); err != nil {
fmt.Println(err)
os.Exit(1)
}
2015-11-03 19:31:25 +00:00
2015-11-18 21:47:08 +00:00
// set default values
if len(vargs.Method) == 0 {
vargs.Method = "POST"
}
if len(vargs.ContentType) == 0 {
vargs.ContentType = "application/json"
}
2015-11-20 23:51:50 +00:00
// data structure
data := struct {
System drone.System `json:"system"`
Repo drone.Repo `json:"repo"`
Build drone.Build `json:"build"`
}{sys, repo, build}
2015-11-20 23:51:50 +00:00
2015-11-18 21:47:08 +00:00
// creates the payload. by default the payload
// is the build details in json format, but a custom
// template may also be used.
var buf bytes.Buffer
2015-11-18 21:47:08 +00:00
if len(vargs.Template) == 0 {
if err := json.NewEncoder(&buf).Encode(&data); err != nil {
2015-11-24 23:35:01 +00:00
fmt.Printf("Error encoding json payload. %s\n", err)
os.Exit(1)
}
2015-11-18 21:47:08 +00:00
} else {
2015-11-24 23:35:01 +00:00
err := template.Write(&buf, vargs.Template, &drone.Payload{
Build: &build,
Repo: &repo,
System: &sys,
})
2015-11-18 21:47:08 +00:00
if err != nil {
fmt.Printf("Error executing content template. %s\n", err)
os.Exit(1)
}
2015-11-03 19:31:25 +00:00
}
2015-11-20 23:51:50 +00:00
// build and execute a request for each url.
// all auth, headers, method, template (payload),
// and content_type values will be applied to
// every webhook request.
for i, rawurl := range vargs.Urls {
2015-11-18 21:47:08 +00:00
uri, err := url.Parse(rawurl)
if err != nil {
fmt.Printf("Error parsing hook url. %s\n", err)
os.Exit(1)
}
2015-11-20 23:51:50 +00:00
// vargs.Method defaults to POST, no need to check
b := buf.Bytes()
r := bytes.NewReader(b)
req, err := http.NewRequest(vargs.Method, uri.String(), r)
2015-11-18 21:47:08 +00:00
if err != nil {
fmt.Printf("Error creating http request. %s\n", err)
os.Exit(1)
}
2015-11-20 23:51:50 +00:00
// vargs.ContentType defaults to application/json, no need to check
2015-11-18 21:47:08 +00:00
req.Header.Set("Content-Type", vargs.ContentType)
for key, value := range vargs.Headers {
req.Header.Set(key, value)
}
2015-11-20 23:51:50 +00:00
// 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, "")
}
}
2015-11-18 21:47:08 +00:00
resp, err := http.DefaultClient.Do(req)
2015-11-03 19:31:25 +00:00
if err != nil {
2015-11-18 21:47:08 +00:00
fmt.Printf("Error executing http request. %s\n", err)
2015-11-03 19:31:25 +00:00
os.Exit(1)
}
2015-11-20 23:51:50 +00:00
defer resp.Body.Close()
2015-11-21 16:47:06 +00:00
// if debug is on or response status code is bad
if vargs.Debug || resp.StatusCode >= http.StatusBadRequest {
2015-11-20 23:51:50 +00:00
2015-11-21 16:47:06 +00:00
// read the response body
2015-11-20 23:51:50 +00:00
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)
}
2015-11-21 16:47:06 +00:00
// debug/info print
if vargs.Debug {
fmt.Printf("[debug] Webhook %d\n URL: %s\n METHOD: %s\n HEADERS: %s\n REQUEST BODY: %s\n RESPONSE STATUS: %s\n RESPONSE BODY: %s\n", i+1, req.URL, req.Method, req.Header, string(b), resp.Status, string(body))
} else {
fmt.Printf("[info] Webhook %d\n URL: %s\n RESPONSE STATUS: %s\n RESPONSE BODY: %s\n", i+1, req.URL, resp.Status, string(body))
2015-11-21 03:09:59 +00:00
}
2015-11-20 23:51:50 +00:00
}
2015-11-03 19:31:25 +00:00
}
}