List multiple PRs and offer choice

This commit is contained in:
harryzcy 2023-01-07 17:47:11 -05:00
parent e4ff7b8397
commit 5a0ac6e1e8
No known key found for this signature in database
GPG Key ID: E3C2287691E40E35

View File

@ -6,12 +6,14 @@ package pulls
import ( import (
"fmt" "fmt"
"strings"
"code.gitea.io/sdk/gitea" "code.gitea.io/sdk/gitea"
"code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/cmd/flags"
"code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/context"
"code.gitea.io/tea/modules/utils" "code.gitea.io/tea/modules/utils"
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
@ -61,7 +63,7 @@ var CmdPullsMerge = cli.Command{
return err return err
} }
idx, err = getPullIndexByBranch(ctx, branch) idx, err = getPullIndex(ctx, branch)
if err != nil { if err != nil {
return err return err
} }
@ -83,18 +85,50 @@ var CmdPullsMerge = cli.Command{
}, },
} }
func getPullIndexByBranch(ctx *context.TeaContext, branch string) (int64, error) { func getPullIndex(ctx *context.TeaContext, branch string) (int64, error) {
prs, _, err := ctx.Login.Client().ListRepoPullRequests(ctx.Owner, ctx.Repo, gitea.ListPullRequestsOptions{ prs, _, err := ctx.Login.Client().ListRepoPullRequests(ctx.Owner, ctx.Repo, gitea.ListPullRequestsOptions{
State: gitea.StateOpen, State: gitea.StateOpen,
}) })
if err != nil { if err != nil {
return 0, err return 0, err
} }
if len(prs) == 0 {
return 0, fmt.Errorf("No open PRs found")
}
prOptions := make([]string, len(prs))
// get the PR index for the current branch first
for _, pr := range prs { for _, pr := range prs {
if pr.Head.Ref == branch { if pr.Head.Ref == branch {
return pr.Index, nil prOptions[0] = fmt.Sprintf("#%d: %s", pr.Index, pr.Title)
} }
} }
return 0, fmt.Errorf("No open PR for branch %s", branch)
// then get the rest of the PRs
i := 1
for _, pr := range prs {
if pr.Head.Ref != branch {
prOptions[i] = fmt.Sprintf("#%d: %s", pr.Index, pr.Title)
}
i++
}
selected := ""
q := &survey.Select{
Message: "Select a PR to merge",
Options: prOptions,
PageSize: 10,
}
survey.AskOne(q, &selected)
// get the index from the selected option
before, _, _ := strings.Cut(selected, ":")
before = strings.TrimPrefix(before, "#")
idx, err := utils.ArgToIndex(before)
if err != nil {
return 0, err
}
return idx, nil
} }