Support auto detecting branch for PRs

This commit is contained in:
harryzcy 2023-01-04 03:21:28 -05:00
parent 6a848cb72a
commit bff965e84c
No known key found for this signature in database
GPG Key ID: E3C2287691E40E35

View File

@ -44,15 +44,29 @@ var CmdPullsMerge = cli.Command{
ctx := context.InitCommand(cmd) ctx := context.InitCommand(cmd)
ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
if ctx.Args().Len() != 1 { var idx int64
var err error
if ctx.Args().Len() == 1 {
idx, err = utils.ArgToIndex(ctx.Args().First())
if err != nil {
return err
}
} else {
if ctx.LocalRepo == nil {
return fmt.Errorf("Must specify a PR index") return fmt.Errorf("Must specify a PR index")
} }
idx, err := utils.ArgToIndex(ctx.Args().First()) branch, _, err := ctx.LocalRepo.TeaGetCurrentBranchNameAndSHA()
if err != nil { if err != nil {
return err return err
} }
idx, err = GetPullIndexByBranch(ctx, branch)
if err != nil {
return err
}
}
success, _, err := ctx.Login.Client().MergePullRequest(ctx.Owner, ctx.Repo, idx, gitea.MergePullRequestOption{ success, _, err := ctx.Login.Client().MergePullRequest(ctx.Owner, ctx.Repo, idx, gitea.MergePullRequestOption{
Style: gitea.MergeStyle(ctx.String("style")), Style: gitea.MergeStyle(ctx.String("style")),
Title: ctx.String("title"), Title: ctx.String("title"),
@ -68,3 +82,19 @@ var CmdPullsMerge = cli.Command{
return nil return nil
}, },
} }
func GetPullIndexByBranch(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
}
for _, pr := range prs {
if pr.Head.Ref == branch {
return pr.Index, nil
}
}
return 0, fmt.Errorf("No open PR for branch %s", branch)
}