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
This commit is contained in:
Rafael Bodill 2017-09-11 07:34:47 +03:00 committed by Bo-Yi Wu
parent 709c4ce6a8
commit 6f651a91d2
5 changed files with 79 additions and 49 deletions

30
DOCS.md
View File

@ -207,6 +207,24 @@ repo.owner
repo.name repo.name
: repository 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
: build status type enumeration, either `success` or `failure` : build status type enumeration, either `success` or `failure`
@ -216,21 +234,9 @@ build.event
build.number build.number
: build number : build number
build.commit
: git sha for current commit
build.branch
: git branch for current commit
build.tag build.tag
: git tag for current commit : git tag for current commit
build.ref
: git ref for current commit
build.author
: git author for current commit
build.link build.link
: link the the build results in drone : link the the build results in drone

View File

@ -81,6 +81,7 @@ docker run --rm \
-e DRONE_REPO_NAME=go-hello \ -e DRONE_REPO_NAME=go-hello \
-e DRONE_COMMIT_SHA=e5e82b5eb3737205c25955dcc3dcacc839b7be52 \ -e DRONE_COMMIT_SHA=e5e82b5eb3737205c25955dcc3dcacc839b7be52 \
-e DRONE_COMMIT_BRANCH=master \ -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=appleboy \
-e DRONE_COMMIT_AUTHOR_EMAIL=appleboy@gmail.com \ -e DRONE_COMMIT_AUTHOR_EMAIL=appleboy@gmail.com \
-e DRONE_BUILD_NUMBER=1 \ -e DRONE_BUILD_NUMBER=1 \

18
main.go
View File

