mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-05 21:38:13 +02:00
Adds a CLI client for the Gitea EE pull request stack API
(GET/POST /repos/{owner}/{repo}/stacks[/{id}[/entries|/restack|/merge]]):
- modules/stack holds the Stack/Entry/MergeResult types shared between
the API client and its output formatting, avoiding an import cycle
between modules/task and modules/print.
- modules/task/pull_stack.go implements the HTTP calls on top of
modules/api.Client, which gains a context-aware DoContext method.
- modules/print/pull_stack.go formats stack listings (table/json/...)
and a single stack's ordered entries.
- cmd/pulls/stack adds the list, show, push, restack and merge
subcommands, wired in as 'tea pr stack' via cmd/pulls.go, following
the repo/login flag routing used by the rest of the pulls commands.
Assisted-by: Codet:GPT-5-Codex
74 lines
1.9 KiB
Go
74 lines
1.9 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package stack
|
|
|
|
import (
|
|
stdctx "context"
|
|
"fmt"
|
|
|
|
"gitea.dev/tea/cmd/flags"
|
|
"gitea.dev/tea/modules/context"
|
|
"gitea.dev/tea/modules/task"
|
|
"gitea.dev/tea/modules/utils"
|
|
|
|
"github.com/urfave/cli/v3"
|
|
)
|
|
|
|
// CmdStackMerge represents a sub command to merge a pull request stack in order
|
|
var CmdStackMerge = cli.Command{
|
|
Name: "merge",
|
|
Aliases: []string{"m"},
|
|
Usage: "Merge a pull request stack in order",
|
|
Description: "Merge every pull request of a stack in order, bottom first",
|
|
ArgsUsage: "<stack id>",
|
|
Action: RunStackMerge,
|
|
Flags: append([]cli.Flag{
|
|
&cli.StringFlag{
|
|
Name: "style",
|
|
Aliases: []string{"s"},
|
|
Usage: "Kind of merge to perform: merge, rebase, squash, rebase-merge",
|
|
Value: "merge",
|
|
},
|
|
&cli.BoolFlag{
|
|
Name: "wait-for-ci",
|
|
Usage: "Schedule the merge to run once required checks pass, instead of merging immediately",
|
|
},
|
|
}, flags.AllDefaultFlags...),
|
|
}
|
|
|
|
// RunStackMerge merges every pull request of a stack in order
|
|
func RunStackMerge(requestCtx stdctx.Context, cmd *cli.Command) error {
|
|
if cmd.Args().Len() != 1 {
|
|
return fmt.Errorf("stack id is required")
|
|
}
|
|
id, err := utils.ArgToIndex(cmd.Args().First())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
ctx, err := context.InitCommand(cmd)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
|
return err
|
|
}
|
|
|
|
result, queued, err := task.MergePullStack(requestCtx, ctx.Login, ctx.Owner, ctx.Repo, id,
|
|
cmd.String("style"), cmd.Bool("wait-for-ci"))
|
|
if queued {
|
|
fmt.Fprintf(ctx.Writer, "merge for stack #%d scheduled\n", id)
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
if result != nil {
|
|
return fmt.Errorf("merged %d pull request(s) before failing: %w", result.Merged, err)
|
|
}
|
|
return err
|
|
}
|
|
|
|
fmt.Fprintf(ctx.Writer, "merged %d pull request(s) of stack #%d\n", result.Merged, id)
|
|
return nil
|
|
}
|