From 6f651a91d27e5af872c5bf9b584ee6259bfd71b6 Mon Sep 17 00:00:00 2001 From: Rafael Bodill Date: Mon, 11 Sep 2017 07:34:47 +0300 Subject: [PATCH] Decouple 'commit' object and add commit.link (#51) * Decouple commit out from build and add commit.link * Fix tests for Commit decouple * Update documentation for Commit decouple --- DOCS.md | 30 ++++++++++++++++++------------ README.md | 1 + main.go | 18 +++++++++++++----- plugin.go | 39 ++++++++++++++++++++++++--------------- plugin_test.go | 40 +++++++++++++++++++++++----------------- 5 files changed, 79 insertions(+), 49 deletions(-) diff --git a/DOCS.md b/DOCS.md index e10639f..0dc73be 100644 --- a/DOCS.md +++ b/DOCS.md @@ -207,6 +207,24 @@ repo.owner repo.name : repository name +commit.sha +: git sha for current commit + +commit.branch +: git branch for current commit + +commit.link +: git commit link in remote + +commit.author +: git author for current commit + +commit.email +: git author email for current commit + +commit.message +: git current commit message + build.status : build status type enumeration, either `success` or `failure` @@ -216,21 +234,9 @@ build.event build.number : build number -build.commit -: git sha for current commit - -build.branch -: git branch for current commit - build.tag : git tag for current commit -build.ref -: git ref for current commit - -build.author -: git author for current commit - build.link : link the the build results in drone diff --git a/README.md b/README.md index b1ffb3c..5f53588 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,7 @@ docker run --rm \ -e DRONE_REPO_NAME=go-hello \ -e DRONE_COMMIT_SHA=e5e82b5eb3737205c25955dcc3dcacc839b7be52 \ -e DRONE_COMMIT_BRANCH=master \ + -e DRONE_COMMIT_LINK=https://github.com/appleboy/go-hello/compare/master... \ -e DRONE_COMMIT_AUTHOR=appleboy \ -e DRONE_COMMIT_AUTHOR_EMAIL=appleboy@gmail.com \ -e DRONE_BUILD_NUMBER=1 \ diff --git a/main.go b/main.go index 59a962f..5427c15 100644 --- a/main.go +++ b/main.go @@ -115,6 +115,11 @@ func main() { Usage: "git commit branch", EnvVar: "DRONE_COMMIT_BRANCH", }, + cli.StringFlag{ + Name: "commit.link", + Usage: "git commit link", + EnvVar: "DRONE_COMMIT_LINK", + }, cli.StringFlag{ Name: "commit.author", Usage: "git author name", @@ -191,16 +196,19 @@ func run(c *cli.Context) error { Owner: c.String("repo.owner"), Name: c.String("repo.name"), }, + Commit: Commit{ + Sha: c.String("commit.sha"), + Branch: c.String("commit.branch"), + Link: c.String("commit.link"), + Author: c.String("commit.author"), + Email: c.String("commit.author.email"), + Message: c.String("commit.message"), + }, 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"), - Branch: c.String("commit.branch"), - Author: c.String("commit.author"), - Email: c.String("commit.author.email"), - Message: c.String("commit.message"), Link: c.String("build.link"), Started: c.Float64("job.started"), Finished: c.Float64("job.finished"), diff --git a/plugin.go b/plugin.go index ab0aad3..a160f5d 100644 --- a/plugin.go +++ b/plugin.go @@ -19,16 +19,21 @@ type ( Name string } + // Commit information. + Commit struct { + Sha string + Branch string + Link string + Author string + Email string + Message string + } + // Build information. Build struct { Tag string Event string Number int - Commit string - Message string - Branch string - Author string - Email string Status string Link string Started float64 @@ -58,6 +63,7 @@ type ( // Plugin values. Plugin struct { Repo Repo + Commit Commit Build Build Config Config } @@ -212,7 +218,7 @@ func (p Plugin) Exec() error { if len(p.Config.Message) > 0 { message = p.Config.Message } else { - message = p.Message(p.Repo, p.Build) + message = p.Message(p.Repo, p.Commit, p.Build) } bot, err := tgbotapi.NewBotAPI(p.Config.Token) @@ -225,7 +231,7 @@ func (p Plugin) Exec() error { bot.Debug = p.Config.Debug - ids := parseTo(p.Config.To, p.Build.Email, p.Config.MatchEmail) + ids := parseTo(p.Config.To, p.Commit.Email, p.Config.MatchEmail) photos := fileExist(trimElement(p.Config.Photo)) documents := fileExist(trimElement(p.Config.Document)) stickers := fileExist(trimElement(p.Config.Sticker)) @@ -240,10 +246,13 @@ func (p Plugin) Exec() error { if p.Config.Format == "markdown" { message = escapeMarkdown(message) - p.Build.Message = escapeMarkdownOne(p.Build.Message) - p.Build.Branch = escapeMarkdownOne(p.Build.Branch) - p.Build.Author = escapeMarkdownOne(p.Build.Author) - p.Build.Email = escapeMarkdownOne(p.Build.Email) + p.Commit.Message = escapeMarkdownOne(p.Commit.Message) + p.Commit.Branch = escapeMarkdownOne(p.Commit.Branch) + p.Commit.Link = escapeMarkdownOne(p.Commit.Link) + p.Commit.Author = escapeMarkdownOne(p.Commit.Author) + p.Commit.Email = escapeMarkdownOne(p.Commit.Email) + + p.Build.Tag = escapeMarkdownOne(p.Build.Tag) p.Build.Link = escapeMarkdownOne(p.Build.Link) p.Build.PR = escapeMarkdownOne(p.Build.PR) @@ -333,12 +342,12 @@ func (p Plugin) Send(bot *tgbotapi.BotAPI, msg tgbotapi.Chattable) { } // Message is plugin default message. -func (p Plugin) Message(repo Repo, build Build) []string { +func (p Plugin) Message(repo Repo, commit Commit, build Build) []string { return []string{fmt.Sprintf("[%s] <%s> (%s)『%s』by %s", build.Status, build.Link, - build.Branch, - build.Message, - build.Author, + commit.Branch, + commit.Message, + commit.Author, )} } diff --git a/plugin_test.go b/plugin_test.go index bf76611..5e6f4a3 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -33,18 +33,20 @@ func TestDefaultMessageFormat(t *testing.T) { Name: "go-hello", Owner: "appleboy", }, - Build: Build{ - Number: 101, - Status: "success", - Link: "https://github.com/appleboy/go-hello", + Commit: Commit{ + Sha: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2", Author: "Bo-Yi Wu", Branch: "master", Message: "update travis", - Commit: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2", + }, + Build: Build{ + Number: 101, + Status: "success", + Link: "https://github.com/appleboy/go-hello", }, } - message := plugin.Message(plugin.Repo, plugin.Build) + message := plugin.Message(plugin.Repo, plugin.Commit, plugin.Build) assert.Equal(t, []string{"[success] (master)『update travis』by Bo-Yi Wu"}, message) } @@ -55,17 +57,19 @@ func TestSendMessage(t *testing.T) { Name: "go-hello", Owner: "appleboy", }, - Build: Build{ - Tag: "1.0.0", - Number: 101, - Status: "success", - Link: "https://github.com/appleboy/go-hello", + Commit: Commit{ + Sha: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2", Author: "Bo-Yi Wu", Branch: "master", Message: "update travis by drone plugin", - Commit: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2", Email: "test@gmail.com", }, + Build: Build{ + Tag: "1.0.0", + Number: 101, + Status: "success", + Link: "https://github.com/appleboy/go-hello", + }, Config: Config{ Token: os.Getenv("TELEGRAM_TOKEN"), @@ -103,14 +107,16 @@ func TestBotError(t *testing.T) { Name: "go-hello", Owner: "appleboy", }, - Build: Build{ - Number: 101, - Status: "success", - Link: "https://github.com/appleboy/go-hello", + Commit: Commit{ + Sha: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2", Author: "Bo-Yi Wu", Branch: "master", Message: "update travis by drone plugin", - Commit: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2", + }, + Build: Build{ + Number: 101, + Status: "success", + Link: "https://github.com/appleboy/go-hello", }, Config: Config{