Skip to content

Commit

Permalink
Merge pull request #6 from asecurityteam/fix-create-pr
Browse files Browse the repository at this point in the history
Fix create pr
  • Loading branch information
gcase555 authored Jun 24, 2024
2 parents 6e09ef6 + 8c14b0f commit 2432aa3
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 36 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1022,9 +1022,3 @@ 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.
2 changes: 1 addition & 1 deletion cmd/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func configurePlatform(cmd *cobra.Command) {
flags.StringP("base-url", "g", "", "Base URL of the target platform, needs to be changed for GitHub enterprise, a self-hosted GitLab instance, Gitea or BitBucket.")
flags.BoolP("insecure", "", false, "Insecure controls whether a client verifies the server certificate chain and host name. Used only for Bitbucket server.")
flags.StringP("username", "u", "", "The Bitbucket server username.")
flags.StringP("token", "T", "", "The personal access token for the targeting platform. Can also be set using the GITHUB_TOKEN/GITLAB_TOKEN/GITEA_TOKEN/BITBUCKET_SERVER_TOKEN environment variable.")
flags.StringP("token", "T", "", "The personal access token for the targeting platform. Can also be set using the GITHUB_TOKEN/GITLAB_TOKEN/GITEA_TOKEN/BITBUCKET_SERVER_TOKEN/BITBUCKET_CLOUD_APP_PASSWORD environment variable.")

flags.StringSliceP("org", "O", nil, "The name of a GitHub organization. All repositories in that organization will be used.")
flags.StringSliceP("group", "G", nil, "The name of a GitLab organization. All repositories in that group will be used.")
Expand Down
24 changes: 24 additions & 0 deletions docs/README.template.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,27 @@ All configuration in multi-gitter can be done through command line flags, config
{{end}}{{end}}

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

In order to use bitbucket cloud you will need to create and use an [App Password](https://support.atlassian.com/bitbucket-cloud/docs/app-passwords/). The app password you create needs sufficient permissions so ensure you grant it Read and Write access to projects, repositories and pull requests and at least Read access to your account and workspace membership.

You will need to configure the bitbucket workspace using the `org` option for multi-gitter for the repositories you want to make changes to e.g. `multi-gitter run examples/go/upgrade-go-version.sh -u your_username --org "your_workspace"`

### Example
Here is an example of using the command line options to run a script from the `examples/` directory and make pull-requests for a few repositories in a specified workspace.
```shell
export BITBUCKET_CLOUD_APP_PASSWORD="your_app_password"
multi-gitter run examples/go/upgrade-go-version.sh -u your_username --org "your_workspace" --repo "your_first_repository,your_second_repository" --platform bitbucket_cloud -m "your_commit_message" -B your_branch_name
```

### Bitbucket Cloud Limitations
Currently, we add the repositories default reviewers as a reviewer for any pull-request you create. If you want to specify specific reviewers, you will need to add them using their `UUID` instead of their username since bitbucket does not allow us to look up a `UUID` using their username. [This article has more information about where you can get a users UUID.](https://community.atlassian.com/t5/Bitbucket-articles/Retrieve-the-Atlassian-Account-ID-AAID-in-bitbucket-org/ba-p/2471787)

We don't support specifying specific projects for bitbucket cloud yet, you should still be able to make changes to the repositories you want but certain functionality, like forking, does not work as well until we implement that feature.
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 for now until better support is added, as `git-type: go` causes inconsistent behavior(intermittent unauthorized errors).

We also only support modifying a single workspace, any additional workspaces passed into the multi-gitter `org` option will be ignored after the first value.
18 changes: 15 additions & 3 deletions internal/scm/bitbucketcloud/bitbucket_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,22 @@ func (bbc *BitbucketCloud) CreatePullRequest(ctx context.Context, repo scm.Repos
prOptions.SourceRepository = fmt.Sprintf("%s/%s", bbc.newOwner, repoOptions.RepoSlug)
}

_, err = bbc.bbClient.Repositories.PullRequests.Create(prOptions)
resp, err := bbc.bbClient.Repositories.PullRequests.Create(prOptions)
if err != nil {
return nil, err
}
createBytes, err := json.Marshal(resp)
if err != nil {
return nil, err
}
r := newPrResponse{}
err = json.Unmarshal(createBytes, &r)
if err != nil {
return nil, err
}

return &pullRequest{
number: r.ID,
guiURL: r.Links.Html.Href,
project: bbcRepo.project,
repoName: bbcRepo.name,
branchName: newPR.Head,
Expand Down Expand Up @@ -128,6 +138,8 @@ func (bbc *BitbucketCloud) UpdatePullRequest(ctx context.Context, repo scm.Repos
}

return &pullRequest{
number: bbcPR.number,
guiURL: bbcPR.guiURL,
project: bbcPR.project,
repoName: bbcPR.repoName,
branchName: updatedPR.Head,
Expand All @@ -145,7 +157,7 @@ func (bbc *BitbucketCloud) GetPullRequests(ctx context.Context, branchName strin
}
for _, repo := range repositories {
bbcRepo := repo.(repository)
prs, err := bbc.bbClient.Repositories.PullRequests.Gets(&bitbucket.PullRequestsOptions{Owner: bbc.workspaces[0], RepoSlug: bbcRepo.name})
prs, err := bbc.bbClient.Repositories.PullRequests.Gets(&bitbucket.PullRequestsOptions{Owner: bbc.workspaces[0], RepoSlug: bbcRepo.name, SourceBranch: branchName})
if err != nil {
return nil, err
}
Expand Down
53 changes: 29 additions & 24 deletions internal/scm/bitbucketcloud/custom_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package bitbucketcloud

import (
"fmt"

"github.com/ktrysmt/go-bitbucket"
"github.com/lindell/multi-gitter/internal/scm"
)
Expand All @@ -14,39 +14,44 @@ const (
stateDeclined = "DECLINED"
)

type newPrResponse struct {
ID int `json:"id"`
Links links `json:"links"`
}

type bitbucketPullRequests struct {
Next string `json:"next"`
Page int `json:"page"`
PageLen int `json:"pagelen"`
Previous string `json:"previous"`
Size int `json:"size"`
Values []bbPullRequest `json:"values"`
Next string `json:"next"`
Page int `json:"page"`
PageLen int `json:"pagelen"`
Previous string `json:"previous"`
Size int `json:"size"`
Values []bbPullRequest `json:"values"`
}

type bbPullRequest struct {
State string `json:"state"`
Source pullRequestRef `json:"source"`
Destination pullRequestRef `json:"destination"`
Links links `json:"links"`
Title string `json:"title"`
Type string `json:"type"`
ID int `json:"id"`
State string `json:"state"`
Source pullRequestRef `json:"source"`
Destination pullRequestRef `json:"destination"`
Links links `json:"links"`
Title string `json:"title"`
Type string `json:"type"`
ID int `json:"id"`
}

type pullRequestRef struct {
Branch branch `json:"branch"`
Commit commit `json:"Commit"`
Repository bitbucket.Repository `json:"repository"`
Branch branch `json:"branch"`
Commit commit `json:"Commit"`
Repository bitbucket.Repository `json:"repository"`
}

type branch struct {
Name string `json:"name"`
Name string `json:"name"`
}

type commit struct {
Hash string `json:"hash"`
Type string `json:"type"`
Links links `json:"links"`
Hash string `json:"hash"`
Type string `json:"type"`
Links links `json:"links"`
}

type links struct {
Expand All @@ -56,8 +61,8 @@ type links struct {

type repoLinks struct {
Clone []hrefLink `json:"clone,omitempty"`
Self []hrefLink `json:"self,omitempty"`
Html []hrefLink `json:"html,omitempty"`
Self []hrefLink `json:"self,omitempty"`
Html []hrefLink `json:"html,omitempty"`

Check failure on line 65 in internal/scm/bitbucketcloud/custom_structs.go

View workflow job for this annotation

GitHub Actions / golangci-lint

[golangci] reported by reviewdog 🐶 var-naming: struct field Html should be HTML (revive) Raw Output: internal/scm/bitbucketcloud/custom_structs.go:65:2: var-naming: struct field Html should be HTML (revive) Html []hrefLink `json:"html,omitempty"` ^
}

type hrefLink struct {
Expand Down Expand Up @@ -106,4 +111,4 @@ func (r repository) DefaultBranch() string {

func (r repository) FullName() string {
return r.project + "/" + r.name
}
}
1 change: 0 additions & 1 deletion internal/scm/bitbucketcloud/pullrequest.go

This file was deleted.

1 change: 0 additions & 1 deletion internal/scm/bitbucketcloud/repository.go

This file was deleted.

0 comments on commit 2432aa3

Please sign in to comment.