Skip to content
Closed
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
48 changes: 37 additions & 11 deletions server/forge/bitbucketdatacenter/bitbucketdatacenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,12 +526,22 @@ func (c *client) Hook(ctx context.Context, r *http.Request) (*model.Repo, *model
switch e := hook.Event.(type) {
case *bitbucket.RepositoryPushEvent:
pipe, err = c.updatePipelineFromCommits(ctx, user, repo, hook.Pipeline, currCommit, prevCommit)
if err != nil {
return nil, nil, fmt.Errorf("failed to update pipeline: %w", err)
}
// For tag events, we need to resolve the actual commit SHA early.
// In Bitbucket Data Center, when using annotated tags, the webhook's ToHash is the tag object SHA,
// not the actual commit SHA. We need to resolve this before the config is fetched.
if pipe != nil && pipe.Event == model.EventTag {
if err := c.resolveTagCommit(ctx, user, repo, pipe); err != nil {
return nil, nil, fmt.Errorf("failed to resolve tag commit: %w", err)
}
}
case *bitbucket.PullRequestEvent:
pipe, err = c.updatePipelineFromPullRequest(ctx, user, repo, hook.Pipeline, e.PullRequest.ID)
}

if err != nil {
return nil, nil, fmt.Errorf("failed to update pipeline: %w", err)
if err != nil {
return nil, nil, fmt.Errorf("failed to update pipeline: %w", err)
}
}

if pipe == nil {
Expand Down Expand Up @@ -580,13 +590,6 @@ func (c *client) updatePipelineFromCommits(ctx context.Context, u *model.User, r
return nil, fmt.Errorf("unable to read commit: %w", err)
}

// In Bitbucket Data Center, when using annotated tags, the webhook's ToHash is the tag object SHA, not the actual commit SHA.
// Update p.Commit so that build statuses are posted to the correct commit SHA.
if p.Event == model.EventTag && commit.ID != "" && commit.ID != p.Commit {
p.Commit = commit.ID
p.ForgeURL = fmt.Sprintf("%s/projects/%s/repos/%s/commits/%s", c.url, r.Owner, r.Name, commit.ID)
}

p.Message = commit.Message

opts := &bitbucket.CompareChangesOptions{}
Expand All @@ -613,6 +616,29 @@ func (c *client) updatePipelineFromCommits(ctx context.Context, u *model.User, r
return p, nil
}

// resolveTagCommit resolves the actual commit SHA for tag events.
// In Bitbucket Data Center, when using annotated tags, the webhook's ToHash is the tag object SHA,
// not the actual commit SHA. This function resolves the tag SHA to the actual commit SHA.
func (c *client) resolveTagCommit(ctx context.Context, u *model.User, r *model.Repo, p *model.Pipeline) error {
bc, err := c.newClient(ctx, u)
if err != nil {
return fmt.Errorf("unable to create bitbucket client: %w", err)
}

commit, _, err := bc.Projects.GetCommit(ctx, r.Owner, r.Name, p.Commit)
if err != nil {
return fmt.Errorf("unable to resolve tag commit: %w", err)
}

// Update the commit SHA and ForgeURL to point to the actual commit
if commit.ID != "" && commit.ID != p.Commit {
p.Commit = commit.ID
p.ForgeURL = fmt.Sprintf("%s/projects/%s/repos/%s/commits/%s", c.url, r.Owner, r.Name, commit.ID)
}

return nil
}

func (c *client) updatePipelineFromPullRequest(ctx context.Context, u *model.User, r *model.Repo, p *model.Pipeline, pullRequestID uint64) (*model.Pipeline, error) {
bc, err := c.newClient(ctx, u)
if err != nil {
Expand Down
52 changes: 50 additions & 2 deletions server/forge/bitbucketdatacenter/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,17 @@ func Test_convertRepo(t *testing.T) {
func Test_convertRepositoryPushEvent(t *testing.T) {
now := time.Now()
tests := []struct {
name string
from *bitbucket.RepositoryPushEvent
to *model.Pipeline
}{
{
name: "empty event",
from: &bitbucket.RepositoryPushEvent{},
to: nil,
},
{
name: "delete event with zero ToHash",
from: &bitbucket.RepositoryPushEvent{
Changes: []bitbucket.RepositoryPushEventChange{
{
Expand All @@ -113,6 +116,7 @@ func Test_convertRepositoryPushEvent(t *testing.T) {
to: nil,
},
{
name: "delete event",
from: &bitbucket.RepositoryPushEvent{
Changes: []bitbucket.RepositoryPushEventChange{
{
Expand All @@ -125,6 +129,7 @@ func Test_convertRepositoryPushEvent(t *testing.T) {
to: nil,
},
{
name: "branch push event",
from: &bitbucket.RepositoryPushEvent{
Event: bitbucket.Event{
Date: bitbucket.ISOTime(now),
Expand Down Expand Up @@ -164,10 +169,53 @@ func Test_convertRepositoryPushEvent(t *testing.T) {
Event: model.EventPush,
},
},
{
name: "tag push event",
from: &bitbucket.RepositoryPushEvent{
Event: bitbucket.Event{
Date: bitbucket.ISOTime(now),
Actor: bitbucket.User{
Name: "John Doe",
Email: "john.doe@mail.com",
Slug: "john.doe_mail.com",
},
},
Repository: bitbucket.Repository{
Slug: "REPO",
Project: &bitbucket.Project{
Key: "PRJ",
},
},
Changes: []bitbucket.RepositoryPushEventChange{
{
Ref: bitbucket.RepositoryPushEventRef{
ID: "refs/tags/v1.0.0",
DisplayID: "v1.0.0",
},
RefId: "refs/tags/v1.0.0",
ToHash: "abcdef1234567890",
},
},
},
to: &model.Pipeline{
Commit: "abcdef1234567890",
Branch: "v1.0.0",
Message: "",
Avatar: "https://base.url/users/john.doe_mail.com/avatar.png",
Author: "John Doe",
Email: "john.doe@mail.com",
Timestamp: now.UTC().Unix(),
Ref: "refs/tags/v1.0.0",
ForgeURL: "https://base.url/projects/PRJ/repos/REPO/commits/abcdef1234567890",
Event: model.EventTag,
},
},
}
for _, tt := range tests {
to := convertRepositoryPushEvent(tt.from, "https://base.url")
assert.Equal(t, tt.to, to)
t.Run(tt.name, func(t *testing.T) {
to := convertRepositoryPushEvent(tt.from, "https://base.url")
assert.Equal(t, tt.to, to)
})
}
}

Expand Down