Skip to content

Commit

Permalink
chore: add foreground color diff between ci state. fix formatting + o…
Browse files Browse the repository at this point in the history
…ther fixes
  • Loading branch information
Cian911 committed Dec 29, 2024
1 parent cd5fce3 commit e83ab64
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 148 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.swp
debug.log
bin/*
dist/
32 changes: 21 additions & 11 deletions pkg/cli/gomerge/gomerge.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package gomerge

import (
"github.com/cian911/go-merge/pkg/cli/list"
"github.com/cian911/go-merge/pkg/cli/version"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/cian911/go-merge/pkg/cli/list"
"github.com/cian911/go-merge/pkg/cli/version"
)

func New() (c *cobra.Command) {
Expand All @@ -13,16 +14,25 @@ func New() (c *cobra.Command) {
Short: "Gomerge makes it simple to merge an open pull request from your terminal.",
}

c.PersistentFlags().StringP("repo", "r", "", "Pass name of repository as argument (organization/repo).")
c.PersistentFlags().StringArrayP("label", "l", []string{}, "Pass an optional list of labels to filter pull requests.")
c.PersistentFlags().
StringP("repo", "r", "", "Pass name of repository as argument (organization/repo).")
c.PersistentFlags().
StringArrayP("label", "l", []string{}, "Pass an optional list of labels to filter pull requests. (label1,label2,label3)")
c.PersistentFlags().StringP("token", "t", "", "Pass your github personal access token (PAT).")
c.PersistentFlags().StringP("config", "c", "", "Pass an optional config file as an argument with list of repositories.")
c.PersistentFlags().BoolP("approve", "a", false, "Pass an optional approve flag as an argument which will only approve and not merge selected repos.")
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.PersistentFlags().
StringP("config", "c", "", "Pass an optional config file as an argument with list of repositories.")
c.PersistentFlags().
BoolP("approve", "a", false, "Pass an optional approve flag as an argument which will only approve and not merge selected repos.")
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 Down
122 changes: 62 additions & 60 deletions pkg/cli/list/list.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package list

import (
"context"
"fmt"
"log"
"os"
"strings"
"time"

"github.com/AlecAivazis/survey/v2"
"github.com/cian911/go-merge/pkg/gitclient"
"github.com/cian911/go-merge/pkg/printer"
"github.com/cian911/go-merge/pkg/utils"
"github.com/olekukonko/tablewriter"
"github.com/shurcooL/githubv4"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/cian911/go-merge/pkg/gitclient"
"github.com/cian911/go-merge/pkg/printer"
"github.com/cian911/go-merge/pkg/utils"
)

var (
Expand All @@ -26,35 +26,12 @@ var (
)

const (
TokenEnvVar = "GITHUB_TOKEN"
TokenEnvVar = "GITHUB_TOKEN"
STATUS_SUCCESS = 0
STATUS_WAITING = 1
STATUS_FAILED = 2
)

func getMergeMethod() githubv4.PullRequestMergeMethod {
method := viper.GetString("merge-method")
switch method {
case "merge":
return githubv4.PullRequestMergeMethodMerge
case "rebase":
return githubv4.PullRequestMergeMethodRebase
case "squash":
return githubv4.PullRequestMergeMethodSquash
}
if method != "" {
log.Fatalf("Unknown merge method %s. Please use one of the following: merge, rebase, squash", method)
}
return githubv4.PullRequestMergeMethodMerge
}

func getLabels() (labels []githubv4.String) {
raw_labels := viper.GetStringSlice("label")
labels = make([]githubv4.String, len(raw_labels))
for i, label := range raw_labels {
labels[i] = githubv4.String(label)
}
return
}

// TODO: Refactor NewCommnd
func NewCommand() (c *cobra.Command) {
c = &cobra.Command{
Use: "list",
Expand All @@ -78,7 +55,9 @@ func NewCommand() (c *cobra.Command) {
}

if !configPresent && len(orgRepo) <= 0 {
log.Fatal("You must pass either a config file or repository as argument to continue.")
log.Fatal(
"You must pass either a config file or repository as argument to continue.",
)
}
configToken := viper.GetString("token")

Expand Down Expand Up @@ -138,7 +117,6 @@ func NewCommand() (c *cobra.Command) {

selectedIds := promptAndFormat(pullRequestsArray, table)
for i, pr := range selectedIds {

if approveOnly {
gitclient.ApprovePullRequest(ghClient, ctx, pr, skip)
} else if closePr {
Expand All @@ -161,18 +139,31 @@ func NewCommand() (c *cobra.Command) {
return
}

func promptAndFormat(pullRequests []*gitclient.PullRequest, table *tablewriter.Table) (selectedPullRequests []*gitclient.PullRequest) {
func promptAndFormat(
pullRequests []*gitclient.PullRequest,
table *tablewriter.Table,
) (selectedPullRequests []*gitclient.PullRequest) {
prIds := []string{}

for _, pr := range pullRequests {
prIds = append(prIds, fmt.Sprintf("%d | %s/%s", pr.Number, pr.RepositoryOwner, pr.RepositoryName))
prIds = append(
prIds,
fmt.Sprintf("%d | %s/%s", pr.Number, pr.RepositoryOwner, pr.RepositoryName),
)

data := formatTable(pr)
data, status := formatTable(pr)
if len(data) == 0 {
// If there is an issue with the pr, skip
continue
}
table = printer.SuccessStyle(table, data)
switch status {
case STATUS_SUCCESS:
table = printer.SuccessStyle(table, data)
case STATUS_WAITING:
table = printer.WaitingStyle(table, data)
case STATUS_FAILED:
table = printer.ErrorStyle(table, data)
}
}
table.Render()

Expand Down Expand Up @@ -204,25 +195,29 @@ func initTable() (table *tablewriter.Table) {
return
}

func statusIcon(state string) (icon string) {
func statusIcon(state string) (icon string, status int) {
switch state {
case "SUCCESS":
icon = "✅"
icon = ""
status = STATUS_SUCCESS
case "IN_PROGRESS":
icon = "🟠"
icon = ""
status = STATUS_WAITING
case "FAILURE":
icon = "❌"
icon = "󰅙"
status = STATUS_FAILED
default:
icon = ""
}

return
}

func formatTable(pr *gitclient.PullRequest) (data []string) {
func formatTable(pr *gitclient.PullRequest) (data []string, status int) {
icon, status := statusIcon(pr.StatusRollup)
data = []string{
fmt.Sprintf("#%d", pr.Number),
fmt.Sprintf("%s %s", pr.State, statusIcon(pr.StatusRollup)),
fmt.Sprintf("%s %s", pr.State, icon),
pr.Title,
fmt.Sprintf("%s/%s", pr.RepositoryOwner, pr.RepositoryName),
printer.FormatTime(&pr.CreatedAt),
Expand All @@ -231,19 +226,6 @@ func formatTable(pr *gitclient.PullRequest) (data []string) {
return
}

func parseOrgRepo(repo string, configPresent bool) (org, repository string) {
str := strings.Split(repo, "/")

if len(str) <= 1 {
log.Fatal("You must pass your repo name like so: organization/repository to continue.")
}

org = str[0]
repository = str[1]

return
}

func getToken(flag, config string) (str string, err error) {
if flag != str {
return flag, nil
Expand Down Expand Up @@ -276,10 +258,30 @@ func selectPrIds(prIds []string) (*survey.MultiSelect, []string) {
return prompt, selectedIds
}

func commitMsg(ctx context.Context, msg string) context.Context {
if len(msg) != 0 {
return context.WithValue(ctx, "message", msg)
func getMergeMethod() githubv4.PullRequestMergeMethod {
method := viper.GetString("merge-method")
switch method {
case "merge":
return githubv4.PullRequestMergeMethodMerge
case "rebase":
return githubv4.PullRequestMergeMethodRebase
case "squash":
return githubv4.PullRequestMergeMethodSquash
}
if method != "" {
log.Fatalf(
"Unknown merge method %s. Please use one of the following: merge, rebase, squash",
method,
)
}
return githubv4.PullRequestMergeMethodMerge
}

return context.WithValue(ctx, "message", gitclient.DefaultApproveMsg())
func getLabels() (labels []githubv4.String) {
raw_labels := viper.GetStringSlice("label")
labels = make([]githubv4.String, len(raw_labels))
for i, label := range raw_labels {
labels[i] = githubv4.String(label)
}
return
}
Loading

0 comments on commit e83ab64

Please sign in to comment.