/
git.go
367 lines (337 loc) · 11 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
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
package pipeline
import (
"context"
"crypto/rand"
"encoding/base64"
"errors"
gohttp "net/http"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"sync"
"github.com/google/go-github/github"
gossh "golang.org/x/crypto/ssh"
"golang.org/x/oauth2"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/transport"
"gopkg.in/src-d/go-git.v4/plumbing/transport/client"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
"gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
"github.com/gaia-pipeline/gaia"
"github.com/gaia-pipeline/gaia/services"
)
const (
refHead = "refs/heads"
)
// GitLSRemote get remote branches from a git repo
// without actually cloning the repo. This is great
// for looking if we have access to this repo.
func GitLSRemote(repo *gaia.GitRepo) error {
// Validate provided git url
if strings.Contains(repo.URL, "@") {
return errors.New("git url should not include username and/or password")
}
// Create new endpoint
ep, err := transport.NewEndpoint(repo.URL)
if err != nil {
return err
}
// Attach credentials if provided
auth, err := getAuthInfo(repo, nil)
if err != nil {
return err
}
// Create client
cl, err := client.NewClient(ep)
if err != nil {
return err
}
// Open new session
s, err := cl.NewUploadPackSession(ep, auth)
if err != nil {
if strings.Contains(err.Error(), "knownhosts: key is unknown") {
gaia.Cfg.Logger.Warn("Warning: Unknown host key.", "error", err.Error(), "URL", repo.URL)
auth, err := getAuthInfo(repo, gossh.InsecureIgnoreHostKey())
if err != nil {
return err
}
s, err = cl.NewUploadPackSession(ep, auth)
if err != nil {
return err
}
} else {
return err
}
}
defer s.Close()
// Get advertised references (e.g. branches)
ar, err := s.AdvertisedReferences()
if err != nil {
return err
}
// Iterate all references
repo.Branches = []string{}
for ref := range ar.References {
// filter for head refs which is a branch
if strings.Contains(ref, refHead) {
repo.Branches = append(repo.Branches, ref)
}
}
return nil
}
// UpdateRepository takes a git type repository and updates
// it by pulling in new code if it's available.
func (s *GaiaPipelineService) UpdateRepository(pipe *gaia.Pipeline) error {
gaia.Cfg.Logger.Debug("updating repository for pipeline type", "type", pipe.Type)
if pipe.Type == gaia.PTypeNodeJS {
pipe.Repo.LocalDest = filepath.Join(pipe.Repo.LocalDest, nodeJSInternalCloneFolder)
}
r, err := git.PlainOpen(pipe.Repo.LocalDest)
if err != nil {
// We don't stop gaia working because of an automated update failed.
// So we just move on.
gaia.Cfg.Logger.Error("error while opening repo: ", "path", pipe.Repo.LocalDest, "error", err.Error())
return err
}
gaia.Cfg.Logger.Debug("checking pipeline: ", "message", pipe.Name)
gaia.Cfg.Logger.Debug("selected branch: ", "message", pipe.Repo.SelectedBranch)
auth, err := getAuthInfo(pipe.Repo, nil)
if err != nil {
// It's also an error if the repo is already up to date so we just move on.
gaia.Cfg.Logger.Error("error getting auth info while doing a pull request: ", "error", err.Error())
return err
}
tree, _ := r.Worktree()
o := &git.PullOptions{
ReferenceName: plumbing.ReferenceName(pipe.Repo.SelectedBranch),
SingleBranch: true,
RemoteName: "origin",
Auth: auth,
}
err = tree.Pull(o)
if err != nil {
if strings.Contains(err.Error(), "knownhosts: key is unknown") {
gaia.Cfg.Logger.Warn("Warning: Unknown host key.", "error", err.Error(), "host", "URL", pipe.Repo.URL)
auth, err = getAuthInfo(pipe.Repo, gossh.InsecureIgnoreHostKey())
if err != nil {
return err
}
o.Auth = auth
if err := tree.Pull(o); err != nil {
return err
}
} else if strings.Contains(err.Error(), "worktree contains unstaged changes") {
gaia.Cfg.Logger.Error("worktree contains unstaged changes, resetting", "error", err.Error())
// Clean the worktree. Because of various builds, it can happen that the local folder if polluted.
// For example go build tends to edit the go.mod file.
if err := tree.Reset(&git.ResetOptions{
Mode: git.HardReset,
}); err != nil {
gaia.Cfg.Logger.Error("failed to reset worktree", "error", err.Error())
return err
}
// Success, move on.
err = nil
} else {
// It's also an error if the repo is already up to date so we just move on.
gaia.Cfg.Logger.Error("error while doing a pull request: ", "error", err.Error())
return err
}
}
gaia.Cfg.Logger.Debug("updating pipeline: ", "message", pipe.Name)
b := newBuildPipeline(pipe.Type)
createPipeline := &gaia.CreatePipeline{Pipeline: *pipe}
if err := b.ExecuteBuild(createPipeline); err != nil {
gaia.Cfg.Logger.Error("error while executing the build", "error", err.Error())
return err
}
if err := b.SavePipeline(&createPipeline.Pipeline); err != nil {
gaia.Cfg.Logger.Error("failed to save pipeline", "error", err.Error())
return err
}
if err := b.CopyBinary(createPipeline); err != nil {
gaia.Cfg.Logger.Error("error while copying binary to plugin folder", "error", err.Error())
return err
}
gaia.Cfg.Logger.Debug("successfully updated: ", "message", pipe.Name)
return nil
}
// gitCloneRepo clones the given repo to a local folder.
// The destination will be attached to the given repo obj.
func gitCloneRepo(repo *gaia.GitRepo) error {
// Check if credentials were provided
auth, err := getAuthInfo(repo, nil)
if err != nil {
return err
}
o := &git.CloneOptions{
Auth: auth,
URL: repo.URL,
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
SingleBranch: true,
ReferenceName: plumbing.ReferenceName(repo.SelectedBranch),
}
// Clone repo
_, err = git.PlainClone(repo.LocalDest, false, o)
if err != nil {
if strings.Contains(err.Error(), "knownhosts: key is unknown") {
gaia.Cfg.Logger.Warn("Warning: Unknown host key.", "error", err.Error(), "URL", repo.URL)
auth, err = getAuthInfo(repo, gossh.InsecureIgnoreHostKey())
if err != nil {
return err
}
o.Auth = auth
// Remove the already checked out code.
if err := os.RemoveAll(repo.LocalDest); err != nil {
gaia.Cfg.Logger.Warn("Failed to clean partially checked out folder.", "error", err.Error(), "destination", repo.LocalDest)
return err
}
// Clone repo again with no host key verification.
_, err = git.PlainClone(repo.LocalDest, false, o)
if err != nil {
gaia.Cfg.Logger.Error("Failed to clone repository.", "error", err.Error(), "URL", repo.URL, "destination", repo.LocalDest)
return err
}
} else {
gaia.Cfg.Logger.Error("Failed to clone repository.", "error", err.Error(), "URL", repo.URL, "destination", repo.LocalDest)
return err
}
}
return nil
}
// UpdateAllCurrentPipelines will update all current pipelines.
func (s *GaiaPipelineService) UpdateAllCurrentPipelines() {
gaia.Cfg.Logger.Debug("starting updating of pipelines...")
allPipelines := GlobalActivePipelines.GetAll()
var wg sync.WaitGroup
sem := make(chan int, 4)
for _, p := range allPipelines {
wg.Add(1)
go func(pipe gaia.Pipeline) {
defer wg.Done()
sem <- 1
defer func() { <-sem }()
_ = s.UpdateRepository(&pipe)
}(p)
}
wg.Wait()
}
// GithubRepoService is an interface defining the Wrapper Interface
// needed to test the github client.
type GithubRepoService interface {
CreateHook(ctx context.Context, owner, repo string, hook *github.Hook) (*github.Hook, *github.Response, error)
}
// GithubClient is a client that has the ability to replace the actual
// git client.
type GithubClient struct {
Repositories GithubRepoService
*github.Client
}
// NewGithubClient creates a wrapper around the github client. This is
// needed in order to decouple gaia from github client to be
// able to unit test createGithubWebhook and ultimately have
// the ability to replace github with anything else.
func NewGithubClient(httpClient *gohttp.Client, repoMock GithubRepoService) GithubClient {
if repoMock != nil {
return GithubClient{
Repositories: repoMock,
}
}
githubClient := github.NewClient(httpClient)
return GithubClient{
Repositories: githubClient.Repositories,
}
}
func createGithubWebhook(token string, repo *gaia.GitRepo, id string, gitRepo GithubRepoService) error {
name := gaia.SecretNamePrefix + id
vault, err := services.DefaultVaultService()
if err != nil {
gaia.Cfg.Logger.Error("unable to initialize vault: ", "error", err.Error())
return err
}
err = vault.LoadSecrets()
if err != nil {
gaia.Cfg.Logger.Error("unable to open vault: ", "error", err.Error())
return err
}
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
config := make(map[string]interface{})
config["url"] = gaia.Cfg.Hostname + "/api/" + gaia.APIVersion + "/pipeline/githook"
secret, err := vault.Get(name)
if err != nil {
secret = []byte(generateWebhookSecret())
vault.Add(name, secret)
err = vault.SaveSecrets()
if err != nil {
return err
}
}
config["secret"] = string(secret)
config["content_type"] = "json"
githubClient := NewGithubClient(tc, gitRepo)
repoName := path.Base(repo.URL)
repoName = strings.TrimSuffix(repoName, ".git")
// var repoLocation string
re := regexp.MustCompile("^(https|git)(://|@)([^/:]+)[/:]([^/:]+)/(.+)$")
m := re.FindAllStringSubmatch(repo.URL, -1)
if m == nil {
return errors.New("failed to extract url parameters from git url")
}
repoUser := m[0][4]
hook, resp, err := githubClient.Repositories.CreateHook(context.Background(), repoUser, repoName, &github.Hook{
Events: []string{"push"},
Name: github.String("web"),
Active: github.Bool(true),
Config: config,
})
if err != nil {
gaia.Cfg.Logger.Error("error while trying to create webhook: ", "error", err.Error())
return err
}
gaia.Cfg.Logger.Info("hook created: ", github.Stringify(hook.Name), resp.Status)
gaia.Cfg.Logger.Info("hook url: ", "url", hook.GetURL())
return nil
}
func generateWebhookSecret() string {
secret := make([]byte, 16)
_, _ = rand.Read(secret)
based := base64.URLEncoding.EncodeToString(secret)
return strings.TrimSuffix(based, "==")
}
func getAuthInfo(repo *gaia.GitRepo, callBack gossh.HostKeyCallback) (transport.AuthMethod, error) {
var auth transport.AuthMethod
if repo.Username != "" && repo.Password != "" {
// Basic auth provided
auth = &http.BasicAuth{
Username: repo.Username,
Password: repo.Password,
}
} else if repo.PrivateKey.Key != "" {
var err error
username := repo.PrivateKey.Username
// If the user does not specify git user here this will not work with github which requires it.
// If it's set though, in case of a custom install or any other medium then github, we don't overwrite it.
if username == "" {
username = "git"
}
auth, err = ssh.NewPublicKeys(username, []byte(repo.PrivateKey.Key), repo.PrivateKey.Password)
if err != nil {
return nil, err
}
if callBack == nil {
callBack, err = ssh.NewKnownHostsCallback()
if err != nil {
return nil, err
}
}
auth.(*ssh.PublicKeys).HostKeyCallback = callBack
}
return auth, nil
}