/
builder.go
268 lines (232 loc) · 6.36 KB
/
builder.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package builder
import (
"context"
"errors"
"fmt"
"log"
"path"
"github.com/google/go-github/github"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/config"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
client "github.com/mattmoor/bindings/pkg/github"
"github.com/mattmoor/knobots/pkg/comment"
)
const (
statusNotFound = "Not Found"
)
// TODO: support signoff.
type Builder struct {
DryRun bool
Verbose bool
SkipLockChanges bool
// Git options.
Workspace string
Owner string
Repo string
Branch string
CommitBranch *string
// PR options.
Title string
Body string
Token *string
Signature *object.Signature
Signoff bool
// Binding
Username string
Password string
}
// Commit will commit changes to the local branch, and then optionally branch a new branch if set.
func (b *Builder) Commit(r *git.Repository) (plumbing.ReferenceName, error) {
// First, build the worktree.
wt, err := r.Worktree()
if err != nil {
return "", fmt.Errorf("Error fetching worktree: %v", err)
}
// Check the status of the worktree, and if there aren't any changes
// bail out we're done.
st, err := wt.Status()
if err != nil {
return "", fmt.Errorf("Error fetching worktree status: %v", err)
}
changes := true
if len(st) == 0 {
log.Println("No changes")
changes = false
}
nonGopkgCount := 0
for p := range st {
if path.Base(p) != "Gopkg.lock" {
nonGopkgCount++
}
_, err = wt.Add(p)
if err != nil {
return "", fmt.Errorf("Error staging %q: %v", p, err)
}
// Display any changed we do find: `git status --porcelain`
log.Printf("Added - %v", p)
}
// If we want to ignore lock changes, just return.
if b.SkipLockChanges && nonGopkgCount == 0 {
log.Println("Only Gopkg.lock files changed (skipping PR).")
changes = false
}
if changes {
commitMessage := b.Title + "\n\n" + b.Body
// Commit the staged changes to the repo.
if _, err := wt.Commit(commitMessage, &git.CommitOptions{Author: b.Signature}); err != nil {
return "", fmt.Errorf("Error committing changes: %v", err)
}
}
var rn plumbing.ReferenceName
// Commit branch is optional.
if b.CommitBranch != nil {
headRef, err := r.Head()
if err != nil {
return "", fmt.Errorf("Error fetching workspace HEAD: %v", err)
}
// Create and checkout a new branch from the commit of the HEAD reference.
// This should be roughly equivalent to `git checkout -b {new-branch}`
newBranchName := plumbing.NewBranchReferenceName(*b.CommitBranch)
log.Println("new branch", newBranchName)
if err := wt.Checkout(&git.CheckoutOptions{
Hash: headRef.Hash(),
Branch: newBranchName,
Create: true,
Force: true,
}); err != nil {
return "", fmt.Errorf("Error checkout out new branch: %v", err)
}
rn = newBranchName
} else {
ref, err := r.Head()
if err != nil {
return "", err
}
rn = ref.Name()
}
return rn, nil
}
func (b *Builder) Push(r *git.Repository, rs config.RefSpec) error {
// Push the branch to a remote to which we have write access.
// TODO(mattmoor): What if the fork doesn't exist, or has another name?
remote, err := r.CreateRemote(&config.RemoteConfig{
Name: b.Username,
URLs: []string{fmt.Sprintf("https://github.com/%s/%s.git", b.Username, b.Repo)},
})
if err != nil {
if err.Error() == "remote already exists" {
// Skip.
} else {
return fmt.Errorf("Error creating new remote: %v", err)
}
}
remote, err = r.Remote(b.Username)
if err != nil {
return fmt.Errorf("Error creating new remote: %v", err)
}
// Publish all local branches to the remote.
err = remote.Push(&git.PushOptions{
RemoteName: b.Username,
RefSpecs: []config.RefSpec{rs},
Auth: &http.BasicAuth{
Username: b.Username, // This can be anything.
Password: b.Password,
},
})
if err != nil {
return fmt.Errorf("Error pushing to remote: %v", err)
}
return nil
}
func (b *Builder) PR(branch string) error {
// Head has the form source-owner:branch, per the Github API docs.
head := fmt.Sprintf("%s:%s", b.Username, branch)
// Inject a signature into the body that will help us clean up matching older PRs.
bodyWithSignature := comment.WithSignature(b.Title, b.Body)
// Inject the token (if specified) into the body of the PR, so
// that we can identify it's provenance.
if b.Token != nil {
bodyWithSignature = comment.WithSignature(*b.Token, *bodyWithSignature)
}
ctx := context.Background()
ghc, err := client.New(ctx)
if err != nil {
return fmt.Errorf("Error creating github client: %v", err)
}
pr, _, err := ghc.PullRequests.Create(ctx, b.Owner, b.Repo, &github.NewPullRequest{
Title: &b.Title,
Body: bodyWithSignature,
Head: &head,
Base: &b.Branch,
})
if err != nil {
return fmt.Errorf("Error creating PR: %v", err)
}
log.Printf("Created PR: #%d", pr.GetNumber())
return nil
}
func (b *Builder) Do() error {
log.Printf("Debug: %#v\n\n", b)
// Clean up older PRs as the first thing we do so that if the latest batch of
// changes needs nothing we don't leave old PRs around.
if err := b.cleanupOlderPRs(); err != nil {
return fmt.Errorf("Error cleaning up PRs: %v", err)
}
r, err := git.PlainOpen(b.Workspace)
if err != nil {
return fmt.Errorf("Error opening /workspace: %v", err)
}
rn, err := b.Commit(r)
if err != nil {
return err
}
// Always match local name to remote name.
rs := config.RefSpec(fmt.Sprintf("%s:%s", rn, rn))
log.Printf("Pushing to refspec: %s", rs.String())
if err := b.Push(r, rs); err != nil {
return err
}
if err := b.PR(rn.String()); err != nil {
return err
}
return nil
}
func (b *Builder) cleanupOlderPRs() error {
ctx := context.Background()
ghc, err := client.New(ctx)
if err != nil {
return err
}
closed := "closed"
lopt := &github.PullRequestListOptions{
Base: b.Branch,
}
for {
prs, resp, err := ghc.PullRequests.List(ctx, b.Owner, b.Repo, lopt)
if err != nil {
ghe := &github.ErrorResponse{}
if errors.As(err, &ghe) && ghe.Message == statusNotFound {
return nil
}
return err
}
for _, pr := range prs {
if comment.HasSignature(b.Signature.Name, pr.GetBody()) {
_, _, err := ghc.PullRequests.Edit(ctx, b.Owner, b.Repo, pr.GetNumber(), &github.PullRequest{
State: &closed,
})
if err != nil {
return err
}
}
}
if resp.NextPage == 0 {
break
}
lopt.Page = resp.NextPage
}
return nil
}