From 2bd6884dbdb0b071b14a256666e9cab8ddbfd096 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Mon, 19 Sep 2016 22:19:12 +0800 Subject: [PATCH] replace split with strings func. Signed-off-by: Bo-Yi Wu --- remote/bitbucketserver/convert.go | 18 ++++++++++-------- remote/bitbucketserver/convert_test.go | 26 +++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/remote/bitbucketserver/convert.go b/remote/bitbucketserver/convert.go index d5e3b764..7d141622 100644 --- a/remote/bitbucketserver/convert.go +++ b/remote/bitbucketserver/convert.go @@ -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 } diff --git a/remote/bitbucketserver/convert_test.go b/remote/bitbucketserver/convert_test.go index d99d8710..9c0a4153 100644 --- a/remote/bitbucketserver/convert_test.go +++ b/remote/bitbucketserver/convert_test.go @@ -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") })