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

View File

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

18
main.go
View File

@ -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"),

View File

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

View File

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