Skip to content

Commit

Permalink
Merge pull request #6 from craftamap/close-on-merge
Browse files Browse the repository at this point in the history
added impl for close-on-merge
  • Loading branch information
craftamap authored Feb 25, 2021
2 parents 6993863 + ebd6461 commit 6283335
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
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)
}

0 comments on commit 6283335

Please sign in to comment.