2020-02-17 06:28:11 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-05-07 03:09:31 +02:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
|
2020-09-17 18:25:09 +02:00
|
|
|
"gitea.com/jolheiser/sip/config"
|
|
|
|
"gitea.com/jolheiser/sip/flag"
|
2020-05-07 03:09:31 +02:00
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
2020-02-17 06:28:11 +01:00
|
|
|
"github.com/AlecAivazis/survey/v2"
|
|
|
|
"github.com/huandu/xstrings"
|
|
|
|
"github.com/urfave/cli/v2"
|
2020-02-27 03:53:11 +01:00
|
|
|
"go.jolheiser.com/beaver"
|
2020-02-17 06:28:11 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var PullsCheckout = cli.Command{
|
|
|
|
Name: "checkout",
|
|
|
|
Usage: "Checkout a pull request for testing",
|
|
|
|
Action: doPullCheckout,
|
|
|
|
}
|
|
|
|
|
|
|
|
func doPullCheckout(ctx *cli.Context) error {
|
2020-09-17 18:25:09 +02:00
|
|
|
client, err := getClient(true)
|
2020-04-17 05:59:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-17 06:28:11 +01:00
|
|
|
var issue *gitea.Issue
|
|
|
|
questions := []*survey.Question{
|
|
|
|
{
|
|
|
|
Name: "index",
|
2020-02-17 21:50:02 +01:00
|
|
|
Prompt: &survey.Input{Message: "Pull request number", Help: "Don't worry if you aren't sure! Just say -1 and we'll search for it inssipd!"},
|
2020-02-17 06:28:11 +01:00
|
|
|
Validate: validatePRNum,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
prNum := struct {
|
|
|
|
Index int64
|
|
|
|
}{}
|
|
|
|
if err := survey.Ask(questions, &prNum); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if prNum.Index < 0 {
|
|
|
|
var confirmed bool
|
|
|
|
for !confirmed {
|
|
|
|
iss, err := issuesSearch(ctx, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
issue = iss
|
|
|
|
confirmation := &survey.Confirm{Message: "Is this the pull request you want to checkout?"}
|
|
|
|
if err := survey.AskOne(confirmation, &confirmed); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-09-17 18:25:09 +02:00
|
|
|
iss, _, err := client.GetIssue(flag.Upstream.Owner, flag.Upstream.Repo, prNum.Index)
|
2020-02-17 06:28:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
issue = iss
|
|
|
|
}
|
|
|
|
|
|
|
|
if issue == nil {
|
|
|
|
return errors.New("no pull request selected")
|
|
|
|
}
|
|
|
|
|
|
|
|
branch := fmt.Sprintf("pr%d-%s", issue.Index, xstrings.ToKebabCase(issue.Title))
|
|
|
|
cmd := exec.Command("git", "fetch", config.Upstream, fmt.Sprintf("pull/%d/head:%s", issue.Index, branch))
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
beaver.Infof("Pull request successfully checked out. Switch to it using `git checkout %s`", branch)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validatePRNum(ans interface{}) error {
|
|
|
|
if err := survey.Required(ans); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := strconv.Atoi(ans.(string)); err != nil {
|
|
|
|
return errors.New("pull request number must be an number")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|