/
pull_request.go
106 lines (93 loc) · 3.86 KB
/
pull_request.go
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package cmd
import (
"context"
"errors"
"fmt"
"github.com/suzuki-shunsuke/ghcp/pkg/git"
"github.com/suzuki-shunsuke/ghcp/pkg/usecases/pullrequest"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
const pullRequestCmdExample = ` To create a pull request from the feature branch to the default branch:
ghcp pull-request -r OWNER/REPO -b feature --title TITLE --body BODY
To create a pull request from the feature branch to the develop branch:
ghcp pull-request -r OWNER/REPO -b feature --base develop --title TITLE --body BODY
To create a pull request from the feature branch of the OWNER/REPO repository to the default branch of the upstream repository:
ghcp pull-request -r OWNER/REPO -b feature --base-repo UPSTREAM/REPO --title TITLE --body BODY
To create a pull request from the feature branch of the OWNER/REPO repository to the default branch of the upstream repository:
ghcp pull-request -r OWNER/REPO -b feature --base-repo UPSTREAM/REPO --base feature --title TITLE --body BODY
`
func (r *Runner) newPullRequestCmd(ctx context.Context, gOpts *globalOptions) *cobra.Command {
var o pullRequestOptions
c := &cobra.Command{
Use: fmt.Sprintf("%s [flags] FILES...", pullRequestCmdName),
Short: "Create a pull request",
Long: `This creates a pull request. Do nothing if it already exists.`,
Example: pullRequestCmdExample,
RunE: func(_ *cobra.Command, args []string) error {
if err := o.validate(); err != nil {
return fmt.Errorf("invalid flag: %w", err)
}
headRepository, err := o.Head.repositoryID()
if err != nil {
return fmt.Errorf("invalid flag: %w", err)
}
baseRepository := headRepository
if o.Base.RepositoryName != "" {
baseRepository, err = o.Base.repositoryID()
if err != nil {
return fmt.Errorf("invalid flag: %w", err)
}
}
ir, err := r.newInternalRunner(gOpts)
if err != nil {
return fmt.Errorf("error while bootstrap of the dependencies: %w", err)
}
in := pullrequest.Input{
BaseRepository: baseRepository,
BaseBranchName: git.BranchName(o.BaseBranchName),
HeadRepository: headRepository,
HeadBranchName: git.BranchName(o.HeadBranchName),
Title: o.Title,
Body: o.Body,
Reviewer: o.Reviewer,
Draft: o.Draft,
}
if err := ir.PullRequestUseCase.Do(ctx, in); err != nil {
ir.Logger.Debugf("Stacktrace:\n%+v", err)
return fmt.Errorf("could not create a pull request: %s", err)
}
return nil
},
}
o.register(c.Flags())
return c
}
type pullRequestOptions struct {
Base repositoryOptions
Head repositoryOptions
BaseBranchName string
HeadBranchName string
Title string
Body string
Reviewer string
Draft bool
}
func (o pullRequestOptions) validate() error {
if o.HeadBranchName == "" || o.Title == "" {
return errors.New("you need to set -b and --title")
}
return nil
}
func (o *pullRequestOptions) register(f *pflag.FlagSet) {
f.StringVarP(&o.Head.RepositoryName, "head-repo", "r", "", "Head repository name, either -r OWNER/REPO or -u OWNER -r REPO (mandatory)")
f.StringVarP(&o.Head.RepositoryOwner, "head-owner", "u", "", "Head repository owner")
f.StringVarP(&o.HeadBranchName, "head", "b", "", "Head branch name (mandatory)")
f.StringVar(&o.Base.RepositoryName, "base-repo", "", "Base repository name, either --base-repo OWNER/REPO or --base-owner OWNER --base-repo REPO (default: head)")
f.StringVar(&o.Base.RepositoryOwner, "base-owner", "", "Base repository owner (default: head)")
f.StringVar(&o.BaseBranchName, "base", "", "Base branch name (default: default branch of base repository)")
f.StringVar(&o.Title, "title", "", "Title of a pull request (mandatory)")
f.StringVar(&o.Body, "body", "", "Body of a pull request")
f.StringVar(&o.Reviewer, "reviewer", "", "If set, request a review")
f.BoolVar(&o.Draft, "draft", false, "If set, mark as a draft")
}