/
pullrequest.go
203 lines (177 loc) · 5.45 KB
/
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package opts
import (
"fmt"
"strings"
"github.com/jenkins-x/jx/v2/pkg/gits"
"github.com/jenkins-x/jx/v2/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/util/uuid"
)
// PullRequestDetails details to pass in to create a PullRequest if the repository is modified
type PullRequestDetails struct {
Dir string
RepositoryGitURL string
RepositoryBranch string
RepositoryMessage string
BranchNameText string
Title string
Message string
}
// CreatePullRequest creates a Pull Request on the given repository
func (options *CommonOptions) CreatePullRequest(o *PullRequestDetails, modifyFn func() error) error {
if o.RepositoryBranch == "" {
o.RepositoryBranch = "master"
}
dir := o.Dir
originalGitURL := o.RepositoryGitURL
message := o.RepositoryMessage
gitter := options.Git()
gitInfo, err := gits.ParseGitURL(originalGitURL)
if err != nil {
return err
}
provider, err := options.GitProviderForURL(originalGitURL, message)
if err != nil {
return err
}
if o.Message == "" {
log.Logger().Warn("missing option: 'message' when creating a PR")
}
username := provider.CurrentUsername()
if username == "" {
return fmt.Errorf("no git user name found")
}
originalOrg := gitInfo.Organisation
originalRepo := gitInfo.Name
repo, err := provider.GetRepository(username, originalRepo)
if err != nil {
if originalOrg == username {
return err
}
// lets try create a fork - using a blank organisation to force a user specific fork
repo, err = provider.ForkRepository(originalOrg, originalRepo, "")
if err != nil {
return errors.Wrapf(err, "failed to fork GitHub repo %s/%s to user %s", originalOrg, originalRepo, username)
}
log.Logger().Infof("Forked %s to %s\n", message, util.ColorInfo(repo.HTMLURL))
repo, err = provider.GetRepository(username, originalRepo)
if err != nil {
return errors.Wrapf(err, "failed to load GitHub repo %s/%s", username, originalRepo)
}
}
fork := repo.Fork
err = gitter.Clone(repo.CloneURL, dir)
if err != nil {
return errors.Wrapf(err, "cloning the %s %q", message, repo.CloneURL)
}
log.Logger().Infof("cloned fork of %s %s to %s", message, util.ColorInfo(repo.HTMLURL), util.ColorInfo(dir))
err = gitter.SetRemoteURL(dir, "upstream", originalGitURL)
if err != nil {
return errors.Wrapf(err, "setting remote upstream %q in forked %s", originalGitURL, message)
}
err = gitter.PullUpstream(dir)
if err != nil {
return errors.Wrapf(err, "pulling upstream of forked %s", message)
}
if fork {
base := "master"
err = gitter.ResetToUpstream(dir, base)
if err != nil {
return errors.Wrapf(err, "resetting forked branch %s to upstream version", base)
}
}
branchName := gitter.ConvertToValidBranchName(o.BranchNameText)
branchNames, err := gitter.RemoteBranchNames(dir, "remotes/origin/")
if err != nil {
return errors.Wrapf(err, "failed to load remote branch names")
}
if util.StringArrayIndex(branchNames, branchName) >= 0 {
// lets append a UUID as the branch name already exists
branchName += "-" + string(uuid.NewUUID())
}
err = gitter.CreateBranch(dir, branchName)
if err != nil {
return err
}
err = gitter.Checkout(dir, branchName)
if err != nil {
return err
}
err = modifyFn()
if err != nil {
return err
}
err = gitter.Add(dir, "*", "*/*")
if err != nil {
return err
}
changes, err := gitter.HasChanges(dir)
if err != nil {
return err
}
if !changes {
log.Logger().Infof("No source changes so not generating a Pull Request")
return nil
}
err = gitter.CommitDir(dir, o.Title)
if err != nil {
return err
}
// lets find a previous PR so we can force push to its branch
prs, err := provider.ListOpenPullRequests(gitInfo.Organisation, gitInfo.Name)
if err != nil {
return errors.Wrapf(err, "failed to list open pull requests on %s", gitInfo.HTMLURL)
}
for _, pr := range prs {
author := pr.Author
if pr.Title == o.Title && author != nil && author.Login == username {
log.Logger().Infof("found existing PullRequest: %s", util.ColorInfo(pr.URL))
head := pr.HeadRef
if head == nil {
log.Logger().Warnf("No head value!")
} else {
headText := *head
remoteBranch := headText
paths := strings.SplitN(headText, ":", 2)
if len(paths) > 1 {
remoteBranch = paths[1]
}
log.Logger().Infof("force pushing to remote branch %s", util.ColorInfo(remoteBranch))
err := gitter.ForcePushBranch(dir, branchName, remoteBranch)
if err != nil {
return errors.Wrapf(err, "failed to force push to remote branch %s", remoteBranch)
}
pr.Body = o.Message
log.Logger().Infof("force pushed new pull request change to: %s", util.ColorInfo(pr.URL))
err = provider.AddPRComment(pr, o.Message)
if err != nil {
return errors.Wrapf(err, "failed to add message to PR %s", pr.URL)
}
return nil
}
}
}
err = gitter.Push(dir, "origin", false, "HEAD")
if err != nil {
return errors.Wrapf(err, "pushing to %s in dir %q", message, dir)
}
base := o.RepositoryBranch
head := branchName
if fork {
head = username + ":" + branchName
}
gha := &gits.GitPullRequestArguments{
GitRepository: gitInfo,
Title: o.Title,
Body: o.Message,
Base: base,
Head: head,
}
pr, err := provider.CreatePullRequest(gha)
if err != nil {
return errors.Wrapf(err, "failed to create pull quest from base %s with header %s", base, head)
}
log.Logger().Infof("Created Pull Request: %s\n", util.ColorInfo(pr.URL))
return nil
}