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
commands:
- make deps
- make vet
- make build
- make test

View File

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

View File

@ -1,4 +1,4 @@
Apache License
Apache License
Version 2.0, January 2004
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 GOARCH ?= amd64
@ -18,5 +18,14 @@ deps:
test:
go test -cover ./...
fmt:
go fmt ./...
vet:
go vet ./...
build:
go build -ldflags '-s -w $(LDFLAGS)'
docker:
docker build --rm=true -t plugins/drone-webhook .

View File

@ -1,5 +1,8 @@
# 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
## Usage
@ -46,8 +49,7 @@ EOF
Build the Docker container using `make`:
```
make deps build
docker build --rm=true -t plugins/drone-webhook .
make deps build docker
```
### Example

61
main.go
View File

@ -33,29 +33,29 @@ func main() {
plugin.Param("vargs", &vargs)
plugin.MustParse()
if len(vargs.Method) == 0 {
if vargs.Method == "" {
vargs.Method = "POST"
}
if len(vargs.ContentType) == 0 {
if vargs.ContentType == "" {
vargs.ContentType = "application/json"
}
data := struct {
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
// 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
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 {
fmt.Printf("Error encoding json payload. %s\n", err)
fmt.Printf("Error: Failed to encode JSON payload. %s\n", err)
os.Exit(1)
}
} else {
@ -66,7 +66,7 @@ func main() {
})
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)
}
}
@ -80,7 +80,7 @@ func main() {
uri, err := url.Parse(rawurl)
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)
}
@ -90,7 +90,7 @@ func main() {
req, err := http.NewRequest(vargs.Method, uri.String(), r)
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)
}
@ -100,18 +100,14 @@ func main() {
req.Header.Set(key, value)
}
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, "")
}
if vargs.Auth.Username != "" {
req.SetBasicAuth(vargs.Auth.Username, vargs.Auth.Password)
}
resp, err := http.DefaultClient.Do(req)
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)
}
@ -121,15 +117,28 @@ func main() {
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)
fmt.Printf("Error: Failed to read the HTTP response body. %s\n", err)
}
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 {
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),
)
}
}
}