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

Update Docs and improve PR creation during forks #3

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1022,3 +1022,9 @@ replace();


Do you have a nice script that might be useful to others? Please create a PR that adds it to the [examples folder](/examples).

## Bitbucket Cloud Support
Using `fork: true` is currently experimental within multi-gitter for Bitbucket Cloud, and will be addressed in future updates.
Here are the known limitations:
- The forked repository will appear in the user-provided workspace/orgName, with the given repo name, but will appear in a random project within that workspace.
- Using `git-type: cmd` is required for Bitbucket Cloud forking, as `git-type: go` causes inconsistent behavior.
8 changes: 5 additions & 3 deletions cmd/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func configurePlatform(cmd *cobra.Command) {
flags.BoolP("ssh-auth", "", false, `Use SSH cloning URL instead of HTTPS + token. This requires that a setup with ssh keys that have access to all repos and that the server is already in known_hosts.`)
flags.BoolP("skip-forks", "", false, `Skip repositories which are forks.`)

flags.StringP("platform", "p", "github", "The platform that is used. Available values: github, gitlab, gitea, bitbucket_server.")
flags.StringP("platform", "p", "github", "The platform that is used. Available values: github, gitlab, gitea, bitbucket_server, bitbucket_cloud.")
_ = cmd.RegisterFlagCompletionFunc("platform", func(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"github", "gitlab", "gitea", "bitbucket_server"}, cobra.ShellCompDirectiveDefault
return []string{"github", "gitlab", "gitea", "bitbucket_server", "bitbucket_cloud"}, cobra.ShellCompDirectiveDefault
})

// Autocompletion for organizations
Expand Down Expand Up @@ -293,6 +293,8 @@ func createBitbucketCloudClient(flag *flag.FlagSet, verifyFlags bool) (multigitt
repos, _ := flag.GetStringSlice("repo")
username, _ := flag.GetString("username")
sshAuth, _ := flag.GetBool("ssh-auth")
fork, _ := flag.GetBool("fork")
newOwner, _ := flag.GetString("fork-owner")

if verifyFlags && len(workspaces) == 0 && len(users) == 0 && len(repos) == 0 {
return nil, errors.New("no workspace, user or repository set")
Expand All @@ -307,7 +309,7 @@ func createBitbucketCloudClient(flag *flag.FlagSet, verifyFlags bool) (multigitt
return nil, err
}

vc, err := bitbucketcloud.New(username, token, repos, workspaces, users, sshAuth, http.NewLoggingRoundTripper)
vc, err := bitbucketcloud.New(username, token, repos, workspaces, users, fork, sshAuth, newOwner, http.NewLoggingRoundTripper)
if err != nil {
return nil, err
}
Expand Down
16 changes: 15 additions & 1 deletion internal/scm/bitbucketcloud/bitbucket_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ type BitbucketCloud struct {
repositories []string
workspaces []string
users []string
fork bool
username string
token string
sshAuth bool
newOwner string
httpClient *http.Client
bbClient *bitbucket.Client
}

func New(username string, token string, repositories []string, workspaces []string, users []string, sshAuth bool, transportMiddleware func(http.RoundTripper) http.RoundTripper) (*BitbucketCloud, error) {
func New(username string, token string, repositories []string, workspaces []string, users []string, fork bool, sshAuth bool, newOwner string, transportMiddleware func(http.RoundTripper) http.RoundTripper) (*BitbucketCloud, error) {
if strings.TrimSpace(token) == "" {
return nil, errors.New("bearer token is empty")
}
Expand All @@ -35,9 +37,11 @@ func New(username string, token string, repositories []string, workspaces []stri
bitbucketCloud.repositories = repositories
bitbucketCloud.workspaces = workspaces
bitbucketCloud.users = users
bitbucketCloud.fork = fork
bitbucketCloud.username = username
bitbucketCloud.token = token
bitbucketCloud.sshAuth = sshAuth
bitbucketCloud.newOwner = newOwner
bitbucketCloud.httpClient = &http.Client{
Transport: transportMiddleware(&http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: false}, // nolint: gosec
Expand All @@ -61,6 +65,10 @@ func (bbc *BitbucketCloud) CreatePullRequest(ctx context.Context, repo scm.Repos
newPR.Reviewers = append(newPR.Reviewers, reviewer.Uuid)
}

if bbc.newOwner == "" {
bbc.newOwner = bbc.username
}

prOptions := &bitbucket.PullRequestsOptions{
Owner: bbc.workspaces[0],
RepoSlug: splitRepoFullName[1],
Expand All @@ -71,6 +79,12 @@ func (bbc *BitbucketCloud) CreatePullRequest(ctx context.Context, repo scm.Repos
Reviewers: newPR.Reviewers,
}

// If we are performing a fork, set the source repository.
// We do not if we are just creating a PR within the same repo, as it will cause issues
if bbc.fork {
prOptions.SourceRepository = fmt.Sprintf("%s/%s", bbc.newOwner, repoOptions.RepoSlug)
}

_, err := bbc.bbClient.Repositories.PullRequests.Create(prOptions)
if err != nil {
return nil, err
Expand Down