-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.go
223 lines (186 loc) · 5.96 KB
/
git.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
/*
MIT License
Copyright (c) 2020 akhettar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package main
import (
"fmt"
"log"
"os"
"regexp"
"strconv"
"time"
"golang.org/x/crypto/openpgp"
"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"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
git "gopkg.in/src-d/go-git.v4"
)
const (
// GithubUser for oauth2 authentication
GithubUser string = "githubTokenAuth"
// GithubAuthToken environment variable
GithubAuthToken string = "GITHUB_AUTH_TOKEN"
// GPGPassword the gpg password
GPGPassword = "GPG_PASSWORD"
// GithubEmail the github email of the user running the action generator
GithubEmail = "GITHUB_EMAIL"
// DryRun flag to disable commiting the changes to the branch
DryRun = "DryRun"
)
var regex = regexp.MustCompile(".+(\\/)(.+)")
// GitClient type
type GitClient struct {
client *github.Client
repo *git.Repository
tree *git.Worktree
gpgEnabled bool
}
// NewGitClient create a new instance of the GitClient
func NewGitClient() *GitClient {
//create the PR in GitHub
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv(GithubAuthToken)},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
client := github.NewClient(tc)
return &GitClient{client: client}
}
func (g *GitClient) prepareRepo(repoLocation string, branchName string, url string) error {
//clone the repo
r, err := git.PlainClone(repoLocation, false, &git.CloneOptions{
Auth: getGitHubAuth(),
URL: url,
})
if err != nil {
log.Printf("failed to clone the repository: %s with error:%v", url, err.Error())
return err
}
err = r.Fetch(&git.FetchOptions{
Auth: getGitHubAuth(),
RefSpecs: []config.RefSpec{"refs/*:refs/*", "HEAD:refs/heads/HEAD"},
})
if err != nil {
println("failed fetching repository", err.Error())
}
//get the work tree
worktree, err := r.Worktree()
//create the new branch
branch := fmt.Sprintf("refs/heads/%s", branchName)
b := plumbing.ReferenceName(branch)
err = worktree.Checkout(&git.CheckoutOptions{Create: true, Force: true, Branch: b})
if err != nil {
log.Printf("looks like the repo already exist: %v\n", err.Error())
err = worktree.Checkout(&git.CheckoutOptions{Create: false, Force: false, Branch: b})
if err != nil {
log.Println("failed to checkout the repo the branch")
return err
}
}
g.repo, g.tree = r, worktree
return nil
}
func (g *GitClient) push(branchName string, url string) error {
//add the change to the stage
_, err := g.tree.Add(".github/")
if err != nil {
log.Printf("failed to stage the file: %v", err.Error())
return err
}
cmtOptions, err := commitOptions()
if err != nil {
return err
}
//commit the change
if _, err = g.tree.Commit("Updating .github/workflows/config.yml", cmtOptions); err != nil {
log.Println("failed to commit the changes")
return err
}
if dryRun, _ := strconv.ParseBool(os.Getenv(DryRun)); !dryRun {
//push the change up to the remote
err = g.repo.Push(&git.PushOptions{Auth: getGitHubAuth()})
if err != nil {
log.Println("failed pushing the changes")
return err
}
pl, _, err := g.client.PullRequests.Create(oauth2.NoContext, "cirtak", getRepoName(url), &github.NewPullRequest{
Title: github.String("Github action config update"),
Head: github.String(branchName),
Base: github.String("master"),
Body: github.String("Updating Github Action config file"),
MaintainerCanModify: github.Bool(true),
})
if err != nil {
log.Printf("failed to create a PR with erro: %v. Repo not found", err.Error())
return err
}
log.Printf("Pull request has been successfuly created: %s", *pl.HTMLURL)
}
return nil
}
func commitOptions() (*git.CommitOptions, error) {
// if GPG is eanbled set the signing key
if os.Getenv(GPGPassword) != "" {
//grab the private key, decrypt it
privKey, err := os.Open("gpg-private-key")
if err != nil {
log.Printf("failed to read pgp private key: %v", err.Error())
return nil, err
}
es, err := openpgp.ReadArmoredKeyRing(privKey)
if err != nil {
log.Printf("failed to read pgp private key: %v", err.Error())
return nil, err
}
key := es[0]
if err = key.PrivateKey.Decrypt([]byte(os.Getenv(GPGPassword))); err != nil {
return nil, err
}
return &git.CommitOptions{
Author: &object.Signature{
Name: "Github Action Templater",
Email: os.Getenv(GithubEmail),
When: time.Now(),
},
All: true,
SignKey: key,
}, nil
}
// return without signature
return &git.CommitOptions{
Author: &object.Signature{
Name: "Github Action Templater",
Email: os.Getenv(GithubEmail),
When: time.Now(),
},
All: true,
}, nil
}
func getRepoName(url string) string {
parts := regex.FindAllStringSubmatch(url, -1)[0]
return parts[len(parts)-1]
}
func getGitHubAuth() *http.BasicAuth {
return &http.BasicAuth{
Username: GithubUser,
Password: os.Getenv(GithubAuthToken),
}
}