From 0888ac7f7ea18c35d46bab9fd81796f9dcc8f390 Mon Sep 17 00:00:00 2001 From: Michael de Wit Date: Tue, 17 Jan 2017 10:58:35 +0100 Subject: [PATCH] Add all Drone env variables to template context --- Makefile | 2 +- defaults.go | 10 +-- main.go | 218 ++++++++++++++++++++++++++++++++++++++++++++-------- plugin.go | 114 ++++++++++++++++++++------- 4 files changed, 279 insertions(+), 65 deletions(-) diff --git a/Makefile b/Makefile index 57f19f1..7f90167 100644 --- a/Makefile +++ b/Makefile @@ -9,4 +9,4 @@ build: docker build -t drillster/drone-email:latest . publish: - bash publish.sh \ No newline at end of file + bash publish.sh \ No newline at end of file diff --git a/defaults.go b/defaults.go index d1efea2..516ee37 100644 --- a/defaults.go +++ b/defaults.go @@ -11,7 +11,7 @@ const ( // DefaultSubject is the default subject template to use for the email const DefaultSubject = ` -[{{ build.status }}] {{ repo.owner }}/{{ repo.name }} ({{ build.branch }} - {{ truncate build.commit 8 }}) +[{{ build.status }}] {{ repo.owner }}/{{ repo.name }} ({{ commit.branch }} - {{ truncate commit.sha 8 }}) ` // DefaultTemplate is the default body template to use for the email @@ -219,7 +219,7 @@ const DefaultTemplate = ` Author: - {{ build.author.name }} ({{ build.author.email }}) + {{ commit.author.name }} ({{ commit.author.email }}) @@ -227,7 +227,7 @@ const DefaultTemplate = ` Branch: - {{ build.branch }} + {{ commit.branch }} @@ -235,7 +235,7 @@ const DefaultTemplate = ` Commit: - {{ truncate build.commit 8 }} + {{ truncate commit.sha 8 }} @@ -251,7 +251,7 @@ const DefaultTemplate = `
- {{ build.message }} + {{ commit.message }}
diff --git a/main.go b/main.go index b59b4d9..e2d7357 100644 --- a/main.go +++ b/main.go @@ -76,6 +76,12 @@ func main() { }, // Drone environment + // Repo + cli.StringFlag{ + Name: "repo.fullName", + Usage: "repository full name", + EnvVar: "DRONE_REPO", + }, cli.StringFlag{ Name: "repo.owner", Usage: "repository owner", @@ -86,6 +92,47 @@ func main() { Usage: "repository name", EnvVar: "DRONE_REPO_NAME", }, + cli.StringFlag{ + Name: "repo.scm", + Value: "git", + Usage: "respository scm", + EnvVar: "DRONE_REPO_SCM", + }, + cli.StringFlag{ + Name: "repo.link", + Usage: "repository link", + EnvVar: "DRONE_REPO_LINK", + }, + cli.StringFlag{ + Name: "repo.avatar", + Usage: "repository avatar", + EnvVar: "DRONE_REPO_AVATAR", + }, + cli.StringFlag{ + Name: "repo.branch", + Value: "master", + Usage: "repository default branch", + EnvVar: "DRONE_REPO_BRANCH", + }, + cli.BoolFlag{ + Name: "repo.private", + Usage: "repository is private", + EnvVar: "DRONE_REPO_PRIVATE", + }, + cli.BoolFlag{ + Name: "repo.trusted", + Usage: "repository is trusted", + EnvVar: "DRONE_REPO_TRUSTED", + }, + + // Remote + cli.StringFlag{ + Name: "remote.url", + Usage: "repository clone url", + EnvVar: "DRONE_REMOTE_URL", + }, + + // Commit cli.StringFlag{ Name: "commit.sha", Usage: "git commit sha", @@ -103,6 +150,16 @@ func main() { Usage: "git commit branch", EnvVar: "DRONE_COMMIT_BRANCH", }, + cli.StringFlag{ + Name: "commit.link", + Usage: "commit link", + EnvVar: "DRONE_COMMIT_LINK", + }, + cli.StringFlag{ + Name: "commit.message", + Usage: "git commit message", + EnvVar: "DRONE_COMMIT_MESSAGE", + }, cli.StringFlag{ Name: "commit.author.name", Usage: "git author name", @@ -118,10 +175,12 @@ func main() { Usage: "git author avatar", EnvVar: "DRONE_COMMIT_AUTHOR_AVATAR", }, - cli.StringFlag{ - Name: "commit.message", - Usage: "git commit message", - EnvVar: "DRONE_COMMIT_MESSAGE", + + // Build + cli.IntFlag{ + Name: "build.number", + Usage: "build number", + EnvVar: "DRONE_BUILD_NUMBER", }, cli.StringFlag{ Name: "build.event", @@ -129,11 +188,6 @@ func main() { Usage: "build event", EnvVar: "DRONE_BUILD_EVENT", }, - cli.IntFlag{ - Name: "build.number", - Usage: "build number", - EnvVar: "DRONE_BUILD_NUMBER", - }, cli.StringFlag{ Name: "build.status", Usage: "build status", @@ -145,26 +199,98 @@ func main() { Usage: "build link", EnvVar: "DRONE_BUILD_LINK", }, + cli.Int64Flag{ + Name: "build.created", + Usage: "build created", + EnvVar: "DRONE_BUILD_CREATED", + }, cli.Int64Flag{ Name: "build.started", Usage: "build started", EnvVar: "DRONE_BUILD_STARTED", }, cli.Int64Flag{ - Name: "build.created", - Usage: "build created", - EnvVar: "DRONE_BUILD_CREATED", + Name: "build.finished", + Usage: "build finished", + EnvVar: "DRONE_BUILD_FINISHED", + }, + + // Prev + cli.StringFlag{ + Name: "prev.build.status", + Usage: "prior build status", + EnvVar: "DRONE_PREV_BUILD_STATUS", + }, + cli.IntFlag{ + Name: "prev.build.number", + Usage: "prior build number", + EnvVar: "DRONE_PREV_BUILD_NUMBER", }, cli.StringFlag{ - Name: "build.tag", - Usage: "build tag", - EnvVar: "DRONE_TAG", + Name: "prev.commit.sha", + Usage: "prior commit sha", + EnvVar: "DRONE_PREV_COMMIT_SHA", + }, + + // Job + cli.IntFlag{ + Name: "job.number", + Usage: "job number", + EnvVar: "DRONE_JOB_NUMBER", + }, + cli.StringFlag{ + Name: "job.status", + Usage: "job status", + EnvVar: "DRONE_JOB_STATUS", + }, + cli.IntFlag{ + Name: "job.exitCode", + Usage: "job exit code", + EnvVar: "DRONE_JOB_EXIT_CODE", }, cli.Int64Flag{ Name: "job.started", Usage: "job started", EnvVar: "DRONE_JOB_STARTED", }, + cli.Int64Flag{ + Name: "job.finished", + Usage: "job finished", + EnvVar: "DRONE_JOB_FINISHED", + }, + + // Yaml + cli.BoolFlag{ + Name: "yaml.signed", + Usage: "yaml is signed", + EnvVar: "DRONE_YAML_SIGNED", + }, + cli.BoolFlag{ + Name: "yaml.verified", + Usage: "yaml is signed and verified", + EnvVar: "DRONE_YAML_VERIFIED", + }, + + // Tag + cli.StringFlag{ + Name: "tag", + Usage: "git tag", + EnvVar: "DRONE_TAG", + }, + + // PullRequest + cli.IntFlag{ + Name: "pullRequest", + Usage: "pull request number", + EnvVar: "DRONE_PULL_REQUEST", + }, + + // DeployTo + cli.StringFlag{ + Name: "deployTo", + Usage: "deployment target", + EnvVar: "DRONE_DEPLOY_TO", + }, } if err := app.Run(os.Args); err != nil { @@ -176,30 +302,62 @@ func main() { func run(c *cli.Context) error { plugin := Plugin{ Repo: Repo{ - Owner: c.String("repo.owner"), - Name: c.String("repo.name"), + FullName: c.String("repo.fullName"), + Owner: c.String("repo.owner"), + Name: c.String("repo.name"), + SCM: c.String("repo.scm"), + Link: c.String("repo.link"), + Avatar: c.String("repo.avatar"), + Branch: c.String("repo.branch"), + Private: c.Bool("repo.private"), + Trusted: c.Bool("repo.trusted"), }, - Build: Build{ - Tag: c.String("build.tag"), - Number: c.Int("build.number"), - Event: c.String("build.event"), - Status: c.String("build.status"), - Commit: c.String("commit.sha"), - Ref: c.String("commit.ref"), - Branch: c.String("commit.branch"), + Remote: Remote{ + URL: c.String("remote.url"), + }, + Commit: Commit{ + Sha: c.String("commit.sha"), + Ref: c.String("commit.ref"), + Branch: c.String("commit.branch"), + Link: c.String("commit.link"), + Message: c.String("commit.message"), Author: Author{ Name: c.String("commit.author.name"), Email: c.String("commit.author.email"), Avatar: c.String("commit.author.avatar"), }, - Message: c.String("commit.message"), - Link: c.String("build.link"), - Started: c.Int64("build.started"), - Created: c.Int64("build.created"), + }, + Build: Build{ + Number: c.Int("build.number"), + Event: c.String("build.event"), + Status: c.String("build.status"), + Link: c.String("build.link"), + Created: c.Int64("build.created"), + Started: c.Int64("build.started"), + Finished: c.Int64("build.finished"), + }, + Prev: Prev{ + Build: PrevBuild{ + Status: c.String("prev.build.status"), + Number: c.Int("prev.build.number"), + }, + Commit: PrevCommit{ + Sha: c.String("prev.commit.sha"), + }, }, Job: Job{ - Started: c.Int64("job.started"), + Status: c.String("job.status"), + ExitCode: c.Int("job.exitCode"), + Started: c.Int64("job.started"), + Finished: c.Int64("job.finished"), }, + Yaml: Yaml{ + Signed: c.Bool("yaml.signed"), + Verified: c.Bool("yaml.verified"), + }, + Tag: c.String("tag"), + PullRequest: c.Int("pullRequest"), + DeployTo: c.String("deployTo"), Config: Config{ From: c.String("from"), Host: c.String("host"), diff --git a/plugin.go b/plugin.go index 6aa9c31..0ec19ca 100644 --- a/plugin.go +++ b/plugin.go @@ -11,8 +11,19 @@ import ( type ( Repo struct { - Owner string - Name string + FullName string + Owner string + Name string + SCM string + Link string + Avatar string + Branch string + Private bool + Trusted bool + } + + Remote struct { + URL string } Author struct { @@ -21,19 +32,49 @@ type ( Avatar string } - Build struct { - Tag string - Event string - Number int - Commit string + Commit struct { + Sha string Ref string Branch string - Author Author - Message string - Status string Link string - Started int64 - Created int64 + Message string + Author Author + } + + Build struct { + Number int + Event string + Status string + Link string + Created int64 + Started int64 + Finished int64 + } + + PrevBuild struct { + Status string + Number int + } + + PrevCommit struct { + Sha string + } + + Prev struct { + Build PrevBuild + Commit PrevCommit + } + + Job struct { + Status string + ExitCode int + Started int64 + Finished int64 + } + + Yaml struct { + Signed bool + Verified bool } Config struct { @@ -49,15 +90,18 @@ type ( Body string } - Job struct { - Started int64 - } - Plugin struct { - Repo Repo - Build Build - Config Config - Job Job + Repo Repo + Remote Remote + Commit Commit + Build Build + Prev Prev + Job Job + Yaml Yaml + Tag string + PullRequest int + DeployTo string + Config Config } ) @@ -66,7 +110,7 @@ func (p Plugin) Exec() error { var dialer *gomail.Dialer if !p.Config.RecipientsOnly { - p.Config.Recipients = append(p.Config.Recipients, p.Build.Author.Email) + p.Config.Recipients = append(p.Config.Recipients, p.Commit.Author.Email) } if p.Config.Username == "" && p.Config.Password == "" { @@ -85,16 +129,28 @@ func (p Plugin) Exec() error { } type Context struct { - Job Job - Repo Repo - Build Build - Config Config + Repo Repo + Remote Remote + Commit Commit + Build Build + Prev Prev + Job Job + Yaml Yaml + Tag string + PullRequest int + DeployTo string } ctx := Context{ - Job: p.Job, - Repo: p.Repo, - Build: p.Build, - Config: p.Config, + Repo: p.Repo, + Remote: p.Remote, + Commit: p.Commit, + Build: p.Build, + Prev: p.Prev, + Job: p.Job, + Yaml: p.Yaml, + Tag: p.Tag, + PullRequest: p.PullRequest, + DeployTo: p.DeployTo, } // Render body in HTML and plain text