drone-webhook/main.go

159 lines
3.2 KiB
Go
Raw Normal View History

2015-11-03 19:31:25 +00:00
package main
import (
"bytes"
"crypto/tls"
2015-11-03 19:31:25 +00:00
"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
)
const (
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"
)
var (
buildCommit string
)
2015-11-03 19:31:25 +00:00
func main() {
fmt.Printf("Drone Webhook Plugin built from %s\n", buildCommit)
2015-11-20 23:51:50 +00:00
system := drone.System{}
repo := drone.Repo{}
build := drone.Build{}
vargs := Params{}
2015-11-03 19:31:25 +00:00
plugin.Param("system", &system)
2015-11-03 19:31:25 +00:00
plugin.Param("repo", &repo)
plugin.Param("build", &build)
plugin.Param("vargs", &vargs)
plugin.MustParse()
2015-11-03 19:31:25 +00:00
2016-01-01 11:57:11 +00:00
if vargs.Method == "" {
2015-11-18 21:47:08 +00:00
vargs.Method = "POST"
}
2016-01-01 11:57:11 +00:00
if vargs.ContentType == "" {
2015-11-18 21:47:08 +00:00
vargs.ContentType = "application/json"
}
2016-01-01 11:57:11 +00:00
// Creates the payload, by default the payload
2015-11-18 21:47:08 +00:00
// is the build details in json format, but a custom
// template may also be used.
var buf bytes.Buffer
2016-01-01 11:57:11 +00:00
if vargs.Template == "" {
data := struct {
System drone.System `json:"system"`
Repo drone.Repo `json:"repo"`
Build drone.Build `json:"build"`
}{system, repo, build}
if err := json.NewEncoder(&buf).Encode(&data); err != nil {
2016-01-01 12:24:28 +00:00
fmt.Printf("Error: Failed to encode 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: &system,
2015-11-24 23:35:01 +00:00
})
2015-11-18 21:47:08 +00:00
if err != nil {
2016-01-01 12:24:28 +00:00
fmt.Printf("Error: Failed to execute the content template. %s\n", err)
2015-11-18 21:47:08 +00:00
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.
2015-11-18 21:47:08 +00:00
2016-01-08 18:27:49 +00:00
for i, rawurl := range vargs.URLs {
2015-11-18 21:47:08 +00:00
uri, err := url.Parse(rawurl)
2015-11-18 21:47:08 +00:00
if err != nil {
2016-01-01 12:24:28 +00:00
fmt.Printf("Error: Failed to parse the hook URL. %s\n", err)
2015-11-18 21:47:08 +00:00
os.Exit(1)
}
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 {
2016-01-01 12:24:28 +00:00
fmt.Printf("Error: Failed to create the HTTP request. %s\n", err)
2015-11-18 21:47:08 +00:00
os.Exit(1)
}
2015-11-20 23:51:50 +00:00
2015-11-18 21:47:08 +00:00
req.Header.Set("Content-Type", vargs.ContentType)
2015-11-18 21:47:08 +00:00
for key, value := range vargs.Headers {
req.Header.Set(key, value)
}
2016-01-01 11:57:11 +00:00
if vargs.Auth.Username != "" {
req.SetBasicAuth(vargs.Auth.Username, vargs.Auth.Password)
2015-11-20 23:51:50 +00:00
}
client := http.DefaultClient
if vargs.SkipVerify {
client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
}
resp, err := client.Do(req)
2015-11-03 19:31:25 +00:00
if err != nil {
2016-01-01 12:24:28 +00:00
fmt.Printf("Error: Failed to execute the 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 vargs.Debug || resp.StatusCode >= http.StatusBadRequest {
2015-11-20 23:51:50 +00:00
body, err := ioutil.ReadAll(resp.Body)
2015-11-20 23:51:50 +00:00
if err != nil {
2016-01-01 12:24:28 +00:00
fmt.Printf("Error: Failed to read the HTTP response body. %s\n", err)
2015-11-20 23:51:50 +00:00
}
2015-11-21 16:47:06 +00:00
if vargs.Debug {
2016-01-01 11:57:11 +00:00
fmt.Printf(
debugRespFormat,
2016-01-01 11:57:11 +00:00
i+1,
req.URL,
req.Method,
req.Header,
string(b),
resp.Status,
string(body),
)
2015-11-21 16:47:06 +00:00
} else {
2016-01-01 11:57:11 +00:00
fmt.Printf(
respFormat,
2016-01-01 11:57:11 +00:00
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
}
}