use errors.Wrap

This commit is contained in:
vilmibm 2019-11-13 15:00:57 -06:00
parent 933086bae9
commit 237fd04ad0
3 changed files with 17 additions and 13 deletions

View file

@ -9,6 +9,7 @@ import (
"github.com/github/gh-cli/api"
"github.com/github/gh-cli/context"
"github.com/github/gh-cli/git"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
@ -31,12 +32,12 @@ func prCreate(cmd *cobra.Command, _ []string) error {
head, err := ctx.Branch()
if err != nil {
return fmt.Errorf("could not determine current branch: %s", err)
return errors.Wrap(err, "could not determine current branch")
}
remote, err := guessRemote(ctx)
if err != nil {
return err
return errors.Wrap(err, "could not determine suitable remote")
}
if err = git.Push(remote, fmt.Sprintf("HEAD:%s", head)); err != nil {
@ -45,11 +46,11 @@ func prCreate(cmd *cobra.Command, _ []string) error {
title, err := cmd.Flags().GetString("title")
if err != nil {
return err
return errors.Wrap(err, "could not parse title")
}
body, err := cmd.Flags().GetString("body")
if err != nil {
return err
return errors.Wrap(err, "could not parse body")
}
interactive := title == "" || body == ""
@ -92,7 +93,7 @@ func prCreate(cmd *cobra.Command, _ []string) error {
err := survey.Ask(qs, &inProgress)
if err != nil {
return fmt.Errorf("could not prompt: %s", err)
return errors.Wrap(err, "could not prompt")
}
confirmAnswers := struct {
Confirmation string
@ -113,7 +114,7 @@ func prCreate(cmd *cobra.Command, _ []string) error {
err = survey.Ask(confirmQs, &confirmAnswers)
if err != nil {
return fmt.Errorf("could not prompt: %s", err)
return errors.Wrap(err, "could not prompt")
}
switch confirmAnswers.Confirmation {
@ -136,7 +137,7 @@ func prCreate(cmd *cobra.Command, _ []string) error {
}
base, err := cmd.Flags().GetString("base")
if err != nil {
return err
return errors.Wrap(err, "could not parse base")
}
if base == "" {
// TODO: use default branch for the repo
@ -145,17 +146,17 @@ func prCreate(cmd *cobra.Command, _ []string) error {
client, err := apiClientForContext(ctx)
if err != nil {
return fmt.Errorf("could not initialize api client: %s", err)
return errors.Wrap(err, "could not initialize api client")
}
repo, err := ctx.BaseRepo()
if err != nil {
return fmt.Errorf("could not determine GitHub repo: %s", err)
return errors.Wrap(err, "could not determine GitHub repo")
}
isDraft, err := cmd.Flags().GetBool("draft")
if err != nil {
return err
return errors.Wrap(err, "could not parse draft")
}
params := map[string]interface{}{
@ -168,7 +169,7 @@ func prCreate(cmd *cobra.Command, _ []string) error {
pr, err := api.CreatePullRequest(client, repo, params)
if err != nil {
return fmt.Errorf("failed to create PR: %s", err)
return errors.Wrap(err, "failed to create PR")
}
fmt.Fprintln(cmd.OutOrStdout(), pr.URL)
@ -178,14 +179,14 @@ func prCreate(cmd *cobra.Command, _ []string) error {
func guessRemote(ctx context.Context) (string, error) {
remotes, err := ctx.Remotes()
if err != nil {
return "", fmt.Errorf("could not determine suitable remote: %s", err)
return "", errors.Wrap(err, "could not determine suitable remote")
}
// TODO: consolidate logic with fsContext.BaseRepo
// TODO: check if the GH repo that the remote points to is writeable
remote, err := remotes.FindByName("upstream", "github", "origin", "*")
if err != nil {
return "", fmt.Errorf("could not determine suitable remote: %s", err)
return "", errors.Wrap(err, "could not determine suitable remote")
}
return remote.Name, nil

1
go.mod
View file

@ -9,6 +9,7 @@ require (
github.com/mattn/go-isatty v0.0.9
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/errors v0.8.1
github.com/spf13/cobra v0.0.5
github.com/stretchr/testify v1.3.0 // indirect
golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5

2
go.sum
View file

@ -34,6 +34,8 @@ github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=