Skip to content

Commit

Permalink
Add state flag
Browse files Browse the repository at this point in the history
  • Loading branch information
seachicken committed Nov 21, 2024
1 parent 95dae5f commit 0ebfa43
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ var (
)

func main() {
var state shared.PullRequestState
var dryRun bool
var debug bool
flag.Var(&state, "state", "Filter by state: {open|closed|merged}")
flag.BoolVar(&dryRun, "dry-run", false, "Show branches to delete")
flag.BoolVar(&debug, "debug", false, "Enable debug logs")
flag.Usage = func() {
Expand All @@ -46,7 +48,7 @@ func main() {
args := flag.Args()

if len(args) == 0 {
runMain(dryRun, debug)
runMain(state, dryRun, debug)
} else {
subcmd, args := args[0], args[1:]
switch subcmd {
Expand Down Expand Up @@ -76,7 +78,7 @@ func main() {
}
}

func runMain(dryRun bool, debug bool) {
func runMain(state shared.PullRequestState, dryRun bool, debug bool) {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

Expand Down
9 changes: 5 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (

"github.com/fatih/color"
"github.com/stretchr/testify/assert"
"github.com/seachicken/gh-poi/shared"
)

func Test_DeletingBranchesWhenDryRunOptionIsFalse(t *testing.T) {
onlyCI(t)

results := captureOutput(func() { runMain(false, false) })
results := captureOutput(func() { runMain(shared.Merged, false, false) })

expected := fmt.Sprintf("%s %s", green("✔"), "Deleting branches...")
assert.Contains(t, results, expected)
Expand All @@ -23,7 +24,7 @@ func Test_DeletingBranchesWhenDryRunOptionIsFalse(t *testing.T) {
func Test_DoNotDeleteBranchesWhenDryRunOptionIsTrue(t *testing.T) {
onlyCI(t)

results := captureOutput(func() { runMain(true, false) })
results := captureOutput(func() { runMain(shared.Merged, true, false) })

expected := fmt.Sprintf("%s %s", hiBlack("-"), "Deleting branches...")
assert.Contains(t, results, expected)
Expand All @@ -33,12 +34,12 @@ func Test_ProtectAndUnprotect(t *testing.T) {
onlyCI(t)

runProtect([]string{"main"}, false)
protectResults := captureOutput(func() { runMain(true, false) })
protectResults := captureOutput(func() { runMain(shared.Merged, true, false) })
expected := fmt.Sprintf("main %s", hiBlack("[protected]"))
assert.Contains(t, protectResults, expected)

runUnprotect([]string{"main"}, false)
unprotectResults := captureOutput(func() { runMain(true, false) })
unprotectResults := captureOutput(func() { runMain(shared.Merged, true, false) })
assert.NotContains(t, unprotectResults, expected)
}

Expand Down
22 changes: 22 additions & 0 deletions shared/State.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package shared

import (
// "slices"
)

type State string

//const (
// Open State = "open"
// Closed State = "closed"
// Merged State = "merged"
// All State = "all"
//)
//
//func (s State) Set(value string) error {
// if slices.Contains([]State{"open", "closed", "merged"}, value) {
// return value
// } else {
// return errors.New("invalid state")
// }
//}
35 changes: 35 additions & 0 deletions shared/pull_request.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package shared

import (
"slices"

"github.com/pkg/errors"
)

type (
PullRequestState int

Expand All @@ -19,3 +25,32 @@ const (
Merged
Open
)

func (s PullRequestState) String() string {
switch s {
case Closed:
return "closed"
case Merged:
return "merged"
case Open:
return "open"
default:
return ""
}
}

func (s PullRequestState) Set(value string) error {
if slices.Contains([]string{"closed", "merged", "open"}, value) {
switch value {
case "closed":
s = Closed
case "merged":
s = Merged
case "open":
s = Open
}
return nil
} else {
return errors.New("invalid state")
}
}

0 comments on commit 0ebfa43

Please sign in to comment.