-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for BitBucket Cloud (#311)
Signed-off-by: Sergiy Kulanov <[email protected]>
- Loading branch information
Showing
9 changed files
with
409 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
Oops, something went wrong.