Files
gitea-tea/modules/interact/pull_create.go
T
2727270931 feat(pulls): allow disabling agit prompt (#1025)
Allow users to set TEA_USE_AGIT=0 or TEA_USE_AGIT=false to skip the interactive agit flow prompt during pull request creation.

This keeps the current default behavior unchanged and only suppresses the agit confirmation when the environment variable explicitly parses to false.

Verified with:

- go test ./modules/interact
- go build ./...

---------

Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: Leon Müller <mueller4leon@gmail.com>
Reviewed-on: https://gitea.com/gitea/tea/pulls/1025
Reviewed-by: silverwind <2021+silverwind@noreply.gitea.com>
Co-authored-by: LeonM <206012+leonm@noreply.gitea.com>
2026-10-23 22:02:03 +00:00

167 lines
3.2 KiB
Go

// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package interact
import (
stdctx "context"
"os"
"strconv"
gitea "gitea.dev/sdk"
"gitea.dev/tea/modules/context"
"gitea.dev/tea/modules/print"
"gitea.dev/tea/modules/task"
"gitea.dev/tea/modules/theme"
"charm.land/huh/v2"
)
// CreatePull interactively creates a PR
func CreatePull(requestCtx stdctx.Context, ctx *context.TeaContext) (err error) {
var (
base, head string
allowMaintainerEdits = true
agit bool
)
// owner, repo
if ctx.Owner, ctx.Repo, err = promptRepoSlug(ctx.Owner, ctx.Repo); err != nil {
return err
}
// base
if base, err = task.GetDefaultPRBase(requestCtx, ctx.Login, ctx.Owner, ctx.Repo); err != nil {
return err
}
var headOwner, headBranch string
validator := huh.ValidateNotEmpty()
if ctx.LocalRepo != nil {
headOwner, headBranch, err = task.GetDefaultPRHead(ctx.LocalRepo)
if err == nil {
validator = func(string) error { return nil }
}
}
if isAgitPromptEnabled() {
if err := huh.NewConfirm().
Title("Do you want to create an agit flow pull request?").
Value(&agit).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return err
}
}
if agit {
var (
topic string
baseRemote string
)
topic = headBranch
head = "HEAD"
baseRemote = "origin"
if err := huh.NewForm(
huh.NewGroup(
huh.NewInput().
Title("Target branch:").
Value(&base).
Validate(huh.ValidateNotEmpty()),
huh.NewInput().
Title("Source repo remote:").
Value(&baseRemote),
huh.NewInput().
Title("Topic branch:").
Value(&topic).
Validate(validator),
huh.NewInput().
Title("Head branch:").
Value(&head).
Validate(validator),
),
).Run(); err != nil {
return err
}
opts := gitea.CreateIssueOption{Title: task.GetDefaultPRTitle(head)}
if err = promptIssueProperties(requestCtx, ctx.Login, ctx.Owner, ctx.Repo, &opts); err != nil {
return err
}
return task.CreateAgitFlowPull(
requestCtx,
ctx,
baseRemote,
head,
base,
topic,
&opts,
PromptPassword,
)
}
if err := huh.NewForm(
huh.NewGroup(
huh.NewInput().
Title("Target branch:").
Value(&base).
Validate(huh.ValidateNotEmpty()),
huh.NewInput().
Title("Source repo owner:").
Value(&headOwner),
huh.NewInput().
Title("Source branch:").
Value(&headBranch).
Validate(validator),
huh.NewConfirm().
Title("Allow maintainers to push to the base branch:").
Value(&allowMaintainerEdits),
),
).Run(); err != nil {
return err
}
head = task.GetHeadSpec(headOwner, headBranch, ctx.Owner)
opts := gitea.CreateIssueOption{Title: task.GetDefaultPRTitle(head)}
if err = promptIssueProperties(requestCtx, ctx.Login, ctx.Owner, ctx.Repo, &opts); err != nil {
return err
}
pr, err := task.CreatePull(
requestCtx,
ctx,
base,
head,
&allowMaintainerEdits,
&opts)
if err != nil {
return err
}
print.PullDetails(pr, nil, nil)
return nil
}
func isAgitPromptEnabled() bool {
value, ok := os.LookupEnv("TEA_USE_AGIT")
if !ok {
return true
}
useAgit, err := strconv.ParseBool(value)
return err != nil || useAgit
}