replace split with strings func.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-09-19 22:19:12 +08:00
parent 240f2a8ec5
commit 2bd6884dbd
2 changed files with 33 additions and 11 deletions

View file

@ -102,10 +102,13 @@ func convertRepoLite(from *internal.Repo) *model.RepoLite {
// convertPushHook is a helper function used to convert a Bitbucket push
// hook to the Drone build struct holding commit information.
func convertPushHook(hook *internal.PostHook, baseURL string) *model.Build {
//get the ref parts to see if it's a tags or heads
refParts := strings.Split(hook.RefChanges[0].RefID, "/")
name := refParts[2]
commitType := refParts[1]
branch := strings.TrimPrefix(
strings.TrimPrefix(
hook.RefChanges[0].RefID,
"refs/heads/",
),
"refs/tags/",
)
//Ensuring the author label is not longer then 40 for the label of the commit author (default size in the db)
authorLabel := hook.Changesets.Values[0].ToCommit.Author.Name
@ -115,7 +118,7 @@ func convertPushHook(hook *internal.PostHook, baseURL string) *model.Build {
build := &model.Build{
Commit: hook.RefChanges[0].ToHash, // TODO check for index value
Branch: name,
Branch: branch,
Message: hook.Changesets.Values[0].ToCommit.Message, //TODO check for index Values
Avatar: avatarLink(hook.Changesets.Values[0].ToCommit.Author.EmailAddress),
Author: authorLabel,
@ -124,10 +127,9 @@ func convertPushHook(hook *internal.PostHook, baseURL string) *model.Build {
Ref: hook.RefChanges[0].RefID, // TODO check for index Values
Link: fmt.Sprintf("%s/projects/%s/repos/%s/commits/%s", baseURL, hook.Repository.Project.Key, hook.Repository.Slug, hook.RefChanges[0].ToHash),
}
switch commitType {
case "tags":
if strings.HasPrefix(hook.RefChanges[0].RefID, "refs/tags/") {
build.Event = model.EventTag
default:
} else {
build.Event = model.EventPush
}

View file

@ -69,11 +69,31 @@ func Test_helper(t *testing.T) {
g.Assert(result.Token).Equal("foo")
})
g.It("branch should be empty", func() {
change := internal.PostHook{}
change.RefChanges = append(change.RefChanges, internal.RefChange{
RefID: "refs/heads/",
ToHash: "73f9c44d",
})
value := internal.Value{}
value.ToCommit.Author.Name = "John Doe, Appleboy, Mary, Janet E. Dawson and Ann S. Palmer"
value.ToCommit.Author.EmailAddress = "huh@huh.com"
value.ToCommit.Message = "message"
change.Changesets.Values = append(change.Changesets.Values, value)
change.Repository.Project.Key = "octocat"
change.Repository.Slug = "hello-world"
build := convertPushHook(&change, "http://base.com")
g.Assert(build.Branch).Equal("")
})
g.It("should convert push hook to build", func() {
change := internal.PostHook{}
change.RefChanges = append(change.RefChanges, internal.RefChange{
RefID: "refs/heads/master",
RefID: "refs/heads/release/some-feature",
ToHash: "73f9c44d",
})
@ -93,9 +113,9 @@ func Test_helper(t *testing.T) {
g.Assert(build.Author).Equal("John Doe, Appleboy, Mary, Janet E. Da...")
g.Assert(build.Avatar).Equal(avatarLink("huh@huh.com"))
g.Assert(build.Commit).Equal("73f9c44d")
g.Assert(build.Branch).Equal("master")
g.Assert(build.Branch).Equal("release/some-feature")
g.Assert(build.Link).Equal("http://base.com/projects/octocat/repos/hello-world/commits/73f9c44d")
g.Assert(build.Ref).Equal("refs/heads/master")
g.Assert(build.Ref).Equal("refs/heads/release/some-feature")
g.Assert(build.Message).Equal("message")
})