Skip to content

Commit

Permalink
Merge pull request #57 from Cian911/issues-56/seg-fault
Browse files Browse the repository at this point in the history
BUG: Fix bug in close operation
  • Loading branch information
Cian911 authored Oct 2, 2022
2 parents 4330275 + b07307a commit c68e6e0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
2 changes: 2 additions & 0 deletions pkg/cli/gomerge/gomerge.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func New() (c *cobra.Command) {
c.PersistentFlags().StringP("merge-method", "m", "", "Pass an optional merge method for the pull request (merge [default], squash, rebase).")
c.PersistentFlags().BoolP("skip", "s", false, "Pass an optional flag to skip a pull request and continue if one or more are not mergable.")
c.PersistentFlags().BoolP("close", "", false, "Pass an optional argument to close a pull request.")
c.PersistentFlags().IntP("delay", "d", 6, "Set the value of delay, which will determine how long to wait between mergeing pull requests. Default is (6) seconds.")
c.PersistentFlags().StringP("enterprise-base-url", "e", "", "For Github Enterprise users, you can pass your enterprise base. Format: http(s)://[hostname]/")

c.MarkFlagRequired("token")
Expand All @@ -30,6 +31,7 @@ func New() (c *cobra.Command) {
viper.BindPFlag("approve", c.PersistentFlags().Lookup("approve"))
viper.BindPFlag("merge-method", c.PersistentFlags().Lookup("merge-method"))
viper.BindPFlag("skip", c.PersistentFlags().Lookup("skip"))
viper.BindPFlag("delay", c.PersistentFlags().Lookup("delay"))
viper.BindPFlag("close", c.PersistentFlags().Lookup("close"))
viper.BindPFlag("enterprise-base-url", c.PersistentFlags().Lookup("enterprise-base-url"))

Expand Down
8 changes: 8 additions & 0 deletions pkg/cli/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"strconv"
"strings"
"time"

"github.com/AlecAivazis/survey/v2"
"github.com/cian911/go-merge/pkg/gitclient"
Expand Down Expand Up @@ -44,6 +45,7 @@ func NewCommand() (c *cobra.Command) {
skip := viper.GetBool("skip")
closePr := viper.GetBool("close")
enterpriseUrl := viper.GetString("enterprise-base-url")
delay := viper.GetInt("delay")

if len(configFile) > 0 {
utils.ReadConfigFile(configFile)
Expand Down Expand Up @@ -98,6 +100,9 @@ func NewCommand() (c *cobra.Command) {
gitclient.ClosePullRequest(ghClient, ctx, org, repo, prId, pullRequestsArray[x])
} else {
gitclient.MergePullRequest(ghClient, ctx, org, p[1], prId, mergeMethod, skip)

// delay between merges to allow other active PRs to get synced
time.Sleep(time.Duration(delay) * time.Second)
}
}
} else {
Expand All @@ -123,6 +128,9 @@ func NewCommand() (c *cobra.Command) {
gitclient.ClosePullRequest(ghClient, ctx, org, repo, prId, pullRequests[x])
} else {
gitclient.MergePullRequest(ghClient, ctx, org, repo, prId, mergeMethod, skip)

// delay between merges to allow other active PRs to get synced
time.Sleep(time.Duration(delay) * time.Second)
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/gitclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ func ApprovePullRequest(ghClient *github.Client, ctx context.Context, org, repo

func MergePullRequest(ghClient *github.Client, ctx context.Context, org, repo string, prId int, mergeMethod string, skip bool) {
result, _, err := ghClient.PullRequests.Merge(ctx, org, repo, prId, defaultCommitMsg(), &github.PullRequestOptions{MergeMethod: mergeMethod})
if err != nil && !skip {
log.Fatal(err)
} else {
if err != nil {
log.Printf("Could not merge PR #%d, skipping: %v\n", prId, err)

return
}

fmt.Println(fmt.Sprintf("PR #%d: %v.", prId, *result.Message))
Expand All @@ -70,8 +70,9 @@ func ClosePullRequest(ghClient *github.Client, ctx context.Context, org, repo st
result, _, err := ghClient.PullRequests.Edit(ctx, org, repo, prId, prRef)
if err != nil {
log.Printf("Could not close PR #%d - %v", prId, err)
} else {
fmt.Println(fmt.Sprintf("PR #%d: %v.", prId, *result.State))
}
fmt.Println(fmt.Sprintf("PR #%d: %v.", prId, *result.State))
}

func defaultCommitMsg() string {
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/config_test.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
organization: Cian911
token: 1234test@gh*token
repositories:
- pr-test
- gomerge-test
- syncwave

0 comments on commit c68e6e0

Please sign in to comment.