From 3237a14a2a600a510efbe702396f3a670484eeec Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Fri, 1 Jan 2016 12:44:57 +0100 Subject: [PATCH 1/8] Added make task for docker container --- Makefile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 6644e77..532f6d6 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: clean deps test build +.PHONY: clean deps test build docker export GOOS ?= linux export GOARCH ?= amd64 @@ -20,3 +20,6 @@ test: build: go build -ldflags '-s -w $(LDFLAGS)' + +docker: + docker build --rm=true -t plugins/drone-webhook . \ No newline at end of file From 2b39abade75313395080fcf014e9f5ae918f47dc Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Fri, 1 Jan 2016 12:46:21 +0100 Subject: [PATCH 2/8] Added badges to readme --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index e58d1ab..d070ee6 100644 --- a/README.md +++ b/README.md @@ -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 From 2d7d0c5169e32d93a968e64f63783925c008f5cb Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Fri, 1 Jan 2016 12:46:56 +0100 Subject: [PATCH 3/8] Documented docker make task within readme --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index d070ee6..854cfcc 100644 --- a/README.md +++ b/README.md @@ -49,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 From be5a0ad5a060ccc1d4d7085a5051a1bd611912af Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Fri, 1 Jan 2016 12:47:20 +0100 Subject: [PATCH 4/8] Switched to 3.3 base image, documented docker make task --- Dockerfile | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index d74fa64..ed0c0dd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 \ From d35d69a9838b1f688e958bbfc052cf202fe9f6ae Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Fri, 1 Jan 2016 12:47:38 +0100 Subject: [PATCH 5/8] Fixed license styling --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index e06d208..8f71f43 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Apache License + Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ From 1f50e69fa286459bde90cb3dc638efa95b2d8496 Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Fri, 1 Jan 2016 12:57:11 +0100 Subject: [PATCH 6/8] Some minor code cleanups --- main.go | 47 +++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index 6f580e8..fd92b04 100644 --- a/main.go +++ b/main.go @@ -33,27 +33,27 @@ 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) os.Exit(1) @@ -100,12 +100,8 @@ 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) @@ -127,9 +123,24 @@ func main() { } 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), + ) } } } From d25f5eeeaf6b329a34a3278087158be67accbe67 Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Fri, 1 Jan 2016 12:57:45 +0100 Subject: [PATCH 7/8] Added make tasks for fmt and vet, added vet task to drone.yml --- .drone.yml | 1 + Makefile | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/.drone.yml b/.drone.yml index 25def7d..219f9fa 100644 --- a/.drone.yml +++ b/.drone.yml @@ -2,6 +2,7 @@ build: image: golang:1.5 commands: - make deps + - make vet - make build - make test diff --git a/Makefile b/Makefile index 532f6d6..b4dce8a 100644 --- a/Makefile +++ b/Makefile @@ -18,6 +18,12 @@ deps: test: go test -cover ./... +fmt: + go fmt ./... + +vet: + go vet ./... + build: go build -ldflags '-s -w $(LDFLAGS)' From 3a38dc2311a3eb6e8f50fcb1bf2cd97c1a80ad6d Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Fri, 1 Jan 2016 13:24:28 +0100 Subject: [PATCH 8/8] Updated wording of error messages --- main.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index fd92b04..94fdf44 100644 --- a/main.go +++ b/main.go @@ -55,7 +55,7 @@ func main() { }{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) } @@ -107,7 +107,7 @@ func main() { 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) } @@ -117,9 +117,7 @@ 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 {