2020-12-08 16:41:50 -05:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2023-09-07 21:40:02 -04:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-08 16:41:50 -05:00
|
|
|
|
|
|
|
package interact
|
|
|
|
|
|
|
|
import (
|
2021-03-08 06:48:03 -05:00
|
|
|
"code.gitea.io/sdk/gitea"
|
2021-09-22 11:48:21 -04:00
|
|
|
"code.gitea.io/tea/modules/context"
|
2020-12-08 16:41:50 -05:00
|
|
|
"code.gitea.io/tea/modules/task"
|
|
|
|
|
|
|
|
"github.com/AlecAivazis/survey/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CreatePull interactively creates a PR
|
2021-09-22 11:48:21 -04:00
|
|
|
func CreatePull(ctx *context.TeaContext) (err error) {
|
2022-09-27 11:36:36 -04:00
|
|
|
var (
|
|
|
|
base, head string
|
|
|
|
allowMaintainerEdits bool
|
|
|
|
)
|
2020-12-08 16:41:50 -05:00
|
|
|
|
|
|
|
// owner, repo
|
2021-09-22 11:48:21 -04:00
|
|
|
if ctx.Owner, ctx.Repo, err = promptRepoSlug(ctx.Owner, ctx.Repo); err != nil {
|
2020-12-08 16:41:50 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// base
|
2021-09-22 11:48:21 -04:00
|
|
|
if base, err = task.GetDefaultPRBase(ctx.Login, ctx.Owner, ctx.Repo); err != nil {
|
2020-12-08 16:41:50 -05:00
|
|
|
return err
|
|
|
|
}
|
2021-03-08 06:48:03 -05:00
|
|
|
promptI := &survey.Input{Message: "Target branch:", Default: base}
|
2020-12-08 16:41:50 -05:00
|
|
|
if err := survey.AskOne(promptI, &base); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// head
|
2021-09-22 11:48:21 -04:00
|
|
|
var headOwner, headBranch string
|
2020-12-08 16:41:50 -05:00
|
|
|
promptOpts := survey.WithValidator(survey.Required)
|
2021-09-22 11:48:21 -04:00
|
|
|
|
|
|
|
if ctx.LocalRepo != nil {
|
|
|
|
headOwner, headBranch, err = task.GetDefaultPRHead(ctx.LocalRepo)
|
|
|
|
if err == nil {
|
|
|
|
promptOpts = nil
|
|
|
|
}
|
2020-12-08 16:41:50 -05:00
|
|
|
}
|
2021-03-08 06:48:03 -05:00
|
|
|
promptI = &survey.Input{Message: "Source repo owner:", Default: headOwner}
|
|
|
|
if err := survey.AskOne(promptI, &headOwner); err != nil {
|
2020-12-08 16:41:50 -05:00
|
|
|
return err
|
|
|
|
}
|
2021-03-08 06:48:03 -05:00
|
|
|
promptI = &survey.Input{Message: "Source branch:", Default: headBranch}
|
|
|
|
if err := survey.AskOne(promptI, &headBranch, promptOpts); err != nil {
|
2020-12-08 16:41:50 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-27 11:36:36 -04:00
|
|
|
promptC := &survey.Confirm{Message: "Allow Maintainers to push to the base branch", Default: true}
|
|
|
|
if err := survey.AskOne(promptC, &allowMaintainerEdits); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-22 11:48:21 -04:00
|
|
|
head = task.GetHeadSpec(headOwner, headBranch, ctx.Owner)
|
2020-12-08 16:41:50 -05:00
|
|
|
|
2021-03-08 06:48:03 -05:00
|
|
|
opts := gitea.CreateIssueOption{Title: task.GetDefaultPRTitle(head)}
|
2021-09-22 11:48:21 -04:00
|
|
|
if err = promptIssueProperties(ctx.Login, ctx.Owner, ctx.Repo, &opts); err != nil {
|
2020-12-08 16:41:50 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return task.CreatePull(
|
2021-09-22 11:48:21 -04:00
|
|
|
ctx,
|
2020-12-08 16:41:50 -05:00
|
|
|
base,
|
|
|
|
head,
|
2022-09-27 11:36:36 -04:00
|
|
|
allowMaintainerEdits,
|
2021-03-08 06:48:03 -05:00
|
|
|
&opts)
|
2020-12-08 16:41:50 -05:00
|
|
|
}
|