From 03becbc2d3cbbf2ff7de582ce28847d02e010a61 Mon Sep 17 00:00:00 2001 From: Leo Feyer <1192057+leofeyer@users.noreply.github.com> Date: Fri, 26 Apr 2024 12:48:25 +0200 Subject: [PATCH] Remove the --admin and --auto flags and ask interactively instead --- .github/workflows/release.yml | 2 +- api/merge-pr.go | 30 +++++++++++++++++++----------- go.mod | 2 +- main.go | 12 +----------- 4 files changed, 22 insertions(+), 24 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 4c279ea..06b3012 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -13,4 +13,4 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - - uses: cli/gh-extension-precompile@v1.3.1 + - uses: cli/gh-extension-precompile@v1.4.0 diff --git a/api/merge-pr.go b/api/merge-pr.go index 8ceff0a..b931e52 100644 --- a/api/merge-pr.go +++ b/api/merge-pr.go @@ -11,7 +11,7 @@ import ( "github.com/leofeyer/gh-merge/util" ) -func MergePr(pr string, auto bool, admin bool) error { +func MergePr(pr string) error { err := checkStatus(pr) if err != nil { return err @@ -31,23 +31,31 @@ func MergePr(pr string, auto bool, admin bool) error { fmt.Println(body) prompt := util.Confirm("Merge '"+subject+"' now?", false) - if prompt == false { + if !prompt { return errors.New("Cancelled.") } args := []string{"pr", "merge", pr, "--subject", subject, "--body", body, "--squash"} - if auto { - args = append(args, "--auto") - } - - if admin { - args = append(args, "--admin") - } - data, _, err := gh.Exec(args...) if err != nil { - return err + if !strings.Contains(err.Error(), "not mergeable: the base branch policy prohibits the merge") { + return err + } + + fmt.Println("The pull request is not mergeable because the base branch policy prohibits the merge.") + + prompt = util.Confirm("Merge with admin privileges instead?", false) + if prompt { + args = append(args, "--admin") + } else { + args = append(args, "--auto") + } + + data, _, err = gh.Exec(args...) + if err != nil { + return err + } } fmt.Print(data.String()) diff --git a/go.mod b/go.mod index 671bb88..4f232a5 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/leofeyer/gh-merge -go 1.18 +go 1.19 require ( github.com/cli/go-gh v1.2.1 diff --git a/main.go b/main.go index 4f89527..5e7aa81 100644 --- a/main.go +++ b/main.go @@ -8,14 +8,7 @@ import ( "github.com/spf13/cobra" ) -type Options struct { - Auto bool - Admin bool -} - func rootCmd() *cobra.Command { - opts := &Options{} - cmd := &cobra.Command{ Use: "gh merge ", Short: "Merge a pull request", @@ -26,7 +19,7 @@ func rootCmd() *cobra.Command { return err } - err = api.MergePr(number, opts.Auto, opts.Admin) + err = api.MergePr(number) if err != nil { return err } @@ -42,9 +35,6 @@ func rootCmd() *cobra.Command { SilenceUsage: true, } - cmd.Flags().BoolVarP(&opts.Auto, "auto", "", false, "enable auto-merging the pull request") - cmd.Flags().BoolVarP(&opts.Admin, "admin", "", false, "merge the pull request with admin rights") - return cmd }