Skip to content

Commit

Permalink
Merge pull request #5 from asecurityteam/cleanup-2
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
gcase555 committed Jun 24, 2024
2 parents 5046599 + 080287c commit 6e09ef6
Showing 1 changed file with 70 additions and 53 deletions.
123 changes: 70 additions & 53 deletions internal/scm/bitbucketcloud/bitbucket_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package bitbucketcloud

import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"net/url"
"slices"
"strings"

internalHTTP "github.com/lindell/multi-gitter/internal/http"

"github.com/ktrysmt/go-bitbucket"
"github.com/lindell/multi-gitter/internal/scm"
"github.com/pkg/errors"
Expand Down Expand Up @@ -43,28 +44,28 @@ func New(username string, token string, repositories []string, workspaces []stri
bitbucketCloud.sshAuth = sshAuth
bitbucketCloud.newOwner = newOwner
bitbucketCloud.httpClient = &http.Client{
Transport: transportMiddleware(&http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: false}, // nolint: gosec
}),
Transport: internalHTTP.LoggingRoundTripper{},
}
bitbucketCloud.bbClient = bitbucket.NewBasicAuth(username, token)

return bitbucketCloud, nil
}

func (bbc *BitbucketCloud) CreatePullRequest(ctx context.Context, repo scm.Repository, prRepo scm.Repository, newPR scm.NewPullRequest) (scm.PullRequest, error) {

Check failure on line 54 in internal/scm/bitbucketcloud/bitbucket_cloud.go

View workflow job for this annotation

GitHub Actions / golangci-lint

[golangci] reported by reviewdog 🐶 unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive) Raw Output: internal/scm/bitbucketcloud/bitbucket_cloud.go:54:46: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive) func (bbc *BitbucketCloud) CreatePullRequest(ctx context.Context, repo scm.Repository, prRepo scm.Repository, newPR scm.NewPullRequest) (scm.PullRequest, error) { ^

splitRepoFullName := strings.Split(prRepo.FullName(), "/")
bbcRepo := prRepo.(repository)

repoOptions := &bitbucket.RepositoryOptions{
Owner: bbc.workspaces[0],
RepoSlug: splitRepoFullName[1],
RepoSlug: bbcRepo.name,
}
currentUser, err := bbc.bbClient.User.Profile()
if err != nil {
return nil, err
}
defaultReviewers, _ := bbc.bbClient.Repositories.Repository.ListDefaultReviewers(repoOptions)
defaultReviewers, err := bbc.bbClient.Repositories.Repository.ListDefaultReviewers(repoOptions)
if err != nil {
return nil, err
}
for _, reviewer := range defaultReviewers.DefaultReviewers {
if currentUser.Uuid != reviewer.Uuid {
newPR.Reviewers = append(newPR.Reviewers, reviewer.Uuid)
Expand All @@ -77,7 +78,7 @@ func (bbc *BitbucketCloud) CreatePullRequest(ctx context.Context, repo scm.Repos

prOptions := &bitbucket.PullRequestsOptions{
Owner: bbc.workspaces[0],
RepoSlug: splitRepoFullName[1],
RepoSlug: bbcRepo.name,
SourceBranch: newPR.Head,
DestinationBranch: newPR.Base,
Title: newPR.Title,
Expand All @@ -97,24 +98,23 @@ func (bbc *BitbucketCloud) CreatePullRequest(ctx context.Context, repo scm.Repos
}

return &pullRequest{
project: splitRepoFullName[0],
repoName: splitRepoFullName[1],
project: bbcRepo.project,
repoName: bbcRepo.name,
branchName: newPR.Head,
prProject: splitRepoFullName[0],
prRepoName: splitRepoFullName[1],
prProject: bbcRepo.project,
prRepoName: bbcRepo.name,
status: scm.PullRequestStatusSuccess,
}, nil
}

func (bbc *BitbucketCloud) UpdatePullRequest(ctx context.Context, repo scm.Repository, pullReq scm.PullRequest, updatedPR scm.NewPullRequest) (scm.PullRequest, error) {
bbcPR := pullReq.(pullRequest)
repoSlug := strings.Split(bbcPR.guiURL, "/")

// Note the specs of the bitbucket client here, reviewers field must be UUID of the reviewers, not their usernames
prOptions := &bitbucket.PullRequestsOptions{
ID: fmt.Sprintf("%d", bbcPR.number),
Owner: bbc.workspaces[0],
RepoSlug: repoSlug[4],
RepoSlug: bbcPR.repoName,
Title: updatedPR.Title,
Description: updatedPR.Body,
CloseSourceBranch: true,
Expand All @@ -139,33 +139,49 @@ func (bbc *BitbucketCloud) UpdatePullRequest(ctx context.Context, repo scm.Repos

func (bbc *BitbucketCloud) GetPullRequests(ctx context.Context, branchName string) ([]scm.PullRequest, error) {

Check failure on line 140 in internal/scm/bitbucketcloud/bitbucket_cloud.go

View workflow job for this annotation

GitHub Actions / golangci-lint

[golangci] reported by reviewdog 🐶 unused-parameter: parameter 'branchName' seems to be unused, consider removing or renaming it as _ (revive) Raw Output: internal/scm/bitbucketcloud/bitbucket_cloud.go:140:65: unused-parameter: parameter 'branchName' seems to be unused, consider removing or renaming it as _ (revive) func (bbc *BitbucketCloud) GetPullRequests(ctx context.Context, branchName string) ([]scm.PullRequest, error) { ^
var responsePRs []scm.PullRequest
for _, repoName := range bbc.repositories {
prs, _ := bbc.bbClient.Repositories.PullRequests.Gets(&bitbucket.PullRequestsOptions{Owner: bbc.workspaces[0], RepoSlug: repoName})
prBytes, _ := json.Marshal(prs)
repositories, err := bbc.GetRepositories(ctx)
if err != nil {
return nil, err
}
for _, repo := range repositories {
bbcRepo := repo.(repository)
prs, err := bbc.bbClient.Repositories.PullRequests.Gets(&bitbucket.PullRequestsOptions{Owner: bbc.workspaces[0], RepoSlug: bbcRepo.name})
if err != nil {
return nil, err
}
prBytes, err := json.Marshal(prs)
if err != nil {
return nil, err
}
bbPullRequests := &bitbucketPullRequests{}
err := json.Unmarshal(prBytes, bbPullRequests)
err = json.Unmarshal(prBytes, bbPullRequests)
if err != nil {
return nil, err
}
for _, pr := range bbPullRequests.Values {
convertedPr, err := bbc.convertPullRequest(bbc.workspaces[0], repoName, &pr)
convertedPr, err := bbc.convertPullRequest(bbc.workspaces[0], bbcRepo.name, &pr)

Check failure on line 162 in internal/scm/bitbucketcloud/bitbucket_cloud.go

View workflow job for this annotation

GitHub Actions / golangci-lint

[golangci] reported by reviewdog 🐶 G601: Implicit memory aliasing in for loop. (gosec) Raw Output: internal/scm/bitbucketcloud/bitbucket_cloud.go:162:80: G601: Implicit memory aliasing in for loop. (gosec) convertedPr, err := bbc.convertPullRequest(bbc.workspaces[0], bbcRepo.name, &pr) ^
if err != nil {
return nil, err
}
responsePRs = append(responsePRs, convertedPr)
}
}
fmt.Println(responsePRs)
return responsePRs, nil
}

func (bbc *BitbucketCloud) getPullRequests(ctx context.Context, repoName string) ([]pullRequest, error) {

var repoPRs []pullRequest
prs, _ := bbc.bbClient.Repositories.PullRequests.Gets(&bitbucket.PullRequestsOptions{Owner: bbc.workspaces[0], RepoSlug: repoName})
prBytes, _ := json.Marshal(prs)
prs, err := bbc.bbClient.Repositories.PullRequests.Gets(&bitbucket.PullRequestsOptions{Owner: bbc.workspaces[0], RepoSlug: repoName})
if err != nil {
return nil, err
}
prBytes, err := json.Marshal(prs)
if err != nil {
return nil, err
}
bbPullRequests := &bitbucketPullRequests{}
err := json.Unmarshal(prBytes, bbPullRequests)
err = json.Unmarshal(prBytes, bbPullRequests)
if err != nil {
return nil, err
}
Expand All @@ -180,10 +196,7 @@ func (bbc *BitbucketCloud) getPullRequests(ctx context.Context, repoName string)
}

func (bbc *BitbucketCloud) convertPullRequest(project, repoName string, pr *bbPullRequest) (pullRequest, error) {
status, err := bbc.pullRequestStatus(pr)
if err != nil {
return pullRequest{}, err
}
status := bbc.pullRequestStatus(pr)

return pullRequest{
repoName: repoName,
Expand All @@ -199,21 +212,23 @@ func (bbc *BitbucketCloud) convertPullRequest(project, repoName string, pr *bbPu
}, nil
}

func (bbc *BitbucketCloud) pullRequestStatus(pr *bbPullRequest) (scm.PullRequestStatus, error) {
func (bbc *BitbucketCloud) pullRequestStatus(pr *bbPullRequest) scm.PullRequestStatus {
switch pr.State {
case stateMerged:
return scm.PullRequestStatusMerged, nil
return scm.PullRequestStatusMerged
case stateDeclined:
return scm.PullRequestStatusClosed, nil
return scm.PullRequestStatusClosed
}

return scm.PullRequestStatusSuccess, nil
return scm.PullRequestStatusSuccess
}

func (bbc *BitbucketCloud) GetOpenPullRequest(ctx context.Context, repo scm.Repository, branchName string) (scm.PullRequest, error) {
repoFN := repo.FullName()
repoSlug := strings.Split(repoFN, "/")
repoPRs, _ := bbc.getPullRequests(ctx, repoSlug[1])
bbcRepo := repo.(repository)
repoPRs, err := bbc.getPullRequests(ctx, bbcRepo.name)
if err != nil {
return nil, err
}
for _, repoPR := range repoPRs {
pr := pullRequest(repoPR)

Check failure on line 233 in internal/scm/bitbucketcloud/bitbucket_cloud.go

View workflow job for this annotation

GitHub Actions / golangci-lint

[golangci] reported by reviewdog 🐶 unnecessary conversion (unconvert) Raw Output: internal/scm/bitbucketcloud/bitbucket_cloud.go:233:20: unnecessary conversion (unconvert) pr := pullRequest(repoPR) ^
if pr.branchName == branchName && pr.status == scm.PullRequestStatusSuccess {
Expand All @@ -225,11 +240,11 @@ func (bbc *BitbucketCloud) GetOpenPullRequest(ctx context.Context, repo scm.Repo

func (bbc *BitbucketCloud) MergePullRequest(ctx context.Context, pr scm.PullRequest) error {
bbcPR := pr.(pullRequest)
repoSlug := strings.Split(bbcPR.guiURL, "/")
repoSlug := strings.Split(bbcPR.guiURL, "/")[4]
prOptions := &bitbucket.PullRequestsOptions{
ID: fmt.Sprintf("%d", bbcPR.number),
SourceBranch: bbcPR.branchName,
RepoSlug: repoSlug[4],
RepoSlug: repoSlug,
Owner: bbc.workspaces[0],
}
_, err := bbc.bbClient.Repositories.PullRequests.Merge(prOptions)
Expand All @@ -238,11 +253,11 @@ func (bbc *BitbucketCloud) MergePullRequest(ctx context.Context, pr scm.PullRequ

func (bbc *BitbucketCloud) ClosePullRequest(ctx context.Context, pr scm.PullRequest) error {
bbcPR := pr.(pullRequest)
repoSlug := strings.Split(bbcPR.guiURL, "/")
repoSlug := strings.Split(bbcPR.guiURL, "/")[4]
prOptions := &bitbucket.PullRequestsOptions{
ID: fmt.Sprintf("%d", bbcPR.number),
SourceBranch: bbcPR.branchName,
RepoSlug: repoSlug[4],
RepoSlug: repoSlug,
Owner: bbc.workspaces[0],
}
_, err := bbc.bbClient.Repositories.PullRequests.Decline(prOptions)
Expand Down Expand Up @@ -276,16 +291,15 @@ func (bbc *BitbucketCloud) GetRepositories(ctx context.Context) ([]scm.Repositor
}

func (bbc *BitbucketCloud) ForkRepository(ctx context.Context, repo scm.Repository, newOwner string) (scm.Repository, error) {
splitRepoFullName := strings.Split(repo.FullName(), "/")

bbcRepo := repo.(repository)
if newOwner == "" {
newOwner = bbc.username
}
options := &bitbucket.RepositoryForkOptions{
FromOwner: bbc.workspaces[0],
FromSlug: splitRepoFullName[1],
FromSlug: bbcRepo.name,
Owner: newOwner,
Name: splitRepoFullName[1],
Name: bbcRepo.name,
}
// TODO: Support for selecting Bitbucket project to fork into
resp, err := bbc.bbClient.Repositories.Repository.Fork(options)
Expand All @@ -303,18 +317,21 @@ func (bbc *BitbucketCloud) convertRepository(repo bitbucket.Repository) (*reposi
var cloneURL string

rLinks := &repoLinks{}
linkBytes, _ := json.Marshal(repo.Links)
linkBytes, err := json.Marshal(repo.Links)
if err != nil {
return nil, err
}
_ = json.Unmarshal(linkBytes, rLinks)

if bbc.sshAuth {
cloneURL = findLinkType(rLinks.Clone, cloneSSHType)
if cloneURL == "" {
return nil, errors.Errorf("unable to find clone url for repository %s using clone type %s", repo.Name, cloneSSHType)
cloneURL, err = findLinkType(rLinks.Clone, cloneSSHType, repo.Name)
if err != nil {
return nil, err
}
} else {
httpURL := findLinkType(rLinks.Clone, cloneHTTPType)
if httpURL == "" {
return nil, errors.Errorf("unable to find clone url for repository %s using clone type %s", repo.Name, cloneHTTPType)
httpURL, err := findLinkType(rLinks.Clone, cloneHTTPType, repo.Name)
if err != nil {
return nil, err
}
parsedURL, err := url.Parse(httpURL)
if err != nil {
Expand All @@ -333,12 +350,12 @@ func (bbc *BitbucketCloud) convertRepository(repo bitbucket.Repository) (*reposi
}, nil
}

func findLinkType(cloneLinks []hrefLink, cloneType string) string {
func findLinkType(cloneLinks []hrefLink, cloneType string, repoName string) (string, error) {
for _, clone := range cloneLinks {
if strings.EqualFold(clone.Name, cloneType) {
return clone.Href
return clone.Href, nil
}
}

return ""
return "", errors.Errorf("unable to find clone url for repository %s using clone type %s", repoName, cloneType)
}

0 comments on commit 6e09ef6

Please sign in to comment.