Skip to content

Commit

Permalink
Merge pull request harness#1795 from appleboy/feature/branch
Browse files Browse the repository at this point in the history
Fix branch name for bitbucket server.
  • Loading branch information
bradrydzewski authored Sep 23, 2016
2 parents 240f2a8 + 2bd6884 commit 524fba6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
18 changes: 10 additions & 8 deletions remote/bitbucketserver/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
}

Expand Down
26 changes: 23 additions & 3 deletions remote/bitbucketserver/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
})

Expand All @@ -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")
})

Expand Down

0 comments on commit 524fba6

Please sign in to comment.