Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Bitbucket PR hook to point the source branch, commit & ref #3965

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions server/forge/bitbucket/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions server/forge/bitbucket/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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())
Expand Down
4 changes: 4 additions & 0 deletions server/forge/bitbucket/internal/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions server/forge/bitbucket/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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")
})
})

Expand Down