Skip to content

Commit 4329364

Browse files
committed
propagate errMsg
1 parent b1eb971 commit 4329364

File tree

7 files changed

+110
-90
lines changed

7 files changed

+110
-90
lines changed

pkg/RepoManages.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -366,17 +366,21 @@ func (impl RepoManagerImpl) checkoutMaterial(gitCtx git.GitContext, material *sq
366366

367367
checkoutLocationForFetching := impl.repositoryManager.GetCheckoutLocation(gitCtx, material, gitProvider.Url, checkoutPath)
368368

369-
err = impl.repositoryManager.Add(gitCtx, material.GitProviderId, checkoutPath, material.Url, gitProvider.AuthMode, gitProvider.SshPrivateKey)
369+
errMsg, err := impl.repositoryManager.Add(gitCtx, material.GitProviderId, checkoutPath, material.Url, gitProvider.AuthMode, gitProvider.SshPrivateKey)
370370
if gitCtx.Err() != nil {
371371
impl.logger.Errorw("context error in git checkout", "err", gitCtx.Err())
372372
return material, gitCtx.Err()
373-
} else if err == nil {
374-
material.CheckoutLocation = checkoutLocationForFetching
375-
material.CheckoutStatus = true
376-
} else {
373+
} else if err != nil {
377374
material.CheckoutStatus = false
378375
material.CheckoutMsgAny = err.Error()
379-
material.FetchErrorMessage = err.Error()
376+
if errMsg != "" {
377+
material.FetchErrorMessage = errMsg
378+
} else {
379+
material.FetchErrorMessage = err.Error()
380+
}
381+
} else {
382+
material.CheckoutLocation = checkoutLocationForFetching
383+
material.CheckoutStatus = true
380384
}
381385
err = impl.materialRepository.Update(material)
382386
if err != nil {
@@ -668,19 +672,24 @@ func (impl RepoManagerImpl) GetLatestCommitForBranch(gitCtx git.GitContext, pipe
668672

669673
gitCtx = gitCtx.WithCredentials(userName, password).
670674
WithTLSData(gitMaterial.GitProvider.CaCert, gitMaterial.GitProvider.TlsKey, gitMaterial.GitProvider.TlsCert, gitMaterial.GitProvider.EnableTLSVerification)
671-
updated, repo, err := impl.repositoryManager.Fetch(gitCtx, gitMaterial.Url, gitMaterial.CheckoutLocation)
675+
updated, repo, errMsg, err := impl.repositoryManager.Fetch(gitCtx, gitMaterial.Url, gitMaterial.CheckoutLocation)
672676
if !updated {
673677
impl.logger.Warn("repository is up to date")
674678
}
675-
if err == nil {
676-
gitMaterial.CheckoutStatus = true
677-
} else {
679+
if err != nil {
678680
gitMaterial.CheckoutStatus = false
679681
gitMaterial.CheckoutMsgAny = err.Error()
680-
gitMaterial.FetchErrorMessage = err.Error()
682+
if errMsg != "" {
683+
gitMaterial.FetchErrorMessage = errMsg
684+
} else {
685+
gitMaterial.FetchErrorMessage = err.Error()
686+
}
681687

682688
impl.logger.Errorw("error in fetching the repository ", "gitMaterial", gitMaterial, "err", err)
683689
return nil, err
690+
} else {
691+
gitMaterial.CheckoutStatus = true
692+
684693
}
685694

686695
err = impl.materialRepository.Update(gitMaterial)

pkg/git/GitBaseManager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type GitManager interface {
4545
// OpenRepoPlain opens a new git repo at the given path
4646
OpenRepoPlain(checkoutPath string) (*GitRepository, error)
4747
// Init initializes a git repo
48-
Init(gitCtx GitContext, rootDir string, remoteUrl string, isBare bool) error
48+
Init(gitCtx GitContext, rootDir string, remoteUrl string, isBare bool) (string, error)
4949
}
5050

5151
// GitManagerBase Base methods which will be available to all implementation of the parent interface

pkg/git/GitCliManager.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,19 @@ const (
4444
LOCK_REF_MESSAGE = "cannot lock ref"
4545
)
4646

47-
func (impl *GitCliManagerImpl) Init(gitCtx GitContext, rootDir string, remoteUrl string, isBare bool) error {
47+
func (impl *GitCliManagerImpl) Init(gitCtx GitContext, rootDir string, remoteUrl string, isBare bool) (string, error) {
4848
//-----------------
4949

50-
err := os.MkdirAll(rootDir, 0755)
50+
var err error
51+
var errMsg string
52+
err = os.MkdirAll(rootDir, 0755)
5153
if err != nil {
52-
return err
54+
return "", err
5355
}
5456

55-
err = impl.GitInit(gitCtx, rootDir)
57+
errMsg, err = impl.GitInit(gitCtx, rootDir)
5658
if err != nil {
57-
return err
59+
return errMsg, err
5860
}
5961
return impl.GitCreateRemote(gitCtx, rootDir, remoteUrl)
6062

@@ -102,18 +104,18 @@ func openGitRepo(path string) error {
102104
}
103105
return nil
104106
}
105-
func (impl *GitCliManagerImpl) GitInit(gitCtx GitContext, rootDir string) error {
107+
func (impl *GitCliManagerImpl) GitInit(gitCtx GitContext, rootDir string) (string, error) {
106108
impl.logger.Debugw("git", "-C", rootDir, "init")
107109
output, errMsg, err := impl.GitManagerBase.ExecuteCustomCommand(gitCtx, "git", "-C", rootDir, "init")
108110
impl.logger.Debugw("root", rootDir, "opt", output, "errMsg", errMsg, "error", err)
109-
return err
111+
return errMsg, err
110112
}
111113

112-
func (impl *GitCliManagerImpl) GitCreateRemote(gitCtx GitContext, rootDir string, url string) error {
114+
func (impl *GitCliManagerImpl) GitCreateRemote(gitCtx GitContext, rootDir string, url string) (string, error) {
113115
impl.logger.Debugw("git", "-C", rootDir, "remote", "add", "origin", url)
114116
output, errMsg, err := impl.GitManagerBase.ExecuteCustomCommand(gitCtx, "git", "-C", rootDir, "remote", "add", "origin", url)
115117
impl.logger.Debugw("url", url, "opt", output, "errMsg", errMsg, "error", err)
116-
return err
118+
return errMsg, err
117119
}
118120

119121
func (impl *GitCliManagerImpl) GetCommits(gitCtx GitContext, branchRef string, branch string, rootDir string, numCommits int, from string, to string) (commits []GitCommit, cliOutput string, errMsg string, err error) {

pkg/git/GoGitSDKManager.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,23 +119,26 @@ func (impl *GoGitSDKManagerImpl) OpenRepoPlain(checkoutPath string) (*GitReposit
119119
return &GitRepository{Repository: r}, err
120120
}
121121

122-
func (impl *GoGitSDKManagerImpl) Init(gitCtx GitContext, rootDir string, remoteUrl string, isBare bool) error {
122+
func (impl *GoGitSDKManagerImpl) Init(gitCtx GitContext, rootDir string, remoteUrl string, isBare bool) (string, error) {
123123
//-----------------
124124

125125
err := os.MkdirAll(rootDir, 0755)
126126
if err != nil {
127-
return err
127+
return "", err
128128
}
129129

130130
repo, err := git.PlainInit(rootDir, isBare)
131131
if err != nil {
132-
return err
132+
return "", err
133133
}
134134
_, err = repo.CreateRemote(&config.RemoteConfig{
135135
Name: git.DefaultRemoteName,
136136
URLs: []string{remoteUrl},
137137
})
138-
return err
138+
if err != nil {
139+
return "", err
140+
}
141+
return "", nil
139142
}
140143

141144
func (impl *GoGitSDKManagerImpl) GetCommitStats(gitCtx GitContext, commit GitCommit, checkoutPath string) (FileStats, error) {

pkg/git/RepositoryManager.go

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ import (
3838
type RepositoryManager interface {
3939
// Fetch Fetches latest commit for repo. Creates a new repo if it doesn't already exist
4040
// and returns the reference to the repo
41-
Fetch(gitCtx GitContext, url string, location string) (updated bool, repo *GitRepository, err error)
41+
Fetch(gitCtx GitContext, url string, location string) (updated bool, repo *GitRepository, errMsg string, err error)
4242
// Add adds and initializes a new git repo , cleans the directory if not empty and fetches latest commits
43-
Add(gitCtx GitContext, gitProviderId int, location, url string, authMode sql.AuthMode, sshPrivateKeyContent string) error
44-
InitRepoAndGetSshPrivateKeyPath(gitCtx GitContext, gitProviderId int, location, url string, authMode sql.AuthMode, sshPrivateKeyContent string) (string, error)
45-
FetchRepo(gitCtx GitContext, location string) error
43+
Add(gitCtx GitContext, gitProviderId int, location, url string, authMode sql.AuthMode, sshPrivateKeyContent string) (errMsg string, err error)
44+
InitRepoAndGetSshPrivateKeyPath(gitCtx GitContext, gitProviderId int, location, url string, authMode sql.AuthMode, sshPrivateKeyContent string) (string, string, error)
45+
FetchRepo(gitCtx GitContext, location string) (errMsg string, err error)
4646
GetCheckoutLocationFromGitUrl(material *sql.GitMaterial, cloningMode string) (location string, httpMatched bool, shMatched bool, err error)
4747
GetCheckoutLocation(gitCtx GitContext, material *sql.GitMaterial, url, checkoutPath string) string
4848
TrimLastGitCommit(gitCommits []*GitCommitBase, count int) []*GitCommitBase
@@ -55,7 +55,7 @@ type RepositoryManager interface {
5555
// GetCommitForTag retrieves the commit metadata for given tag
5656
GetCommitForTag(gitCtx GitContext, checkoutPath, tag string) (*GitCommitBase, error)
5757
// CreateSshFileIfNotExistsAndConfigureSshCommand creates ssh file with creds and configures it at the location
58-
CreateSshFileIfNotExistsAndConfigureSshCommand(gitCtx GitContext, location string, gitProviderId int, sshPrivateKeyContent string) (string, error)
58+
CreateSshFileIfNotExistsAndConfigureSshCommand(gitCtx GitContext, location string, gitProviderId int, sshPrivateKeyContent string) (privateKeyPath string, errMsg string, err error)
5959
}
6060

6161
type RepositoryManagerImpl struct {
@@ -106,63 +106,64 @@ func (impl *RepositoryManagerImpl) GetCheckoutLocation(gitCtx GitContext, materi
106106
return checkoutPath
107107
}
108108

109-
func (impl *RepositoryManagerImpl) Add(gitCtx GitContext, gitProviderId int, location, url string, authMode sql.AuthMode, sshPrivateKeyContent string) error {
110-
_, err := impl.InitRepoAndGetSshPrivateKeyPath(gitCtx, gitProviderId, location, url, authMode, sshPrivateKeyContent)
109+
func (impl *RepositoryManagerImpl) Add(gitCtx GitContext, gitProviderId int, location, url string, authMode sql.AuthMode, sshPrivateKeyContent string) (string, error) {
110+
_, errMsg, err := impl.InitRepoAndGetSshPrivateKeyPath(gitCtx, gitProviderId, location, url, authMode, sshPrivateKeyContent)
111111
if err != nil {
112-
return err
112+
return errMsg, err
113113
}
114114
return impl.FetchRepo(gitCtx, location)
115115
}
116116

117-
func (impl *RepositoryManagerImpl) InitRepoAndGetSshPrivateKeyPath(gitCtx GitContext, gitProviderId int, location, url string, authMode sql.AuthMode, sshPrivateKeyContent string) (string, error) {
117+
func (impl *RepositoryManagerImpl) InitRepoAndGetSshPrivateKeyPath(gitCtx GitContext, gitProviderId int, location, url string, authMode sql.AuthMode, sshPrivateKeyContent string) (string, string, error) {
118118
var err error
119+
var errMsg string
119120
start := time.Now()
120121
defer func() {
121122
util.TriggerGitOperationMetrics("add", start, err)
122123
}()
123-
err = impl.CleanupAndInitRepo(gitCtx, location, url)
124+
errMsg, err = impl.CleanupAndInitRepo(gitCtx, location, url)
124125
if err != nil {
125-
return "", err
126+
return "", "", err
126127
}
127128
var sshPrivateKeyPath string
128129
// check ssh
129130
if authMode == sql.AUTH_MODE_SSH {
130-
sshPrivateKeyPath, err = impl.CreateSshFileIfNotExistsAndConfigureSshCommand(gitCtx, location, gitProviderId, sshPrivateKeyContent)
131+
sshPrivateKeyPath, errMsg, err = impl.CreateSshFileIfNotExistsAndConfigureSshCommand(gitCtx, location, gitProviderId, sshPrivateKeyContent)
131132
if err != nil {
132133
impl.logger.Errorw("error while creating ssh file for shallow clone", "checkoutPath", location, "sshPrivateKeyPath", sshPrivateKeyPath, "err", err)
133-
return "", err
134+
return "", errMsg, err
134135
}
135136
}
136137

137-
return sshPrivateKeyPath, nil
138+
return sshPrivateKeyPath, "", nil
138139
}
139140

140-
func (impl *RepositoryManagerImpl) CleanupAndInitRepo(gitCtx GitContext, location string, url string) error {
141+
func (impl *RepositoryManagerImpl) CleanupAndInitRepo(gitCtx GitContext, location string, url string) (string, error) {
141142
err := os.RemoveAll(location)
142143
if err != nil {
143144
impl.logger.Errorw("error in cleaning checkout path", "err", err)
144-
return err
145+
return "", err
145146
}
146147
if !impl.IsSpaceAvailableOnDisk() {
147148
err = errors.New("git-sensor PVC - disk full, please increase space")
148-
return err
149+
return "", err
149150
}
150-
err = impl.gitManager.Init(gitCtx, location, url, true)
151+
errMsg, err := impl.gitManager.Init(gitCtx, location, url, true)
151152
if err != nil {
152153
impl.logger.Errorw("err in git init", "err", err)
153-
return err
154+
return errMsg, err
154155
}
155-
return nil
156+
return "", nil
156157
}
157158

158-
func (impl *RepositoryManagerImpl) FetchRepo(gitCtx GitContext, location string) error {
159-
opt, errorMsg, err := impl.gitManager.Fetch(gitCtx, location)
159+
func (impl *RepositoryManagerImpl) FetchRepo(gitCtx GitContext, location string) (string, error) {
160+
opt, errMsg, err := impl.gitManager.Fetch(gitCtx, location)
160161
if err != nil {
161-
impl.logger.Errorw("error in fetching repo", "errorMsg", errorMsg, "err", err)
162-
return err
162+
impl.logger.Errorw("error in fetching repo", "errorMsg", errMsg, "err", err)
163+
return errMsg, err
163164
}
164165
impl.logger.Debugw("opt msg", "opt", opt)
165-
return nil
166+
return "", nil
166167
}
167168

168169
func (impl *RepositoryManagerImpl) Clean(dir string) error {
@@ -175,35 +176,35 @@ func (impl *RepositoryManagerImpl) Clean(dir string) error {
175176
return err
176177
}
177178

178-
func (impl *RepositoryManagerImpl) Fetch(gitCtx GitContext, url string, location string) (updated bool, repo *GitRepository, err error) {
179+
func (impl *RepositoryManagerImpl) Fetch(gitCtx GitContext, url string, location string) (updated bool, repo *GitRepository, errMsg string, err error) {
179180
start := time.Now()
180181
defer func() {
181182
util.TriggerGitOperationMetrics("fetch", start, err)
182183
}()
183184
middleware.GitMaterialPollCounter.WithLabelValues().Inc()
184185
if !impl.IsSpaceAvailableOnDisk() {
185186
err = errors.New("git-sensor PVC - disk full, please increase space")
186-
return false, nil, err
187+
return false, nil, "", err
187188
}
188-
r, err := impl.openNewRepo(gitCtx, location, url)
189+
r, errMsg, err := impl.openNewRepo(gitCtx, location, url)
189190
if err != nil {
190-
return false, r, err
191+
return false, r, errMsg, err
191192
}
192-
res, errorMsg, err := impl.gitManager.Fetch(gitCtx, location)
193+
res, errMsg, err := impl.gitManager.Fetch(gitCtx, location)
193194

194195
if err == nil && len(res) > 0 {
195196
impl.logger.Infow("repository updated", "location", url)
196197
//updated
197198
middleware.GitPullDuration.WithLabelValues("true", "true").Observe(time.Since(start).Seconds())
198-
return true, r, nil
199+
return true, r, "", nil
199200
} else if err == nil && len(res) == 0 {
200201
impl.logger.Debugw("no update for ", "path", url)
201202
middleware.GitPullDuration.WithLabelValues("true", "false").Observe(time.Since(start).Seconds())
202-
return false, r, nil
203+
return false, r, "", nil
203204
} else {
204-
impl.logger.Errorw("error in updating repository", "err", err, "location", url, "error msg", errorMsg)
205+
impl.logger.Errorw("error in updating repository", "err", err, "location", url, "error msg", errMsg)
205206
middleware.GitPullDuration.WithLabelValues("false", "false").Observe(time.Since(start).Seconds())
206-
return false, r, err
207+
return false, r, "", err
207208
}
208209

209210
}
@@ -345,7 +346,7 @@ func (impl *RepositoryManagerImpl) TrimLastGitCommit(gitCommits []*GitCommitBase
345346
return gitCommits
346347
}
347348

348-
func (impl *RepositoryManagerImpl) CreateSshFileIfNotExistsAndConfigureSshCommand(gitCtx GitContext, location string, gitProviderId int, sshPrivateKeyContent string) (string, error) {
349+
func (impl *RepositoryManagerImpl) CreateSshFileIfNotExistsAndConfigureSshCommand(gitCtx GitContext, location string, gitProviderId int, sshPrivateKeyContent string) (string, string, error) {
349350
// add private key
350351
var err error
351352
var sshPrivateKeyPath string
@@ -356,38 +357,39 @@ func (impl *RepositoryManagerImpl) CreateSshFileIfNotExistsAndConfigureSshComman
356357
sshPrivateKeyPath, err = GetOrCreateSshPrivateKeyOnDisk(gitProviderId, sshPrivateKeyContent)
357358
if err != nil {
358359
impl.logger.Errorw("error in creating ssh private key", "err", err)
359-
return sshPrivateKeyPath, err
360+
return sshPrivateKeyPath, "", err
360361
}
361362

362363
//git config core.sshCommand
363364
_, errorMsg, err := impl.gitManager.ConfigureSshCommand(gitCtx, location, sshPrivateKeyPath)
364365
if err != nil {
365366
impl.logger.Errorw("error in configuring ssh command while adding repo", "errorMsg", errorMsg, "err", err)
366-
return sshPrivateKeyPath, err
367+
return sshPrivateKeyPath, errorMsg, err
367368
}
368369

369-
return sshPrivateKeyPath, nil
370+
return sshPrivateKeyPath, "", nil
370371
}
371372

372-
func (impl *RepositoryManagerImpl) openNewRepo(gitCtx GitContext, location string, url string) (*GitRepository, error) {
373+
func (impl *RepositoryManagerImpl) openNewRepo(gitCtx GitContext, location string, url string) (*GitRepository, string, error) {
373374

375+
var errMsg string
374376
r, err := impl.gitManager.OpenRepoPlain(location)
375377
if err != nil {
376378
err = os.RemoveAll(location)
377379
if err != nil {
378380
impl.logger.Errorw("error in cleaning checkout path: %s", err)
379-
return r, err
381+
return r, "", err
380382
}
381-
err = impl.gitManager.Init(gitCtx, location, url, true)
383+
errMsg, err = impl.gitManager.Init(gitCtx, location, url, true)
382384
if err != nil {
383385
impl.logger.Errorw("err in git init: %s", err)
384-
return r, err
386+
return r, errMsg, err
385387
}
386388
r, err = impl.gitManager.OpenRepoPlain(location)
387389
if err != nil {
388390
impl.logger.Errorw("err in git init: %s", err)
389-
return r, err
391+
return r, "", err
390392
}
391393
}
392-
return r, nil
394+
return r, "", nil
393395
}

0 commit comments

Comments
 (0)