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,71 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package print
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gitea.dev/tea/modules/stack"
|
||||
)
|
||||
|
||||
// PullStackFields are all available fields to print with PullStacksList()
|
||||
var PullStackFields = []string{
|
||||
"id",
|
||||
"title",
|
||||
"base",
|
||||
"state",
|
||||
"auto-restack",
|
||||
"entries",
|
||||
}
|
||||
|
||||
// PullStacksList prints a listing of pull request stacks
|
||||
func PullStacksList(stacks []*stack.Stack, output string, fields []string) error {
|
||||
printables := make([]printable, len(stacks))
|
||||
for i, s := range stacks {
|
||||
printables[i] = &printablePullStack{s}
|
||||
}
|
||||
t := tableFromItems(fields, printables, isMachineReadable(output))
|
||||
return t.print(output)
|
||||
}
|
||||
|
||||
type printablePullStack struct {
|
||||
*stack.Stack
|
||||
}
|
||||
|
||||
func (x printablePullStack) FormatField(field string, machineReadable bool) string {
|
||||
switch field {
|
||||
case "id":
|
||||
return fmt.Sprintf("%d", x.ID)
|
||||
case "title":
|
||||
return x.Title
|
||||
case "base":
|
||||
return x.BaseBranch
|
||||
case "state":
|
||||
return x.State
|
||||
case "auto-restack":
|
||||
return formatBoolean(x.AutoRestack, !machineReadable)
|
||||
case "entries":
|
||||
return fmt.Sprintf("%d", len(x.Entries))
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// PullStackDetail prints a pull request stack, bottom entry first, rendered to stdout
|
||||
func PullStackDetail(s *stack.Stack) {
|
||||
out := fmt.Sprintf(
|
||||
"# Stack #%d: %s (%s)\nbase: **%s**\tauto-restack: %s\n\n",
|
||||
s.ID, s.Title, s.State, s.BaseBranch, formatBoolean(s.AutoRestack, true),
|
||||
)
|
||||
|
||||
for _, entry := range s.Entries {
|
||||
if entry.ParentPullID == 0 {
|
||||
out += fmt.Sprintf("- #%d (%s)\n", entry.PullID, entry.State)
|
||||
} else {
|
||||
out += fmt.Sprintf("- #%d <- #%d (%s)\n", entry.PullID, entry.ParentPullID, entry.State)
|
||||
}
|
||||
}
|
||||
|
||||
outputMarkdown(strings.TrimRight(out, "\n")+"\n", "")
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package print
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"gitea.dev/tea/modules/stack"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPullStacksListJSON(t *testing.T) {
|
||||
stacks := []*stack.Stack{
|
||||
{
|
||||
ID: 1, Title: "feat: base", BaseBranch: "main", State: "open", AutoRestack: true,
|
||||
Entries: []stack.Entry{{PullID: 10}, {PullID: 11}},
|
||||
},
|
||||
}
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
tbl := tableFromItems(
|
||||
PullStackFields,
|
||||
[]printable{&printablePullStack{stacks[0]}},
|
||||
true,
|
||||
)
|
||||
require.NoError(t, tbl.fprint(buf, "json"))
|
||||
|
||||
var result []map[string]string
|
||||
require.NoError(t, json.Unmarshal(buf.Bytes(), &result))
|
||||
require.Len(t, result, 1)
|
||||
assert.Equal(t, "1", result[0]["id"])
|
||||
assert.Equal(t, "feat: base", result[0]["title"])
|
||||
assert.Equal(t, "main", result[0]["base"])
|
||||
assert.Equal(t, "open", result[0]["state"])
|
||||
assert.Equal(t, "true", result[0]["auto-restack"])
|
||||
assert.Equal(t, "2", result[0]["entries"])
|
||||
}
|
||||
|
||||
func TestPullStackDetailShowsEntries(t *testing.T) {
|
||||
s := &stack.Stack{
|
||||
ID: 3, Title: "feat: base", BaseBranch: "main", State: "open", AutoRestack: true,
|
||||
Entries: []stack.Entry{
|
||||
{PullID: 10, Position: 0, State: "merged"},
|
||||
{PullID: 11, ParentPullID: 10, Position: 1, State: "open"},
|
||||
},
|
||||
}
|
||||
|
||||
out := captureStdout(t, func() {
|
||||
PullStackDetail(s)
|
||||
})
|
||||
|
||||
assert.Contains(t, out, "feat: base")
|
||||
assert.Contains(t, out, "main")
|
||||
assert.Contains(t, out, "#10")
|
||||
assert.Contains(t, out, "#11")
|
||||
}
|
||||
Reference in New Issue
Block a user