mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 19:11:25 +01:00
- **switch tea to the Cobra command tree:** `internal/app` wires the packages from parts 1–4 into explicit noun/verb commands with `--repo`, `--json` / `--jq`, `--sudo` and `--debug`; `main.go` points at it. Build, release and docs tooling (`tools/cli-docs`, Makefile, goreleaser, snap, CI) and README / CONTRIBUTING are updated. - **remove the urfave/cli command tree:** deletes `cmd/`, `modules/`, the old integration tests, contrib completion scripts and the generated `docs/CLI.md`, and drops their dependencies. This commit only deletes. ## Breaking changes This is a breaking release. It requires Gitea 28 or newer; configuration moves to separate `config.yml` and `hosts.yml` files and requires fresh authentication, and command names, aliases, flags and output formats change. `docs/migration-1.0.md` maps every old command, flag and environment variable to its replacement. ## Issues fixes #1118 fixes #1116 fixes #1115 fixes #1094 fixes #1091 fixes #1073 fixes #1052 fixes #1032 fixes #1023 fixes #966 fixes #908 fixes #833 fixes #831 fixes #817 fixes #804 fixes #779 fixes #770 fixes #737 fixes #697 fixes #726 fixes #677 fixes #672 fixes #633 fixes #632 fixes #560 fixes #556 fixes #523 fixes #516 fixes #510 fixes #504 fixes #500 fixes #482 fixes #406 fixes #403 fixes #373 fixes #351 fixes #344 fixes #326 fixes #325 fixes #323 fixes #246 fixes #240 fixes #232 fixes #197 fixes #172 fixes #141 fixes #69 fixes #1110 fixes #569 fixes #480 fixes #666 fixes #670 fixes #457 fixes #643 fixes #498 fixes #490 fixes #1069 fixes #834 fixes #602 fixes #328 fixes #417 fixes #571 fixes #1029 fixes #489 fixes #1050 fixes #162 fixes #430 fixes #1087 fixes #683 Reviewed-on: https://gitea.com/gitea/tea/pulls/1126 Reviewed-by: TheFox0x7 <95654+thefox0x7@noreply.gitea.com>
167 lines
9.5 KiB
Go
167 lines
9.5 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package app
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.dev/tea/internal/auth"
|
|
"gitea.dev/tea/internal/git"
|
|
"gitea.dev/tea/internal/services"
|
|
"gitea.dev/tea/internal/settings"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestGiteaGitCredentialHelperClonesPrivateRepository(t *testing.T) {
|
|
gitea, deps, out, diagnostics := giteaDeps(t)
|
|
deps.Settings = settings.New(filepath.Join(t.TempDir(), "configuration with spaces"))
|
|
deps.Credentials.Dir = deps.Settings.Dir
|
|
var repo services.Repository
|
|
require.NoError(t, Execute(context.Background(), deps, []string{"repo", "create", fmt.Sprintf("tea-git-helper-%d", time.Now().UnixNano()), "--private", "--auto-init", "--json"}))
|
|
require.NoError(t, json.Unmarshal(out.Bytes(), &repo))
|
|
require.True(t, repo.Private)
|
|
t.Cleanup(func() {
|
|
if err := Execute(context.Background(), deps, []string{"repo", "delete", "-R", repo.FullName, "--yes"}); err != nil {
|
|
t.Errorf("repository cleanup: %v", err)
|
|
}
|
|
})
|
|
user, _, _ := strings.Cut(repo.FullName, "/")
|
|
require.NoError(t, testManager(t, deps).Register(gitea.url, user, "file", "token", auth.HostOptions{}, auth.Credential{AccessToken: gitea.token}))
|
|
binaryDir := filepath.Join(t.TempDir(), "binary with 'quote'")
|
|
require.NoError(t, os.MkdirAll(binaryDir, 0o700))
|
|
binary := filepath.Join(binaryDir, "tea")
|
|
if os.PathSeparator == '\\' {
|
|
binary += ".exe"
|
|
}
|
|
build := exec.CommandContext(context.Background(), "go", "build", "-o", binary, ".")
|
|
build.Dir = filepath.Join("..", "..")
|
|
output, err := build.CombinedOutput()
|
|
require.NoError(t, err, "build: %s", output)
|
|
deps.Executable = func() (string, error) { return binary, nil }
|
|
// The temporary global file exercises setup's default scope without touching
|
|
// the user's Git configuration. Git invokes the compiled helper as a child.
|
|
environment := []string{}
|
|
for _, entry := range os.Environ() {
|
|
key, _, _ := strings.Cut(entry, "=")
|
|
if strings.HasPrefix(key, "GIT_TRACE") || key == "GIT_CURL_VERBOSE" {
|
|
continue
|
|
}
|
|
environment = append(environment, entry)
|
|
}
|
|
globalConfig := filepath.Join(t.TempDir(), "gitconfig")
|
|
environment = append(environment, "GIT_CONFIG_GLOBAL="+globalConfig, "GIT_CONFIG_NOSYSTEM=1", "TEA_CONFIG_DIR="+deps.Settings.Dir, "GITEA_TOKEN=", "GITEA_INSTANCE_URL=", "TEA_HOST=", "TEA_USER=", "TEA_REPO=")
|
|
runner := git.System{Env: environment, ErrOut: diagnostics}
|
|
deps.Git = runner
|
|
_, anonymousErr := runner.Output(context.Background(), "", false, "-c", "credential.helper=", "clone", "--", repo.CloneURL, filepath.Join(t.TempDir(), "anonymous-clone"))
|
|
require.Error(t, anonymousErr, "private repository must reject an unauthenticated clone")
|
|
out.Reset()
|
|
diagnostics.Reset()
|
|
teaClone := filepath.Join(t.TempDir(), "tea-private-clone")
|
|
require.NoError(t, Execute(context.Background(), deps, []string{"repo", "clone", repo.FullName, teaClone, "--no-input", "--json=id"}))
|
|
require.JSONEq(t, fmt.Sprintf(`{"id":%d}`, repo.ID), out.String())
|
|
_, localHelperErr := runner.Output(context.Background(), teaClone, false, "config", "--local", "--get-all", "credential."+gitea.url+".helper")
|
|
var missingLocalHelper *git.CommandError
|
|
require.ErrorAs(t, localHelperErr, &missingLocalHelper)
|
|
require.Equal(t, 1, missingLocalHelper.ExitCode)
|
|
// Build a changed PR on the private repository, then fetch it without setup.
|
|
out.Reset()
|
|
require.NoError(t, Execute(context.Background(), deps, []string{"branch", "create", "feature", "-R", repo.FullName}))
|
|
deps.Streams.In = strings.NewReader(`{"branch":"feature","content":"dGVzdAo=","message":"Disposable private PR fixture"}`)
|
|
require.NoError(t, Execute(context.Background(), deps, []string{"api", "repos/" + repo.FullName + "/contents/change.txt", "--input", "-"}))
|
|
out.Reset()
|
|
require.NoError(t, Execute(context.Background(), deps, []string{"pr", "create", "-R", repo.FullName, "--title", "Private checkout", "--head", "feature", "--base", repo.DefaultBranch, "--json"}))
|
|
var pull services.Pull
|
|
require.NoError(t, json.Unmarshal(out.Bytes(), &pull))
|
|
out.Reset()
|
|
require.NoError(t, Execute(context.Background(), deps, []string{"pr", "checkout", fmt.Sprint(pull.Number), "-R", repo.FullName, "--cwd", teaClone, "--no-input", "--json"}))
|
|
var checkout git.CheckoutResult
|
|
require.NoError(t, json.Unmarshal(out.Bytes(), &checkout))
|
|
require.Equal(t, pull.HeadSHA, checkout.Commit)
|
|
require.Equal(t, "pr/"+fmt.Sprint(pull.Number), checkout.Branch)
|
|
branchesBefore, err := runner.Output(context.Background(), teaClone, false, "for-each-ref", "--format=%(refname):%(objectname)", "refs/heads/")
|
|
require.NoError(t, err)
|
|
out.Reset()
|
|
require.NoError(t, Execute(context.Background(), deps, []string{"pr", "checkout", fmt.Sprint(pull.Number), "-R", repo.FullName, "--cwd", teaClone, "--detach", "--no-input", "--json"}))
|
|
require.NoError(t, json.Unmarshal(out.Bytes(), &checkout))
|
|
require.True(t, checkout.Detached)
|
|
require.Empty(t, checkout.Branch)
|
|
require.Equal(t, pull.HeadSHA, checkout.Commit)
|
|
branchesAfter, err := runner.Output(context.Background(), teaClone, false, "for-each-ref", "--format=%(refname):%(objectname)", "refs/heads/")
|
|
require.NoError(t, err)
|
|
require.Equal(t, branchesBefore, branchesAfter)
|
|
out.Reset()
|
|
require.NoError(t, Execute(context.Background(), deps, []string{"pr", "checkout", fmt.Sprint(pull.Number), "-R", repo.FullName, "--cwd", teaClone, "--no-input", "--json"}))
|
|
require.NoError(t, json.Unmarshal(out.Bytes(), &checkout))
|
|
require.False(t, checkout.Detached)
|
|
require.Equal(t, "pr/"+fmt.Sprint(pull.Number), checkout.Branch)
|
|
branchesAfter, err = runner.Output(context.Background(), teaClone, false, "for-each-ref", "--format=%(refname):%(objectname)", "refs/heads/")
|
|
require.NoError(t, err)
|
|
require.Equal(t, branchesBefore, branchesAfter)
|
|
_, err = runner.Output(context.Background(), teaClone, false, "switch", "--detach", pull.HeadSHA)
|
|
require.NoError(t, err)
|
|
_, headErr := runner.Output(context.Background(), teaClone, false, "symbolic-ref", "--quiet", "HEAD")
|
|
var detachedHead *git.CommandError
|
|
require.ErrorAs(t, headErr, &detachedHead)
|
|
require.Equal(t, 1, detachedHead.ExitCode)
|
|
deps.Streams.In = strings.NewReader(`{"branch":"feature","content":"bmV4dAo=","message":"Disposable fast-forward fixture"}`)
|
|
require.NoError(t, Execute(context.Background(), deps, []string{"api", "repos/" + repo.FullName + "/contents/next.txt", "--input", "-"}))
|
|
out.Reset()
|
|
require.NoError(t, Execute(context.Background(), deps, []string{"pr", "view", fmt.Sprint(pull.Number), "-R", repo.FullName, "--json"}))
|
|
require.NoError(t, json.Unmarshal(out.Bytes(), &pull))
|
|
out.Reset()
|
|
require.NoError(t, Execute(context.Background(), deps, []string{"pr", "checkout", fmt.Sprint(pull.Number), "-R", repo.FullName, "--cwd", teaClone, "--update", "--no-input", "--json"}))
|
|
require.NoError(t, json.Unmarshal(out.Bytes(), &checkout))
|
|
require.Equal(t, pull.HeadSHA, checkout.Commit)
|
|
localHead, err := runner.Output(context.Background(), teaClone, false, "rev-parse", "HEAD")
|
|
require.NoError(t, err)
|
|
require.Equal(t, pull.HeadSHA, localHead)
|
|
deps.Streams.In = strings.NewReader(fmt.Sprintf(`{"branch":%q,"content":"c3luYwo=","message":"Disposable sync fixture"}`, repo.DefaultBranch))
|
|
require.NoError(t, Execute(context.Background(), deps, []string{"api", "repos/" + repo.FullName + "/contents/sync.txt", "--input", "-"}))
|
|
out.Reset()
|
|
require.NoError(t, Execute(context.Background(), deps, []string{"repo", "sync", "-R", repo.FullName, "--cwd", teaClone, "--no-input", "--json=branch,commit,remote"}))
|
|
var synced git.SyncResult
|
|
require.NoError(t, json.Unmarshal(out.Bytes(), &synced))
|
|
require.Equal(t, repo.DefaultBranch, synced.Branch)
|
|
localHead, err = runner.Output(context.Background(), teaClone, false, "rev-parse", "HEAD")
|
|
require.NoError(t, err)
|
|
require.Equal(t, synced.Commit, localHead)
|
|
contents, err := os.ReadFile(filepath.Join(teaClone, "sync.txt"))
|
|
require.NoError(t, err)
|
|
require.Equal(t, "sync\n", string(contents))
|
|
if endpoint := os.Getenv("GITEA_TEA_TEST_SSH_ENDPOINT"); endpoint != "" {
|
|
verifySSHCloneAndCheckout(t, deps, repo, pull, user, environment, endpoint)
|
|
}
|
|
// The per-operation helper does not create a global Git configuration file.
|
|
_, err = os.Stat(globalConfig)
|
|
require.True(t, os.IsNotExist(err))
|
|
out.Reset()
|
|
diagnostics.Reset()
|
|
require.NoError(t, Execute(context.Background(), deps, []string{"auth", "setup-git", "--host", gitea.url, "--no-input"}))
|
|
require.NoError(t, Execute(context.Background(), deps, []string{"auth", "setup-git", "--host", gitea.url, "--no-input"}))
|
|
require.Empty(t, out.String())
|
|
destination := filepath.Join(t.TempDir(), "private-clone")
|
|
_, err = runner.Output(context.Background(), "", false, "clone", "--", repo.CloneURL, destination)
|
|
require.NoError(t, err, "private repository clone failed")
|
|
inside, err := runner.Output(context.Background(), destination, false, "rev-parse", "--is-inside-work-tree")
|
|
require.NoError(t, err)
|
|
require.Equal(t, "true", inside)
|
|
// No credential is embedded in either the persisted remote or helper command.
|
|
remote, err := runner.Output(context.Background(), destination, false, "remote", "get-url", "origin")
|
|
require.NoError(t, err)
|
|
require.Equal(t, repo.CloneURL, remote)
|
|
require.False(t, strings.Contains(remote, gitea.token), "remote contains credentials")
|
|
helper, err := runner.Output(context.Background(), "", false, "config", "--global", "--get-all", "credential."+gitea.url+".helper")
|
|
require.NoError(t, err)
|
|
require.Len(t, strings.Split(helper, "\n"), 1)
|
|
require.False(t, strings.Contains(helper, gitea.token), "helper command contains credentials")
|
|
}
|