Skip to content

Commit 2b2451b

Browse files
Merge pull request #58 from devtron-labs/shallow-clone
feat: integration test cases added and refactoring around gitContext
2 parents 8438953 + 4ada12e commit 2b2451b

File tree

5 files changed

+998
-12
lines changed

5 files changed

+998
-12
lines changed

pkg/RepoManages.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,11 @@ func (impl RepoManagerImpl) checkoutMaterial(material *sql.GitMaterial) (*sql.Gi
343343
if err != nil {
344344
return material, err
345345
}
346-
err = impl.repositoryManager.Add(material.GitProviderId, checkoutPath, material.Url, userName, password, gitProvider.AuthMode, gitProvider.SshPrivateKey)
346+
gitContext := &git.GitContext{
347+
Username: userName,
348+
Password: password,
349+
}
350+
err = impl.repositoryManager.Add(material.GitProviderId, checkoutPath, material.Url, gitContext, gitProvider.AuthMode, gitProvider.SshPrivateKey)
347351
if err == nil {
348352
material.CheckoutLocation = checkoutPath
349353
material.CheckoutStatus = true
@@ -618,7 +622,11 @@ func (impl RepoManagerImpl) GetLatestCommitForBranch(pipelineMaterialId int, bra
618622
}()
619623

620624
userName, password, err := git.GetUserNamePassword(gitMaterial.GitProvider)
621-
updated, repo, err := impl.repositoryManager.Fetch(userName, password, gitMaterial.Url, gitMaterial.CheckoutLocation)
625+
gitContext := &git.GitContext{
626+
Username: userName,
627+
Password: password,
628+
}
629+
updated, repo, err := impl.repositoryManager.Fetch(gitContext, gitMaterial.Url, gitMaterial.CheckoutLocation)
622630

623631
if err != nil {
624632
impl.logger.Errorw("error in fetching the repository ", "err", err)

pkg/git/GitCliUtil.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package git
22

33
import (
4+
"context"
45
"fmt"
56
"go.uber.org/zap"
67
"gopkg.in/src-d/go-git.v4"
@@ -10,6 +11,13 @@ import (
1011
"strings"
1112
)
1213

14+
type GitContext struct {
15+
context.Context // Embedding original Go context
16+
Username string
17+
Password string
18+
CloningMode string
19+
}
20+
1321
type GitUtil struct {
1422
logger *zap.SugaredLogger
1523
}
@@ -22,10 +30,10 @@ func NewGitUtil(logger *zap.SugaredLogger) *GitUtil {
2230

2331
const GIT_ASK_PASS = "/git-ask-pass.sh"
2432

25-
func (impl *GitUtil) Fetch(rootDir string, username string, password string) (response, errMsg string, err error) {
33+
func (impl *GitUtil) Fetch(gitContext *GitContext, rootDir string) (response, errMsg string, err error) {
2634
impl.logger.Debugw("git fetch ", "location", rootDir)
2735
cmd := exec.Command("git", "-C", rootDir, "fetch", "origin", "--tags", "--force")
28-
output, errMsg, err := impl.runCommandWithCred(cmd, username, password)
36+
output, errMsg, err := impl.runCommandWithCred(cmd, gitContext.Username, gitContext.Password)
2937
impl.logger.Debugw("fetch output", "root", rootDir, "opt", output, "errMsg", errMsg, "error", err)
3038
return output, errMsg, err
3139
}

pkg/git/RepositoryManager.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ import (
3737
)
3838

3939
type RepositoryManager interface {
40-
Fetch(userName, password string, url string, location string) (updated bool, repo *git.Repository, err error)
41-
Add(gitProviderId int, location, url string, userName, password string, authMode sql.AuthMode, sshPrivateKeyContent string) error
40+
Fetch(gitContext *GitContext, url string, location string) (updated bool, repo *git.Repository, err error)
41+
Add(gitProviderId int, location, url string, gitContext *GitContext, authMode sql.AuthMode, sshPrivateKeyContent string) error
4242
Clean(cloneDir string) error
4343
ChangesSince(checkoutPath string, branch string, from string, to string, count int) ([]*GitCommit, error)
4444
ChangesSinceByRepository(repository *git.Repository, branch string, from string, to string, count int) ([]*GitCommit, error)
@@ -58,7 +58,7 @@ func NewRepositoryManagerImpl(logger *zap.SugaredLogger, gitUtil *GitUtil, confi
5858
return &RepositoryManagerImpl{logger: logger, gitUtil: gitUtil, configuration: configuration}
5959
}
6060

61-
func (impl RepositoryManagerImpl) Add(gitProviderId int, location string, url string, userName, password string, authMode sql.AuthMode, sshPrivateKeyContent string) error {
61+
func (impl RepositoryManagerImpl) Add(gitProviderId int, location string, url string, gitContext *GitContext, authMode sql.AuthMode, sshPrivateKeyContent string) error {
6262
var err error
6363
start := time.Now()
6464
defer func() {
@@ -83,7 +83,7 @@ func (impl RepositoryManagerImpl) Add(gitProviderId int, location string, url st
8383
}
8484
}
8585

86-
opt, errorMsg, err := impl.gitUtil.Fetch(location, userName, password)
86+
opt, errorMsg, err := impl.gitUtil.Fetch(gitContext, location)
8787
if err != nil {
8888
impl.logger.Errorw("error in cloning repo", "errorMsg", errorMsg, "err", err)
8989
return err
@@ -117,7 +117,7 @@ func (impl RepositoryManagerImpl) clone(auth transport.AuthMethod, cloneDir stri
117117
return repo, err
118118
}
119119

120-
func (impl RepositoryManagerImpl) Fetch(userName, password string, url string, location string) (updated bool, repo *git.Repository, err error) {
120+
func (impl RepositoryManagerImpl) Fetch(gitContext *GitContext, url string, location string) (updated bool, repo *git.Repository, err error) {
121121
start := time.Now()
122122
defer func() {
123123
util.TriggerGitOperationMetrics("fetch", start, err)
@@ -127,7 +127,7 @@ func (impl RepositoryManagerImpl) Fetch(userName, password string, url string, l
127127
if err != nil {
128128
return false, nil, err
129129
}
130-
res, errorMsg, err := impl.gitUtil.Fetch(location, userName, password)
130+
res, errorMsg, err := impl.gitUtil.Fetch(gitContext, location)
131131
if err == nil && len(res) > 0 {
132132
impl.logger.Infow("repository updated", "location", url)
133133
//updated

0 commit comments

Comments
 (0)