-
Notifications
You must be signed in to change notification settings - Fork 18
/
local_clone_change_pusher.go
137 lines (118 loc) · 2.89 KB
/
local_clone_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
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
package internal
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/solo-io/go-utils/githubutils"
"github.com/solo-io/go-utils/pkgmgmtutils/formula_updater_types"
"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"
)
func NewLocalCloneChangePusher() formula_updater_types.ChangePusher {
return &localCloneChangePusher{}
}
type localCloneChangePusher struct {
}
func (l *localCloneChangePusher) UpdateAndPush(
_ context.Context,
version string,
versionSha string,
branchName string,
commitMessage string,
perPlatformShas *formula_updater_types.PerPlatformSha256,
formulaOptions *formula_updater_types.FormulaOptions,
) error {
// create temp dir for local git clone
dirTemp, err := ioutil.TempDir("", formulaOptions.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/" + formulaOptions.RepoOwner + "/" + formulaOptions.RepoName,
})
if err != nil {
return err
}
// Get git repo contents locally
w, err := repo.Worktree()
if err != nil {
return err
}
// git Pull remote PR repo
_, err = repo.CreateRemote(&config.RemoteConfig{
Name: "upstream",
URLs: []string{"https://github.com/" + formulaOptions.PRRepoOwner + "/" + formulaOptions.PRRepoName + ".git"},
})
if err != nil {
return err
}
base := formulaOptions.PRBranch
if base == "" {
base = "master"
}
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, formulaOptions.Path)
byt, err := ioutil.ReadFile(formulaPath)
if err != nil {
return err
}
// Update formula with new version information
byt, err = UpdateFormulaBytes(byt, version, versionSha, perPlatformShas, formulaOptions)
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(commitMessage, &git.CommitOptions{
All: true,
Author: &object.Signature{
Name: formulaOptions.PRCommitName,
Email: formulaOptions.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
}