diff --git a/server/forge/bitbucket/convert.go b/server/forge/bitbucket/convert.go index af573cbff5..a3b1376b71 100644 --- a/server/forge/bitbucket/convert.go +++ b/server/forge/bitbucket/convert.go @@ -168,22 +168,30 @@ func convertPullHook(from *internal.PullRequestHook) *model.Pipeline { event = model.EventPullClosed } - return &model.Pipeline{ + pipeline := &model.Pipeline{ Event: event, - Commit: from.PullRequest.Dest.Commit.Hash, - Ref: fmt.Sprintf("refs/heads/%s", from.PullRequest.Dest.Branch.Name), + Commit: from.PullRequest.Source.Commit.Hash, + Ref: fmt.Sprintf("refs/heads/%s", from.PullRequest.Source.Branch.Name), Refspec: fmt.Sprintf("%s:%s", from.PullRequest.Source.Branch.Name, from.PullRequest.Dest.Branch.Name, ), ForgeURL: from.PullRequest.Links.HTML.Href, - Branch: from.PullRequest.Dest.Branch.Name, + Branch: from.PullRequest.Source.Branch.Name, Message: from.PullRequest.Desc, Avatar: from.Actor.Links.Avatar.Href, Author: from.Actor.Login, Sender: from.Actor.Login, Timestamp: from.PullRequest.Updated.UTC().Unix(), } + + if from.PullRequest.State == stateClosed { + pipeline.Commit = from.PullRequest.MergeCommit.Hash + pipeline.Ref = fmt.Sprintf("refs/heads/%s", from.PullRequest.Dest.Branch.Name) + pipeline.Branch = from.PullRequest.Dest.Branch.Name + } + + return pipeline } // convertPushHook is a helper function used to convert a Bitbucket push diff --git a/server/forge/bitbucket/convert_test.go b/server/forge/bitbucket/convert_test.go index 2ce3e27f61..ee47a27705 100644 --- a/server/forge/bitbucket/convert_test.go +++ b/server/forge/bitbucket/convert_test.go @@ -129,6 +129,7 @@ func Test_helper(t *testing.T) { hook.PullRequest.Dest.Repo.Links.HTML.Href = "https://bitbucket.org/foo/bar" hook.PullRequest.Source.Branch.Name = "change" hook.PullRequest.Source.Repo.FullName = "baz/bar" + hook.PullRequest.Source.Commit.Hash = "c8411d7" hook.PullRequest.Links.HTML.Href = "https://bitbucket.org/foo/bar/pulls/5" hook.PullRequest.Desc = "updated README" hook.PullRequest.Updated = time.Now() @@ -137,10 +138,10 @@ func Test_helper(t *testing.T) { g.Assert(pipeline.Event).Equal(model.EventPull) g.Assert(pipeline.Author).Equal(hook.Actor.Login) g.Assert(pipeline.Avatar).Equal(hook.Actor.Links.Avatar.Href) - g.Assert(pipeline.Commit).Equal(hook.PullRequest.Dest.Commit.Hash) - g.Assert(pipeline.Branch).Equal(hook.PullRequest.Dest.Branch.Name) + g.Assert(pipeline.Commit).Equal(hook.PullRequest.Source.Commit.Hash) + g.Assert(pipeline.Branch).Equal(hook.PullRequest.Source.Branch.Name) g.Assert(pipeline.ForgeURL).Equal(hook.PullRequest.Links.HTML.Href) - g.Assert(pipeline.Ref).Equal("refs/heads/main") + g.Assert(pipeline.Ref).Equal("refs/heads/change") g.Assert(pipeline.Refspec).Equal("change:main") g.Assert(pipeline.Message).Equal(hook.PullRequest.Desc) g.Assert(pipeline.Timestamp).Equal(hook.PullRequest.Updated.Unix()) diff --git a/server/forge/bitbucket/internal/types.go b/server/forge/bitbucket/internal/types.go index a5dc4acb4e..bbb03ab1ca 100644 --- a/server/forge/bitbucket/internal/types.go +++ b/server/forge/bitbucket/internal/types.go @@ -164,6 +164,10 @@ type PullRequestHook struct { Created time.Time `json:"created_on"` Updated time.Time `json:"updated_on"` + MergeCommit struct { + Hash string `json:"hash"` + } `json:"merge_commit"` + Source struct { Repo Repo `json:"repository"` Commit struct { diff --git a/server/forge/bitbucket/parse_test.go b/server/forge/bitbucket/parse_test.go index aa9b811175..a5562fc39f 100644 --- a/server/forge/bitbucket/parse_test.go +++ b/server/forge/bitbucket/parse_test.go @@ -63,7 +63,7 @@ func Test_parser(t *testing.T) { g.Assert(err).IsNil() g.Assert(r.FullName).Equal("user_name/repo_name") g.Assert(b.Event).Equal(model.EventPull) - g.Assert(b.Commit).Equal("ce5965ddd289") + g.Assert(b.Commit).Equal("d3022fc0ca3d") }) g.It("should return pull-request details for a pull-request merged payload", func() { @@ -76,7 +76,7 @@ func Test_parser(t *testing.T) { g.Assert(err).IsNil() g.Assert(r.FullName).Equal("anbraten/test-2") g.Assert(b.Event).Equal(model.EventPullClosed) - g.Assert(b.Commit).Equal("6c5f0bc9b2aa") + g.Assert(b.Commit).Equal("006704dbeab2") }) g.It("should return pull-request details for a pull-request closed payload", func() { @@ -89,7 +89,7 @@ func Test_parser(t *testing.T) { g.Assert(err).IsNil() g.Assert(r.FullName).Equal("anbraten/test-2") g.Assert(b.Event).Equal(model.EventPullClosed) - g.Assert(b.Commit).Equal("006704dbeab2") + g.Assert(b.Commit).Equal("f90e18fc9d45") }) })