File: pull_create.go

package info (click to toggle)
tea-cli 0.11.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,648 kB
  • sloc: makefile: 116; sh: 17
file content (77 lines) | stat: -rw-r--r-- 1,679 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package interact

import (
	"code.gitea.io/sdk/gitea"
	"code.gitea.io/tea/modules/context"
	"code.gitea.io/tea/modules/task"

	"github.com/charmbracelet/huh"
)

// CreatePull interactively creates a PR
func CreatePull(ctx *context.TeaContext) (err error) {
	var (
		base, head           string
		allowMaintainerEdits = true
	)

	// owner, repo
	if ctx.Owner, ctx.Repo, err = promptRepoSlug(ctx.Owner, ctx.Repo); err != nil {
		return err
	}

	// base
	if base, err = task.GetDefaultPRBase(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 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(ctx.Login, ctx.Owner, ctx.Repo, &opts); err != nil {
		return err
	}

	return task.CreatePull(
		ctx,
		base,
		head,
		&allowMaintainerEdits,
		&opts)
}