mirror of
https://gitea.com/gitea/tea.git
synced 2026-06-05 18:58:43 +02:00
feat(pulls): add --draft to create and --draft/--ready to edit (#1008)
Closes #979. An alternative to the approach in #980 — this one is purely client-side title-mangling, no SDK changes needed. Gitea already treats any PR with a `WIP:` or `[WIP]` title prefix (case-insensitive) as a draft. This patch wires three flags around that behavior: - `tea pulls create --draft` — prepend `WIP: ` to the title at creation time - `tea pulls edit --draft <idx>` — add `WIP: ` to an existing PR's title - `tea pulls edit --ready <idx>` — strip any recognized draft prefix All three are idempotent. `--draft` and `--ready` on edit are mutually exclusive. If the user also passes `--title` on edit, the toggle applies to the supplied title; otherwise the current title is fetched from the server first. Why this approach over a server-payload-based one: Gitea's draft state is *defined* as the title-prefix convention (see the Gitea source for `HasWIPPrefix`). Modeling it server-side would either duplicate or fight that. A small string helper covers it without needing the SDK to add a `Draft` field. Verified against `gitea.com` (1.26.0+dev) with a throwaway repo: - create with `--draft` → server reports `draft: true` ✓ - `edit --ready` strips → `draft: false` ✓ - `edit --draft` adds back → `draft: true` ✓ - second `edit --draft` is idempotent ✓ - `edit --draft --ready` errors ✓ Unit tests for the prefix detection live in `modules/utils/draft_test.go`. --- This patch was authored interactively with an AI assistant, driven and reviewed by a human (Tyler / @dinsmoor) every step. Reproduction, design decisions, and the choice not to follow #980's payload approach were mine — happy to discuss any of it. *pull request created by Tyler's lovingly wrangled demon machine <3* --------- Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-on: https://gitea.com/gitea/tea/pulls/1008 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: Tyler <tyler@dinsmoor.us> Co-committed-by: Tyler <tyler@dinsmoor.us>
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
"gitea.dev/tea/modules/context"
|
||||
"gitea.dev/tea/modules/interact"
|
||||
"gitea.dev/tea/modules/task"
|
||||
"gitea.dev/tea/modules/utils"
|
||||
)
|
||||
|
||||
// CmdPullsCreate creates a pull request
|
||||
@@ -46,6 +47,10 @@ var CmdPullsCreate = cli.Command{
|
||||
Name: "topic",
|
||||
Usage: "Topic name for agit flow pull request",
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "draft",
|
||||
Usage: "Create as a draft (prepends \"WIP: \" to the title; Gitea treats WIP-prefixed PRs as drafts)",
|
||||
},
|
||||
}, flags.IssuePRCreateFlags...),
|
||||
}
|
||||
|
||||
@@ -81,6 +86,10 @@ func runPullsCreate(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if ctx.Bool("draft") {
|
||||
opts.Title = utils.AddDraftPrefix(opts.Title)
|
||||
}
|
||||
|
||||
if ctx.Bool("agit") {
|
||||
return task.CreateAgitFlowPull(
|
||||
requestCtx,
|
||||
|
||||
@@ -18,6 +18,42 @@ import (
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
// applyDraftFlag mutates opts.Title according to --draft / --ready.
|
||||
// If a flag is set but --title isn't, it fetches the current title from the server.
|
||||
// Returns an error if both --draft and --ready are set, or on a server error.
|
||||
func applyDraftFlag(requestCtx stdctx.Context, ctx *context.TeaContext, client *gitea.Client, idx int64, opts *task.EditIssueOption) error {
|
||||
draft := ctx.Bool("draft")
|
||||
ready := ctx.Bool("ready")
|
||||
if !draft && !ready {
|
||||
return nil
|
||||
}
|
||||
if draft && ready {
|
||||
return fmt.Errorf("--draft and --ready are mutually exclusive")
|
||||
}
|
||||
|
||||
var current string
|
||||
if opts.Title != nil {
|
||||
current = *opts.Title
|
||||
} else {
|
||||
pr, _, err := client.PullRequests.GetPullRequest(requestCtx, ctx.Owner, ctx.Repo, idx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not fetch pull request #%d: %s", idx, err)
|
||||
}
|
||||
current = pr.Title
|
||||
}
|
||||
|
||||
var next string
|
||||
if draft {
|
||||
next = utils.AddDraftPrefix(current)
|
||||
} else {
|
||||
next = utils.StripDraftPrefix(current)
|
||||
}
|
||||
if next != current || opts.Title != nil {
|
||||
opts.Title = &next
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CmdPullsEdit is the subcommand of pulls to edit pull requests
|
||||
var CmdPullsEdit = cli.Command{
|
||||
Name: "edit",
|
||||
@@ -37,6 +73,14 @@ use an empty string (eg. --milestone "").`,
|
||||
Name: "remove-reviewers",
|
||||
Usage: "Comma-separated list of usernames to remove from reviewers",
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "draft",
|
||||
Usage: "Mark as draft by prepending \"WIP: \" to the title (idempotent)",
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "ready",
|
||||
Usage: "Mark as ready for review by stripping any leading \"WIP: \" or \"[WIP]\" prefix",
|
||||
},
|
||||
),
|
||||
}
|
||||
|
||||
@@ -72,6 +116,9 @@ func runPullsEdit(requestCtx stdctx.Context, cmd *cli.Command) error {
|
||||
|
||||
client := ctx.Login.Client()
|
||||
for _, opts.Index = range indices {
|
||||
if err := applyDraftFlag(requestCtx, ctx, client, opts.Index, opts); err != nil {
|
||||
return err
|
||||
}
|
||||
pr, err := task.EditPull(requestCtx, ctx, client, *opts)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user