-
Notifications
You must be signed in to change notification settings - Fork 18
/
update_formulas.go
386 lines (326 loc) · 10.5 KB
/
update_formulas.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package pkgmgmtutils
import (
"bytes"
"context"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"regexp"
"time"
"github.com/google/go-github/github"
"github.com/solo-io/go-utils/githubutils"
"github.com/solo-io/go-utils/versionutils"
"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"
)
const PRBaseBranchDefault = "master"
type FormulaOptions struct {
Name string // Descriptive name to be used for logging and general identification
FormulaName string // proper formula name without file extension
Path string // repo relative path with file extension
RepoOwner string // repo owner for Formula change
RepoName string // repo name for Formula change
PRRepoOwner string // optional, empty means use RepoOwner
PRRepoName string // optional, empty means use RepoName
PRBranch string // optional, default to master
PRDescription string
PRCommitName string
PRCommitEmail string
VersionRegex string
VersionShaRegex string
DarwinShaRegex string
LinuxShaRegex string
WindowsShaRegex string
dryRun bool
}
type FormulaStatus struct {
Name string
Updated bool
Err error
}
func UpdateFormulas(projectRepoOwner string, projectRepoName string, parentPathSha256 string, reShaFilenames string,
fOpts []FormulaOptions) ([]FormulaStatus, error) {
versionStr := versionutils.GetReleaseVersionOrExitGracefully().String()
version := versionStr[1:]
ctx := context.Background()
client, err := githubutils.GetClient(ctx)
if err != nil {
return nil, err
}
// Get version tag SHA
// GitHub API docs: https://developer.github.com/v3/git/refs/#get-a-reference
ref, _, err := client.Git.GetRef(ctx, projectRepoOwner, projectRepoName, "refs/tags/"+versionStr)
if err != nil {
return nil, err
}
versionSha := *ref.Object.SHA
// Get list of release assets from GitHub
// GitHub API docs: https://developer.github.com/v3/repos/releases/#get-a-release-by-tag-name
rr, _, err := client.Repositories.GetReleaseByTag(ctx, projectRepoOwner, projectRepoName, versionStr)
if err != nil {
return nil, err
}
// shas, err := getLocalBinarySha256(dir, reShaFilenames)
shas, err := getGitHubSha256(rr.Assets, reShaFilenames)
if err != nil {
return nil, err
}
status := make([]FormulaStatus, len(fOpts))
for i, fOpt := range fOpts {
status[i].Name = fOpt.Name
status[i].Updated = false
// Suffix branch name with random number to prevent collisions in rebuilding releases
rand.Seed(time.Now().UnixNano())
branchName := fmt.Sprintf("%s-%s-%d", fOpt.FormulaName, version, rand.Intn(1000))
commitString := fOpt.FormulaName + " " + version
if fOpt.PRRepoName == fOpt.RepoName && fOpt.PRRepoOwner == fOpt.RepoOwner {
err = updateAndPushAllRemote(client, ctx, version, versionSha, branchName, commitString, shas, &fOpt)
} else {
// GitHub APIs do NOT have a way to git pull --ff <remote repo>, so need to clone implementation repo locally
// and pull remote updates.
err = updateAndPushLocalClone(version, versionSha, branchName, commitString, shas, &fOpt, true)
}
if err != nil {
if err == ErrAlreadyUpdated {
status[i].Updated = true
}
status[i].Err = err
continue
}
// Do NOT create PR when dryRun
if !fOpt.dryRun {
prRepoOwner := fOpt.PRRepoOwner
prRepoName := fOpt.PRRepoName
prHead := branchName // For same repo PR case
if prRepoOwner == "" {
prRepoOwner = fOpt.RepoOwner
prRepoName = fOpt.RepoName
} else if prRepoOwner != fOpt.RepoOwner {
// For cross-repo PR, prHead should be in format of "<change repo owner>:branch"
prHead = fOpt.RepoOwner + ":" + branchName
}
base := fOpt.PRBranch
if base == "" {
base = PRBaseBranchDefault
}
// Create GitHub Pull Request
// GitHub API docs: https://developer.github.com/v3/pulls/#create-a-pull-request
_, _, err = client.PullRequests.Create(ctx, prRepoOwner, prRepoName, &github.NewPullRequest{
Title: github.String(commitString),
Head: github.String(prHead),
Base: github.String(base),
Body: github.String(fOpt.PRDescription),
MaintainerCanModify: github.Bool(true),
})
if err != nil {
status[i].Err = err
continue
}
}
status[i].Updated = true
}
return status, nil
}
var (
ErrAlreadyUpdated = errors.New("pkgmgmtutils: formula already updated")
ErrMissingRequiredVersion = errors.New("pkgmgmtutils: missing required version")
ErrMissingRequiredVersionTagSha = errors.New("pkgmgmtutils: missing required version tag sha")
ErrMissingRequiredDarwinSha = errors.New("pkgmgmtutils: missing required sha for darwin binary")
ErrMissingRequiredLinuxSha = errors.New("pkgmgmtutils: missing required sha for linux binary")
ErrMissingRequiredWindowsSha = errors.New("pkgmgmtutils: missing required sha for windows binary")
)
func updateFormula(byt []byte, version string, versionSha string, shas *sha256Outputs, fOpt *FormulaOptions) ([]byte, error) {
// Update Version
if fOpt.VersionRegex != "" {
if version == "" {
return nil, ErrMissingRequiredVersion
}
re := regexp.MustCompile(fOpt.VersionRegex)
// Check if formula has already been updated
if matches := re.FindSubmatch(byt); len(matches) > 1 && bytes.Compare(matches[1], []byte(version)) == 0 {
return byt, ErrAlreadyUpdated
}
byt = replaceSubmatch(byt, []byte(version), re)
}
// Update Version SHA (git tag sha)
if fOpt.VersionShaRegex != "" {
if versionSha == "" {
return nil, ErrMissingRequiredVersionTagSha
}
byt = replaceSubmatch(byt, []byte(versionSha), regexp.MustCompile(fOpt.VersionShaRegex))
}
// Update Mac SHA256
if fOpt.DarwinShaRegex != "" {
if len(shas.darwinSha) == 0 {
return nil, ErrMissingRequiredDarwinSha
}
byt = replaceSubmatch(byt, shas.darwinSha, regexp.MustCompile(fOpt.DarwinShaRegex))
}
// Update Linux SHA256
if fOpt.LinuxShaRegex != "" {
if len(shas.linuxSha) == 0 {
return nil, ErrMissingRequiredLinuxSha
}
byt = replaceSubmatch(byt, shas.linuxSha, regexp.MustCompile(fOpt.LinuxShaRegex))
}
// Update Windows SHA256
if fOpt.WindowsShaRegex != "" {
if len(shas.windowsSha) == 0 {
return nil, ErrMissingRequiredWindowsSha
}
byt = replaceSubmatch(byt, shas.windowsSha, regexp.MustCompile(fOpt.WindowsShaRegex))
}
return byt, nil
}
func updateAndPushLocalClone(version string, versionSha string, branchName string, commitString string, shas *sha256Outputs, fOpt *FormulaOptions, mergeRemote bool) error {
// create temp dir for local git clone
dirTemp, err := ioutil.TempDir("", fOpt.RepoName)
if err != nil {
return err
}
defer os.RemoveAll(dirTemp) // Cleanup local clone when done
// git Clone Github repo
repo, err := git.PlainClone(dirTemp, false, &git.CloneOptions{
URL: "https://github.com/" + fOpt.RepoOwner + "/" + fOpt.RepoName,
})
if err != nil {
return err
}
// Get git repo contents locally
w, err := repo.Worktree()
if err != nil {
return err
}
if mergeRemote {
// git Pull remote PR repo
_, err = repo.CreateRemote(&config.RemoteConfig{
Name: "upstream",
URLs: []string{"https://github.com/" + fOpt.PRRepoOwner + "/" + fOpt.PRRepoName + ".git"},
})
if err != nil {
return err
}
base := fOpt.PRBranch
if base == "" {
base = PRBaseBranchDefault
}
err = w.Pull(&git.PullOptions{
RemoteName: "upstream",
ReferenceName: plumbing.NewBranchReferenceName(base),
})
if err != nil {
return err
}
}
// git checkout -b <branchName>
err = w.Checkout(&git.CheckoutOptions{
Branch: plumbing.NewBranchReferenceName(branchName),
Create: true,
})
if err != nil {
return err
}
formulaPath := filepath.Join(dirTemp, fOpt.Path)
byt, err := ioutil.ReadFile(formulaPath)
if err != nil {
return err
}
// Update formula with new version information
byt, err = updateFormula(byt, version, versionSha, shas, fOpt)
if err != nil {
return err
}
// Write Updated file to git clone directory
err = ioutil.WriteFile(formulaPath, byt, 0644)
if err != nil {
return err
}
// git commit --all
_, err = w.Commit(commitString, &git.CommitOptions{
All: true,
Author: &object.Signature{
Name: fOpt.PRCommitName,
Email: fOpt.PRCommitEmail,
When: time.Now(),
},
})
if err != nil {
return err
}
token, err := githubutils.GetGithubToken()
if err != nil {
return err
}
// git push origin
err = repo.Push(&git.PushOptions{
RemoteName: "origin",
Auth: &http.BasicAuth{
Username: "GitHub Token",
Password: token,
},
})
if err != nil {
return err
}
return nil
}
func updateAndPushAllRemote(client *github.Client, ctx context.Context, version string, versionSha string, branchName string, commitString string, shas *sha256Outputs, fOpt *FormulaOptions) error {
// Get original Formula contents
// GitHub API docs: https://developer.github.com/v3/repos/contents/#get-contents
fileContent, _, _, err := client.Repositories.GetContents(ctx, fOpt.RepoOwner, fOpt.RepoName, fOpt.Path, &github.RepositoryContentGetOptions{
Ref: "refs/heads/master",
})
if err != nil {
return err
}
c, err := fileContent.GetContent()
if err != nil {
return err
}
// Update formula with new version information
byt, err := updateFormula([]byte(c), version, versionSha, shas, fOpt)
if err != nil {
return err
}
// get master branch reference
// GitHub API docs: https://developer.github.com/v3/git/refs/#get-a-reference
baseRef, _, err := client.Git.GetRef(ctx, fOpt.RepoOwner, fOpt.RepoName, "refs/heads/master")
if err != nil {
return err
}
// create new branch from master branch
// GitHub API docs: https://developer.github.com/v3/git/refs/#create-a-reference
_, _, err = client.Git.CreateRef(ctx, fOpt.RepoOwner, fOpt.RepoName, &github.Reference{
Ref: github.String("refs/heads/" + branchName),
Object: &github.GitObject{
SHA: baseRef.Object.SHA,
},
})
if err != nil {
return err
}
now := time.Now()
// git commit and push equivalent
// GitHub API docs: https://developer.github.com/v3/repos/contents/#update-a-file
_, _, err = client.Repositories.UpdateFile(ctx, fOpt.RepoOwner, fOpt.RepoName, fOpt.Path, &github.RepositoryContentFileOptions{
Message: github.String(commitString),
Content: byt,
SHA: fileContent.SHA,
Branch: github.String(branchName),
Committer: &github.CommitAuthor{
Name: github.String(fOpt.PRCommitName),
Email: github.String(fOpt.PRCommitEmail),
Date: &now,
},
})
if err != nil {
return err
}
return nil
}