Skip to content
Open
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
71 changes: 70 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ bitbucket-cli repo -k "KEY" \
```


##### Usage
##### Usage

```
Usage: bitbucket-cli repo pr create --title TITLE [--description DESCRIPTION] --from-ref FROM-REF --to-ref TO-REF [--from-key FROM-KEY] [--from-slug FROM-SLUG]
Expand Down Expand Up @@ -157,3 +157,72 @@ Options:
bitbucket-cli repo -k ABC -n some-repo security scan
```


## PR

Dashboard-level operations on Pull Requests. Unlike `repo pr`, these commands operate across all repositories visible to the authenticated user.

### List

Lists all Pull Requests visible on your dashboard.

```
$ bitbucket-cli pr list
Some Title - Jane Doe - https://your-bitbucket-hostname/projects/KEY/repos/some-repo/pull-requests/2
feature 1 - John Doe - https://your-bitbucket-hostname/projects/KEY/repos/some-repo/pull-requests/1
```

```
$ bitbucket-cli pr list -s OPEN
```

##### Usage

```plain
Usage: bitbucket-cli pr list [--state STATE] [--output OUTPUT] [--filter-title FILTER-TITLE] [--filter-desc FILTER-DESC]

Options:
--state STATE, -s STATE
--output OUTPUT, -o OUTPUT
--filter-title FILTER-TITLE, -t FILTER-TITLE
--filter-desc FILTER-DESC, -d FILTER-DESC
--help, -h display this help and exit
```

### Create

Creates a Pull Request in the specified repository.

```
$ bitbucket-cli pr create \
-k "KEY" \
-n "bitbucket-playground" \
-t "Some Title" \
-d "Some Description :thumbsup:" \
-F "refs/heads/feature/2" -T "refs/heads/master"
```

##### Usage

```plain
Usage: bitbucket-cli pr create --key KEY --name NAME --title TITLE [--description DESCRIPTION] --from-ref FROM-REF --to-ref TO-REF [--from-key FROM-KEY] [--from-slug FROM-SLUG] [--reviewers REVIEWERS]

