2020-12-09 05:41:50 +08:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package interact
|
|
|
|
|
|
|
|
import (
|
2021-03-08 19:48:03 +08:00
|
|
|
"code.gitea.io/sdk/gitea"
|
2021-09-22 23:48:21 +08:00
|
|
|
"code.gitea.io/tea/modules/context"
|
2020-12-09 05:41:50 +08:00
|
|
|
"code.gitea.io/tea/modules/task"
|
|
|
|
|
|
|
|
"github.com/AlecAivazis/survey/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CreatePull interactively creates a PR
|
2021-09-22 23:48:21 +08:00
|
|
|
func CreatePull(ctx *context.TeaContext) (err error) {
|
2022-09-27 23:36:36 +08:00
|
|
|
var (
|
|
|
|
base, head string
|
|
|
|
allowMaintainerEdits bool
|
|
|
|
)
|
2020-12-09 05:41:50 +08:00
|
|
|
|
|
|
|
// owner, repo
|
2021-09-22 23:48:21 +08:00
|
|
|
if ctx.Owner, ctx.Repo, err = promptRepoSlug(ctx.Owner, ctx.Repo); err != nil {
|
2020-12-09 05:41:50 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// base
|
2021-09-22 23:48:21 +08:00
|
|
|
if base, err = task.GetDefaultPRBase(ctx.Login, ctx.Owner, ctx.Repo); err != nil {
|
2020-12-09 05:41:50 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-03-08 19:48:03 +08:00
|
|
|
promptI := &survey.Input{Message: "Target branch:", Default: base}
|
2020-12-09 05:41:50 +08:00
|
|
|
if err := survey.AskOne(promptI, &base); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// head
|
2021-09-22 23:48:21 +08:00
|
|
|
var headOwner, headBranch string
|
2020-12-09 05:41:50 +08:00
|
|
|
promptOpts := survey.WithValidator(survey.Required)
|
2021-09-22 23:48:21 +08:00
|
|
|
|
|
|
|
if ctx.LocalRepo != nil {
|
|
|
|
headOwner, headBranch, err = task.GetDefaultPRHead(ctx.LocalRepo)
|
|
|
|
if err == nil {
|
|
|
|
promptOpts = nil
|
|
|
|
}
|
2020-12-09 05:41:50 +08:00
|
|
|
}
|
2021-03-08 19:48:03 +08:00
|
|
|
promptI = &survey.Input{Message: "Source repo owner:", Default: headOwner}
|
|
|
|
if err := survey.AskOne(promptI, &headOwner); err != nil {
|
2020-12-09 05:41:50 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-03-08 19:48:03 +08:00
|
|
|
promptI = &survey.Input{Message: "Source branch:", Default: headBranch}
|
|
|
|
if err := survey.AskOne(promptI, &headBranch, promptOpts); err != nil {
|
2020-12-09 05:41:50 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-27 23:36:36 +08: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 23:48:21 +08:00
|
|
|
head = task.GetHeadSpec(headOwner, headBranch, ctx.Owner)
|
2020-12-09 05:41:50 +08:00
|
|
|
|
2021-03-08 19:48:03 +08:00
|
|
|
opts := gitea.CreateIssueOption{Title: task.GetDefaultPRTitle(head)}
|
2021-09-22 23:48:21 +08:00
|
|
|
if err = promptIssueProperties(ctx.Login, ctx.Owner, ctx.Repo, &opts); err != nil {
|
2020-12-09 05:41:50 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return task.CreatePull(
|
2021-09-22 23:48:21 +08:00
|
|
|
ctx,
|
2020-12-09 05:41:50 +08:00
|
|
|
base,
|
|
|
|
head,
|
2022-09-27 23:36:36 +08:00
|
|
|
allowMaintainerEdits,
|
2021-03-08 19:48:03 +08:00
|
|
|
&opts)
|
2020-12-09 05:41:50 +08:00
|
|
|
}
|