Skip to content

Commit

Permalink
fix: bitbucket list files test
Browse files Browse the repository at this point in the history
Signed-off-by: Gosha <[email protected]>
  • Loading branch information
gosharo committed Sep 3, 2023
1 parent 2ee9671 commit 510b801
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 18 deletions.
22 changes: 11 additions & 11 deletions pkg/git_provider/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"strings"
)

type BitbucketServerClientImpl struct {
type BitbucketClientImpl struct {
client *bitbucket.Client
cfg *conf.GlobalConfig
HooksHashTable map[string]int64
Expand All @@ -29,14 +29,14 @@ func NewBitbucketServerClient(cfg *conf.GlobalConfig) (Client, error) {
return nil, err
}

return &BitbucketServerClientImpl{
return &BitbucketClientImpl{
client: client,
cfg: cfg,
HooksHashTable: make(map[string]int64),
}, err
}

func (b BitbucketServerClientImpl) ListFiles(ctx *context.Context, repo string, branch string, path string) ([]string, error) {
func (b BitbucketClientImpl) ListFiles(ctx *context.Context, repo string, branch string, path string) ([]string, error) {
var filesList []string
fileOptions := bitbucket.RepositoryFilesOptions{
Owner: b.cfg.GitProviderConfig.OrgName,
Expand All @@ -58,7 +58,7 @@ func (b BitbucketServerClientImpl) ListFiles(ctx *context.Context, repo string,
return filesList, nil
}

func (b BitbucketServerClientImpl) GetFile(ctx *context.Context, repo string, branch string, path string) (*CommitFile, error) {
func (b BitbucketClientImpl) GetFile(ctx *context.Context, repo string, branch string, path string) (*CommitFile, error) {
fileOptions := bitbucket.RepositoryFilesOptions{
Owner: b.cfg.GitProviderConfig.OrgName,
RepoSlug: repo,
Expand All @@ -78,7 +78,7 @@ func (b BitbucketServerClientImpl) GetFile(ctx *context.Context, repo string, br
}, nil
}

func (b BitbucketServerClientImpl) GetFiles(ctx *context.Context, repo string, branch string, paths []string) ([]*CommitFile, error) {
func (b BitbucketClientImpl) GetFiles(ctx *context.Context, repo string, branch string, paths []string) ([]*CommitFile, error) {
var commitFiles []*CommitFile
for _, path := range paths {
file, err := b.GetFile(ctx, repo, branch, path)
Expand All @@ -94,7 +94,7 @@ func (b BitbucketServerClientImpl) GetFiles(ctx *context.Context, repo string, b
return commitFiles, nil
}

func (b BitbucketServerClientImpl) SetWebhook(ctx *context.Context, repo *string) (*HookWithStatus, error) {
func (b BitbucketClientImpl) SetWebhook(ctx *context.Context, repo *string) (*HookWithStatus, error) {
webhookOptions := &bitbucket.WebhooksOptions{
Owner: b.cfg.GitProviderConfig.OrgName,
RepoSlug: *repo,
Expand Down Expand Up @@ -139,12 +139,12 @@ func (b BitbucketServerClientImpl) SetWebhook(ctx *context.Context, repo *string
}, nil
}

func (b BitbucketServerClientImpl) UnsetWebhook(ctx *context.Context, hook *HookWithStatus) error {
func (b BitbucketClientImpl) UnsetWebhook(ctx *context.Context, hook *HookWithStatus) error {
//TODO implement me
panic("implement me")
}

func (b BitbucketServerClientImpl) HandlePayload(ctx *context.Context, request *http.Request, secret []byte) (*WebhookPayload, error) {
func (b BitbucketClientImpl) HandlePayload(ctx *context.Context, request *http.Request, secret []byte) (*WebhookPayload, error) {
var webhookPayload *WebhookPayload

var buf bytes.Buffer
Expand Down Expand Up @@ -192,7 +192,7 @@ func (b BitbucketServerClientImpl) HandlePayload(ctx *context.Context, request *
return webhookPayload, nil
}

func (b BitbucketServerClientImpl) SetStatus(ctx *context.Context, repo *string, commit *string, linkURL *string, status *string, message *string) error {
func (b BitbucketClientImpl) SetStatus(ctx *context.Context, repo *string, commit *string, linkURL *string, status *string, message *string) error {
commitOptions := bitbucket.CommitsOptions{
Owner: b.cfg.GitProviderConfig.OrgName,
RepoSlug: *repo,
Expand All @@ -212,12 +212,12 @@ func (b BitbucketServerClientImpl) SetStatus(ctx *context.Context, repo *string,
return nil
}

func (b BitbucketServerClientImpl) PingHook(ctx *context.Context, hook *HookWithStatus) error {
func (b BitbucketClientImpl) PingHook(ctx *context.Context, hook *HookWithStatus) error {
//TODO implement me
panic("implement me")
}

func (b BitbucketServerClientImpl) isRepoWebhookExists(repo string) (*bitbucket.Webhook, bool) {
func (b BitbucketClientImpl) isRepoWebhookExists(repo string) (*bitbucket.Webhook, bool) {
emptyHook := bitbucket.Webhook{}

webhookOptions := bitbucket.WebhooksOptions{
Expand Down
60 changes: 60 additions & 0 deletions pkg/git_provider/bitbucket_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package git_provider

import (
"encoding/json"
"fmt"
"github.com/ktrysmt/go-bitbucket"
"github.com/rookout/piper/pkg/conf"
assertion "github.com/stretchr/testify/assert"
"golang.org/x/net/context"
"net/http"
"testing"
)

func TestBitbucketListFiles(t *testing.T) {
// Prepare
client, mux, _, teardown := setupBitbucket()
defer teardown()

repoContent := &bitbucket.RepositoryFile{
Type: "file",
Path: ".workflows/exit.yaml",
}

repoContent2 := &bitbucket.RepositoryFile{
Type: "file",
Path: ".workflows/main.yaml",
}

data := map[string]interface{}{"values": []bitbucket.RepositoryFile{*repoContent, *repoContent2}}
jsonBytes, _ := json.Marshal(data)

mux.HandleFunc("/repositories/test/test-repo1/src/branch1/.workflows/", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
//testFormValues(t, r, values{})

_, _ = fmt.Fprint(w, string(jsonBytes))
})

c := BitbucketClientImpl{
client: client,
cfg: &conf.GlobalConfig{
GitProviderConfig: conf.GitProviderConfig{
OrgLevelWebhook: false,
OrgName: "test",
RepoList: "test-repo1",
},
},
}
ctx := context.Background()

// Execute
actualContent, err := c.ListFiles(&ctx, "test-repo1", "branch1", ".workflows")
expectedContent := []string{"exit.yaml", "main.yaml"}

// Assert
assert := assertion.New(t)
assert.NotNil(t, err)
assert.Equal(expectedContent, actualContent)

}
3 changes: 1 addition & 2 deletions pkg/git_provider/bitbucket_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ func ValidateBitbucketPermissions(client *bitbucket.Client, cfg *conf.GlobalConf

func GetBitbucketTokenScopes(client *bitbucket.Client, cfg *conf.GlobalConfig) ([]string, error) {

client.GetApiBaseURL()
req, err := http.NewRequest("GET", fmt.Sprintf("https://api.bitbucket.org/2.0/repositories/%s", cfg.GitProviderConfig.OrgName), nil)
req, err := http.NewRequest("GET", fmt.Sprintf("%s/repositories/%s", client.GetApiBaseURL(), cfg.GitProviderConfig.OrgName), nil)
if err != nil {
log.Println("Error creating request:", err)
return nil, err
Expand Down
25 changes: 20 additions & 5 deletions pkg/git_provider/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/google/go-cmp/cmp"
"github.com/google/go-github/v52/github"
"github.com/ktrysmt/go-bitbucket"
"net/http"
"net/http/httptest"
"net/url"
Expand All @@ -14,16 +15,13 @@ import (
const (
// baseURLPath is a non-empty Client.BaseURL path to use during tests,
// to ensure relative URLs are used for all endpoints. See issue #752.
baseURLPath = "/api-v3"
baseURLPath = "/api-v3"
bitbucketBaseURLPath = "/2.0"
)

func setup() (client *github.Client, mux *http.ServeMux, serverURL string, teardown func()) {
// mux is the HTTP request multiplexer used with the test server.
mux = http.NewServeMux()

// We want to ensure that tests catch mistakes where the endpoint URL is
// specified as absolute rather than relative. It only makes a difference
// when there's a non-empty base URL path. So, use that. See issue #752.
apiHandler := http.NewServeMux()
apiHandler.Handle(baseURLPath+"/", http.StripPrefix(baseURLPath, mux))
apiHandler.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
Expand Down Expand Up @@ -73,3 +71,20 @@ func testFormValues(t *testing.T, r *http.Request, values values) {
t.Errorf("Request parameters: %v, want %v", got, want)
}
}

func setupBitbucket() (client *bitbucket.Client, mux *http.ServeMux, serverURL string, teardown func()) {
mux = http.NewServeMux()

apiHandler := http.NewServeMux()
apiHandler.Handle(bitbucketBaseURLPath+"/", http.StripPrefix(bitbucketBaseURLPath, mux))
apiHandler.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
http.Error(w, "Client.BaseURL path prefix is not preserved in the request URL.", http.StatusInternalServerError)
})

server := httptest.NewServer(apiHandler)
url, _ := url.Parse(server.URL + bitbucketBaseURLPath)
client = bitbucket.NewBasicAuth("username", "password")
client.SetApiBaseURL(*url)

return client, mux, server.URL, server.Close
}

0 comments on commit 510b801

Please sign in to comment.