Options:
--key KEY, -k KEY Project key
--name NAME, -n NAME Repository slug
--title TITLE, -t TITLE
Title of this PR
--description DESCRIPTION, -d DESCRIPTION
Description of the PR
--from-ref FROM-REF, -F FROM-REF
Reference of the incoming PR, e.g: refs/heads/feature-ABC-123
--to-ref TO-REF, -T TO-REF
Target reference, e.g: refs/heads/master
--from-key FROM-KEY, -K FROM-KEY
Project Key of the "from" repository
--from-slug FROM-SLUG, -S FROM-SLUG
Repository slug of the "from" repository
--reviewers REVIEWERS, -r REVIEWERS
Comma separated list of reviewers
--help, -h display this help and exit
```
7 changes: 6 additions & 1 deletion internal/pr.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package cli

type PrCmd struct {
List *PrListCmd `arg:"subcommand:list"`
Create *PrCreateCmd `arg:"subcommand:create"`
List *PrListCmd `arg:"subcommand:list"`
}

func (b *BitbucketCLI) RunPRCmd(cmd *PrCmd) {
if cmd == nil {
return
}
if cmd.Create != nil {
b.RunPRCreateCmd(cmd.Create)
return
}
if cmd.List != nil {
b.RunPRListCmd(cmd.List)
return
Expand Down
66 changes: 66 additions & 0 deletions internal/pr_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cli

import (
"fmt"
bitbucketv1 "github.com/gfleury/go-bitbucket-v1"
)

type PrCreateCmd struct {
ProjectKey string `arg:"-k,--key,required,env:BITBUCKET_PROJECT" help:"Project key"`
Slug string `arg:"-n,--name,required,env:BITBUCKET_REPO" help:"Repository slug"`
Title string `arg:"-t,--title,required" help:"Title of this PR"`
Description string `arg:"-d,--description" help:"Description of the PR"`

FromRef string `arg:"-F,--from-ref,required" help:"Reference of the incoming PR, e.g: refs/heads/feature-ABC-123"`
ToRef string `arg:"-T,--to-ref,required" help:"Target reference, e.g: refs/heads/master"`

// From which repo? Defaults to self
FromRepoKey string `arg:"-K,--from-key" help:"Project Key of the \"from\" repository"`
FromRepoSlug string `arg:"-S,--from-slug" help:"Repository slug of the \"from\" repository"`

Reviewers string `arg:"-r,--reviewers,env:BITBUCKET_REVIEWERS" help:"Comma separated list of reviewers"`
}

func (b *BitbucketCLI) RunPRCreateCmd(cmd *PrCreateCmd) {
if cmd == nil {
return
}

if cmd.FromRepoKey == "" && cmd.FromRepoSlug == "" {
// From = To
cmd.FromRepoKey = cmd.ProjectKey
cmd.FromRepoSlug = cmd.Slug
}

pr := bitbucketv1.PullRequest{
Title: cmd.Title,
Description: cmd.Description,
FromRef: bitbucketv1.PullRequestRef{
ID: cmd.FromRef,
Repository: bitbucketv1.Repository{
Slug: cmd.FromRepoSlug,
Project: &bitbucketv1.Project{Key: cmd.FromRepoKey},
},
},
ToRef: bitbucketv1.PullRequestRef{
ID: cmd.ToRef,
Repository: bitbucketv1.Repository{
Slug: cmd.Slug,
Project: &bitbucketv1.Project{Key: cmd.ProjectKey},
},
},
Reviewers: b.GetReviewers(cmd.Reviewers),
}

resp, err := b.client.DefaultApi.CreatePullRequest(cmd.ProjectKey, cmd.Slug, pr)
if err != nil {
b.logger.Fatalf("unable to create PR: %v", err)
}

prRes, err := bitbucketv1.GetPullRequestResponse(resp)
if err != nil {
b.logger.Fatalf("unable to parse PR: %v", err)
}

fmt.Printf("%s", prRes.Links.Self[0].Href)
}
22 changes: 22 additions & 0 deletions internal/pr_create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cli_test

import (
cli "github.com/swisscom/bitbucket-cli/internal"
"github.com/swisscom/bitbucket-cli/test"
"testing"
)

func TestPRCreate(t *testing.T) {
test.SkipIfNoServer(t)
c := test.MustGetCLI()
c.RunPRCmd(&cli.PrCmd{
Create: &cli.PrCreateCmd{
ProjectKey: "TOOL",
Slug: "bitbucket-playground",
Title: "Test PR",
Description: "Test PR created by bitbucket-cli",
FromRef: "refs/heads/feature/test",
ToRef: "refs/heads/master",
},
})
}
15 changes: 15 additions & 0 deletions internal/pr_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cli_test

import (
cli "github.com/swisscom/bitbucket-cli/internal"
"github.com/swisscom/bitbucket-cli/test"
"testing"
)

func TestPRList(t *testing.T) {
test.SkipIfNoServer(t)
c := test.MustGetCLI()
c.RunPRCmd(&cli.PrCmd{
List: &cli.PrListCmd{},
})
}
1 change: 1 addition & 0 deletions internal/project_clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

func TestProjectClone(t *testing.T) {
test.SkipIfNoServer(t)
c := test.MustGetCLI()
c.RunProjectCmd(&cli.ProjectCmd{
Key: "TOOL",
Expand Down
1 change: 1 addition & 0 deletions internal/project_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

func TestProjectList(t *testing.T) {
test.SkipIfNoServer(t)
c := test.MustGetCLI()
c.RunProjectCmd(&cli.ProjectCmd{
Key: "TOOL",
Expand Down
8 changes: 8 additions & 0 deletions test/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@ package test
import (
cli "github.com/swisscom/bitbucket-cli/internal"
"os"
"testing"
)

func SkipIfNoServer(t *testing.T) {
t.Helper()
if os.Getenv("BITBUCKET_URL") == "" {
t.Skip("BITBUCKET_URL not set, skipping integration test")
}
}

func MustGetCLI() *cli.BitbucketCLI {
c, err := cli.NewCLI(
&cli.BasicAuth{
Expand Down