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

added impl for close-on-merge #6

Merged
merged 1 commit into from
Feb 25, 2021
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
23 changes: 23 additions & 0 deletions client/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,29 @@ func (c Client) PrMerge(repoOrga string, repoSlug string, id string) (*PullReque
return &pullRequest, nil
}

func (c Client) PrMergeWithCloseBranch(repoOrga string, repoSlug string, id string, closeBranch bool) (*PullRequest, error) {
client := bitbucket.NewBasicAuth(c.Username, c.Password)

opt := &bitbucket.PullRequestsOptions{
Owner: repoOrga,
RepoSlug: repoSlug,
ID: id,
CloseSourceBranch: closeBranch,
}

response, err := client.Repositories.PullRequests.Merge(opt)
if err != nil {
return nil, err
}

var pullRequest PullRequest
err = mapstructure.Decode(response, &pullRequest)
if err != nil {
return nil, err
}
return &pullRequest, nil
}

func (c Client) PrComments(repoOrga string, repoSlug string, id string) (*Comments, error) {
client := bitbucket.NewBasicAuth(c.Username, c.Password)

Expand Down
3 changes: 2 additions & 1 deletion cmd/commands/pr/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package create

import (
"fmt"
"os"

"github.com/craftamap/bb/client"
"github.com/craftamap/bb/util/logging"
"github.com/ktrysmt/go-bitbucket"
"os"

"github.com/AlecAivazis/survey/v2"
"github.com/charmbracelet/glamour"
Expand Down
33 changes: 27 additions & 6 deletions cmd/commands/pr/merge/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@ package merge

import (
"fmt"
"github.com/craftamap/bb/util/logging"
"strconv"
"strings"

"github.com/craftamap/bb/client"
"github.com/craftamap/bb/util/logging"

"github.com/cli/cli/git"
"github.com/craftamap/bb/cmd/commands/pr/view"
"github.com/craftamap/bb/cmd/options"
"github.com/spf13/cobra"
)

var (
CloseBranch bool
)

func Add(prCmd *cobra.Command, globalOpts *options.GlobalOptions) {
mergeCmd := &cobra.Command{
var mergeCmd *cobra.Command
mergeCmd = &cobra.Command{
Use: "merge [<number of pr>]",
Long: "Merge a pull request on Bitbucket.org",
Short: "Merge a pull request",
Expand Down Expand Up @@ -53,10 +60,22 @@ func Add(prCmd *cobra.Command, globalOpts *options.GlobalOptions) {

id = prs.Values[0].ID
}
pr, err := c.PrMerge(bbrepo.RepoOrga, bbrepo.RepoSlug, fmt.Sprintf("%d", id))
if err != nil {
logging.Error(err)
return

var pr *client.PullRequest

if mergeCmd.Flag("close-source-branch").Changed {
pr, err = c.PrMergeWithCloseBranch(bbrepo.RepoOrga, bbrepo.RepoSlug, fmt.Sprintf("%d", id), CloseBranch)
if err != nil {
logging.Error(err)
return
}

} else {
pr, err = c.PrMerge(bbrepo.RepoOrga, bbrepo.RepoSlug, fmt.Sprintf("%d", id))
if err != nil {
logging.Error(err)
return
}
}

commits, err := c.PrCommits(bbrepo.RepoOrga, bbrepo.RepoSlug, fmt.Sprintf("%d", id))
Expand All @@ -68,5 +87,7 @@ func Add(prCmd *cobra.Command, globalOpts *options.GlobalOptions) {
},
}

mergeCmd.Flags().BoolVar(&CloseBranch, "close-source-branch", false, "close the source branch (pr setting if omited)")

prCmd.AddCommand(mergeCmd)
}