mirror of
https://gitea.com/gitea/tea.git
synced 2026-06-05 18:58:43 +02:00
feat: add wiki CLI commands (#998)
## Summary Add first-class `tea wiki` commands backed by the existing Gitea wiki API and SDK support. ## What this adds - `tea wiki list` - `tea wiki view <page>` - `tea wiki revisions <page>` - `tea wiki create` - `tea wiki edit <page>` - `tea wiki delete <page>` ## Implementation details - registers a new top-level `wiki` entity command - keeps command logic under `cmd/wiki/` - adds wiki-specific renderers in `modules/print/wiki.go` - adds wiki task helpers in `modules/task/wiki.go` - reuses existing repo/login/output/pagination patterns used elsewhere in `tea` - base64-encodes wiki content for create/edit API calls - requires explicit `--confirm` for delete - preserves the current page title during edit when `--title` is omitted ## Test coverage The PR is intentionally split into two commits: 1. `feat: add wiki CLI commands` 2. `test: add wiki integration coverage` Validation performed: - focused command, task, and print tests for the new wiki functionality - integration coverage for the wiki command lifecycle - `make lint` - `make fmt-check` - `make docs-check` - `make build` - upstream PR CI passed: - `check-and-test / Integration Test` - `check-and-test / Lint Build And Unit Coverage` ## Motivation This makes `tea` a better interface for both human and agent-driven workflows by exposing wiki operations as stable first-class CLI commands instead of requiring ad-hoc API calls or custom wrappers. --- Generated by Hermes Agent with GPT-5.4 --------- Co-authored-by: nitro <nitro@nitroui-Macmini.local> Reviewed-on: https://gitea.com/gitea/tea/pulls/998 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: kuil09 <202447+kuil09@noreply.gitea.com> Co-committed-by: kuil09 <202447+kuil09@noreply.gitea.com>
This commit is contained in:
@@ -0,0 +1,170 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package integration
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.dev/sdk"
|
||||
teacmd "gitea.dev/tea/cmd"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestWikiCommandLifecycle(t *testing.T) {
|
||||
login := createIntegrationLogin(t)
|
||||
client := login.Client()
|
||||
repoName := fmt.Sprintf("tea-wiki-integration-%d", time.Now().UnixNano())
|
||||
|
||||
_, _, err := client.CreateRepo(context.Background(), gitea.CreateRepoOption{Name: repoName})
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
if _, delErr := client.DeleteRepo(context.Background(), login.User, repoName); delErr != nil {
|
||||
t.Logf("failed to delete integration test repo %q: %v", repoName, delErr)
|
||||
}
|
||||
})
|
||||
|
||||
repoSlug := fmt.Sprintf("%s/%s", login.User, repoName)
|
||||
|
||||
createOutput := runTeaCommand(t,
|
||||
"wiki", "create",
|
||||
"--login", login.Name,
|
||||
"--repo", repoSlug,
|
||||
"--title", "Home",
|
||||
"--content", "# Home",
|
||||
"--message", "create home",
|
||||
"--output", "json",
|
||||
)
|
||||
assert.Contains(t, createOutput, "\"title\": \"Home\"")
|
||||
|
||||
page, _, err := client.Wiki.GetPage(context.Background(), login.User, repoName, "Home")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "Home", page.Title)
|
||||
assert.Contains(t, decodeBase64String(t, page.ContentBase64), "# Home")
|
||||
|
||||
listOutput := runTeaCommand(t,
|
||||
"wiki", "list",
|
||||
"--login", login.Name,
|
||||
"--repo", repoSlug,
|
||||
"--output", "json",
|
||||
)
|
||||
assert.Contains(t, listOutput, "\"title\": \"Home\"")
|
||||
|
||||
emptyRepoName := fmt.Sprintf("tea-wiki-empty-%d", time.Now().UnixNano())
|
||||
_, _, err = client.CreateRepo(context.Background(), gitea.CreateRepoOption{Name: emptyRepoName})
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
if _, delErr := client.DeleteRepo(context.Background(), login.User, emptyRepoName); delErr != nil {
|
||||
t.Logf("failed to delete empty wiki repo %q: %v", emptyRepoName, delErr)
|
||||
}
|
||||
})
|
||||
emptyRepoSlug := fmt.Sprintf("%s/%s", login.User, emptyRepoName)
|
||||
emptyListOutput := runTeaCommand(t,
|
||||
"wiki", "list",
|
||||
"--login", login.Name,
|
||||
"--repo", emptyRepoSlug,
|
||||
)
|
||||
assert.Contains(t, emptyListOutput, "No wiki pages found")
|
||||
|
||||
viewOutput := runTeaCommand(t,
|
||||
"wiki", "view", "Home",
|
||||
"--login", login.Name,
|
||||
"--repo", repoSlug,
|
||||
"--output", "json",
|
||||
)
|
||||
assert.Contains(t, viewOutput, "\"title\": \"Home\"")
|
||||
assert.Contains(t, viewOutput, "\"content\": \"# Home\"")
|
||||
|
||||
runTeaCommand(t,
|
||||
"wiki", "edit", "Home",
|
||||
"--login", login.Name,
|
||||
"--repo", repoSlug,
|
||||
"--content", "# Updated",
|
||||
"--message", "edit home",
|
||||
"--output", "json",
|
||||
)
|
||||
|
||||
page, _, err = client.Wiki.GetPage(context.Background(), login.User, repoName, "Home")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "Home", page.Title)
|
||||
assert.Contains(t, decodeBase64String(t, page.ContentBase64), "# Updated")
|
||||
|
||||
viewRenderedOutput := runTeaCommand(t,
|
||||
"wiki", "view", "Home",
|
||||
"--login", login.Name,
|
||||
"--repo", repoSlug,
|
||||
)
|
||||
assert.Contains(t, viewRenderedOutput, "# Updated")
|
||||
assert.NotContains(t, viewRenderedOutput, "Last updated")
|
||||
|
||||
revisionsOutput := runTeaCommand(t,
|
||||
"wiki", "revisions", "Home",
|
||||
"--login", login.Name,
|
||||
"--repo", repoSlug,
|
||||
"--output", "json",
|
||||
)
|
||||
assert.Contains(t, revisionsOutput, "create home")
|
||||
assert.Contains(t, revisionsOutput, "edit home")
|
||||
|
||||
runTeaCommand(t,
|
||||
"wiki", "delete", "Home",
|
||||
"--login", login.Name,
|
||||
"--repo", repoSlug,
|
||||
"--confirm",
|
||||
)
|
||||
|
||||
_, _, err = client.Wiki.GetPage(context.Background(), login.User, repoName, "Home")
|
||||
require.Error(t, err)
|
||||
assert.True(t, strings.Contains(strings.ToLower(err.Error()), "not found") || strings.Contains(strings.ToLower(err.Error()), "404"))
|
||||
}
|
||||
|
||||
func runTeaCommand(t *testing.T, args ...string) string {
|
||||
t.Helper()
|
||||
|
||||
app := teacmd.App()
|
||||
errBuf := &bytes.Buffer{}
|
||||
app.Writer = io.Discard
|
||||
app.ErrWriter = errBuf
|
||||
|
||||
oldStdout := os.Stdout
|
||||
reader, writer, err := os.Pipe()
|
||||
require.NoError(t, err)
|
||||
os.Stdout = writer
|
||||
defer func() {
|
||||
os.Stdout = oldStdout
|
||||
}()
|
||||
|
||||
done := make(chan string, 1)
|
||||
go func() {
|
||||
var outBuf bytes.Buffer
|
||||
_, _ = io.Copy(&outBuf, reader)
|
||||
done <- outBuf.String()
|
||||
}()
|
||||
|
||||
runErr := app.Run(context.Background(), append([]string{"tea"}, args...))
|
||||
require.NoError(t, writer.Close())
|
||||
output := <-done
|
||||
require.NoError(t, reader.Close())
|
||||
|
||||
require.NoError(t, runErr, "tea %s failed with stdout: %s\nstderr: %s", strings.Join(args, " "), output, errBuf.String())
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
func decodeBase64String(t *testing.T, encoded string) string {
|
||||
t.Helper()
|
||||
|
||||
decoded, err := base64.StdEncoding.DecodeString(encoded)
|
||||
require.NoError(t, err)
|
||||
|
||||
return string(decoded)
|
||||
}
|
||||
Reference in New Issue
Block a user