mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-05 21:38:13 +02:00
feat(pulls): add tea pr stack list|show|push|restack|merge
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
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package pulls
|
||||
|
||||
import (
|
||||
stdctx "context"
|
||||
|
||||
"gitea.dev/tea/cmd/pulls/stack"
|
||||
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
// CmdPullsStack represents the stack command to manage pull request stacks
|
||||
var CmdPullsStack = cli.Command{
|
||||
Name: "stack",
|
||||
Usage: "Manage pull request stacks",
|
||||
Description: "List, show, push, restack, and merge pull request stacks",
|
||||
Action: runPullsStackDefault,
|
||||
Commands: []*cli.Command{
|
||||
&stack.CmdStackList,
|
||||
&stack.CmdStackShow,
|
||||
&stack.CmdStackPush,
|
||||
&stack.CmdStackRestack,
|
||||
&stack.CmdStackMerge,
|
||||
},
|
||||
}
|
||||
|
||||
func runPullsStackDefault(ctx stdctx.Context, cmd *cli.Command) error {
|
||||
return stack.RunStackList(ctx, cmd)
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package stack
|
||||
|
||||
import (
|
||||
stdctx "context"
|
||||
|
||||
"gitea.dev/tea/cmd/flags"
|
||||
"gitea.dev/tea/modules/context"
|
||||
"gitea.dev/tea/modules/print"
|
||||
"gitea.dev/tea/modules/task"
|
||||
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
var stackFieldsFlag = flags.FieldsFlag(print.PullStackFields, []string{
|
||||
"id", "title", "base", "state", "entries",
|
||||
})
|
||||
|
||||
// CmdStackList represents a sub command to list pull request stacks
|
||||
var CmdStackList = cli.Command{
|
||||
Name: "list",
|
||||
Aliases: []string{"ls"},
|
||||
Usage: "List pull request stacks of the repository",
|
||||
Description: "List pull request stacks of the repository",
|
||||
ArgsUsage: " ", // command does not accept arguments
|
||||
Action: RunStackList,
|
||||
Flags: append([]cli.Flag{stackFieldsFlag}, flags.AllDefaultFlags...),
|
||||
}
|
||||
|
||||
// RunStackList lists pull request stacks of the repository
|
||||
func RunStackList(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||
ctx, err := context.InitCommand(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ctx.Ensure(context.CtxRequirement{RemoteRepo: true}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
stacks, err := task.ListPullStacks(requestCtx, ctx.Login, ctx.Owner, ctx.Repo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fields, err := stackFieldsFlag.GetValues(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return print.PullStacksList(stacks, ctx.Output, fields)
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package stack
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/urfave/cli/v3"
|
||||
|
||||
"gitea.dev/tea/modules/config"
|
||||
)
|
||||
|
||||
func TestRunStackListRequiresRepoContext(t *testing.T) {
|
||||
oldWd, err := os.Getwd()
|
||||
require.NoError(t, err)
|
||||
|
||||
require.NoError(t, os.Chdir(t.TempDir()))
|
||||
t.Cleanup(func() {
|
||||
require.NoError(t, os.Chdir(oldWd))
|
||||
})
|
||||
|
||||
config.SetConfigForTesting(config.LocalConfig{
|
||||
Logins: []config.Login{{
|
||||
Name: "test",
|
||||
URL: "https://gitea.example.com",
|
||||
Token: "token",
|
||||
User: "tester",
|
||||
Default: true,
|
||||
}},
|
||||
})
|
||||
|
||||
cmd := &cli.Command{
|
||||
Name: CmdStackList.Name,
|
||||
Flags: CmdStackList.Flags,
|
||||
}
|
||||
require.NoError(t, cmd.Set("login", "test"))
|
||||
|
||||
err = RunStackList(t.Context(), cmd)
|
||||
require.ErrorContains(t, err, "remote repository required")
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
// 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
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package stack
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"gitea.dev/tea/modules/config"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestStackMergeRequiresStackID(t *testing.T) {
|
||||
config.SetConfigForTesting(config.LocalConfig{
|
||||
Logins: []config.Login{{
|
||||
Name: "testLogin",
|
||||
URL: "https://gitea.example.com",
|
||||
Token: "test-token",
|
||||
User: "testUser",
|
||||
Default: true,
|
||||
}},
|
||||
})
|
||||
t.Cleanup(func() {
|
||||
config.SetConfigForTesting(config.LocalConfig{})
|
||||
})
|
||||
|
||||
cmd := CmdStackMerge
|
||||
err := cmd.Run(context.Background(), []string{"merge", "--login", "testLogin", "--repo", "user/repo"})
|
||||
assert.ErrorContains(t, err, "stack id is required")
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
// 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/print"
|
||||
pullstack "gitea.dev/tea/modules/stack"
|
||||
"gitea.dev/tea/modules/task"
|
||||
"gitea.dev/tea/modules/utils"
|
||||
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
// CmdStackPush represents a sub command to link a pull request onto a stack
|
||||
var CmdStackPush = cli.Command{
|
||||
Name: "push",
|
||||
Usage: "Link a pull request onto a stack",
|
||||
Description: "Link a child pull request onto its parent, creating a new stack or extending an existing one with --stack",
|
||||
ArgsUsage: "<parent index> <child index>",
|
||||
Action: RunStackPush,
|
||||
Flags: append([]cli.Flag{
|
||||
&cli.Int64Flag{
|
||||
Name: "stack",
|
||||
Usage: "id of an existing stack to append the pull requests to, instead of creating a new stack",
|
||||
},
|
||||
}, flags.AllDefaultFlags...),
|
||||
}
|
||||
|
||||
// RunStackPush links a parent and a child pull request into a stack
|
||||
func RunStackPush(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||
if cmd.Args().Len() != 2 {
|
||||
return fmt.Errorf("parent index and child index are required")
|
||||
}
|
||||
parentIndex, err := utils.ArgToIndex(cmd.Args().Get(0))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
childIndex, err := utils.ArgToIndex(cmd.Args().Get(1))
|
||||
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
|
||||
}
|
||||
|
||||
var s *pullstack.Stack
|
||||
if stackID := cmd.Int64("stack"); stackID != 0 {
|
||||
s, err = task.AddPullStackEntry(requestCtx, ctx.Login, ctx.Owner, ctx.Repo, stackID, parentIndex, childIndex)
|
||||
} else {
|
||||
s, err = task.CreatePullStack(requestCtx, ctx.Login, ctx.Owner, ctx.Repo, parentIndex, childIndex)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
print.PullStackDetail(s)
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package stack
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"gitea.dev/tea/modules/config"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestStackPush(t *testing.T) {
|
||||
config.SetConfigForTesting(config.LocalConfig{
|
||||
Logins: []config.Login{{
|
||||
Name: "testLogin",
|
||||
URL: "https://gitea.example.com",
|
||||
Token: "test-token",
|
||||
User: "testUser",
|
||||
Default: true,
|
||||
}},
|
||||
})
|
||||
t.Cleanup(func() {
|
||||
config.SetConfigForTesting(config.LocalConfig{})
|
||||
})
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
errContains string
|
||||
}{
|
||||
{
|
||||
name: "no arguments",
|
||||
args: []string{},
|
||||
errContains: "parent index and child index are required",
|
||||
},
|
||||
{
|
||||
name: "missing child index",
|
||||
args: []string{"1"},
|
||||
errContains: "parent index and child index are required",
|
||||
},
|
||||
{
|
||||
name: "non-numeric parent index",
|
||||
args: []string{"abc", "2"},
|
||||
errContains: "",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cmd := CmdStackPush
|
||||
args := append([]string{"push"}, tt.args...)
|
||||
args = append(args, "--login", "testLogin", "--repo", "user/repo")
|
||||
err := cmd.Run(context.Background(), args)
|
||||
assert.Error(t, err)
|
||||
if tt.errContains != "" {
|
||||
assert.Contains(t, err.Error(), tt.errContains)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
// 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"
|
||||
)
|
||||
|
||||
// CmdStackRestack represents a sub command to queue a stack restack
|
||||
var CmdStackRestack = cli.Command{
|
||||
Name: "restack",
|
||||
Usage: "Queue a pull request stack restack",
|
||||
Description: "Retarget every open pull request above a merged one in the stack onto its parent's new base",
|
||||
ArgsUsage: "<stack id>",
|
||||
Action: RunStackRestack,
|
||||
Flags: flags.AllDefaultFlags,
|
||||
}
|
||||
|
||||
// RunStackRestack queues a restack of a pull request stack
|
||||
func RunStackRestack(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
|
||||
}
|
||||
|
||||
if err := task.RestackPullStack(requestCtx, ctx.Login, ctx.Owner, ctx.Repo, id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Fprintf(ctx.Writer, "restack for stack #%d queued\n", id)
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package stack
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"gitea.dev/tea/modules/config"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestStackRestackRequiresStackID(t *testing.T) {
|
||||
config.SetConfigForTesting(config.LocalConfig{
|
||||
Logins: []config.Login{{
|
||||
Name: "testLogin",
|
||||
URL: "https://gitea.example.com",
|
||||
Token: "test-token",
|
||||
User: "testUser",
|
||||
Default: true,
|
||||
}},
|
||||
})
|
||||
t.Cleanup(func() {
|
||||
config.SetConfigForTesting(config.LocalConfig{})
|
||||
})
|
||||
|
||||
cmd := CmdStackRestack
|
||||
err := cmd.Run(context.Background(), []string{"restack", "--login", "testLogin", "--repo", "user/repo"})
|
||||
assert.ErrorContains(t, err, "stack id is required")
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package stack
|
||||
|
||||
import (
|
||||
stdctx "context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"gitea.dev/tea/cmd/flags"
|
||||
"gitea.dev/tea/modules/context"
|
||||
"gitea.dev/tea/modules/print"
|
||||
"gitea.dev/tea/modules/task"
|
||||
"gitea.dev/tea/modules/utils"
|
||||
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
// CmdStackShow represents a sub command to show a pull request stack
|
||||
var CmdStackShow = cli.Command{
|
||||
Name: "show",
|
||||
Aliases: []string{"get"},
|
||||
Usage: "Show a pull request stack",
|
||||
Description: "Show a pull request stack, its base branch and its ordered pull requests",
|
||||
ArgsUsage: "<stack id>",
|
||||
Action: RunStackShow,
|
||||
Flags: flags.AllDefaultFlags,
|
||||
}
|
||||
|
||||
// RunStackShow shows a pull request stack
|
||||
func RunStackShow(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
|
||||
}
|
||||
|
||||
s, err := task.GetPullStack(requestCtx, ctx.Login, ctx.Owner, ctx.Repo, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if ctx.String("output") == "json" {
|
||||
encoder := json.NewEncoder(ctx.Writer)
|
||||
encoder.SetIndent("", "\t")
|
||||
return encoder.Encode(s)
|
||||
}
|
||||
|
||||
print.PullStackDetail(s)
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package stack
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"gitea.dev/tea/modules/config"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestStackShowRequiresStackID(t *testing.T) {
|
||||
config.SetConfigForTesting(config.LocalConfig{
|
||||
Logins: []config.Login{{
|
||||
Name: "testLogin",
|
||||
URL: "https://gitea.example.com",
|
||||
Token: "test-token",
|
||||
User: "testUser",
|
||||
Default: true,
|
||||
}},
|
||||
})
|
||||
t.Cleanup(func() {
|
||||
config.SetConfigForTesting(config.LocalConfig{})
|
||||
})
|
||||
|
||||
cmd := CmdStackShow
|
||||
err := cmd.Run(context.Background(), []string{"show", "--login", "testLogin", "--repo", "user/repo"})
|
||||
assert.ErrorContains(t, err, "stack id is required")
|
||||
}
|
||||
|
||||
func TestStackShowRejectsInvalidStackID(t *testing.T) {
|
||||
config.SetConfigForTesting(config.LocalConfig{
|
||||
Logins: []config.Login{{
|
||||
Name: "testLogin",
|
||||
URL: "https://gitea.example.com",
|
||||
Token: "test-token",
|
||||
User: "testUser",
|
||||
Default: true,
|
||||
}},
|
||||
})
|
||||
t.Cleanup(func() {
|
||||
config.SetConfigForTesting(config.LocalConfig{})
|
||||
})
|
||||
|
||||
cmd := CmdStackShow
|
||||
err := cmd.Run(context.Background(), []string{"show", "notanumber", "--login", "testLogin", "--repo", "user/repo"})
|
||||
assert.Error(t, err)
|
||||
}
|
||||
Reference in New Issue
Block a user