paginate PRs

This commit is contained in:
techknowlogick 2024-07-26 15:05:21 -04:00
parent b494ce0c83
commit 6d76ff3be5

View File

@ -11,6 +11,7 @@ import (
"code.gitea.io/tea/modules/context"
"code.gitea.io/tea/modules/task"
"code.gitea.io/tea/modules/utils"
"github.com/AlecAivazis/survey/v2"
)
@ -39,18 +40,23 @@ func MergePull(ctx *context.TeaContext) error {
// getPullIndex interactively determines the PR index
func getPullIndex(ctx *context.TeaContext, branch string) (int64, error) {
// FIXME: pagination to loop over all PRs. Currently only the first page is shown
// note: for repos with many PRs, this may cause latency
prs, _, err := ctx.Login.Client().ListRepoPullRequests(ctx.Owner, ctx.Repo, gitea.ListPullRequestsOptions{
c := ctx.Login.Client()
opts := gitea.ListPullRequestsOptions{
State: gitea.StateOpen,
})
if err != nil {
return 0, err
ListOptions: ctx.GetListOptions(),
}
selected := ""
loadMoreOption := "PR not found? Load more PRs..."
// paginated fetch
var prs []*gitea.PullRequest
var err error
for {
prs, _, err = c.ListRepoPullRequests(ctx.Owner, ctx.Repo, opts)
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
@ -68,7 +74,8 @@ func getPullIndex(ctx *context.TeaContext, branch string) (int64, error) {
}
}
selected := ""
prOptions = append(prOptions, loadMoreOption)
q := &survey.Select{
Message: "Select a PR to merge",
Options: prOptions,
@ -78,6 +85,10 @@ func getPullIndex(ctx *context.TeaContext, branch string) (int64, error) {
if err != nil {
return 0, err
}
if selected != loadMoreOption {
break
}
}
// get the index from the selected option
before, _, _ := strings.Cut(selected, ":")