@ -115,6 +115,11 @@ func main() {
Usage: "git commit branch", Usage: "git commit branch",
EnvVar: "DRONE_COMMIT_BRANCH", EnvVar: "DRONE_COMMIT_BRANCH",
}, },
cli.StringFlag{
Name: "commit.link",
Usage: "git commit link",
EnvVar: "DRONE_COMMIT_LINK",
},
cli.StringFlag{ cli.StringFlag{
Name: "commit.author", Name: "commit.author",
Usage: "git author name", Usage: "git author name",
@ -191,16 +196,19 @@ func run(c *cli.Context) error {
Owner: c.String("repo.owner"), Owner: c.String("repo.owner"),
Name: c.String("repo.name"), 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{ Build: Build{
Tag: c.String("build.tag"), Tag: c.String("build.tag"),
Number: c.Int("build.number"), Number: c.Int("build.number"),
Event: c.String("build.event"), Event: c.String("build.event"),
Status: c.String("build.status"), 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"), Link: c.String("build.link"),
Started: c.Float64("job.started"), Started: c.Float64("job.started"),
Finished: c.Float64("job.finished"), Finished: c.Float64("job.finished"),

View File

@ -19,16 +19,21 @@ type (
Name string Name string
} }
// Commit information.
Commit struct {
Sha string
Branch string
Link string
Author string
Email string
Message string
}
// Build information. // Build information.
Build struct { Build struct {
Tag string Tag string
Event string Event string
Number int Number int
Commit string
Message string
Branch string
Author string
Email string
Status string Status string
Link string Link string
Started float64 Started float64
@ -58,6 +63,7 @@ type (
// Plugin values. // Plugin values.
Plugin struct { Plugin struct {
Repo Repo Repo Repo
Commit Commit
Build Build Build Build
Config Config Config Config
} }
@ -212,7 +218,7 @@ func (p Plugin) Exec() error {
if len(p.Config.Message) > 0 { if len(p.Config.Message) > 0 {
message = p.Config.Message message = p.Config.Message
} else { } else {
message = p.Message(p.Repo, p.Build) message = p.Message(p.Repo, p.Commit, p.Build)
} }
bot, err := tgbotapi.NewBotAPI(p.Config.Token) bot, err := tgbotapi.NewBotAPI(p.Config.Token)
@ -225,7 +231,7 @@ func (p Plugin) Exec() error {
bot.Debug = p.Config.Debug 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)) photos := fileExist(trimElement(p.Config.Photo))
documents := fileExist(trimElement(p.Config.Document)) documents := fileExist(trimElement(p.Config.Document))
stickers := fileExist(trimElement(p.Config.Sticker)) stickers := fileExist(trimElement(p.Config.Sticker))
@ -240,10 +246,13 @@ func (p Plugin) Exec() error {
if p.Config.Format == "markdown" { if p.Config.Format == "markdown" {
message = escapeMarkdown(message) message = escapeMarkdown(message)
p.Build.Message = escapeMarkdownOne(p.Build.Message) p.Commit.Message = escapeMarkdownOne(p.Commit.Message)
p.Build.Branch = escapeMarkdownOne(p.Build.Branch) p.Commit.Branch = escapeMarkdownOne(p.Commit.Branch)
p.Build.Author = escapeMarkdownOne(p.Build.Author) p.Commit.Link = escapeMarkdownOne(p.Commit.Link)
p.Build.Email = escapeMarkdownOne(p.Build.Email) 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.Link = escapeMarkdownOne(p.Build.Link)
p.Build.PR = escapeMarkdownOne(p.Build.PR) 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. // 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", return []string{fmt.Sprintf("[%s] <%s> (%s)『%s』by %s",
build.Status, build.Status,
build.Link, build.Link,
build.Branch, commit.Branch,
build.Message, commit.Message,
build.Author, commit.Author,
)} )}
} }

View File

@ -33,18 +33,20 @@ func TestDefaultMessageFormat(t *testing.T) {
Name: "go-hello", Name: "go-hello",
Owner: "appleboy", Owner: "appleboy",
}, },
Build: Build{ Commit: Commit{
Number: 101, Sha: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2",
Status: "success",
Link: "https://github.com/appleboy/go-hello",
Author: "Bo-Yi Wu", Author: "Bo-Yi Wu",
Branch: "master", Branch: "master",
Message: "update travis", 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] <https://github.com/appleboy/go-hello> (master)『update travis』by Bo-Yi Wu"}, message) assert.Equal(t, []string{"[success] <https://github.com/appleboy/go-hello> (master)『update travis』by Bo-Yi Wu"}, message)
} }
@ -55,17 +57,19 @@ func TestSendMessage(t *testing.T) {
Name: "go-hello", Name: "go-hello",
Owner: "appleboy", Owner: "appleboy",
}, },
Build: Build{ Commit: Commit{
Tag: "1.0.0", Sha: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2",
Number: 101,
Status: "success",
Link: "https://github.com/appleboy/go-hello",
Author: "Bo-Yi Wu", Author: "Bo-Yi Wu",
Branch: "master", Branch: "master",
Message: "update travis by drone plugin", Message: "update travis by drone plugin",
Commit: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2",
Email: "test@gmail.com", Email: "test@gmail.com",
}, },
Build: Build{
Tag: "1.0.0",
Number: 101,
Status: "success",
Link: "https://github.com/appleboy/go-hello",
},
Config: Config{ Config: Config{
Token: os.Getenv("TELEGRAM_TOKEN"), Token: os.Getenv("TELEGRAM_TOKEN"),
@ -103,14 +107,16 @@ func TestBotError(t *testing.T) {
Name: "go-hello", Name: "go-hello",
Owner: "appleboy", Owner: "appleboy",
}, },
Build: Build{ Commit: Commit{
Number: 101, Sha: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2",
Status: "success",
Link: "https://github.com/appleboy/go-hello",
Author: "Bo-Yi Wu", Author: "Bo-Yi Wu",
Branch: "master", Branch: "master",
Message: "update travis by drone plugin", Message: "update travis by drone plugin",
Commit: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2", },
Build: Build{
Number: 101,
Status: "success",
Link: "https://github.com/appleboy/go-hello",
}, },
Config: Config{ Config: Config{