Merge pull request #6 from drone-plugins/feature/unification

Multiple minor improvements, more unification
This commit is contained in:
Jack Spirou 2016-01-03 12:20:56 -05:00
commit 9d9ffacca6
6 changed files with 53 additions and 33 deletions

View File

@ -2,6 +2,7 @@ build:
image: golang:1.5 image: golang:1.5
commands: commands:
- make deps - make deps
- make vet
- make build - make build
- make test - make test

View File

@ -1,10 +1,9 @@
# Docker image for the Drone Webhook plugin # Docker image for the Drone Webhook plugin
# #
# cd $GOPATH/src/github.com/drone-plugins/drone-webhook # cd $GOPATH/src/github.com/drone-plugins/drone-webhook
# make deps build # make deps build docker
# docker build --rm=true -t plugins/drone-webhook .
FROM alpine:3.2 FROM alpine:3.3
RUN apk update && \ RUN apk update && \
apk add \ apk add \

View File

@ -1,4 +1,4 @@
Apache License Apache License
Version 2.0, January 2004 Version 2.0, January 2004
http://www.apache.org/licenses/ http://www.apache.org/licenses/

View File

@ -1,4 +1,4 @@
.PHONY: clean deps test build .PHONY: clean deps test build docker
export GOOS ?= linux export GOOS ?= linux
export GOARCH ?= amd64 export GOARCH ?= amd64
@ -18,5 +18,14 @@ deps:
test: test:
go test -cover ./... go test -cover ./...
fmt:
go fmt ./...
vet:
go vet ./...
build: build:
go build -ldflags '-s -w $(LDFLAGS)' go build -ldflags '-s -w $(LDFLAGS)'
docker:
docker build --rm=true -t plugins/drone-webhook .

View File

@ -1,5 +1,8 @@
# drone-webhook # drone-webhook
[![Build Status](http://beta.drone.io/api/badges/drone-plugins/drone-webhook/status.svg)](http://beta.drone.io/drone-plugins/drone-webhook)
[![](https://badge.imagelayers.io/plugins/drone-webhook:latest.svg)](https://imagelayers.io/?images=plugins/drone-webhook:latest 'Get your own badge on imagelayers.io')
Drone plugin for sending notifications via Webhook Drone plugin for sending notifications via Webhook
## Usage ## Usage
@ -46,8 +49,7 @@ EOF
Build the Docker container using `make`: Build the Docker container using `make`:
``` ```
make deps build make deps build docker
docker build --rm=true -t plugins/drone-webhook .
``` ```
### Example ### Example

61
main.go
View File

@ -33,29 +33,29 @@ func main() {
plugin.Param("vargs", &vargs) plugin.Param("vargs", &vargs)
plugin.MustParse() plugin.MustParse()
if len(vargs.Method) == 0 { if vargs.Method == "" {
vargs.Method = "POST" vargs.Method = "POST"
} }
if len(vargs.ContentType) == 0 { if vargs.ContentType == "" {
vargs.ContentType = "application/json" vargs.ContentType = "application/json"
} }
data := struct { // Creates the payload, by default the payload
System drone.System `json:"system"`
Repo drone.Repo `json:"repo"`
Build drone.Build `json:"build"`
}{system, repo, build}
// 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 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 { if err := json.NewEncoder(&buf).Encode(&data); err != nil {
fmt.Printf("Error encoding json payload. %s\n", err) fmt.Printf("Error: Failed to encode JSON payload. %s\n", err)
os.Exit(1) os.Exit(1)
} }
} else { } else {
@ -66,7 +66,7 @@ func main() {
}) })
if err != nil { if err != nil {
fmt.Printf("Error executing content template. %s\n", err) fmt.Printf("Error: Failed to execute the content template. %s\n", err)
os.Exit(1) os.Exit(1)
} }
} }
@ -80,7 +80,7 @@ func main() {
uri, err := url.Parse(rawurl) uri, err := url.Parse(rawurl)
if err != nil { if err != nil {
fmt.Printf("Error parsing hook url. %s\n", err) fmt.Printf("Error: Failed to parse the hook URL. %s\n", err)
os.Exit(1) os.Exit(1)
} }
@ -90,7 +90,7 @@ func main() {
req, err := http.NewRequest(vargs.Method, uri.String(), r) req, err := http.NewRequest(vargs.Method, uri.String(), r)
if err != nil { if err != nil {
fmt.Printf("Error creating http request. %s\n", err) fmt.Printf("Error: Failed to create the HTTP request. %s\n", err)
os.Exit(1) os.Exit(1)
} }
@ -100,18 +100,14 @@ func main() {
req.Header.Set(key, value) req.Header.Set(key, value)
} }
if len(vargs.Auth.Username) > 0 { if vargs.Auth.Username != "" {
if len(vargs.Auth.Password) > 0 { req.SetBasicAuth(vargs.Auth.Username, vargs.Auth.Password)
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: Failed to execute the HTTP request. %s\n", err)
os.Exit(1) os.Exit(1)
} }
@ -121,15 +117,28 @@ func main() {
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
// I do not think we need to os.Exit(1) if we are fmt.Printf("Error: Failed to read the HTTP response body. %s\n", err)
// unable to read a http response body.
fmt.Printf("Error reading http response body. %s\n", err)
} }
if vargs.Debug { 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)) fmt.Printf(
"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 { } 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)) fmt.Printf(
"Webhook %d\n URL: %s\n RESPONSE STATUS: %s\n RESPONSE BODY: %s\n",
i+1,
req.URL,
resp.Status,
string(body),
)
} }
} }
} }