@@ -36,15 +36,16 @@ import static com.cloudogu.gitops.config.Config.ContentSchema.ContentRepositoryS
36
36
// We want to evaluate content last, to allow for changing all other repos
37
37
class Content extends Feature {
38
38
39
- protected Config config
40
- protected K8sClient k8sClient
41
- protected ScmmRepoProvider repoProvider
42
- protected ScmmApiClient scmmApiClient
43
- protected Jenkins jenkins
39
+ private Config config
40
+ private K8sClient k8sClient
41
+ private ScmmRepoProvider repoProvider
42
+ private ScmmApiClient scmmApiClient
43
+ private Jenkins jenkins
44
44
// set by lazy initialisation
45
45
private TemplatingEngine templatingEngine
46
46
// used to clone repos in validation phase
47
47
private List<RepoCoordinate > cachedRepoCoordinates = new ArrayList<> ()
48
+ private File mergedReposFolder
48
49
49
50
Content (
50
51
Config config , K8sClient k8sClient , ScmmRepoProvider repoProvider , ScmmApiClient scmmApiClient , Jenkins jenkins
@@ -109,14 +110,16 @@ class Content extends Feature {
109
110
cachedRepoCoordinates = cloneContentRepos()
110
111
}
111
112
pushTargetRepos(cachedRepoCoordinates)
112
- cachedRepoCoordinates. clear()
113
+ // after all, clean folders and list
114
+ clearCache()
115
+
113
116
}
114
117
115
118
protected List<RepoCoordinate > cloneContentRepos () {
119
+ mergedReposFolder = File . createTempDir(' gitops-playground-based-content-repos-' )
116
120
List<RepoCoordinate > repoCoordinates = []
117
- def mergedReposFolder = File . createTempDir(' gitops-playground-folder-based-content-repos-' )
118
- mergedReposFolder. deleteOnExit()
119
- log. debug(" Aggregating folder structure for all ${ config.content.repos.size()} folder based-repos" )
121
+
122
+ log. debug(" Aggregating structure for all ${ config.content.repos.size()} repos." )
120
123
config. content. repos. each { repoConfig ->
121
124
createRepoCoordinates(repoConfig, mergedReposFolder, repoCoordinates)
122
125
}
@@ -132,8 +135,8 @@ class Content extends Feature {
132
135
}
133
136
134
137
135
- protected void createRepoCoordinates (ContentRepositorySchema repoConfig , File mergedReposFolder , List<RepoCoordinate > repoCoordinates ) {
136
- def repoTmpDir = File . createTempDir(' gitops-playground-folder-based-single- content-repo-' )
138
+ private void createRepoCoordinates (ContentRepositorySchema repoConfig , File mergedReposFolder , List<RepoCoordinate > repoCoordinates ) {
139
+ def repoTmpDir = File . createTempDir(' gitops-playground-content-repo-' )
137
140
log. debug(" Cloning content repo, ${ repoConfig.url} , revision ${ repoConfig.ref} , path ${ repoConfig.path} , overwriteMode ${ repoConfig.overwriteMode} " )
138
141
139
142
cloneToLocalFolder(repoConfig, repoTmpDir)
@@ -145,44 +148,45 @@ class Content extends Feature {
145
148
switch (repoConfig. type) {
146
149
case ContentRepoType . FOLDER_BASED :
147
150
createRepoCoordinatesForTypeFolderBased(repoConfig, repoTmpDir, contentRepoDir, mergedReposFolder, repoCoordinates)
151
+ repoTmpDir. deleteDir()
148
152
break
149
153
case ContentRepoType . COPY :
150
- def repoCoordinate = createRepoCoordinatesForTypeCopy(repoConfig, contentRepoDir, mergedReposFolder, repoTmpDir)
151
- addRepoCoordinates(repoCoordinates, repoCoordinate )
154
+ def repoCoordinate = createRepoCoordinatesForTypeCopy(repoConfig, contentRepoDir, mergedReposFolder, repoTmpDir, repoCoordinates )
155
+ repoTmpDir . deleteDir( )
152
156
break
153
157
case ContentRepoType . MIRROR :
154
- def repoCoordinate = createRepoCoordinateForTypeMirror(repoConfig, repoTmpDir)
155
- addRepoCoordinates(repoCoordinates, repoCoordinate)
158
+ def repoCoordinate = createRepoCoordinateForTypeMirror(repoConfig, repoTmpDir, repoCoordinates )
159
+ // intentionally not deleting repoTmpDir, it is contained in RepoCoordinates for MIRROR usage
156
160
break
157
161
}
158
162
log. debug(" Finished cloning content repos. repoCoordinates=${ repoCoordinates} " )
159
163
}
160
164
161
- private static RepoCoordinate createRepoCoordinatesForTypeCopy (ContentRepositorySchema repoConfig , File contentRepoDir , File mergedReposFolder , File repoTmpDir ) {
165
+ private static void createRepoCoordinatesForTypeCopy (ContentRepositorySchema repoConfig , File contentRepoDir , File mergedReposFolder , File repoTmpDir , List< RepoCoordinate > repoCoordinates ) {
162
166
String namespace = repoConfig. target. split(' /' )[0 ]
163
167
String repoName = repoConfig. target. split(' /' )[1 ]
164
168
165
169
def repoCoordinate = mergeRepoDirs(contentRepoDir, namespace, repoName, mergedReposFolder, repoConfig)
166
170
repoCoordinate. refIsTag = isTag(repoTmpDir, repoConfig. ref)
167
- return repoCoordinate
171
+ addRepoCoordinates(repoCoordinates, repoCoordinate)
168
172
}
169
173
170
174
private static void createRepoCoordinatesForTypeFolderBased (ContentRepositorySchema repoConfig , File repoTmpDir , File contentRepoDir , File mergedReposFolder , List<RepoCoordinate > repoCoordinates ) {
171
175
boolean refIsTag = isTag(repoTmpDir, repoConfig. ref)
172
176
findRepoDirectories(contentRepoDir)
173
177
.each { contentRepoNamespaceDir ->
174
178
findRepoDirectories(contentRepoNamespaceDir)
175
- .each { contentRepoRepoDir ->
179
+ .each { contentRepoFolder ->
176
180
String namespace = contentRepoNamespaceDir. name
177
- String repoName = contentRepoRepoDir . name
178
- def repoCoordinate = mergeRepoDirs(contentRepoRepoDir , namespace, repoName, mergedReposFolder, repoConfig)
181
+ String repoName = contentRepoFolder . name
182
+ def repoCoordinate = mergeRepoDirs(contentRepoFolder , namespace, repoName, mergedReposFolder, repoConfig)
179
183
repoCoordinate. refIsTag = refIsTag
180
184
addRepoCoordinates(repoCoordinates, repoCoordinate)
181
185
}
182
186
}
183
187
}
184
188
185
- private static RepoCoordinate createRepoCoordinateForTypeMirror (ContentRepositorySchema repoConfig , File repoTmpDir ) {
189
+ private static void createRepoCoordinateForTypeMirror (ContentRepositorySchema repoConfig , File repoTmpDir , List< RepoCoordinate > repoCoordinates ) {
186
190
// Don't merge but keep these in separate dirs.
187
191
// This avoids messing up .git folders with possible confusing exceptions for the user
188
192
String namespace = repoConfig. target. split(' /' )[0 ]
@@ -194,7 +198,7 @@ class Content extends Feature {
194
198
repoConfig : repoConfig,
195
199
refIsTag : isTag(repoTmpDir, repoConfig. ref)
196
200
)
197
- return repoCoordinate
201
+ addRepoCoordinates(repoCoordinates, repoCoordinate)
198
202
}
199
203
200
204
/**
@@ -253,8 +257,10 @@ class Content extends Feature {
253
257
254
258
if (ContentRepoType . MIRROR == repoConfig. type) {
255
259
def fetch = git. fetch()
256
- // fetch also needs CredentialProvider, jgit behaviour.
257
- fetch. setCredentialsProvider(new UsernamePasswordCredentialsProvider (repoConfig. username, repoConfig. password))
260
+ if (repoConfig. username != null && repoConfig. password != null ) {
261
+ // fetch also needs CredentialProvider, jgit behaviour.
262
+ fetch. setCredentialsProvider(new UsernamePasswordCredentialsProvider (repoConfig. username, repoConfig. password))
263
+ }
258
264
fetch. setRefSpecs(" +refs/*:refs/*" ). call() // Fetch all branches and tags
259
265
}
260
266
@@ -295,7 +301,7 @@ class Content extends Feature {
295
301
}
296
302
297
303
298
- protected void pushTargetRepos (List<RepoCoordinate > repoCoordinates ) {
304
+ private void pushTargetRepos (List<RepoCoordinate > repoCoordinates ) {
299
305
repoCoordinates. each { repoCoordinate ->
300
306
301
307
ScmmRepo targetRepo = repoProvider. getRepo(repoCoordinate. fullRepoName)
@@ -327,7 +333,7 @@ class Content extends Feature {
327
333
* Copies repoCoordinate to targetRepo, commits and pushes
328
334
* Same logic for both FOLDER_BASED and COPY repo types.
329
335
*/
330
- protected static void handleRepoCopyingOrFolderBased (RepoCoordinate repoCoordinate , ScmmRepo targetRepo ) {
336
+ private static void handleRepoCopyingOrFolderBased (RepoCoordinate repoCoordinate , ScmmRepo targetRepo ) {
331
337
clearTargetRepoIfApplicable(repoCoordinate, targetRepo)
332
338
// Avoid overwriting .git in target to avoid, because we don't need it for copying and
333
339
// git pack files are typically read-only, leading to IllegalArgumentException:
@@ -345,7 +351,7 @@ class Content extends Feature {
345
351
346
352
}
347
353
348
- protected static String setRefSpec (RepoCoordinate repoCoordinate , String targetRefShort ) {
354
+ private static String setRefSpec (RepoCoordinate repoCoordinate , String targetRefShort ) {
349
355
String refSpec
350
356
if ((repoCoordinate. refIsTag && ! repoCoordinate. repoConfig. targetRef. startsWith(' refs/heads' ))
351
357
|| repoCoordinate. repoConfig. targetRef. startsWith(' refs/tags' )) {
@@ -372,7 +378,7 @@ class Content extends Feature {
372
378
/**
373
379
* Force pushes repoCoordinate.repoConfig.ref or all refs to targetRepo
374
380
*/
375
- protected static void handleRepoMirroring (RepoCoordinate repoCoordinate , ScmmRepo targetRepo ) {
381
+ private static void handleRepoMirroring (RepoCoordinate repoCoordinate , ScmmRepo targetRepo ) {
376
382
try (def targetGit = Git . open(new File (targetRepo. absoluteLocalRepoTmpDir))) {
377
383
def remoteUrl = targetGit. repository. config. getString(' remote' , ' origin' , ' url' )
378
384
@@ -415,7 +421,7 @@ class Content extends Feature {
415
421
}
416
422
}
417
423
418
- protected void createJenkinsJobIfApplicable (RepoCoordinate repoCoordinate , ScmmRepo repo ) {
424
+ private void createJenkinsJobIfApplicable (RepoCoordinate repoCoordinate , ScmmRepo repo ) {
419
425
if (repoCoordinate. repoConfig. createJenkinsJob && jenkins. isEnabled()) {
420
426
if (existFileInSomeBranch(repo. absoluteLocalRepoTmpDir, ' Jenkinsfile' )) {
421
427
jenkins. createJenkinsjob(repoCoordinate. namespace, repoCoordinate. namespace)
@@ -545,6 +551,12 @@ class Content extends Feature {
545
551
return true
546
552
}
547
553
554
+ private void clearCache () {
555
+ mergedReposFolder. deleteDir()
556
+ cachedRepoCoordinates. clear()
557
+ mergedReposFolder = null
558
+ }
559
+
548
560
static class RepoCoordinate {
549
561
String namespace
550
562
String repoName
0 commit comments