mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 11:01:25 +01:00
feat(pr): merge with the repository's defaults, ready, gh flags and ✓ lines for pr mutations
pr merge takes the method from the repository's default merge style instead of always merging (D22), accepts gh's --merge, --squash and --rebase next to -s/--method, refuses methods the repository disables, and adds -d/--delete-branch (defaulting to the repository setting), --auto, -t, -b, -F and -e. On a terminal it asks for the method and branch deletion with the defaults preselected, without a picker when the current branch has a pull request. It reports "✓ Squashed and merged pull request alice/x#3 (title)", the deleted remote branch, and switches off and deletes the local head branch; --json returns the merged pull request. Merged, closed and draft pull requests are reported before anything is asked. -s without a value points at --squash. pr checkout names the local branch after the head branch when that name is free or already at the head, falling back to OWNER/BRANCH for forks and pr/NUMBER (D13), fetches and switches quietly, reports "✓ Checked out #1 into feat/one", and has the alias co. pr clean reports what it deleted and that a remote branch was already gone. pr create asks for the title and body, then "What's next?": submit, submit as a draft, add metadata or change the branches, without the AGit question up front or a final confirmation. It pushes an unpushed head with --set-upstream --quiet ("✓ Pushed feat/one to origin"), prints "✓ Created pull request alice/x#9 (title)" and the URL, and adds --fill, --web, the alias new, the gh shorthands, and milestones by title. New tea pr ready [--undo]. pr close and reopen take -c/--comment, close takes -d/--delete-branch, and both say when the pull request already is in that state. pr edit reports "✓ Edited", takes the singular gh reviewer flags (the plurals still work), milestones by title and --remove-milestone. pr diff is colored and paged on a terminal and byte-exact when piped, with --name-only, --color and --web. Every pull request argument accepts #N, URLs and branch names and defaults to the current branch's pull request. Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
28 files changed
+1169
-504
No files matched your search
@@ -5,8 +5,6 @@ package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
@@ -16,22 +14,12 @@ import (
|
||||
|
||||
func TestPullMergeSendsTitleAndMessage(t *testing.T) {
|
||||
var payload map[string]any
|
||||
server := newAuthenticatedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodGet {
|
||||
require.Equal(t, "/api/v1/repos/alice/project/pulls/3", r.URL.Path)
|
||||
_, _ = w.Write([]byte(`{"number":3,"head":{"sha":"abc123"}}`))
|
||||
return
|
||||
}
|
||||
require.Equal(t, http.MethodPost, r.Method)
|
||||
require.Equal(t, "/api/v1/repos/alice/project/pulls/3/merge", r.URL.Path)
|
||||
require.NoError(t, json.NewDecoder(r.Body).Decode(&payload))
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
server := newAuthenticatedServer(mergeServer(t, openPull, &payload))
|
||||
defer server.Close()
|
||||
deps, _, _ := serverDeps(t, server.URL)
|
||||
require.NoError(t, Execute(context.Background(), deps, []string{
|
||||
"pr", "merge", "3", "-R", "alice/project", "--method", "squash",
|
||||
"--title", "Squash title", "--message", "Squash body", "--yes",
|
||||
"--title", "Squash title", "--message", "Squash body", "--delete-branch=false", "--yes",
|
||||
}))
|
||||
require.Equal(t, "squash", payload["Do"])
|
||||
require.Equal(t, "Squash title", payload["MergeTitleField"])
|
||||
@@ -40,82 +28,40 @@ func TestPullMergeSendsTitleAndMessage(t *testing.T) {
|
||||
require.Equal(t, "abc123", payload["head_commit_id"])
|
||||
|
||||
payload = nil
|
||||
require.NoError(t, Execute(context.Background(), deps, []string{
|
||||
"pr", "merge", "3", "-R", "alice/project", "--style", "rebase", "--yes",
|
||||
}))
|
||||
server = newAuthenticatedServer(mergeServer(t, openPull, &payload))
|
||||
defer server.Close()
|
||||
deps.Getenv = serverEnv(server.URL, nil)
|
||||
require.NoError(t, Execute(context.Background(), deps, []string{"pr", "merge", "3", "-R", "alice/project", "--style", "rebase", "--yes"}))
|
||||
require.Equal(t, "rebase", payload["Do"])
|
||||
}
|
||||
|
||||
func TestPullMergeMessageFileAndEditor(t *testing.T) {
|
||||
var payload map[string]any
|
||||
server := newAuthenticatedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodGet {
|
||||
_, _ = w.Write([]byte(`{"number":3,"head":{"sha":"abc123"}}`))
|
||||
return
|
||||
}
|
||||
require.NoError(t, json.NewDecoder(r.Body).Decode(&payload))
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
defer server.Close()
|
||||
deps, _, _ := testDeps(t)
|
||||
deps.Getenv = serverEnv(server.URL, map[string]string{"EDITOR": "chosen-editor"})
|
||||
file := t.TempDir() + "/message.md"
|
||||
require.NoError(t, os.WriteFile(file, []byte("from file"), 0o600))
|
||||
require.NoError(t, Execute(context.Background(), deps, []string{
|
||||
"pr", "merge", "3", "-R", "alice/project", "--message-file", file, "--yes",
|
||||
}))
|
||||
server := newAuthenticatedServer(mergeServer(t, openPull, &payload))
|
||||
defer server.Close()
|
||||
deps.Getenv = serverEnv(server.URL, map[string]string{"EDITOR": "chosen-editor"})
|
||||
require.NoError(t, Execute(context.Background(), deps, []string{"pr", "merge", "3", "-R", "alice/project", "--message-file", file, "--yes"}))
|
||||
require.Equal(t, "from file", payload["MergeMessageField"])
|
||||
|
||||
server = newAuthenticatedServer(mergeServer(t, openPull, &payload))
|
||||
defer server.Close()
|
||||
deps.Getenv = serverEnv(server.URL, map[string]string{"EDITOR": "chosen-editor"})
|
||||
deps.Editor, _ = newDraftEditor(t, "from editor")
|
||||
require.NoError(t, Execute(context.Background(), deps, []string{
|
||||
"pr", "merge", "3", "-R", "alice/project", "--editor", "--yes", "--no-input",
|
||||
}))
|
||||
require.NoError(t, Execute(context.Background(), deps, []string{"pr", "merge", "3", "-R", "alice/project", "--editor", "--yes", "--no-input"}))
|
||||
require.Equal(t, "from editor", payload["MergeMessageField"])
|
||||
require.Equal(t, clierr.Usage, clierr.ExitCode(Execute(context.Background(), deps, []string{
|
||||
"pr", "merge", "3", "-R", "alice/project", "--message", "x", "--editor", "--yes",
|
||||
})))
|
||||
}
|
||||
|
||||
func TestPullMergePromptsToDeleteBranch(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
accept, tty bool
|
||||
flags []string
|
||||
want bool
|
||||
confirmations int
|
||||
}{
|
||||
{"accepted", true, true, nil, true, 2},
|
||||
{"explicit flag skips prompt", true, true, []string{"--delete-branch=false"}, false, 1},
|
||||
{"no terminal keeps default", true, false, []string{"--yes"}, false, 0},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var payload map[string]any
|
||||
views := 0
|
||||
server := newAuthenticatedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case r.Method == http.MethodGet && r.URL.Path == "/api/v1/repos/alice/project/pulls/3":
|
||||
views++
|
||||
_, _ = w.Write([]byte(`{"number":3,"head":{"ref":"feature","sha":"abc123"}}`))
|
||||
case r.Method == http.MethodPost && r.URL.Path == "/api/v1/repos/alice/project/pulls/3/merge":
|
||||
require.NoError(t, json.NewDecoder(r.Body).Decode(&payload))
|
||||
default:
|
||||
t.Errorf("unexpected %s %s", r.Method, r.URL.Path)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
prompt := &scriptedPrompter{accept: tc.accept}
|
||||
deps, _, _ := serverDeps(t, server.URL)
|
||||
deps.Prompter = prompt
|
||||
deps.Streams.InTTY, deps.Streams.ErrTTY = tc.tty, tc.tty
|
||||
require.NoError(t, Execute(context.Background(), deps, append([]string{"pr", "merge", "3", "-R", "alice/project"}, tc.flags...)))
|
||||
require.Equal(t, tc.want, payload["delete_branch_after_merge"])
|
||||
require.Len(t, prompt.confirmations, tc.confirmations)
|
||||
promptViews := 0
|
||||
if tc.confirmations == 2 {
|
||||
promptViews = 1
|
||||
}
|
||||
require.Equal(t, 1+promptViews, views, "the merge reads the current head; the prompt reads the branch")
|
||||
})
|
||||
}
|
||||
func TestPullMergeReturnsTheMergedPullRequest(t *testing.T) {
|
||||
var payload map[string]any
|
||||
server := newAuthenticatedServer(mergeServer(t, openPull, &payload))
|
||||
defer server.Close()
|
||||
deps, out, _ := serverDeps(t, server.URL)
|
||||
require.NoError(t, Execute(context.Background(), deps, []string{"pr", "merge", "3", "-R", "alice/project", "--yes", "--json", "number,merged"}))
|
||||
require.JSONEq(t, `{"number":3,"merged":true}`, out.String())
|
||||
}
|
||||
Reference in new issue
Block a user