Skip to content

Commit 66dcfaa

Browse files
author
Thomas Michael
committed
refactor and release files
disable git gc for test
1 parent b99e8b1 commit 66dcfaa

File tree

16 files changed

+165
-138
lines changed

16 files changed

+165
-138
lines changed

src/main/groovy/com/cloudogu/gitops/features/Content.groovy

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,16 @@ import static com.cloudogu.gitops.config.Config.ContentSchema.ContentRepositoryS
3636
// We want to evaluate content last, to allow for changing all other repos
3737
class Content extends Feature {
3838

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
4444
// set by lazy initialisation
4545
private TemplatingEngine templatingEngine
4646
// used to clone repos in validation phase
4747
private List<RepoCoordinate> cachedRepoCoordinates = new ArrayList<>()
48+
private File mergedReposFolder
4849

4950
Content(
5051
Config config, K8sClient k8sClient, ScmmRepoProvider repoProvider, ScmmApiClient scmmApiClient, Jenkins jenkins
@@ -109,14 +110,16 @@ class Content extends Feature {
109110
cachedRepoCoordinates = cloneContentRepos()
110111
}
111112
pushTargetRepos(cachedRepoCoordinates)
112-
cachedRepoCoordinates.clear()
113+
// after all, clean folders and list
114+
clearCache()
115+
113116
}
114117

115118
protected List<RepoCoordinate> cloneContentRepos() {
119+
mergedReposFolder = File.createTempDir('gitops-playground-based-content-repos-')
116120
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.")
120123
config.content.repos.each { repoConfig ->
121124
createRepoCoordinates(repoConfig, mergedReposFolder, repoCoordinates)
122125
}
@@ -132,8 +135,8 @@ class Content extends Feature {
132135
}
133136

134137

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-')
137140
log.debug("Cloning content repo, ${repoConfig.url}, revision ${repoConfig.ref}, path ${repoConfig.path}, overwriteMode ${repoConfig.overwriteMode}")
138141

139142
cloneToLocalFolder(repoConfig, repoTmpDir)
@@ -145,44 +148,45 @@ class Content extends Feature {
145148
switch (repoConfig.type) {
146149
case ContentRepoType.FOLDER_BASED:
147150
createRepoCoordinatesForTypeFolderBased(repoConfig, repoTmpDir, contentRepoDir, mergedReposFolder, repoCoordinates)
151+
repoTmpDir.deleteDir()
148152
break
149153
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()
152156
break
153157
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
156160
break
157161
}
158162
log.debug("Finished cloning content repos. repoCoordinates=${repoCoordinates}")
159163
}
160164

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) {
162166
String namespace = repoConfig.target.split('/')[0]
163167
String repoName = repoConfig.target.split('/')[1]
164168

165169
def repoCoordinate = mergeRepoDirs(contentRepoDir, namespace, repoName, mergedReposFolder, repoConfig)
166170
repoCoordinate.refIsTag = isTag(repoTmpDir, repoConfig.ref)
167-
return repoCoordinate
171+
addRepoCoordinates(repoCoordinates, repoCoordinate)
168172
}
169173

170174
private static void createRepoCoordinatesForTypeFolderBased(ContentRepositorySchema repoConfig, File repoTmpDir, File contentRepoDir, File mergedReposFolder, List<RepoCoordinate> repoCoordinates) {
171175
boolean refIsTag = isTag(repoTmpDir, repoConfig.ref)
172176
findRepoDirectories(contentRepoDir)
173177
.each { contentRepoNamespaceDir ->
174178
findRepoDirectories(contentRepoNamespaceDir)
175-
.each { contentRepoRepoDir ->
179+
.each { contentRepoFolder ->
176180
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)
179183
repoCoordinate.refIsTag = refIsTag
180184
addRepoCoordinates(repoCoordinates, repoCoordinate)
181185
}
182186
}
183187
}
184188

185-
private static RepoCoordinate createRepoCoordinateForTypeMirror(ContentRepositorySchema repoConfig, File repoTmpDir) {
189+
private static void createRepoCoordinateForTypeMirror(ContentRepositorySchema repoConfig, File repoTmpDir, List<RepoCoordinate> repoCoordinates) {
186190
// Don't merge but keep these in separate dirs.
187191
// This avoids messing up .git folders with possible confusing exceptions for the user
188192
String namespace = repoConfig.target.split('/')[0]
@@ -194,7 +198,7 @@ class Content extends Feature {
194198
repoConfig: repoConfig,
195199
refIsTag: isTag(repoTmpDir, repoConfig.ref)
196200
)
197-
return repoCoordinate
201+
addRepoCoordinates(repoCoordinates, repoCoordinate)
198202
}
199203

200204
/**
@@ -253,8 +257,10 @@ class Content extends Feature {
253257

254258
if (ContentRepoType.MIRROR == repoConfig.type) {
255259
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+
}
258264
fetch.setRefSpecs("+refs/*:refs/*").call() // Fetch all branches and tags
259265
}
260266

@@ -295,7 +301,7 @@ class Content extends Feature {
295301
}
296302

297303

298-
protected void pushTargetRepos(List<RepoCoordinate> repoCoordinates) {
304+
private void pushTargetRepos(List<RepoCoordinate> repoCoordinates) {
299305
repoCoordinates.each { repoCoordinate ->
300306

301307
ScmmRepo targetRepo = repoProvider.getRepo(repoCoordinate.fullRepoName)
@@ -327,7 +333,7 @@ class Content extends Feature {
327333
* Copies repoCoordinate to targetRepo, commits and pushes
328334
* Same logic for both FOLDER_BASED and COPY repo types.
329335
*/
330-
protected static void handleRepoCopyingOrFolderBased(RepoCoordinate repoCoordinate, ScmmRepo targetRepo) {
336+
private static void handleRepoCopyingOrFolderBased(RepoCoordinate repoCoordinate, ScmmRepo targetRepo) {
331337
clearTargetRepoIfApplicable(repoCoordinate, targetRepo)
332338
// Avoid overwriting .git in target to avoid, because we don't need it for copying and
333339
// git pack files are typically read-only, leading to IllegalArgumentException:
@@ -345,7 +351,7 @@ class Content extends Feature {
345351

346352
}
347353

348-
protected static String setRefSpec(RepoCoordinate repoCoordinate, String targetRefShort) {
354+
private static String setRefSpec(RepoCoordinate repoCoordinate, String targetRefShort) {
349355
String refSpec
350356
if ((repoCoordinate.refIsTag && !repoCoordinate.repoConfig.targetRef.startsWith('refs/heads'))
351357
|| repoCoordinate.repoConfig.targetRef.startsWith('refs/tags')) {
@@ -372,7 +378,7 @@ class Content extends Feature {
372378
/**
373379
* Force pushes repoCoordinate.repoConfig.ref or all refs to targetRepo
374380
*/
375-
protected static void handleRepoMirroring(RepoCoordinate repoCoordinate, ScmmRepo targetRepo) {
381+
private static void handleRepoMirroring(RepoCoordinate repoCoordinate, ScmmRepo targetRepo) {
376382
try (def targetGit = Git.open(new File(targetRepo.absoluteLocalRepoTmpDir))) {
377383
def remoteUrl = targetGit.repository.config.getString('remote', 'origin', 'url')
378384

@@ -415,7 +421,7 @@ class Content extends Feature {
415421
}
416422
}
417423

418-
protected void createJenkinsJobIfApplicable(RepoCoordinate repoCoordinate, ScmmRepo repo) {
424+
private void createJenkinsJobIfApplicable(RepoCoordinate repoCoordinate, ScmmRepo repo) {
419425
if (repoCoordinate.repoConfig.createJenkinsJob && jenkins.isEnabled()) {
420426
if (existFileInSomeBranch(repo.absoluteLocalRepoTmpDir, 'Jenkinsfile')) {
421427
jenkins.createJenkinsjob(repoCoordinate.namespace, repoCoordinate.namespace)
@@ -545,6 +551,12 @@ class Content extends Feature {
545551
return true
546552
}
547553

554+
private void clearCache() {
555+
mergedReposFolder.deleteDir()
556+
cachedRepoCoordinates.clear()
557+
mergedReposFolder = null
558+
}
559+
548560
static class RepoCoordinate {
549561
String namespace
550562
String repoName

0 commit comments

Comments
 (0)