-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathRepositoryManager.go
546 lines (515 loc) · 16.5 KB
/
RepositoryManager.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/*
* Copyright (c) 2020 Devtron Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package git
import (
"context"
"fmt"
"github.com/devtron-labs/git-sensor/internal"
"github.com/devtron-labs/git-sensor/util"
"io"
"log"
"os"
"strings"
"time"
"github.com/devtron-labs/git-sensor/internal/middleware"
"github.com/devtron-labs/git-sensor/internal/sql"
"github.com/devtron-labs/go-git"
"github.com/devtron-labs/go-git/plumbing"
"github.com/devtron-labs/go-git/plumbing/object"
"github.com/devtron-labs/go-git/plumbing/transport"
"go.uber.org/zap"
)
type RepositoryManager interface {
Fetch(userName, password string, url string, location string) (updated bool, repo *git.Repository, err error)
Add(gitProviderId int, location, url string, userName, password string, authMode sql.AuthMode, sshPrivateKeyContent string) error
Clean(cloneDir string) error
ChangesSince(checkoutPath string, branch string, from string, to string, count int) ([]*GitCommit, error)
ChangesSinceByRepository(repository *git.Repository, branch string, from string, to string, count int) ([]*GitCommit, error)
GetCommitMetadata(checkoutPath, commitHash string) (*GitCommit, error)
ChangesSinceByRepositoryForAnalytics(checkoutPath string, branch string, Old string, New string) (*GitChanges, error)
GetCommitForTag(checkoutPath, tag string) (*GitCommit, error)
CreateSshFileIfNotExistsAndConfigureSshCommand(location string, gitProviderId int, sshPrivateKeyContent string) error
}
type RepositoryManagerImpl struct {
logger *zap.SugaredLogger
gitUtil *GitUtil
configuration *internal.Configuration
}
func NewRepositoryManagerImpl(logger *zap.SugaredLogger, gitUtil *GitUtil, configuration *internal.Configuration) *RepositoryManagerImpl {
return &RepositoryManagerImpl{logger: logger, gitUtil: gitUtil, configuration: configuration}
}
func (impl RepositoryManagerImpl) Add(gitProviderId int, location string, url string, userName, password string, authMode sql.AuthMode, sshPrivateKeyContent string) error {
var err error
start := time.Now()
defer func() {
util.TriggerGitOperationMetrics("add", start, err)
}()
err = os.RemoveAll(location)
if err != nil {
impl.logger.Errorw("error in cleaning checkout path", "err", err)
return err
}
err = impl.gitUtil.Init(location, url, true)
if err != nil {
impl.logger.Errorw("err in git init", "err", err)
return err
}
// check ssh
if authMode == sql.AUTH_MODE_SSH {
err = impl.CreateSshFileIfNotExistsAndConfigureSshCommand(location, gitProviderId, sshPrivateKeyContent)
if err != nil {
return err
}
}
opt, errorMsg, err := impl.gitUtil.Fetch(location, userName, password)
if err != nil {
impl.logger.Errorw("error in cloning repo", "errorMsg", errorMsg, "err", err)
return err
}
impl.logger.Debugw("opt msg", "opt", opt)
return nil
}
func (impl RepositoryManagerImpl) Clean(dir string) error {
var err error
start := time.Now()
defer func() {
util.TriggerGitOperationMetrics("clean", start, err)
}()
err = os.RemoveAll(dir)
return err
}
func (impl RepositoryManagerImpl) clone(auth transport.AuthMethod, cloneDir string, url string) (*git.Repository, error) {
timeoutContext, _ := context.WithTimeout(context.Background(), CLONE_TIMEOUT_SEC*time.Second)
impl.logger.Infow("cloning repository ", "url", url, "cloneDir", cloneDir)
repo, err := git.PlainCloneContext(timeoutContext, cloneDir, true, &git.CloneOptions{
URL: url,
Auth: auth,
})
if err != nil {
impl.logger.Errorw("error in cloning repo ", "url", url, "err", err)
} else {
impl.logger.Infow("repo cloned", "url", url)
}
return repo, err
}
func (impl RepositoryManagerImpl) Fetch(userName, password string, url string, location string) (updated bool, repo *git.Repository, err error) {
start := time.Now()
defer func() {
util.TriggerGitOperationMetrics("fetch", start, err)
}()
middleware.GitMaterialPollCounter.WithLabelValues().Inc()
r, err := git.PlainOpen(location)
if err != nil {
return false, nil, err
}
res, errorMsg, err := impl.gitUtil.Fetch(location, userName, password)
if err == nil && len(res) > 0 {
impl.logger.Infow("repository updated", "location", url)
//updated
middleware.GitPullDuration.WithLabelValues("true", "true").Observe(time.Since(start).Seconds())
return true, r, nil
} else if err == nil && len(res) == 0 {
impl.logger.Debugw("no update for ", "path", url)
middleware.GitPullDuration.WithLabelValues("true", "false").Observe(time.Since(start).Seconds())
return false, r, nil
} else {
impl.logger.Errorw("error in updating repository", "err", err, "location", url, "error msg", errorMsg)
middleware.GitPullDuration.WithLabelValues("false", "false").Observe(time.Since(start).Seconds())
return false, r, err
}
}
func (impl RepositoryManagerImpl) GetCommitForTag(checkoutPath, tag string) (*GitCommit, error) {
var err error
start := time.Now()
defer func() {
util.TriggerGitOperationMetrics("getCommitForTag", start, err)
}()
tag = strings.TrimSpace(tag)
r, err := git.PlainOpen(checkoutPath)
if err != nil {
return nil, err
}
tagRef, err := r.Tag(tag)
if err != nil {
impl.logger.Errorw("error in fetching tag", "path", checkoutPath, "tag", tag, "err", err)
return nil, err
}
commit, err := r.CommitObject(plumbing.NewHash(tagRef.Hash().String()))
if err != nil {
impl.logger.Errorw("error in fetching tag", "path", checkoutPath, "hash", tagRef, "err", err)
return nil, err
}
gitCommit := &GitCommit{
Author: commit.Author.String(),
Commit: commit.Hash.String(),
Date: commit.Author.When,
Message: commit.Message,
}
return gitCommit, nil
}
func (impl RepositoryManagerImpl) GetCommitMetadata(checkoutPath, commitHash string) (*GitCommit, error) {
var err error
start := time.Now()
defer func() {
util.TriggerGitOperationMetrics("getCommitMetadata", start, err)
}()
r, err := git.PlainOpen(checkoutPath)
if err != nil {
return nil, err
}
commit, err := r.CommitObject(plumbing.NewHash(commitHash))
if err != nil {
impl.logger.Errorw("error in fetching commit", "path", checkoutPath, "hash", commitHash, "err", err)
return nil, err
}
gitCommit := &GitCommit{
Author: commit.Author.String(),
Commit: commit.Hash.String(),
Date: commit.Author.When,
Message: commit.Message,
}
return gitCommit, nil
}
// from -> old commit
// to -> new commit
func (impl RepositoryManagerImpl) ChangesSinceByRepository(repository *git.Repository, branch string, from string, to string, count int) ([]*GitCommit, error) {
// fix for azure devops (manual trigger webhook bases pipeline) :
// branch name comes as 'refs/heads/master', we need to extract actual branch name out of it.
// https://stackoverflow.com/questions/59956206/how-to-get-a-branch-name-with-a-slash-in-azure-devops
var err error
start := time.Now()
defer func() {
util.TriggerGitOperationMetrics("changesSinceByRepository", start, err)
}()
if strings.HasPrefix(branch, "refs/heads/") {
branch = strings.ReplaceAll(branch, "refs/heads/", "")
}
branchRef := fmt.Sprintf("refs/remotes/origin/%s", branch)
ref, err := repository.Reference(plumbing.ReferenceName(branchRef), true)
if err != nil && err == plumbing.ErrReferenceNotFound {
impl.logger.Errorw("ref not found", "branch", branch, "err", err)
return nil, fmt.Errorf("branch %s not found in the repository ", branch)
} else if err != nil {
impl.logger.Errorw("error in getting reference", "branch", branch, "err", err)
return nil, err
}
itr, err := repository.Log(&git.LogOptions{From: ref.Hash()})
if err != nil {
impl.logger.Errorw("error in getting iterator", "branch", branch, "err", err)
return nil, err
}
var gitCommits []*GitCommit
itrCounter := 0
commitToFind := len(to) == 0 //no commit mentioned
breakLoop := false
for {
if breakLoop {
break
}
//TODO: move code out of this dummy function after removing defer inside loop
func() {
if itrCounter > 1000 || len(gitCommits) == count {
breakLoop = true
return
}
commit, err := itr.Next()
if err == io.EOF {
breakLoop = true
return
}
if err != nil {
impl.logger.Errorw("error in iterating", "branch", branch, "err", err)
breakLoop = true
return
}
if !commitToFind && strings.Contains(commit.Hash.String(), to) {
commitToFind = true
}
if !commitToFind {
return
}
if commit.Hash.String() == from && len(from) > 0 {
//found end
breakLoop = true
return
}
gitCommit := &GitCommit{
Author: commit.Author.String(),
Commit: commit.Hash.String(),
Date: commit.Author.When,
Message: commit.Message,
}
gitCommit.TruncateMessageIfExceedsMaxLength()
impl.logger.Debugw("commit dto for repo ", "repo", repository, commit)
gitCommits = append(gitCommits, gitCommit)
itrCounter = itrCounter + 1
if impl.configuration.EnableFileStats {
defer func() {
if err := recover(); err != nil {
impl.logger.Error("file stats function panicked for commit", "err", err, "commit", commit)
}
}()
//TODO: implement below Stats() function using git CLI as it panics in some cases, remove defer function after using git CLI
stats, err := commit.Stats()
if err != nil {
impl.logger.Errorw("error in fetching stats", "err", err)
}
gitCommit.FileStats = &stats
}
}()
}
return gitCommits, err
}
func (impl RepositoryManagerImpl) ChangesSince(checkoutPath string, branch string, from string, to string, count int) ([]*GitCommit, error) {
var err error
start := time.Now()
defer func() {
util.TriggerGitOperationMetrics("changesSince", start, err)
}()
if count == 0 {
count = impl.configuration.GitHistoryCount
}
r, err := git.PlainOpen(checkoutPath)
if err != nil {
return nil, err
}
///---------------------
return impl.ChangesSinceByRepository(r, branch, from, to, count)
///----------------------
}
type GitChanges struct {
Commits []*Commit
FileStats object.FileStats
}
type FileStatsResult struct {
FileStats object.FileStats
Error error
}
// from -> old commit
// to -> new commit
func (impl RepositoryManagerImpl) ChangesSinceByRepositoryForAnalytics(checkoutPath string, branch string, Old string, New string) (*GitChanges, error) {
var err error
start := time.Now()
defer func() {
util.TriggerGitOperationMetrics("changesSinceByRepositoryForAnalytics", start, err)
}()
GitChanges := &GitChanges{}
repository, err := git.PlainOpen(checkoutPath)
if err != nil {
return nil, err
}
newHash := plumbing.NewHash(New)
oldHash := plumbing.NewHash(Old)
old, err := repository.CommitObject(newHash)
if err != nil {
return nil, err
}
new, err := repository.CommitObject(oldHash)
if err != nil {
return nil, err
}
oldTree, err := old.Tree()
if err != nil {
return nil, err
}
newTree, err := new.Tree()
if err != nil {
return nil, err
}
patch, err := oldTree.Patch(newTree)
if err != nil {
impl.logger.Errorw("can't get patch: ", "err", err)
return nil, err
}
commits, err := computeDiff(repository, &newHash, &oldHash)
if err != nil {
impl.logger.Errorw("can't get commits: ", "err", err)
}
var serializableCommits []*Commit
for _, c := range commits {
t, err := repository.TagObject(c.Hash)
if err != nil && err != plumbing.ErrObjectNotFound {
impl.logger.Errorw("can't get tag: ", "err", err)
}
serializableCommits = append(serializableCommits, transform(c, t))
}
GitChanges.Commits = serializableCommits
fileStats := patch.Stats()
impl.logger.Debugw("computed files stats", "filestats", fileStats)
GitChanges.FileStats = fileStats
return GitChanges, nil
}
func (impl RepositoryManagerImpl) CreateSshFileIfNotExistsAndConfigureSshCommand(location string, gitProviderId int, sshPrivateKeyContent string) error {
// add private key
var err error
start := time.Now()
defer func() {
util.TriggerGitOperationMetrics("createSshFileIfNotExistsAndConfigureSshCommand", start, err)
}()
sshPrivateKeyPath, err := GetOrCreateSshPrivateKeyOnDisk(gitProviderId, sshPrivateKeyContent)
if err != nil {
impl.logger.Errorw("error in creating ssh private key", "err", err)
return err
}
//git config core.sshCommand
_, errorMsg, err := impl.gitUtil.ConfigureSshCommand(location, sshPrivateKeyPath)
if err != nil {
impl.logger.Errorw("error in configuring ssh command while adding repo", "errorMsg", errorMsg, "err", err)
return err
}
return nil
}
func computeDiff(r *git.Repository, newHash *plumbing.Hash, oldHash *plumbing.Hash) ([]*object.Commit, error) {
processed := make(map[string]*object.Commit, 0)
//t := time.Now()
h := newHash //plumbing.NewHash(newHash)
h2 := oldHash //plumbing.NewHash(oldHash)
c1, err := r.CommitObject(*h)
if err != nil {
return nil, fmt.Errorf("not found commit %s", h.String())
}
c2, err := r.CommitObject(*h2)
if err != nil {
return nil, fmt.Errorf("not found commit %s", h2.String())
}
var parents, ancestorStack []*object.Commit
ps := c1.Parents()
for {
n, err := ps.Next()
if err == io.EOF {
break
}
if n.Hash.String() != c2.Hash.String() {
parents = append(parents, n)
}
}
ancestorStack = append(ancestorStack, parents...)
processed[c1.Hash.String()] = c1
for len(ancestorStack) > 0 {
lastIndex := len(ancestorStack) - 1
//dont process already processed in this algorithm path is not important
if _, ok := processed[ancestorStack[lastIndex].Hash.String()]; ok {
ancestorStack = ancestorStack[:lastIndex]
continue
}
//if this is old commit provided for processing then ignore it
if ancestorStack[lastIndex].Hash.String() == c2.Hash.String() {
ancestorStack = ancestorStack[:lastIndex]
continue
}
m, err := ancestorStack[lastIndex].MergeBase(c2)
//fmt.Printf("mergebase between %s and %s is %s length %d\n", ancestorStack[lastIndex].Hash.String(), c2.Hash.String(), m[0].Hash.String(), len(m))
if err != nil {
log.Fatal("Error in mergebase " + ancestorStack[lastIndex].Hash.String() + " " + c2.Hash.String())
}
// if commit being analyzed is itself merge commit then dont process as it is common in both old and new
if in(ancestorStack[lastIndex], m) {
ancestorStack = ancestorStack[:lastIndex]
continue
}
d, p := getDiffTillBranchingOrDest(ancestorStack[lastIndex], m)
//fmt.Printf("length of diff %d\n", len(d))
for _, v := range d {
processed[v.Hash.String()] = v
}
curNodes := make(map[string]bool, 0)
for _, v := range ancestorStack {
curNodes[v.Hash.String()] = true
}
processed[ancestorStack[lastIndex].Hash.String()] = ancestorStack[lastIndex]
ancestorStack = ancestorStack[:lastIndex]
for _, v := range p {
if ok2, _ := curNodes[v.Hash.String()]; !ok2 {
ancestorStack = append(ancestorStack, v)
}
}
}
var commits []*object.Commit
for _, d := range processed {
commits = append(commits, d)
}
return commits, nil
}
func getDiffTillBranchingOrDest(src *object.Commit, dst []*object.Commit) (diff, parents []*object.Commit) {
if in(src, dst) {
return
}
new := src
for {
ps := new.Parents()
parents = make([]*object.Commit, 0)
for {
n, err := ps.Next()
if err == io.EOF {
break
}
parents = append(parents, n)
}
if len(parents) > 1 || len(parents) == 0 {
return
}
if in(parents[0], dst) {
parents = nil
return
} else {
//fmt.Printf("added %s when child is %s and merge base is %s", parents[0].Hash.String(), src.Hash.String(), dst[0].Hash.String())
diff = append(diff, parents[0])
}
new = parents[0]
}
}
func in(obj *object.Commit, list []*object.Commit) bool {
for _, v := range list {
if v.Hash.String() == obj.Hash.String() {
return true
}
}
return false
}
func transform(src *object.Commit, tag *object.Tag) (dst *Commit) {
if src == nil {
return nil
}
dst = &Commit{
Hash: &Hash{
Long: src.Hash.String(),
Short: src.Hash.String()[:8],
},
Tree: &Tree{
Long: src.TreeHash.String(),
Short: src.TreeHash.String()[:8],
},
Author: &Author{
Name: src.Author.Name,
Email: src.Author.Email,
Date: src.Author.When,
},
Committer: &Committer{
Name: src.Committer.Name,
Email: src.Committer.Email,
Date: src.Committer.When,
},
Subject: src.Message,
Body: "",
}
if tag != nil {
dst.Tag = &Tag{
Name: tag.Name,
Date: tag.Tagger.When,
}
}
return
}