Merge pull request #2 from drone-plugins/feature/unify

Some streamlining of the next plugin
This commit is contained in:
Jack Spirou 2015-12-13 10:27:58 -06:00
commit 969221aae8
11 changed files with 184 additions and 86 deletions

View file

@ -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

1
.gitignore vendored
View file

@ -24,4 +24,3 @@ _testmain.go
*.prof
drone-email

20
DOCS.md
View file

@ -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:

View file

@ -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"]
ENTRYPOINT ["/bin/drone-email"]

View file

@ -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.

22
Makefile Normal file
View file

@ -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)'

View file

@ -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 <<EOF
{
"repo" : {
@ -34,7 +35,56 @@ This plugin is responsible for sending build notifications via email:
"port": 587,
"username": "",
"password": "",
"recipients": ["octocat@github.com"]
"recipients": [
"octocat@github.com"
]
}
}
EOF
```
## Docker
Build the Docker container using `make`:
```sh
make deps build
docker build --rm=true -t plugins/drone-email .
```
### Example
```sh
docker run -i plugins/drone-email <<EOF
{
"repo" : {
"owner": "foo",
"name": "bar",
"full_name": "foo/bar"
},
"system": {
"link_url": "http://drone.mycompany.com"
},
"build" : {
"number": 22,
"status": "success",
"started_at": 1421029603,
"finished_at": 1421029813,
"commit": "64908ed2414b771554fda6508dd56a0c43766831",
"branch": "master",
"message": "Update the Readme",
"author": "johnsmith",
"author_email": "john.smith@gmail.com"
},
"vargs": {
"from": "noreply@foo.com",
"host": "smtp.mailgun.org",
"port": 587,
"username": "",
"password": "",
"recipients": [
"octocat@github.com"
]
}
}
EOF

58
main.go
View file

@ -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
}
}

View file

@ -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))
}

View file

@ -18,10 +18,10 @@ var successTemplate = template.Must(template.New("_").Parse(`
<b>Build was Successful</b>
(<a href="{{.System.Link}}/{{.Repo.Owner}}/{{.Repo.Name}}/{{.Build.Number}}">see results</a>)
</p>
<p>Repository : {{.Repo.Owner}}/{{.Repo.Name}}</p>
<p>Commit : {{.Build.Commit}}</p>
<p>Author : {{.Build.Author}}</p>
<p>Branch : {{.Build.Branch}}</p>
<p>Repository: {{.Repo.Owner}}/{{.Repo.Name}}</p>
<p>Commit: {{.Build.Commit}}</p>
<p>Author: {{.Build.Author}}</p>
<p>Branch: {{.Build.Branch}}</p>
<p>Message:</p>
<p>{{ .Build.Message }}</p>
`))
@ -32,10 +32,10 @@ var failureTemplate = template.Must(template.New("_").Parse(`
<b>Build Failed</b>
(<a href="{{.System.Link}}/{{.Repo.Owner}}/{{.Repo.Name}}/{{.Build.Number}}">see results</a>)
</p>
<p>Repository : {{.Repo.Owner}}/{{.Repo.Name}}</p>
<p>Commit : {{.Build.Commit}}</p>
<p>Author : {{.Build.Author}}</p>
<p>Branch : {{.Build.Branch}}</p>
<p>Repository: {{.Repo.Owner}}/{{.Repo.Name}}</p>
<p>Commit: {{.Build.Commit}}</p>
<p>Author: {{.Build.Author}}</p>
<p>Branch: {{.Build.Branch}}</p>
<p>Message:</p>
<p>{{ .Build.Message }}</p>
`))

21
types.go Normal file
View file

@ -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
}