-
Notifications
You must be signed in to change notification settings - Fork 18
/
remote_change_pusher.go
89 lines (77 loc) · 2.48 KB
/
remote_change_pusher.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
package internal
import (
"context"
"time"
"github.com/google/go-github/github"
"github.com/solo-io/go-utils/pkgmgmtutils/formula_updater_types"
)
func NewRemoteChangePusher(gitClient *github.Client) formula_updater_types.ChangePusher {
return &remoteChangePusher{
gitClient: gitClient,
}
}
type remoteChangePusher struct {
gitClient *github.Client
}
func (r *remoteChangePusher) UpdateAndPush(
ctx context.Context,
version string,
versionSha string,
branchName string,
commitMessage string,
perPlatformShas *formula_updater_types.PerPlatformSha256,
formulaOptions *formula_updater_types.FormulaOptions,
) error {
// Get original Formula contents
// GitHub API docs: https://developer.github.com/v3/repos/contents/#get-contents
fileContent, _, _, err := r.gitClient.Repositories.GetContents(ctx, formulaOptions.RepoOwner, formulaOptions.RepoName, formulaOptions.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 := UpdateFormulaBytes([]byte(c), version, versionSha, perPlatformShas, formulaOptions)
if err != nil {
return err
}
// get master branch reference
// GitHub API docs: https://developer.github.com/v3/git/refs/#get-a-reference
baseRef, _, err := r.gitClient.Git.GetRef(ctx, formulaOptions.RepoOwner, formulaOptions.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 = r.gitClient.Git.CreateRef(ctx, formulaOptions.RepoOwner, formulaOptions.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 = r.gitClient.Repositories.UpdateFile(ctx, formulaOptions.RepoOwner, formulaOptions.RepoName, formulaOptions.Path, &github.RepositoryContentFileOptions{
Message: github.String(commitMessage),
Content: byt,
SHA: fileContent.SHA,
Branch: github.String(branchName),
Committer: &github.CommitAuthor{
Name: github.String(formulaOptions.PRCommitName),
Email: github.String(formulaOptions.PRCommitEmail),
Date: &now,
},
})
if err != nil {
return err
}
return nil
}