mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 19:11:25 +01:00
Every confirmation defaulted to No, so Enter after a filled-in form canceled it; prompts were titled with raw flag names and aborted on an empty answer; and the shared issue and pull request form lost input: - Multi-selects hid their last option, so a single assignee looked like an empty list and labels showed four of five. They now get an explicit height and scroll past eight options. - A pty without a window size, such as `script` run without a terminal, made bubbles panic. Forms now refuse it with "terminal size unknown; pass the values as flags or use --no-input". - Without a configured editor, the form ran to the end and then failed with "configure an editor", discarding everything typed. The editor is now resolved before the form; without one the body is typed into an inline multi-line field. - The pull request form said "Issue title" and "Issue description". The shared fields read "Title (required):" and "Body (markdown):", and the review body label gained its missing space. New prompt API for the area commands: confirmAction with a terminal.Confirmation (DefaultYes for constructive actions, Typed for type-to-confirm, NonTTY to name the action in the --yes error) and requireInput with a human label and inline validation. Declined or aborted prompts return terminal.ErrCanceled (exit code 3). The existing confirm and require helpers keep working on top of them, and prompters without the new terminal.Asker methods keep the plain behavior. Signed-off-by: bircni <bircni@icloud.com>
95 lines
2.8 KiB
Go
95 lines
2.8 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package terminal
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"charm.land/huh/v2"
|
|
)
|
|
|
|
// ReviewCommentDraft is an inline comment collected during interactive review.
|
|
type ReviewCommentDraft struct {
|
|
Path, Body string
|
|
OldLine, NewLine int64
|
|
}
|
|
|
|
// ReviewFormChoices supplies callbacks for interactive pull review.
|
|
type ReviewFormChoices struct {
|
|
// ReviewDiff, when non-nil, runs after the user opts into annotating the diff.
|
|
ReviewDiff func(context.Context) ([]ReviewCommentDraft, error)
|
|
EditBody BodyEditor
|
|
}
|
|
|
|
// ReviewFormValues holds interactively collected review fields.
|
|
type ReviewFormValues struct {
|
|
Event string
|
|
Body string
|
|
Comments []ReviewCommentDraft
|
|
Draft Draft
|
|
}
|
|
|
|
// ReviewForm collects interactive pull review input.
|
|
type ReviewForm interface {
|
|
CreateReview(context.Context, ReviewFormChoices) (ReviewFormValues, error)
|
|
}
|
|
|
|
// CreateReview runs the interactive review form on stderr.
|
|
func (p *FormPrompter) CreateReview(ctx context.Context, choices ReviewFormChoices) (ReviewFormValues, error) {
|
|
var comments []ReviewCommentDraft
|
|
if choices.ReviewDiff != nil {
|
|
reviewDiff := true
|
|
if err := p.runForm(ctx, huh.NewForm(huh.NewGroup(
|
|
huh.NewConfirm().Title("Review / comment the diff?").Value(&reviewDiff),
|
|
))); err != nil {
|
|
return ReviewFormValues{}, err
|
|
}
|
|
if reviewDiff {
|
|
parsed, err := choices.ReviewDiff(ctx)
|
|
if err != nil {
|
|
return ReviewFormValues{}, fmt.Errorf("diff review failed: %w", err)
|
|
}
|
|
comments = append(comments, parsed...)
|
|
fmt.Fprintf(p.Streams.ErrOut, "Found %d code comments in your review\n", len(comments))
|
|
}
|
|
}
|
|
|
|
event := "COMMENT"
|
|
if err := p.runForm(ctx, huh.NewForm(huh.NewGroup(
|
|
huh.NewSelect[string]().Title("Your assessment:").Options(
|
|
huh.NewOption("comment", "COMMENT"),
|
|
huh.NewOption("request changes", "REQUEST_CHANGES"),
|
|
huh.NewOption("approve", "APPROVED"),
|
|
).Value(&event),
|
|
))); err != nil {
|
|
return ReviewFormValues{}, err
|
|
}
|
|
|
|
body := ""
|
|
values := ReviewFormValues{Event: event, Comments: comments}
|
|
required := (event == "COMMENT" && len(comments) == 0) || event == "REQUEST_CHANGES"
|
|
if choices.EditBody != nil {
|
|
var err error
|
|
if body, values.Draft, err = editBody(ctx, choices.EditBody, ""); err != nil {
|
|
return values, err
|
|
}
|
|
} else {
|
|
field := bodyField("Concluding comment (markdown):", &body)
|
|
if required {
|
|
field = field.Title("Concluding comment (markdown, required):").Validate(huh.ValidateNotEmpty())
|
|
}
|
|
if err := p.runForm(ctx, huh.NewForm(huh.NewGroup(field))); err != nil {
|
|
return ReviewFormValues{}, err
|
|
}
|
|
}
|
|
body = strings.TrimSpace(body)
|
|
if required && body == "" {
|
|
return values, fmt.Errorf("concluding comment cannot be empty")
|
|
}
|
|
values.Body = body
|
|
return values, nil
|
|
}
|