-
Notifications
You must be signed in to change notification settings - Fork 786
/
create_pullrequest.go
182 lines (155 loc) · 4.31 KB
/
create_pullrequest.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package cmd
import (
"os"
"strings"
"github.com/spf13/cobra"
"fmt"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
)
var (
createPullRequestLong = templates.LongDesc(`
Creates a Pull Request in a the git project of the current directory
`)
createPullRequestExample = templates.Examples(`
# Create a Pull Request in the current project
jx create pullrequest -t "my PR title"
# Create a Pull Request with a title and a body
jx create pullrequest -t "my PR title" --body "
some more
text
goes
here
""
"
`)
)
// CreatePullRequestOptions the options for the create spring command
type CreatePullRequestOptions struct {
CreateOptions
Dir string
Title string
Body string
Labels []string
Base string
Results CreatePullRequestResults
}
type CreatePullRequestResults struct {
GitInfo *gits.GitRepository
GitProvider gits.GitProvider
PullRequest *gits.GitPullRequest
}
// NewCmdCreatePullRequest creates a command object for the "create" command
func NewCmdCreatePullRequest(commonOpts *CommonOptions) *cobra.Command {
options := &CreatePullRequestOptions{
CreateOptions: CreateOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "pullrequest",
Short: "Create a Pull Request on the git project for the current directory",
Aliases: []string{"pr", "pull request"},
Long: createPullRequestLong,
Example: createPullRequestExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Dir, "dir", "", "", "The source directory used to detect the Git repository. Defaults to the current directory")
cmd.Flags().StringVarP(&options.Title, optionTitle, "t", "", "The title of the pullrequest to create")
cmd.Flags().StringVarP(&options.Body, "body", "", "", "The body of the pullrequest")
cmd.Flags().StringVarP(&options.Base, "base", "", "master", "The base branch to create the pull request into")
cmd.Flags().StringArrayVarP(&options.Labels, "label", "l", []string{}, "The labels to add to the pullrequest")
return cmd
}
// Run implements the command
func (o *CreatePullRequestOptions) Run() error {
// lets discover the git dir
if o.Dir == "" {
dir, err := os.Getwd()
if err != nil {
return err
}
o.Dir = dir
}
gitInfo, provider, _, err := o.createGitProvider(o.Dir)
if err != nil {
return err
}
o.Results.GitInfo = gitInfo
o.Results.GitProvider = provider
branchName, err := o.Git().Branch(o.Dir)
if err != nil {
return err
}
arguments := &gits.GitPullRequestArguments{
Base: o.Base,
Head: branchName,
}
err = o.PopulatePullRequest(arguments, gitInfo)
if err != nil {
return err
}
pr, err := provider.CreatePullRequest(arguments)
if err != nil {
return err
}
o.Results.PullRequest = pr
log.Infof("\nCreated PullRequest %s at %s\n", util.ColorInfo(pr.NumberString()), util.ColorInfo(pr.URL))
return nil
}
func (o *CreatePullRequestOptions) PopulatePullRequest(pullRequest *gits.GitPullRequestArguments, gitInfo *gits.GitRepository) error {
title := o.Title
if title == "" {
if o.BatchMode {
return util.MissingOption(optionTitle)
}
defaultValue, body, err := o.findLastCommitTitle()
if err != nil {
log.Warnf("Failed to find last git commit title: %s\n", err)
}
if o.Body == "" {
o.Body = body
}
title, err = util.PickValue("PullRequest title:", defaultValue, true, "", o.In, o.Out, o.Err)
if err != nil {
return err
}
}
body := o.Body
pullRequest.Title = title
pullRequest.Body = body
pullRequest.GitRepository = gitInfo
if title == "" {
return fmt.Errorf("No title specified!")
}
return nil
}
func (o *CreatePullRequestOptions) findLastCommitTitle() (string, string, error) {
title := ""
body := ""
dir := o.Dir
gitDir, gitConfDir, err := o.Git().FindGitConfigDir(dir)
if err != nil {
return title, body, err
}
if gitDir == "" || gitConfDir == "" {
log.Warnf("No git directory could be found from dir %s\n", dir)
return title, body, err
}
message, err := o.Git().GetLatestCommitMessage(dir)
if err != nil {
return title, body, err
}
lines := strings.SplitN(message, "\n", 2)
if len(lines) < 2 {
return message, "", nil
}
return lines[0], lines[1], nil
}