Skip to content

Commit

Permalink
feat: Add support for BitBucket Cloud (#311)
Browse files Browse the repository at this point in the history
Signed-off-by: Sergiy Kulanov <[email protected]>
  • Loading branch information
SergK committed Oct 2, 2024
1 parent 2269f46 commit b86e9da
Show file tree
Hide file tree
Showing 9 changed files with 409 additions and 23 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ clean: ## clean up
-rm -rf ${DIST_DIR}

.PHONY: test ## Run tests
test: test-chart
test: test-chart test-go

test-go:
go test ./... -coverprofile=coverage.out `go list ./...`

.PHONY: lint
Expand Down
4 changes: 3 additions & 1 deletion cmd/interceptor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ import (
codebaseApiV1 "github.com/epam/edp-codebase-operator/v2/api/v1"
buildInfo "github.com/epam/edp-common/pkg/config"

"github.com/epam/edp-tekton/pkg/event_processor/bitbucket"
"github.com/epam/edp-tekton/pkg/event_processor/gerrit"
"github.com/epam/edp-tekton/pkg/event_processor/github"
"github.com/epam/edp-tekton/pkg/event_processor/gitlab"
"github.com/epam/edp-tekton/pkg/github"
"github.com/epam/edp-tekton/pkg/interceptor"
)

Expand Down Expand Up @@ -102,6 +103,7 @@ func main() {
github.NewEventProcessor(client, &github.EventProcessorOptions{Logger: logger}),
gitlab.NewEventProcessor(client, logger),
gerrit.NewEventProcessor(client, logger),
bitbucket.NewEventProcessor(client, logger),
logger,
),
Logger: logger,
Expand Down
105 changes: 105 additions & 0 deletions pkg/event_processor/bitbucket/bitbucket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package bitbucket

import (
"context"
"encoding/json"
"errors"
"fmt"

"go.uber.org/zap"
ctrlClient "sigs.k8s.io/controller-runtime/pkg/client"

"github.com/epam/edp-tekton/pkg/event_processor"
)

type EventProcessor struct {
ksClient ctrlClient.Reader
logger *zap.SugaredLogger
}

func NewEventProcessor(ksClient ctrlClient.Reader, logger *zap.SugaredLogger) *EventProcessor {
return &EventProcessor{
ksClient: ksClient,
logger: logger,
}
}

func (p *EventProcessor) Process(ctx context.Context, body []byte, ns, eventType string) (*event_processor.EventInfo, error) {
switch eventType {
case event_processor.BitbucketEventTypeCommentAdded:
return p.processCommentEvent(ctx, body, ns)
default:
return p.processMergeEvent(ctx, body, ns)
}
}

func (p *EventProcessor) processMergeEvent(ctx context.Context, body []byte, ns string) (*event_processor.EventInfo, error) {
bitbucketEvent := &event_processor.BitbucketEvent{}
if err := json.Unmarshal(body, bitbucketEvent); err != nil {
return nil, fmt.Errorf("failed to unmarshal Bitbucket event: %w", err)
}

if bitbucketEvent.Repository.FullName == "" {
return nil, errors.New("bitbucket repository path empty")
}

if bitbucketEvent.PullRequest.Destination.Branch.Name == "" {
return nil, errors.New("bitbucket target branch empty")
}

repoPath := event_processor.ConvertRepositoryPath(bitbucketEvent.Repository.FullName)

codebase, err := event_processor.GetCodebaseByRepoPath(ctx, p.ksClient, ns, repoPath)
if err != nil {
return nil, fmt.Errorf("failed to get codebase by repo path: %w", err)
}

return &event_processor.EventInfo{
GitProvider: event_processor.GitProviderBitbucket,
RepoPath: repoPath,
TargetBranch: bitbucketEvent.PullRequest.Destination.Branch.Name,
Type: event_processor.EventTypeMerge,
Codebase: codebase,
PullRequest: &event_processor.PullRequest{
HeadSha: bitbucketEvent.PullRequest.Source.Commit.Hash,
Title: bitbucketEvent.PullRequest.Title,
HeadRef: bitbucketEvent.PullRequest.Source.Branch.Name,
ChangeNumber: bitbucketEvent.PullRequest.ID,
LastCommitMessage: bitbucketEvent.PullRequest.LastCommit.Hash,
},
}, nil
}

func (p *EventProcessor) processCommentEvent(ctx context.Context, body []byte, ns string) (*event_processor.EventInfo, error) {
bitbucketEvent := &event_processor.BitbucketCommentEvent{}
if err := json.Unmarshal(body, bitbucketEvent); err != nil {
return nil, fmt.Errorf("failed to unmarshal Bitbucket event: %w", err)
}

if bitbucketEvent.Repository.FullName == "" {
return nil, errors.New("bitbucket repository path empty")
}

repoPath := event_processor.ConvertRepositoryPath(bitbucketEvent.Repository.FullName)

codebase, err := event_processor.GetCodebaseByRepoPath(ctx, p.ksClient, ns, repoPath)
if err != nil {
return nil, fmt.Errorf("failed to get codebase by repo path: %w", err)
}

return &event_processor.EventInfo{
GitProvider: event_processor.GitProviderBitbucket,
RepoPath: repoPath,
TargetBranch: bitbucketEvent.PullRequest.Destination.Branch.Name,
Type: event_processor.EventTypeReviewComment,
Codebase: codebase,
HasPipelineRecheck: event_processor.ContainsPipelineRecheck(bitbucketEvent.Comment.Content.Raw),
PullRequest: &event_processor.PullRequest{
HeadSha: bitbucketEvent.PullRequest.Source.Commit.Hash,
Title: bitbucketEvent.PullRequest.Title,
HeadRef: bitbucketEvent.PullRequest.Source.Branch.Name,
ChangeNumber: bitbucketEvent.PullRequest.ID,
LastCommitMessage: bitbucketEvent.PullRequest.LastCommit.Hash,
},
}, nil
}
188 changes: 188 additions & 0 deletions pkg/event_processor/bitbucket/bitbucket_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package bitbucket

import (
"context"
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

codebaseApi "github.com/epam/edp-codebase-operator/v2/api/v1"

"github.com/epam/edp-tekton/pkg/event_processor"
)

func TestBitbucketEventProcessor_processCommentEvent(t *testing.T) {
t.Parallel()

scheme := runtime.NewScheme()
require.NoError(t, codebaseApi.AddToScheme(scheme))

type args struct {
body any
}

tests := []struct {
name string
args args
kubeObjects []client.Object
wantErr require.ErrorAssertionFunc
want *event_processor.EventInfo
}{
{
name: "comment event process successfully",
args: args{
body: event_processor.BitbucketCommentEvent{
BitbucketEvent: event_processor.BitbucketEvent{
Repository: event_processor.BitbucketRepository{
FullName: "o/r",
},
PullRequest: event_processor.BitbucketPullRequest{
ID: 1,
Title: "fix",
Source: event_processor.BitbucketPullRequestSrc{
Branch: event_processor.BitbucketBranch{
Name: "feature1",
},
Commit: event_processor.BitbucketCommit{
Hash: "123",
},
},
Destination: event_processor.BitbucketPullRequestDest{
Branch: event_processor.BitbucketBranch{
Name: "master",
},
Commit: event_processor.BitbucketCommit{
Hash: "456",
},
},
LastCommit: event_processor.BitbucketCommit{
Hash: "123",
},
},
},
Comment: event_processor.BitbucketComment{
Content: event_processor.BitbucketCommentContent{
Raw: "/recheck",
},
},
},
},
kubeObjects: []client.Object{
&codebaseApi.Codebase{
ObjectMeta: metav1.ObjectMeta{
Name: "test-codebase",
Namespace: "default",
},
Spec: codebaseApi.CodebaseSpec{
GitUrlPath: "/o/r",
},
},
},
wantErr: require.NoError,
want: &event_processor.EventInfo{
GitProvider: event_processor.GitProviderBitbucket,
RepoPath: "/o/r",
TargetBranch: "master",
Type: event_processor.EventTypeReviewComment,
HasPipelineRecheck: true,
Codebase: &codebaseApi.Codebase{
ObjectMeta: metav1.ObjectMeta{
Name: "test-codebase",
Namespace: "default",
ResourceVersion: "999",
},
Spec: codebaseApi.CodebaseSpec{
GitUrlPath: "/o/r",
},
},
PullRequest: &event_processor.PullRequest{
HeadRef: "feature1",
HeadSha: "123",
Title: "fix",
ChangeNumber: 1,
LastCommitMessage: "123",
},
},
},
{
name: "repository path empty",
args: args{
body: event_processor.BitbucketCommentEvent{},
},
wantErr: func(t require.TestingT, err error, i ...interface{}) {
require.Error(t, err)
require.Contains(t, err.Error(), "repository path empty")
},
},
{
name: "failed to get codebase",
args: args{
body: event_processor.BitbucketCommentEvent{
BitbucketEvent: event_processor.BitbucketEvent{
Repository: event_processor.BitbucketRepository{
FullName: "o/r",
},
PullRequest: event_processor.BitbucketPullRequest{
ID: 1,
Title: "fix",
Source: event_processor.BitbucketPullRequestSrc{
Branch: event_processor.BitbucketBranch{
Name: "feature1",
},
Commit: event_processor.BitbucketCommit{
Hash: "123",
},
},
Destination: event_processor.BitbucketPullRequestDest{
Branch: event_processor.BitbucketBranch{
Name: "master",
},
Commit: event_processor.BitbucketCommit{
Hash: "456",
},
},
LastCommit: event_processor.BitbucketCommit{
Hash: "123",
},
},
},
Comment: event_processor.BitbucketComment{
Content: event_processor.BitbucketCommentContent{
Raw: "/recheck",
},
},
},
},
wantErr: func(t require.TestingT, err error, i ...interface{}) {
require.Error(t, err)
require.Contains(t, err.Error(), "failed to get codebase by repo path")
},
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

body, err := json.Marshal(tt.args.body)

require.NoError(t, err)

p := NewEventProcessor(
fake.NewClientBuilder().WithScheme(scheme).WithObjects(tt.kubeObjects...).Build(),
zap.NewNop().Sugar(),
)
got, err := p.processCommentEvent(context.Background(), body, "default")
tt.wantErr(t, err)
assert.Equal(t, tt.want, got)
})
}
}
Loading

0 comments on commit b86e9da

Please sign in to comment.