paginate PRs
This commit is contained in:
parent
b494ce0c83
commit
6d76ff3be5
@ -11,6 +11,7 @@ import (
|
|||||||
"code.gitea.io/tea/modules/context"
|
"code.gitea.io/tea/modules/context"
|
||||||
"code.gitea.io/tea/modules/task"
|
"code.gitea.io/tea/modules/task"
|
||||||
"code.gitea.io/tea/modules/utils"
|
"code.gitea.io/tea/modules/utils"
|
||||||
|
|
||||||
"github.com/AlecAivazis/survey/v2"
|
"github.com/AlecAivazis/survey/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -39,44 +40,54 @@ func MergePull(ctx *context.TeaContext) error {
|
|||||||
|
|
||||||
// getPullIndex interactively determines the PR index
|
// getPullIndex interactively determines the PR index
|
||||||
func getPullIndex(ctx *context.TeaContext, branch string) (int64, error) {
|
func getPullIndex(ctx *context.TeaContext, branch string) (int64, error) {
|
||||||
// FIXME: pagination to loop over all PRs. Currently only the first page is shown
|
c := ctx.Login.Client()
|
||||||
// note: for repos with many PRs, this may cause latency
|
opts := gitea.ListPullRequestsOptions{
|
||||||
prs, _, err := ctx.Login.Client().ListRepoPullRequests(ctx.Owner, ctx.Repo, gitea.ListPullRequestsOptions{
|
State: gitea.StateOpen,
|
||||||
State: gitea.StateOpen,
|
ListOptions: ctx.GetListOptions(),
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
}
|
||||||
if len(prs) == 0 {
|
|
||||||
return 0, fmt.Errorf("No open PRs found")
|
|
||||||
}
|
|
||||||
|
|
||||||
prOptions := make([]string, 0)
|
|
||||||
|
|
||||||
// get the PR indexes where head branch is the current branch
|
|
||||||
for _, pr := range prs {
|
|
||||||
if pr.Head.Ref == branch {
|
|
||||||
prOptions = append(prOptions, fmt.Sprintf("#%d: %s", pr.Index, pr.Title))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// then get the PR indexes where base branch is the current branch
|
|
||||||
for _, pr := range prs {
|
|
||||||
// don't add the same PR twice, so `pr.Head.Ref != branch`
|
|
||||||
if pr.Base.Ref == branch && pr.Head.Ref != branch {
|
|
||||||
prOptions = append(prOptions, fmt.Sprintf("#%d: %s", pr.Index, pr.Title))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
selected := ""
|
selected := ""
|
||||||
q := &survey.Select{
|
loadMoreOption := "PR not found? Load more PRs..."
|
||||||
Message: "Select a PR to merge",
|
|
||||||
Options: prOptions,
|
// paginated fetch
|
||||||
PageSize: 10,
|
var prs []*gitea.PullRequest
|
||||||
}
|
var err error
|
||||||
err = survey.AskOne(q, &selected)
|
for {
|
||||||
if err != nil {
|
prs, _, err = c.ListRepoPullRequests(ctx.Owner, ctx.Repo, opts)
|
||||||
return 0, err
|
if len(prs) == 0 {
|
||||||
|
return 0, fmt.Errorf("No open PRs found")
|
||||||
|
}
|
||||||
|
opts.ListOptions.Page++
|
||||||
|
prOptions := make([]string, 0)
|
||||||
|
|
||||||
|
// get the PR indexes where head branch is the current branch
|
||||||
|
for _, pr := range prs {
|
||||||
|
if pr.Head.Ref == branch {
|
||||||
|
prOptions = append(prOptions, fmt.Sprintf("#%d: %s", pr.Index, pr.Title))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// then get the PR indexes where base branch is the current branch
|
||||||
|
for _, pr := range prs {
|
||||||
|
// don't add the same PR twice, so `pr.Head.Ref != branch`
|
||||||
|
if pr.Base.Ref == branch && pr.Head.Ref != branch {
|
||||||
|
prOptions = append(prOptions, fmt.Sprintf("#%d: %s", pr.Index, pr.Title))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
prOptions = append(prOptions, loadMoreOption)
|
||||||
|
|
||||||
|
q := &survey.Select{
|
||||||
|
Message: "Select a PR to merge",
|
||||||
|
Options: prOptions,
|
||||||
|
PageSize: 10,
|
||||||
|
}
|
||||||
|
err = survey.AskOne(q, &selected)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if selected != loadMoreOption {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the index from the selected option
|
// get the index from the selected option
|
||||||
|
Loading…
x
Reference in New Issue
Block a user