Move interactive part to interact package
This commit is contained in:
parent
807fd93554
commit
86bb8dbcb8
@ -5,15 +5,13 @@
|
||||
package pulls
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"code.gitea.io/tea/cmd/flags"
|
||||
"code.gitea.io/tea/modules/context"
|
||||
"code.gitea.io/tea/modules/interact"
|
||||
"code.gitea.io/tea/modules/task"
|
||||
"code.gitea.io/tea/modules/utils"
|
||||
|
||||
"github.com/AlecAivazis/survey/v2"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
@ -54,83 +52,14 @@ var CmdPullsMerge = cli.Command{
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if ctx.LocalRepo == nil {
|
||||
return fmt.Errorf("Must specify a PR index")
|
||||
}
|
||||
|
||||
branch, _, err := ctx.LocalRepo.TeaGetCurrentBranchNameAndSHA()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
idx, err = getPullIndex(ctx, branch)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// If no PR index is provided, try interactive mode
|
||||
return interact.MergePull(ctx)
|
||||
}
|
||||
|
||||
success, _, err := ctx.Login.Client().MergePullRequest(ctx.Owner, ctx.Repo, idx, gitea.MergePullRequestOption{
|
||||
return task.PullMerge(ctx.Login, ctx.Owner, ctx.Repo, idx, gitea.MergePullRequestOption{
|
||||
Style: gitea.MergeStyle(ctx.String("style")),
|
||||
Title: ctx.String("title"),
|
||||
Message: ctx.String("message"),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !success {
|
||||
return fmt.Errorf("Failed to merge PR. Is it still open?")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func getPullIndex(ctx *context.TeaContext, branch string) (int64, error) {
|
||||
prs, _, err := ctx.Login.Client().ListRepoPullRequests(ctx.Owner, ctx.Repo, gitea.ListPullRequestsOptions{
|
||||
State: gitea.StateOpen,
|
||||
})
|
||||
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 := ""
|
||||
q := &survey.Select{
|
||||
Message: "Select a PR to merge",
|
||||
Options: prOptions,
|
||||
PageSize: 10,
|
||||
}
|
||||
err = survey.AskOne(q, &selected)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
86
modules/interact/pull_merge.go
Normal file
86
modules/interact/pull_merge.go
Normal file
@ -0,0 +1,86 @@
|
||||
package interact
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"code.gitea.io/tea/modules/context"
|
||||
"code.gitea.io/tea/modules/task"
|
||||
"code.gitea.io/tea/modules/utils"
|
||||
"github.com/AlecAivazis/survey/v2"
|
||||
)
|
||||
|
||||
// MergePull interactively creates a PR
|
||||
func MergePull(ctx *context.TeaContext) error {
|
||||
if ctx.LocalRepo == nil {
|
||||
return fmt.Errorf("Must specify a PR index")
|
||||
}
|
||||
|
||||
branch, _, err := ctx.LocalRepo.TeaGetCurrentBranchNameAndSHA()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
idx, err := getPullIndex(ctx, branch)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return task.PullMerge(ctx.Login, ctx.Owner, ctx.Repo, idx, gitea.MergePullRequestOption{
|
||||
Style: gitea.MergeStyle(ctx.String("style")),
|
||||
Title: ctx.String("title"),
|
||||
Message: ctx.String("message"),
|
||||
})
|
||||
}
|
||||
|
||||
// getPullIndex interactively determines the PR index
|
||||
func getPullIndex(ctx *context.TeaContext, branch string) (int64, error) {
|
||||
prs, _, err := ctx.Login.Client().ListRepoPullRequests(ctx.Owner, ctx.Repo, gitea.ListPullRequestsOptions{
|
||||
State: gitea.StateOpen,
|
||||
})
|
||||
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 := ""
|
||||
q := &survey.Select{
|
||||
Message: "Select a PR to merge",
|
||||
Options: prOptions,
|
||||
PageSize: 10,
|
||||
}
|
||||
err = survey.AskOne(q, &selected)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
21
modules/task/pull_merge.go
Normal file
21
modules/task/pull_merge.go
Normal file
@ -0,0 +1,21 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"code.gitea.io/tea/modules/config"
|
||||
)
|
||||
|
||||
// PullMerge merges a PR
|
||||
func PullMerge(login *config.Login, repoOwner, repoName string, index int64, opt gitea.MergePullRequestOption) error {
|
||||
client := login.Client()
|
||||
success, _, err := client.MergePullRequest(repoOwner, repoName, index, opt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !success {
|
||||
return fmt.Errorf("Failed to merge PR. Is it still open?")
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user