Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to urfave/cli/v3 #202

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (

"github.com/cupcicm/opp/core"
"github.com/cupcicm/opp/core/story"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)

func MakeApp(out io.Writer, in io.Reader, repo *core.Repo, gh func(context.Context) core.Gh, sf func(string, string) story.StoryFetcher) *cli.App {
return &cli.App{
func MakeApp(out io.Writer, in io.Reader, repo *core.Repo, gh func(context.Context) core.Gh, sf func(string, string) story.StoryFetcher) *cli.Command {
return &cli.Command{
Name: "opp",
Usage: "Create, update and merge Github pull requests from the command line.",
Commands: []*cli.Command{
Expand All @@ -29,14 +29,14 @@ func MakeApp(out io.Writer, in io.Reader, repo *core.Repo, gh func(context.Conte
PushCommand(repo),
CommentCommand(repo, gh),
},
Action: func(ctx *cli.Context) error {
Action: func(ctx context.Context, cmd *cli.Command) error {
// Called only if no subcommand match.
args := ctx.Args()
args := cmd.Args()
if !args.Present() {
return errors.New("no subcommand provided")
}
subcommand := args.First()
return runCustomScript(ctx.Context, subcommand, args.Slice()[1:])
return runCustomScript(ctx, subcommand, args.Slice()[1:])
},
}
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/cupcicm/opp/core"
"github.com/go-git/go-git/v5/plumbing"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
"golang.org/x/sync/semaphore"
)

Expand All @@ -18,12 +18,12 @@ func CleanCommand(repo *core.Repo, gh func(context.Context) core.Gh) *cli.Comman
Name: "clean",
Aliases: []string{"gc"},
Description: "Deletes all local PRs that have been closed on github",
Action: func(cCtx *cli.Context) error {
repo.Fetch(cCtx.Context)
localPrs := repo.AllPrs(cCtx.Context)
pullRequests := gh(cCtx.Context).PullRequests()
Action: func(ctx context.Context, cmd *cli.Command) error {
repo.Fetch(ctx)
localPrs := repo.AllPrs(ctx)
pullRequests := gh(ctx).PullRequests()

return cleaner{repo, localPrs, pullRequests}.Clean(cCtx.Context)
return cleaner{repo, localPrs, pullRequests}.Clean(ctx)
},
}
return cmd
Expand Down
8 changes: 4 additions & 4 deletions cmd/close.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import (
"fmt"

"github.com/cupcicm/opp/core"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)

func CloseCommand(repo *core.Repo, gh func(context.Context) core.Gh) *cli.Command {
cmd := &cli.Command{
Name: "close",
Aliases: []string{"abandon"},
Description: "Closes an open PR without merging it. Also deletes its local branch",
Action: func(cCtx *cli.Context) error {
pr, currentBranch, err := PrFromFirstArgument(repo, cCtx)
Action: func(ctx context.Context, cmd *cli.Command) error {
pr, currentBranch, err := PrFromFirstArgument(repo, cmd)
if err != nil {
return err
}
Expand All @@ -23,7 +23,7 @@ func CloseCommand(repo *core.Repo, gh func(context.Context) core.Gh) *cli.Comman
}
// Deleting the remote branch closes the PR.
fmt.Printf("Closing %s... ", pr.LocalBranch())
err = repo.DeleteLocalAndRemoteBranch(cCtx.Context, pr)
err = repo.DeleteLocalAndRemoteBranch(ctx, pr)
if err != nil {
PrintFailure(nil)
return err
Expand Down
17 changes: 9 additions & 8 deletions cmd/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,28 @@ package cmd
import (
"context"
"fmt"

"github.com/cupcicm/opp/core"
"github.com/google/go-github/v56/github"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)

func CommentCommand(repo *core.Repo, gh func(context.Context) core.Gh) *cli.Command {
cmd := &cli.Command{
Name: "comment",
Description: "Add a comment to a PR",
Usage: "Adds a comment to a PR",
Action: func(cCtx *cli.Context) error {
Action: func(ctx context.Context, cmd *cli.Command) error {

var prParam string
var comment string
switch cCtx.NArg() {
switch cmd.NArg() {
case 1:
prParam = ""
comment = cCtx.Args().First()
comment = cmd.Args().First()
case 2:
prParam = cCtx.Args().First()
comment = cCtx.Args().Get(1)
prParam = cmd.Args().First()
comment = cmd.Args().Get(1)
default:
return cli.Exit("Usage: opp comment [pr] $comment", 1)
}
Expand All @@ -33,12 +34,12 @@ func CommentCommand(repo *core.Repo, gh func(context.Context) core.Gh) *cli.Comm
return err
}
ctx, cancel := context.WithTimeoutCause(
cCtx.Context, core.GetGithubTimeout(),
ctx, core.GetGithubTimeout(),
fmt.Errorf("adding comment too slow, increase github.timeout"),
)
defer cancel()

_, _, err = gh(cCtx.Context).Issues().CreateComment(ctx, core.GetGithubOwner(),
_, _, err = gh(ctx).Issues().CreateComment(ctx, core.GetGithubOwner(),
core.GetGithubRepoName(), pr.PrNumber, &github.IssueComment{Body: &comment})
if err != nil {
PrintFailure(nil)
Expand Down
8 changes: 4 additions & 4 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/cupcicm/opp/core"
"github.com/go-git/go-git/v5/plumbing"
"github.com/spf13/viper"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)

const ErrorPattern = "could not %s a global gitignore file, please add .opp to your .gitignore file manually"
Expand All @@ -21,7 +21,7 @@ func InitCommand(repo *core.Repo) *cli.Command {
return &cli.Command{
Name: "init",
Usage: "indicate you are going to use opp in this git repo",
Action: func(cCtx *cli.Context) error {
Action: func(ctx context.Context, cmd *cli.Command) error {
config := repo.Config()
if _, err := os.Stat(config); err == nil {
return cli.Exit("Config file already exists", 1)
Expand All @@ -31,8 +31,8 @@ func InitCommand(repo *core.Repo) *cli.Command {
i := initializer{repo}
i.AskGithubToken()
i.GuessRepoValues()
i.GetGithubValues(cCtx.Context)
err := i.AddOppInGlobalGitignore(cCtx.Context)
i.GetGithubValues(ctx)
err := i.AddOppInGlobalGitignore(ctx)
if err != nil {
fmt.Printf("%v\n", err)
}
Expand Down
14 changes: 7 additions & 7 deletions cmd/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/cupcicm/opp/core"
"github.com/go-git/go-git/v5/plumbing"
"github.com/google/go-github/v56/github"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)

var (
Expand All @@ -28,31 +28,31 @@ func MergeCommand(repo *core.Repo, gh func(context.Context) core.Gh) *cli.Comman
cmd := &cli.Command{
Name: "merge",
Aliases: []string{"m"},
Action: func(cCtx *cli.Context) error {
pr, mergingCurrentBranch, err := PrFromFirstArgument(repo, cCtx)
Action: func(ctx context.Context, cmd *cli.Command) error {
pr, mergingCurrentBranch, err := PrFromFirstArgument(repo, cmd)
if err != nil {
return err
}
merger := merger{Repo: repo, PullRequests: gh(cCtx.Context).PullRequests()}
merger := merger{Repo: repo, PullRequests: gh(ctx).PullRequests()}
ancestors := pr.AllAncestors()
if len(ancestors) >= 1 {
fmt.Printf("%s is not mergeable because it has unmerged dependent PRs.\n", pr.Url())
return fmt.Errorf("please merge %s first", ancestors[0].LocalBranch())
}
fmt.Print("Checking mergeability... ")

isMergeable, err := merger.IsMergeable(cCtx.Context, pr)
isMergeable, err := merger.IsMergeable(ctx, pr)

if errors.Is(err, ErrBeingEvaluated) {
isMergeable, err = merger.WaitForMergeability(cCtx.Context, pr)
isMergeable, err = merger.WaitForMergeability(ctx, pr)
}
if !isMergeable {
PrintFailure(nil)
return cli.Exit(err, 1)
}
PrintSuccess()
mergeContext, cancel := context.WithTimeoutCause(
cCtx.Context, core.GetGithubTimeout(),
ctx, core.GetGithubTimeout(),
fmt.Errorf("merging too slow, increase github.timeout"),
)
defer cancel()
Expand Down
10 changes: 5 additions & 5 deletions cmd/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import (
"strconv"

"github.com/cupcicm/opp/core"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)

// PrFromFirstArgument returns the PR number supplied as a commandline argument, or if no argument is supplied,
// the PR for the current branch.
// The PR number can be supplied as a simple integer, or in the form `pr/$number`.
func PrFromFirstArgument(repo *core.Repo, cCtx *cli.Context) (*core.LocalPr, bool, error) {
func PrFromFirstArgument(repo *core.Repo, cmd *cli.Command) (*core.LocalPr, bool, error) {
var prParam string
if cCtx.Args().Present() {
if cCtx.NArg() > 1 {
if cmd.Args().Present() {
if cmd.NArg() > 1 {
return nil, false, cli.Exit("too many arguments", 1)
}
prParam = cCtx.Args().First()
prParam = cmd.Args().First()

}
return PrFromStringOrCurrentBranch(repo, prParam)
Expand Down
40 changes: 20 additions & 20 deletions cmd/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/google/go-github/v56/github"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)

var (
Expand All @@ -36,7 +36,7 @@ specified a different base for example.
`)
DraftFlagUsage = "Create a draft PR."
ExtractFlagUsage = strings.TrimSpace(`
When set, tries to extract the commits used to create the PR from the current branch.
When set, tries to extract the commits used to create the PR from the current branch.
This means that the current branch will not retain the commits you used to create the PR, they
will be "moved" to the PR branch, and will not stay in your main branch.
`)
Expand Down Expand Up @@ -86,25 +86,25 @@ func PrCommand(in io.Reader, repo *core.Repo, gh func(context.Context) core.Gh,
Usage: ExtractFlagUsage,
},
},
Action: func(cCtx *cli.Context) error {
Action: func(ctx context.Context, cmd *cli.Command) error {
initialRef, err := repo.Head()
if err != nil {
return err
}
pr := create{Repo: repo, Github: gh(cCtx.Context), StoryFetcher: sf}
args, err := pr.SanitizeArgs(cCtx)
pr := create{Repo: repo, Github: gh(ctx), StoryFetcher: sf}
args, err := pr.SanitizeArgs(ctx, cmd)
if err != nil {
return err
}
if args.NeedsRebase {
newArgs, err := pr.RebasePrCommits(cCtx.Context, args)
newArgs, err := pr.RebasePrCommits(ctx, args)
if err != nil {
repo.CheckoutRef(initialRef)
return err
}
args = newArgs
}
localPr, err := pr.Create(cCtx.Context, in, args)
localPr, err := pr.Create(ctx, in, args)
if err != nil {
return err
}
Expand All @@ -115,10 +115,10 @@ func PrCommand(in io.Reader, repo *core.Repo, gh func(context.Context) core.Gh,
if err != nil {
return err
}
if !repo.TryRebaseCurrentBranchSilently(cCtx.Context, localPr) {
if !repo.TryRebaseCurrentBranchSilently(ctx, localPr) {
return fmt.Errorf("problem while rebasing %s on %s\n", args.InitialBranch.LocalName(), localPr.LocalName())
}
if !repo.TryLocalRebaseOntoSilently(cCtx.Context, args.Commits[0].Hash, args.Commits[len(args.Commits)-1].Hash) {
if !repo.TryLocalRebaseOntoSilently(ctx, args.Commits[0].Hash, args.Commits[len(args.Commits)-1].Hash) {
return fmt.Errorf("problem while extracting PR commits from %s\n", args.InitialBranch.LocalName())
}
}
Expand Down Expand Up @@ -151,27 +151,27 @@ type args struct {
Extract bool
}

func (c *create) SanitizeArgs(cCtx *cli.Context) (*args, error) {
forHead := cCtx.Args().Present()
func (c *create) SanitizeArgs(ctx context.Context, cmd *cli.Command) (*args, error) {
forHead := cmd.Args().Present()
var (
overrideAncestorBranch core.Branch
needsRebase = false
headCommit, err = HeadCommit(c.Repo, cCtx.Args())
headCommit, err = HeadCommit(c.Repo, cmd.Args())
)
if err != nil {
return nil, err
}
overrideAncestor := cCtx.String("base")
localChanges := !c.Repo.NoLocalChanges(cCtx.Context)
extract := cCtx.Bool("extract")
overrideAncestor := cmd.String("base")
localChanges := !c.Repo.NoLocalChanges(ctx)
extract := cmd.Bool("extract")
if overrideAncestor != "" {
overrideAncestorBranch, err = c.Repo.GetBranch(overrideAncestor)
needsRebase = true
if err != nil {
return nil, cli.Exit(fmt.Errorf("%s is not a valid branch", overrideAncestor), 1)
}
}
if cCtx.Bool("interactive") {
if cmd.Bool("interactive") {
needsRebase = true
}
// If there are local changes, we need to be very careful before we accept to create this PR
Expand All @@ -181,7 +181,7 @@ func (c *create) SanitizeArgs(cCtx *cli.Context) (*args, error) {
// rebased on top of it, the user needs to stash their changes.
return nil, cli.Exit("You have provided --base but have local changes, please stash them", 1)
}
if cCtx.Bool("checkout") && !forHead {
if cmd.Bool("checkout") && !forHead {
// If the user wants to checkout the PR at the end, it's OK but it needs to be a PR that will end
// up exactly with the same HEAD as before.
return nil, cli.Exit("Cannot checkout the PR since there are local changes. Please stash them", 1)
Expand All @@ -201,7 +201,7 @@ func (c *create) SanitizeArgs(cCtx *cli.Context) (*args, error) {
if ancestor.IsPr() && tip.Hash == headCommit {
return nil, cli.Exit(ErrAlreadyAPrBranch, 1)
}
shouldCheckout := cCtx.Bool("checkout")
shouldCheckout := cmd.Bool("checkout")
head := core.Must(c.Repo.Head())

if !head.Name().IsBranch() {
Expand All @@ -211,8 +211,8 @@ func (c *create) SanitizeArgs(cCtx *cli.Context) (*args, error) {
Commits: commits,
NeedsRebase: needsRebase,
CheckoutPr: shouldCheckout,
Interactive: cCtx.Bool("interactive"),
DraftPr: cCtx.Bool("draft"),
Interactive: cmd.Bool("interactive"),
DraftPr: cmd.Bool("draft"),
Extract: extract,
}
if head.Name().IsBranch() {
Expand Down
10 changes: 5 additions & 5 deletions cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"fmt"

"github.com/cupcicm/opp/core"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)

func PushCommand(repo *core.Repo) *cli.Command {
cmd := &cli.Command{
Name: "push",
Aliases: []string{"up", "p", "push"},
Action: func(cCtx *cli.Context) error {
err := repo.Fetch(cCtx.Context)
Aliases: []string{"up", "p"},
Action: func(ctx context.Context, cmd *cli.Command) error {
err := repo.Fetch(ctx)
if err != nil {
return fmt.Errorf("on fetch: %w", err)
}
Expand All @@ -27,7 +27,7 @@ func PushCommand(repo *core.Repo) *cli.Command {
return nil
}
pr := branch.(*core.LocalPr)
return push(cCtx.Context, repo, pr)
return push(ctx, repo, pr)
},
}

Expand Down
Loading