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,168 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package task
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
stdctx "context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"gitea.dev/tea/modules/api"
|
||||
"gitea.dev/tea/modules/config"
|
||||
"gitea.dev/tea/modules/stack"
|
||||
)
|
||||
|
||||
func stackAPIError(resp *http.Response) error {
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("body read on HTTP error %d: %w", resp.StatusCode, err)
|
||||
}
|
||||
|
||||
var errBody struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &errBody); err == nil && errBody.Message != "" {
|
||||
return fmt.Errorf("%s", errBody.Message)
|
||||
}
|
||||
return fmt.Errorf("%s: %s", resp.Status, string(data))
|
||||
}
|
||||
|
||||
func decodeStackResponse(resp *http.Response, wantStatus int, out any) error {
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
if resp.StatusCode != wantStatus {
|
||||
return stackAPIError(resp)
|
||||
}
|
||||
if out == nil {
|
||||
return nil
|
||||
}
|
||||
return json.NewDecoder(resp.Body).Decode(out)
|
||||
}
|
||||
|
||||
// pullStackLinkOption links a parent and a child pull request in the same repository.
|
||||
type pullStackLinkOption struct {
|
||||
ParentIndex int64 `json:"parent_index"`
|
||||
ChildIndex int64 `json:"child_index"`
|
||||
}
|
||||
|
||||
func postStackJSON(ctx stdctx.Context, login *config.Login, endpoint string, body any, wantStatus int, out any) error {
|
||||
client := api.NewClient(login)
|
||||
|
||||
var reader io.Reader
|
||||
if body != nil {
|
||||
encoded, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
reader = bytes.NewReader(encoded)
|
||||
}
|
||||
|
||||
resp, err := client.DoContext(ctx, http.MethodPost, endpoint, reader, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return decodeStackResponse(resp, wantStatus, out)
|
||||
}
|
||||
|
||||
// ListPullStacks lists the pull request stacks of a repository.
|
||||
func ListPullStacks(ctx stdctx.Context, login *config.Login, owner, repo string) ([]*stack.Stack, error) {
|
||||
client := api.NewClient(login)
|
||||
resp, err := client.DoContext(ctx, http.MethodGet, fmt.Sprintf("/repos/%s/%s/stacks", owner, repo), nil, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var stacks []*stack.Stack
|
||||
if err := decodeStackResponse(resp, http.StatusOK, &stacks); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return stacks, nil
|
||||
}
|
||||
|
||||
// GetPullStack retrieves one stack with its ordered pull requests.
|
||||
func GetPullStack(ctx stdctx.Context, login *config.Login, owner, repo string, id int64) (*stack.Stack, error) {
|
||||
client := api.NewClient(login)
|
||||
resp, err := client.DoContext(ctx, http.MethodGet, fmt.Sprintf("/repos/%s/%s/stacks/%d", owner, repo, id), nil, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var s stack.Stack
|
||||
if err := decodeStackResponse(resp, http.StatusOK, &s); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
// CreatePullStack explicitly links two same-repository pull requests, forming a new stack.
|
||||
func CreatePullStack(ctx stdctx.Context, login *config.Login, owner, repo string, parentIndex, childIndex int64) (*stack.Stack, error) {
|
||||
var s stack.Stack
|
||||
err := postStackJSON(ctx, login, fmt.Sprintf("/repos/%s/%s/stacks", owner, repo),
|
||||
pullStackLinkOption{ParentIndex: parentIndex, ChildIndex: childIndex}, http.StatusCreated, &s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
// AddPullStackEntry appends one pull request to the tip of an existing stack.
|
||||
func AddPullStackEntry(ctx stdctx.Context, login *config.Login, owner, repo string, id, parentIndex, childIndex int64) (*stack.Stack, error) {
|
||||
var s stack.Stack
|
||||
err := postStackJSON(ctx, login, fmt.Sprintf("/repos/%s/%s/stacks/%d/entries", owner, repo, id),
|
||||
pullStackLinkOption{ParentIndex: parentIndex, ChildIndex: childIndex}, http.StatusCreated, &s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
// RestackPullStack queues a restack of every entry above the merged pull request in the stack.
|
||||
func RestackPullStack(ctx stdctx.Context, login *config.Login, owner, repo string, id int64) error {
|
||||
client := api.NewClient(login)
|
||||
resp, err := client.DoContext(ctx, http.MethodPost, fmt.Sprintf("/repos/%s/%s/stacks/%d/restack", owner, repo, id), nil, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return decodeStackResponse(resp, http.StatusAccepted, nil)
|
||||
}
|
||||
|
||||
type pullStackMergeOption struct {
|
||||
Style string `json:"style"`
|
||||
WaitForCI bool `json:"wait_for_ci"`
|
||||
}
|
||||
|
||||
// MergePullStack merges every pull request of a stack in order. If waitForCI is set, the merge is
|
||||
// scheduled instead of applied immediately and MergePullStack returns with queued=true. Otherwise
|
||||
// it returns the number of pull requests merged; a non-nil error means the merge stopped partway,
|
||||
// with result.Merged reporting how many pull requests succeeded before that.
|
||||
func MergePullStack(ctx stdctx.Context, login *config.Login, owner, repo string, id int64, style string, waitForCI bool) (result *stack.MergeResult, queued bool, err error) {
|
||||
client := api.NewClient(login)
|
||||
encoded, err := json.Marshal(pullStackMergeOption{Style: style, WaitForCI: waitForCI})
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
resp, err := client.DoContext(ctx, http.MethodPost, fmt.Sprintf("/repos/%s/%s/stacks/%d/merge", owner, repo, id),
|
||||
bytes.NewReader(encoded), nil)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
if resp.StatusCode == http.StatusAccepted {
|
||||
return nil, true, nil
|
||||
}
|
||||
|
||||
var body stack.MergeResult
|
||||
if decodeErr := json.NewDecoder(resp.Body).Decode(&body); decodeErr != nil {
|
||||
return nil, false, fmt.Errorf("%s: failed to decode response: %w", resp.Status, decodeErr)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return &body, false, fmt.Errorf("%s", body.Error)
|
||||
}
|
||||
return &body, false, nil
|
||||
}
|
||||
@@ -0,0 +1,229 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package task
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"gitea.dev/tea/modules/config"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestListPullStacks(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, http.MethodGet, r.Method)
|
||||
assert.Equal(t, "/api/v1/repos/owner/repo/stacks", r.URL.Path)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`[{"id":1,"repo_id":2,"owner_id":3,"title":"feat: base","base_branch":"main","state":"open","auto_restack":true,"entries":[{"pull_id":10,"parent_pull_id":0,"position":0,"state":"open","last_restacked_sha":"abc"}]}]`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
stacks, err := ListPullStacks(t.Context(), &config.Login{
|
||||
Name: "test",
|
||||
URL: server.URL,
|
||||
Token: "secret-token",
|
||||
}, "owner", "repo")
|
||||
require.NoError(t, err)
|
||||
require.Len(t, stacks, 1)
|
||||
assert.Equal(t, int64(1), stacks[0].ID)
|
||||
assert.Equal(t, "feat: base", stacks[0].Title)
|
||||
assert.Equal(t, "main", stacks[0].BaseBranch)
|
||||
assert.True(t, stacks[0].AutoRestack)
|
||||
require.Len(t, stacks[0].Entries, 1)
|
||||
assert.Equal(t, int64(10), stacks[0].Entries[0].PullID)
|
||||
}
|
||||
|
||||
func TestGetPullStack(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, http.MethodGet, r.Method)
|
||||
assert.Equal(t, "/api/v1/repos/owner/repo/stacks/7", r.URL.Path)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"id":7,"repo_id":2,"owner_id":3,"title":"feat: base","base_branch":"main","state":"open","auto_restack":false,"entries":[]}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
stack, err := GetPullStack(t.Context(), &config.Login{
|
||||
Name: "test",
|
||||
URL: server.URL,
|
||||
Token: "secret-token",
|
||||
}, "owner", "repo", 7)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(7), stack.ID)
|
||||
assert.Equal(t, "open", stack.State)
|
||||
}
|
||||
|
||||
func TestGetPullStackNotFound(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
_, _ = w.Write([]byte(`{"message":"stack does not exist"}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
_, err := GetPullStack(t.Context(), &config.Login{
|
||||
Name: "test",
|
||||
URL: server.URL,
|
||||
Token: "secret-token",
|
||||
}, "owner", "repo", 7)
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, "stack does not exist", err.Error())
|
||||
}
|
||||
|
||||
func TestCreatePullStack(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, http.MethodPost, r.Method)
|
||||
assert.Equal(t, "/api/v1/repos/owner/repo/stacks", r.URL.Path)
|
||||
body, err := io.ReadAll(r.Body)
|
||||
require.NoError(t, err)
|
||||
assert.JSONEq(t, `{"parent_index":1,"child_index":2}`, string(body))
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
_, _ = w.Write([]byte(`{"id":9,"repo_id":2,"owner_id":3,"title":"feat: base","base_branch":"main","state":"open","auto_restack":true,"entries":[]}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
stack, err := CreatePullStack(t.Context(), &config.Login{
|
||||
Name: "test",
|
||||
URL: server.URL,
|
||||
Token: "secret-token",
|
||||
}, "owner", "repo", 1, 2)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(9), stack.ID)
|
||||
}
|
||||
|
||||
func TestCreatePullStackConflict(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
_, _ = w.Write([]byte(`{"message":"parent_index and child_index are required"}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
_, err := CreatePullStack(t.Context(), &config.Login{
|
||||
Name: "test",
|
||||
URL: server.URL,
|
||||
Token: "secret-token",
|
||||
}, "owner", "repo", 0, 2)
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, "parent_index and child_index are required", err.Error())
|
||||
}
|
||||
|
||||
func TestAddPullStackEntry(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, http.MethodPost, r.Method)
|
||||
assert.Equal(t, "/api/v1/repos/owner/repo/stacks/5/entries", r.URL.Path)
|
||||
body, err := io.ReadAll(r.Body)
|
||||
require.NoError(t, err)
|
||||
assert.JSONEq(t, `{"parent_index":2,"child_index":3}`, string(body))
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
_, _ = w.Write([]byte(`{"id":5,"repo_id":2,"owner_id":3,"title":"feat: base","base_branch":"main","state":"open","auto_restack":true,"entries":[]}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
stack, err := AddPullStackEntry(t.Context(), &config.Login{
|
||||
Name: "test",
|
||||
URL: server.URL,
|
||||
Token: "secret-token",
|
||||
}, "owner", "repo", 5, 2, 3)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(5), stack.ID)
|
||||
}
|
||||
|
||||
func TestRestackPullStack(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, http.MethodPost, r.Method)
|
||||
assert.Equal(t, "/api/v1/repos/owner/repo/stacks/4/restack", r.URL.Path)
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
err := RestackPullStack(t.Context(), &config.Login{
|
||||
Name: "test",
|
||||
URL: server.URL,
|
||||
Token: "secret-token",
|
||||
}, "owner", "repo", 4)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestRestackPullStackConflict(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusConflict)
|
||||
_, _ = w.Write([]byte(`{"message":"stack is not open"}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
err := RestackPullStack(t.Context(), &config.Login{
|
||||
Name: "test",
|
||||
URL: server.URL,
|
||||
Token: "secret-token",
|
||||
}, "owner", "repo", 4)
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, "stack is not open", err.Error())
|
||||
}
|
||||
|
||||
func TestMergePullStack(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, http.MethodPost, r.Method)
|
||||
assert.Equal(t, "/api/v1/repos/owner/repo/stacks/6/merge", r.URL.Path)
|
||||
body, err := io.ReadAll(r.Body)
|
||||
require.NoError(t, err)
|
||||
assert.JSONEq(t, `{"style":"squash","wait_for_ci":false}`, string(body))
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"merged":3}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
result, queued, err := MergePullStack(t.Context(), &config.Login{
|
||||
Name: "test",
|
||||
URL: server.URL,
|
||||
Token: "secret-token",
|
||||
}, "owner", "repo", 6, "squash", false)
|
||||
require.NoError(t, err)
|
||||
assert.False(t, queued)
|
||||
require.NotNil(t, result)
|
||||
assert.Equal(t, int64(3), result.Merged)
|
||||
}
|
||||
|
||||
func TestMergePullStackQueued(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
require.NoError(t, err)
|
||||
assert.JSONEq(t, `{"style":"merge","wait_for_ci":true}`, string(body))
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
result, queued, err := MergePullStack(t.Context(), &config.Login{
|
||||
Name: "test",
|
||||
URL: server.URL,
|
||||
Token: "secret-token",
|
||||
}, "owner", "repo", 6, "merge", true)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, queued)
|
||||
assert.Nil(t, result)
|
||||
}
|
||||
|
||||
func TestMergePullStackPartialFailure(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusConflict)
|
||||
_, _ = w.Write([]byte(`{"merged":1,"error":"PR #4 is not mergeable"}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
result, queued, err := MergePullStack(t.Context(), &config.Login{
|
||||
Name: "test",
|
||||
URL: server.URL,
|
||||
Token: "secret-token",
|
||||
}, "owner", "repo", 6, "merge", false)
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, "PR #4 is not mergeable", err.Error())
|
||||
assert.False(t, queued)
|
||||
require.NotNil(t, result)
|
||||
assert.Equal(t, int64(1), result.Merged)
|
||||
}
|
||||
Reference in New Issue
Block a user