Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add BBC fork and update PR support #2

Merged
merged 1 commit into from
May 3, 2024
Merged
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
134 changes: 94 additions & 40 deletions internal/scm/bitbucketcloud/bitbucket_cloud.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package bitbucketcloud

import (
"context"
"crypto/tls"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
Expand All @@ -11,16 +11,16 @@ import (
"strings"

"github.com/ktrysmt/go-bitbucket"
"github.com/lindell/multi-gitter/internal/scm"
"github.com/lindell/multi-gitter/internal/scm"
"github.com/pkg/errors"
)

type BitbucketCloud struct {
repositories []string
workspaces []string
workspaces []string
users []string
username string
token string
username string
token string
sshAuth bool
httpClient *http.Client
bbClient *bitbucket.Client
Expand Down Expand Up @@ -52,14 +52,23 @@ func (bbc *BitbucketCloud) CreatePullRequest(ctx context.Context, repo scm.Repos

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

repoOptions := &bitbucket.RepositoryOptions{
Owner: bbc.workspaces[0],
RepoSlug: splitRepoFullName[1],
}
defaultReviewers, _ := bbc.bbClient.Repositories.Repository.ListDefaultReviewers(repoOptions)
for _, reviewer := range defaultReviewers.DefaultReviewers {
newPR.Reviewers = append(newPR.Reviewers, reviewer.Uuid)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tried testing with no default reviewers on repo?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup - I tried just tested it and confirmed it works. Tried create PR with no default reviewers, PR was created successfully with no reviewers on it

}

prOptions := &bitbucket.PullRequestsOptions{
Owner: bbc.workspaces[0],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for formatting this

RepoSlug: splitRepoFullName[1],
SourceBranch: newPR.Head,
DestinationBranch: newPR.Base,
Title: newPR.Title,
CloseSourceBranch: true,
Reviewers: newPR.Reviewers,
Owner: bbc.workspaces[0],
RepoSlug: splitRepoFullName[1],
SourceBranch: newPR.Head,
DestinationBranch: newPR.Base,
Title: newPR.Title,
CloseSourceBranch: true,
Reviewers: newPR.Reviewers,
}

_, err := bbc.bbClient.Repositories.PullRequests.Create(prOptions)
Expand All @@ -78,8 +87,34 @@ func (bbc *BitbucketCloud) CreatePullRequest(ctx context.Context, repo scm.Repos
}

func (bbc *BitbucketCloud) UpdatePullRequest(ctx context.Context, repo scm.Repository, pullReq scm.PullRequest, updatedPR scm.NewPullRequest) (scm.PullRequest, error) {
//TODO implement me
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment and panic have to go?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's removed on my side, is the diff showing up as included?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, on refresh I can see it, something was with my UI it seems like (it was showing as unchanged still), your good

panic("implement me 1")
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],
Title: updatedPR.Title,
Description: updatedPR.Body,
CloseSourceBranch: true,
SourceBranch: updatedPR.Head,
DestinationBranch: updatedPR.Base,
Reviewers: updatedPR.Reviewers,
}
_, err := bbc.bbClient.Repositories.PullRequests.Update(prOptions)
if err != nil {
return nil, err
}

return &pullRequest{
project: bbcPR.project,
repoName: bbcPR.repoName,
branchName: updatedPR.Head,
prProject: bbcPR.prProject,
prRepoName: bbcPR.prRepoName,
status: scm.PullRequestStatusSuccess,
}, nil
}

func (bbc *BitbucketCloud) GetPullRequests(ctx context.Context, branchName string) ([]scm.PullRequest, error) {
Expand All @@ -92,8 +127,8 @@ func (bbc *BitbucketCloud) GetPullRequests(ctx context.Context, branchName strin
if err != nil {
return nil, err
}
for _, pr := range bbPullRequests.Values{
convertedPr, err := bbc.convertPullRequests(bbc.workspaces[0], repoName, &pr)
for _, pr := range bbPullRequests.Values {
convertedPr, err := bbc.convertPullRequest(bbc.workspaces[0], repoName, &pr)
if err != nil {
return nil, err
}
Expand All @@ -114,8 +149,8 @@ func (bbc *BitbucketCloud) getPullRequests(ctx context.Context, repoName string)
if err != nil {
return nil, err
}
for _, pr := range bbPullRequests.Values{
convertedPr, err := bbc.convertPullRequests(bbc.workspaces[0], repoName, &pr)
for _, pr := range bbPullRequests.Values {
convertedPr, err := bbc.convertPullRequest(bbc.workspaces[0], repoName, &pr)
if err != nil {
return nil, err
}
Expand All @@ -124,7 +159,7 @@ func (bbc *BitbucketCloud) getPullRequests(ctx context.Context, repoName string)
return repoPRs, nil
}

func (bbc *BitbucketCloud) convertPullRequests(project, repoName string, pr *bbPullRequest) (pullRequest, error) {
func (bbc *BitbucketCloud) convertPullRequest(project, repoName string, pr *bbPullRequest) (pullRequest, error) {
status, err := bbc.pullRequestStatus(pr)
if err != nil {
return pullRequest{}, err
Expand All @@ -139,17 +174,17 @@ func (bbc *BitbucketCloud) convertPullRequests(project, repoName string, pr *bbP
prRepoName: pr.Source.Repository.Slug,
number: pr.ID,

guiURL: pr.Links.Html.Href,
status: status,
}, nil
guiURL: pr.Links.Html.Href,
status: status,
}, nil
}

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

return scm.PullRequestStatusSuccess, nil
Expand All @@ -158,7 +193,7 @@ func (bbc *BitbucketCloud) pullRequestStatus(pr *bbPullRequest) (scm.PullRequest
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])
repoPRs, _ := bbc.getPullRequests(ctx, repoSlug[1])
for _, repoPR := range repoPRs {
pr := pullRequest(repoPR)
if pr.branchName == branchName && pr.status == scm.PullRequestStatusSuccess {
Expand All @@ -172,10 +207,10 @@ func (bbc *BitbucketCloud) MergePullRequest(ctx context.Context, pr scm.PullRequ
bbcPR := pr.(pullRequest)
repoSlug := strings.Split(bbcPR.guiURL, "/")
prOptions := &bitbucket.PullRequestsOptions{
ID: fmt.Sprintf("%d", bbcPR.number),
SourceBranch: bbcPR.branchName,
RepoSlug: repoSlug[4],
Owner: bbc.workspaces[0],
ID: fmt.Sprintf("%d", bbcPR.number),
SourceBranch: bbcPR.branchName,
RepoSlug: repoSlug[4],
Owner: bbc.workspaces[0],
}
_, err := bbc.bbClient.Repositories.PullRequests.Merge(prOptions)
return err
Expand All @@ -185,18 +220,18 @@ func (bbc *BitbucketCloud) ClosePullRequest(ctx context.Context, pr scm.PullRequ
bbcPR := pr.(pullRequest)
repoSlug := strings.Split(bbcPR.guiURL, "/")
prOptions := &bitbucket.PullRequestsOptions{
ID: fmt.Sprintf("%d", bbcPR.number),
SourceBranch: bbcPR.branchName,
RepoSlug: repoSlug[4],
Owner: bbc.workspaces[0],
ID: fmt.Sprintf("%d", bbcPR.number),
SourceBranch: bbcPR.branchName,
RepoSlug: repoSlug[4],
Owner: bbc.workspaces[0],
}
_, err := bbc.bbClient.Repositories.PullRequests.Decline(prOptions)
return err
}

func (bbc *BitbucketCloud) GetRepositories(ctx context.Context) ([]scm.Repository, error) {
repoOptions := &bitbucket.RepositoriesOptions{
Role: "member",
Role: "member",
Owner: bbc.workspaces[0],
}

Expand All @@ -221,8 +256,27 @@ func (bbc *BitbucketCloud) GetRepositories(ctx context.Context) ([]scm.Repositor
}

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

if newOwner == "" {
newOwner = bbc.username
}
options := &bitbucket.RepositoryForkOptions{
FromOwner: bbc.workspaces[0],
FromSlug: splitRepoFullName[1],
Owner: newOwner,
Name: splitRepoFullName[1],
}
// TODO: Support for selecting Bitbucket project to fork into
resp, err := bbc.bbClient.Repositories.Repository.Fork(options)
if err != nil {
return nil, err
}
res, err := bbc.convertRepository(*resp)
if err != nil {
return nil, err
}
return *res, nil
}

func (bbc *BitbucketCloud) convertRepository(repo bitbucket.Repository) (*repository, error) {
Expand Down Expand Up @@ -267,4 +321,4 @@ func findLinkType(cloneLinks []hrefLink, cloneType string) string {
}

return ""
}
}