From 07a1259df4ba78657ef19b6d2d9cc9e72912da66 Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Fri, 11 Dec 2015 09:44:27 +0100 Subject: [PATCH 1/4] Streamlined the documentation --- DOCS.md | 20 ++++++++++--------- README.md | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 66 insertions(+), 14 deletions(-) diff --git a/DOCS.md b/DOCS.md index af265e9..5be51c4 100644 --- a/DOCS.md +++ b/DOCS.md @@ -1,20 +1,22 @@ -Use the Email plugin to send an email to a recipient list when a build completes. The following parameters are used to configure the email plugin: +Use this plugin for sending build status notifications via Email. You can +override the default configuration with the following parameters: -* `from` - sends email from this address -* `host` - smtp server host -* `port` - smtp server port (defaults to `587`) -* `username` - smtp server username -* `password` - smtp server password -* `recipient` - list of email recipients (defaults to commit email) +* `from` - Send notifications from this address +* `host` - SMTP server host +* `port` - SMTP server port, defaults to `587` +* `username` - SMTP username +* `password` - SMTP password +* `recipients` - List of recipients, defaults to commit email -Same email configuration: +## Example + +The following is a sample configuration in your .drone.yml file: ```yaml notify: email: from: noreply@github.com host: smtp.mailgun.org - port: 587 username: octocat password: 12345 recipients: diff --git a/README.md b/README.md index 30f1ca7..8fe4adb 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,13 @@ # drone-email -Drone plugin for sending email notifications. +[![Build Status](http://beta.drone.io/api/badges/drone-plugins/drone-email/status.svg)](http://beta.drone.io/drone-plugins/drone-email) +[![](https://badge.imagelayers.io/plugins/drone-email:latest.svg)](https://imagelayers.io/?images=plugins/drone-email:latest 'Get your own badge on imagelayers.io') -## Overview +Drone plugin for sending build status notifications via Email -This plugin is responsible for sending build notifications via email: +## Usage -``` +```sh ./drone-email < Date: Fri, 11 Dec 2015 09:45:18 +0100 Subject: [PATCH 2/4] Removed empty lines --- .gitignore | 1 - LICENSE | 1 - 2 files changed, 2 deletions(-) diff --git a/.gitignore b/.gitignore index 47f3ba5..b732e0a 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,3 @@ _testmain.go *.prof drone-email - diff --git a/LICENSE b/LICENSE index 8f71f43..8dada3e 100644 --- a/LICENSE +++ b/LICENSE @@ -199,4 +199,3 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. - From 40520edfdbd65e8ce391ae105739fab7b15602bf Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Fri, 11 Dec 2015 09:46:07 +0100 Subject: [PATCH 3/4] Integrated a makefile --- .drone.yml | 14 ++++---------- Dockerfile | 16 +++++++++++----- Makefile | 22 ++++++++++++++++++++++ 3 files changed, 37 insertions(+), 15 deletions(-) create mode 100644 Makefile diff --git a/.drone.yml b/.drone.yml index b11694e..8fdb590 100644 --- a/.drone.yml +++ b/.drone.yml @@ -1,14 +1,9 @@ build: image: golang:1.5 - environment: - - GO15VENDOREXPERIMENT=1 - - GOOS=linux - - GOARCH=amd64 - - CGO_ENABLED=0 commands: - - go get - - go build - - go test + - make deps + - make build + - make test publish: docker: @@ -21,9 +16,8 @@ publish: plugin: name: Email - desc: Send build status notifications via email. + desc: Send build status notifications via Email type: notify image: plugins/drone-email labels: - email - diff --git a/Dockerfile b/Dockerfile index e18c00c..c6db236 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,15 @@ -# Docker image for Drone's email notification plugin +# Docker image for the Drone Email plugin # -# CGO_ENABLED=0 go build -a -tags netgo +# cd $GOPATH/src/github.com/drone-plugins/drone-email +# make deps build # docker build --rm=true -t plugins/drone-email . -FROM gliderlabs/alpine:3.1 -RUN apk-install ca-certificates +FROM alpine:3.2 + +RUN apk update && \ + apk add \ + ca-certificates && \ + rm -rf /var/cache/apk/* + ADD drone-email /bin/ -ENTRYPOINT ["/bin/drone-email"] \ No newline at end of file +ENTRYPOINT ["/bin/drone-email"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6644e77 --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +.PHONY: clean deps test build + +export GOOS ?= linux +export GOARCH ?= amd64 +export CGO_ENABLED ?= 0 + +CI_BUILD_NUMBER ?= 0 + +LDFLAGS += -X "main.buildDate=$(shell date -u '+%Y-%m-%d %H:%M:%S %Z')" +LDFLAGS += -X "main.build=$(CI_BUILD_NUMBER)" + +clean: + go clean -i ./... + +deps: + go get -t ./... + +test: + go test -cover ./... + +build: + go build -ldflags '-s -w $(LDFLAGS)' From 9d5c1e333367f78a5b6b8bced7098c894db64585 Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Fri, 11 Dec 2015 09:46:36 +0100 Subject: [PATCH 4/4] Some restructuring of code, no functional change --- main.go | 58 ++++++++++++++++++++--------------------------------- sender.go | 41 +++++++++++++++++++++++++++---------- template.go | 16 +++++++-------- types.go | 21 +++++++++++++++++++ 4 files changed, 81 insertions(+), 55 deletions(-) create mode 100644 types.go diff --git a/main.go b/main.go index 75d1786..aea143f 100644 --- a/main.go +++ b/main.go @@ -8,60 +8,46 @@ import ( "github.com/drone/drone-go/plugin" ) -type Email struct { - Recipients []string `json:"recipients"` - - Host string `json:"host"` - Port int `json:"port"` - From string `json:"from"` - Username string `json:"username"` - Password string `json:"password"` -} - -type Context struct { - Email Email - Build drone.Build - Repo drone.Repo - System drone.System -} +var ( + build string + buildDate string +) func main() { + fmt.Printf("Drone Email Plugin built at %s\n", buildDate) + + system := drone.System{} repo := drone.Repo{} build := drone.Build{} - system := drone.System{} - email := Email{} + vargs := Params{} plugin.Param("system", &system) - plugin.Param("build", &build) plugin.Param("repo", &repo) - plugin.Param("vargs", &email) - err := plugin.Parse() - if err != nil { - fmt.Println(err) - os.Exit(1) - } + plugin.Param("build", &build) + plugin.Param("vargs", &vargs) + plugin.MustParse() - // default to the commit email address - // if no email recipient list is provided - if len(email.Recipients) == 0 { - email.Recipients = []string{ + if len(vargs.Recipients) == 0 { + vargs.Recipients = []string{ build.Email, } } - // default smtp port - if email.Port == 0 { - email.Port = 587 + if vargs.Port == 0 { + vargs.Port = 587 } - err = Send(&Context{ - Email: email, - Build: build, - Repo: repo, + err := Send(&Context{ System: system, + Repo: repo, + Build: build, + Vargs: vargs, }) + if err != nil { fmt.Println(err) + os.Exit(1) + return } } diff --git a/sender.go b/sender.go index de666ec..e90adeb 100644 --- a/sender.go +++ b/sender.go @@ -27,10 +27,10 @@ func Send(context *Context) error { // SendFailure sends email notifications to the list of // recipients indicating the build failed. func SendFailure(context *Context) error { - // generate the email failure template var buf bytes.Buffer err := failureTemplate.ExecuteTemplate(&buf, "_", context) + if err != nil { return err } @@ -51,10 +51,10 @@ func SendFailure(context *Context) error { // SendSuccess sends email notifications to the list of // recipients indicating the build was a success. func SendSuccess(context *Context) error { - // generate the email success template var buf bytes.Buffer err := successTemplate.ExecuteTemplate(&buf, "_", context) + if err != nil { return err } @@ -73,25 +73,44 @@ func SendSuccess(context *Context) error { } func send(subject, body string, c *Context) error { - - if len(c.Email.Recipients) == 0 { - c.Email.Recipients = []string{ + if len(c.Vargs.Recipients) == 0 { + c.Vargs.Recipients = []string{ c.Build.Email, } } var auth smtp.Auth - var addr = net.JoinHostPort(c.Email.Host, strconv.Itoa(c.Email.Port)) + + var addr = net.JoinHostPort( + c.Vargs.Host, + strconv.Itoa(c.Vargs.Port)) // setup the authentication to the smtp server // if the username and password are provided. - if len(c.Email.Username) > 0 { - auth = smtp.PlainAuth("", c.Email.Username, c.Email.Password, c.Email.Host) + if len(c.Vargs.Username) > 0 { + auth = smtp.PlainAuth( + "", + c.Vargs.Username, + c.Vargs.Password, + c.Vargs.Host) } // genereate the raw email message - var to = strings.Join(c.Email.Recipients, ",") - var raw = fmt.Sprintf(rawMessage, c.Email.From, to, subject, body) + var to = strings.Join( + c.Vargs.Recipients, + ",") - return smtp.SendMail(addr, auth, c.Email.From, c.Email.Recipients, []byte(raw)) + var raw = fmt.Sprintf( + rawMessage, + c.Vargs.From, + to, + subject, + body) + + return smtp.SendMail( + addr, + auth, + c.Vargs.From, + c.Vargs.Recipients, + []byte(raw)) } diff --git a/template.go b/template.go index d71e997..c492a53 100644 --- a/template.go +++ b/template.go @@ -18,10 +18,10 @@ var successTemplate = template.Must(template.New("_").Parse(` Build was Successful (see results)

