@@ -38,11 +38,11 @@ import (
38
38
type RepositoryManager interface {
39
39
// Fetch Fetches latest commit for repo. Creates a new repo if it doesn't already exist
40
40
// 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 )
42
42
// 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 )
46
46
GetCheckoutLocationFromGitUrl (material * sql.GitMaterial , cloningMode string ) (location string , httpMatched bool , shMatched bool , err error )
47
47
GetCheckoutLocation (gitCtx GitContext , material * sql.GitMaterial , url , checkoutPath string ) string
48
48
TrimLastGitCommit (gitCommits []* GitCommitBase , count int ) []* GitCommitBase
@@ -55,7 +55,7 @@ type RepositoryManager interface {
55
55
// GetCommitForTag retrieves the commit metadata for given tag
56
56
GetCommitForTag (gitCtx GitContext , checkoutPath , tag string ) (* GitCommitBase , error )
57
57
// 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 )
59
59
}
60
60
61
61
type RepositoryManagerImpl struct {
@@ -106,63 +106,64 @@ func (impl *RepositoryManagerImpl) GetCheckoutLocation(gitCtx GitContext, materi
106
106
return checkoutPath
107
107
}
108
108
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 )
111
111
if err != nil {
112
- return err
112
+ return errMsg , err
113
113
}
114
114
return impl .FetchRepo (gitCtx , location )
115
115
}
116
116
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 ) {
118
118
var err error
119
+ var errMsg string
119
120
start := time .Now ()
120
121
defer func () {
121
122
util .TriggerGitOperationMetrics ("add" , start , err )
122
123
}()
123
- err = impl .CleanupAndInitRepo (gitCtx , location , url )
124
+ errMsg , err = impl .CleanupAndInitRepo (gitCtx , location , url )
124
125
if err != nil {
125
- return "" , err
126
+ return "" , "" , err
126
127
}
127
128
var sshPrivateKeyPath string
128
129
// check ssh
129
130
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 )
131
132
if err != nil {
132
133
impl .logger .Errorw ("error while creating ssh file for shallow clone" , "checkoutPath" , location , "sshPrivateKeyPath" , sshPrivateKeyPath , "err" , err )
133
- return "" , err
134
+ return "" , errMsg , err
134
135
}
135
136
}
136
137
137
- return sshPrivateKeyPath , nil
138
+ return sshPrivateKeyPath , "" , nil
138
139
}
139
140
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 ) {
141
142
err := os .RemoveAll (location )
142
143
if err != nil {
143
144
impl .logger .Errorw ("error in cleaning checkout path" , "err" , err )
144
- return err
145
+ return "" , err
145
146
}
146
147
if ! impl .IsSpaceAvailableOnDisk () {
147
148
err = errors .New ("git-sensor PVC - disk full, please increase space" )
148
- return err
149
+ return "" , err
149
150
}
150
- err = impl .gitManager .Init (gitCtx , location , url , true )
151
+ errMsg , err : = impl .gitManager .Init (gitCtx , location , url , true )
151
152
if err != nil {
152
153
impl .logger .Errorw ("err in git init" , "err" , err )
153
- return err
154
+ return errMsg , err
154
155
}
155
- return nil
156
+ return "" , nil
156
157
}
157
158
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 )
160
161
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
163
164
}
164
165
impl .logger .Debugw ("opt msg" , "opt" , opt )
165
- return nil
166
+ return "" , nil
166
167
}
167
168
168
169
func (impl * RepositoryManagerImpl ) Clean (dir string ) error {
@@ -175,35 +176,35 @@ func (impl *RepositoryManagerImpl) Clean(dir string) error {
175
176
return err
176
177
}
177
178
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 ) {
179
180
start := time .Now ()
180
181
defer func () {
181
182
util .TriggerGitOperationMetrics ("fetch" , start , err )
182
183
}()
183
184
middleware .GitMaterialPollCounter .WithLabelValues ().Inc ()
184
185
if ! impl .IsSpaceAvailableOnDisk () {
185
186
err = errors .New ("git-sensor PVC - disk full, please increase space" )
186
- return false , nil , err
187
+ return false , nil , "" , err
187
188
}
188
- r , err := impl .openNewRepo (gitCtx , location , url )
189
+ r , errMsg , err := impl .openNewRepo (gitCtx , location , url )
189
190
if err != nil {
190
- return false , r , err
191
+ return false , r , errMsg , err
191
192
}
192
- res , errorMsg , err := impl .gitManager .Fetch (gitCtx , location )
193
+ res , errMsg , err := impl .gitManager .Fetch (gitCtx , location )
193
194
194
195
if err == nil && len (res ) > 0 {
195
196
impl .logger .Infow ("repository updated" , "location" , url )
196
197
//updated
197
198
middleware .GitPullDuration .WithLabelValues ("true" , "true" ).Observe (time .Since (start ).Seconds ())
198
- return true , r , nil
199
+ return true , r , "" , nil
199
200
} else if err == nil && len (res ) == 0 {
200
201
impl .logger .Debugw ("no update for " , "path" , url )
201
202
middleware .GitPullDuration .WithLabelValues ("true" , "false" ).Observe (time .Since (start ).Seconds ())
202
- return false , r , nil
203
+ return false , r , "" , nil
203
204
} 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 )
205
206
middleware .GitPullDuration .WithLabelValues ("false" , "false" ).Observe (time .Since (start ).Seconds ())
206
- return false , r , err
207
+ return false , r , "" , err
207
208
}
208
209
209
210
}
@@ -345,7 +346,7 @@ func (impl *RepositoryManagerImpl) TrimLastGitCommit(gitCommits []*GitCommitBase
345
346
return gitCommits
346
347
}
347
348
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 ) {
349
350
// add private key
350
351
var err error
351
352
var sshPrivateKeyPath string
@@ -356,38 +357,39 @@ func (impl *RepositoryManagerImpl) CreateSshFileIfNotExistsAndConfigureSshComman
356
357
sshPrivateKeyPath , err = GetOrCreateSshPrivateKeyOnDisk (gitProviderId , sshPrivateKeyContent )
357
358
if err != nil {
358
359
impl .logger .Errorw ("error in creating ssh private key" , "err" , err )
359
- return sshPrivateKeyPath , err
360
+ return sshPrivateKeyPath , "" , err
360
361
}
361
362
362
363
//git config core.sshCommand
363
364
_ , errorMsg , err := impl .gitManager .ConfigureSshCommand (gitCtx , location , sshPrivateKeyPath )
364
365
if err != nil {
365
366
impl .logger .Errorw ("error in configuring ssh command while adding repo" , "errorMsg" , errorMsg , "err" , err )
366
- return sshPrivateKeyPath , err
367
+ return sshPrivateKeyPath , errorMsg , err
367
368
}
368
369
369
- return sshPrivateKeyPath , nil
370
+ return sshPrivateKeyPath , "" , nil
370
371
}
371
372
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 ) {
373
374
375
+ var errMsg string
374
376
r , err := impl .gitManager .OpenRepoPlain (location )
375
377
if err != nil {
376
378
err = os .RemoveAll (location )
377
379
if err != nil {
378
380
impl .logger .Errorw ("error in cleaning checkout path: %s" , err )
379
- return r , err
381
+ return r , "" , err
380
382
}
381
- err = impl .gitManager .Init (gitCtx , location , url , true )
383
+ errMsg , err = impl .gitManager .Init (gitCtx , location , url , true )
382
384
if err != nil {
383
385
impl .logger .Errorw ("err in git init: %s" , err )
384
- return r , err
386
+ return r , errMsg , err
385
387
}
386
388
r , err = impl .gitManager .OpenRepoPlain (location )
387
389
if err != nil {
388
390
impl .logger .Errorw ("err in git init: %s" , err )
389
- return r , err
391
+ return r , "" , err
390
392
}
391
393
}
392
- return r , nil
394
+ return r , "" , nil
393
395
}
0 commit comments