-

Repository : {{.Repo.Owner}}/{{.Repo.Name}}

-

Commit : {{.Build.Commit}}

-

Author : {{.Build.Author}}

-

Branch : {{.Build.Branch}}

+

Repository: {{.Repo.Owner}}/{{.Repo.Name}}

+

Commit: {{.Build.Commit}}

+

Author: {{.Build.Author}}

+

Branch: {{.Build.Branch}}

Message:

{{ .Build.Message }}

`)) @@ -32,10 +32,10 @@ var failureTemplate = template.Must(template.New("_").Parse(` Build Failed (see results)

-

Repository : {{.Repo.Owner}}/{{.Repo.Name}}

-

Commit : {{.Build.Commit}}

-

Author : {{.Build.Author}}

-

Branch : {{.Build.Branch}}

+

Repository: {{.Repo.Owner}}/{{.Repo.Name}}

+

Commit: {{.Build.Commit}}

+

Author: {{.Build.Author}}

+

Branch: {{.Build.Branch}}

Message:

{{ .Build.Message }}

`)) diff --git a/types.go b/types.go new file mode 100644 index 0000000..fa39fda --- /dev/null +++ b/types.go @@ -0,0 +1,21 @@ +package main + +import ( + "github.com/drone/drone-go/drone" +) + +type Params struct { + Recipients []string `json:"recipients"` + Host string `json:"host"` + Port int `json:"port"` + From string `json:"from"` + Username string `json:"username"` + Password string `json:"password"` +} + +type Context struct { + System drone.System + Repo drone.Repo + Build drone.Build + Vargs Params +}