Skip to content

Commit 1f31b1a

Browse files
Thomas Michaelschnatterer
andcommitted
Polishing
Co-authored-by: Johannes Schnatterer <johannes.schnatterer@cloudogu.com>
1 parent d165166 commit 1f31b1a

File tree

4 files changed

+38
-50
lines changed

4 files changed

+38
-50
lines changed

src/main/groovy/com/cloudogu/gitops/Application.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Application {
4747
statics : new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_32).build().getStaticModels()
4848
]))
4949
}
50+
config.content.namespaces = namespaces.toList()
5051

5152
//iterates over all FeatureWithImages and gets their namespaces
5253
namespaces.addAll(this.features

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

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import org.eclipse.jgit.lib.Ref
2323
import org.eclipse.jgit.lib.Repository
2424
import org.eclipse.jgit.revwalk.RevCommit
2525
import org.eclipse.jgit.revwalk.RevWalk
26-
import org.eclipse.jgit.storage.file.FileRepositoryBuilder
2726
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider
2827
import org.eclipse.jgit.treewalk.TreeWalk
2928
import org.eclipse.jgit.treewalk.filter.PathFilter
@@ -145,7 +144,7 @@ class Content extends Feature {
145144
def repoCoordinate = new RepoCoordinate(
146145
namespace: namespace,
147146
repoName: repoName,
148-
newContent: repoTmpDir,
147+
clonedContentRepo: repoTmpDir,
149148
repoConfig: repoConfig,
150149
refIsTag: isTag(repoTmpDir, repoConfig.ref)
151150
)
@@ -164,15 +163,15 @@ class Content extends Feature {
164163
* Note that existing repoCoordinate objects with different overwriteMode are overwritten. The last repo to be mentioned within config.content.repos wins!
165164
*/
166165
private static RepoCoordinate mergeRepoDirs(File src, String namespace, String repoName, File mergedRepoFolder,
167-
ContentRepositorySchema repoConfig, List<RepoCoordinate> repoCoordinates) {
166+
ContentRepositorySchema repoConfig, List<RepoCoordinate> repoCoordinates) {
168167
File target = new File(new File(mergedRepoFolder, namespace), repoName)
169168
log.debug("Merging content repo, namespace ${namespace}, repoName ${repoName} from ${src} to ${target}")
170169
FileUtils.copyDirectory(src, target, new FileSystemUtils.IgnoreDotGitFolderFilter())
171170

172171
def repoCoordinate = new RepoCoordinate(
173172
namespace: namespace,
174173
repoName: repoName,
175-
newContent: target,
174+
clonedContentRepo: target,
176175
repoConfig: repoConfig,
177176
)
178177
addRepoCoordinates(repoCoordinates, repoCoordinate)
@@ -210,7 +209,6 @@ class Content extends Feature {
210209
new UsernamePasswordCredentialsProvider(repoConfig.username, repoConfig.password))
211210
}
212211

213-
214212
def git = cloneCommand.call()
215213

216214
if (ContentRepoType.MIRROR == repoConfig.type) {
@@ -265,9 +263,9 @@ class Content extends Feature {
265263
"and repo already exists in target: Not pushing content!" +
266264
"If you want to override, set ${OverwriteMode.UPGRADE} or ${OverwriteMode.RESET} .")
267265
} else {
268-
266+
269267
targetRepo.cloneRepo()
270-
268+
271269
if (ContentRepoType.MIRROR == repoCoordinate.repoConfig.type) {
272270
handleRepoMirroring(repoCoordinate, targetRepo)
273271
} else {
@@ -289,7 +287,7 @@ class Content extends Feature {
289287
private static void handleRepoCopying(RepoCoordinate repoCoordinate, ScmmRepo targetRepo) {
290288
if (OverwriteMode.INIT != repoCoordinate.repoConfig.overwriteMode) {
291289
if (OverwriteMode.RESET == repoCoordinate.repoConfig.overwriteMode) {
292-
log.info("OverwriteMode ${Config.OverwriteMode.RESET} set for repo '${repoCoordinate.fullRepoName}': " +
290+
log.info("OverwriteMode ${OverwriteMode.RESET} set for repo '${repoCoordinate.fullRepoName}': " +
293291
"Deleting existing files in repo and replacing them with new content.")
294292
targetRepo.clearRepo()
295293
} else {
@@ -300,24 +298,24 @@ class Content extends Feature {
300298
// Avoid overwriting .git in target to avoid, because we don't need it for copying and
301299
// git pack files are typically read-only, leading to IllegalArgumentException:
302300
// File parameter 'destFile is not writable: .git/objects/pack/pack-123.pack
303-
targetRepo.copyDirectoryContents(repoCoordinate.newContent.absolutePath, new FileSystemUtils.IgnoreDotGitFolderFilter())
301+
targetRepo.copyDirectoryContents(repoCoordinate.clonedContentRepo.absolutePath, new FileSystemUtils.IgnoreDotGitFolderFilter())
304302

305303
String commitMessage = "Initialize content repo ${repoCoordinate.namespace}/${repoCoordinate.repoName}"
306304
String targetRefShort = repoCoordinate.repoConfig.targetRef.replace('refs/heads/', '').replace('refs/tags/', '')
307305
if (targetRefShort) {
308306
String refSpec
309-
if ((repoCoordinate.refIsTag && !repoCoordinate.repoConfig.targetRef.startsWith('refs/heads'))
307+
if ((repoCoordinate.refIsTag && !repoCoordinate.repoConfig.targetRef.startsWith('refs/heads'))
310308
|| repoCoordinate.repoConfig.targetRef.startsWith('refs/tags')) {
311309
refSpec = "refs/tags/${targetRefShort}:refs/tags/${targetRefShort}"
312310
} else {
313-
refSpec = "HEAD:refs/heads/${targetRefShort}"
311+
refSpec = "HEAD:refs/heads/${targetRefShort}"
314312
}
315-
313+
316314
targetRepo.commitAndPush(commitMessage, targetRefShort, refSpec)
317315
} else {
318316
targetRepo.commitAndPush(commitMessage)
319317
}
320-
318+
321319
}
322320

323321
/**
@@ -336,15 +334,15 @@ class Content extends Feature {
336334
// IOException: Source ref someBranch doesn't resolve to any object.
337335
FileSystemUtils.makeWritable(new File(targetRepo.absoluteLocalRepoTmpDir, '.git'))
338336

339-
targetRepo.copyDirectoryContents(repoCoordinate.newContent.absolutePath)
337+
targetRepo.copyDirectoryContents(repoCoordinate.clonedContentRepo.absolutePath)
340338

341339
// Restore remote, it could have been overwritten due to a copied .git folder in MIRROR mode
342340
targetGit.repository.config.setString('remote', 'origin', 'url', remoteUrl)
343341
targetGit.repository.config.save()
344342
}
345343

346344
if (repoCoordinate.repoConfig.ref) {
347-
if (isCommit(repoCoordinate.newContent, repoCoordinate.repoConfig.ref)) {
345+
if (isCommit(repoCoordinate.clonedContentRepo, repoCoordinate.repoConfig.ref)) {
348346
// Mirroring detached commits does not make a lot of sense and is complicated
349347
// We would have to branch, push, delete remote branch. Considering this an edge case at the moment!
350348
throw new RuntimeException("Mirroring commit references is not supported for content repos at the moment. content repository '${repoCoordinate.repoConfig.url}', ref: ${repoCoordinate.repoConfig.ref}")
@@ -355,7 +353,7 @@ class Content extends Feature {
355353
} else {
356354
log.debug("Mirroring repo '${repoCoordinate.repoConfig.url}' ref '${repoCoordinate.repoConfig.ref}' to target repo ${repoCoordinate.fullRepoName}")
357355
targetRepo.pushRef(repoCoordinate.repoConfig.ref, true)
358-
}
356+
}
359357
} else {
360358
log.debug("Mirroring whole repo '${repoCoordinate.repoConfig.url}' to target repo ${repoCoordinate.fullRepoName}")
361359
targetRepo.pushAll(true)
@@ -425,19 +423,17 @@ class Content extends Feature {
425423
def objectId = git.repository.resolve(ref)
426424
return objectId != null
427425

428-
}
426+
}
429427
}
428+
430429
/**
431430
* checks, if file exists in repo in some branch.
432431
* @param pathToRepo
433432
* @param filename
434433
*/
435-
boolean existFileInSomeBranch(String repo, String filename) {
434+
static boolean existFileInSomeBranch(String repo, String filename) {
436435
String filenameToSearch = filename
437436
File repoPath = new File(repo + '/.git')
438-
def repository = new FileRepositoryBuilder()
439-
.setGitDir(repoPath)
440-
.build()
441437

442438
try (def git = Git.open(repoPath)) {
443439
List<Ref> branches = git
@@ -448,31 +444,27 @@ class Content extends Feature {
448444
for (Ref branch : branches) {
449445
String branchName = branch.getName()
450446

451-
ObjectId commitId = repository.resolve(branchName)
447+
ObjectId commitId = git.repository.resolve(branchName)
452448
if (commitId == null) {
453449
continue
454450
}
455-
try (RevWalk revWalk = new RevWalk(repository)) {
451+
try (RevWalk revWalk = new RevWalk(git.repository)) {
456452
RevCommit commit = revWalk.parseCommit(commitId)
457-
TreeWalk treeWalk = new TreeWalk(repository)
458-
treeWalk.addTree(commit.getTree())
459-
treeWalk.setRecursive(true)
460-
treeWalk.setFilter(PathFilter.create(filenameToSearch))
461-
462-
if (treeWalk.next()) {
463-
log.info("File ${filename} found in branch ${branchName}")
464-
treeWalk.close()
465-
revWalk.close()
466-
return true
467-
}
453+
try (TreeWalk treeWalk = new TreeWalk(git.repository)) {
468454

469-
treeWalk.close()
470-
revWalk.close()
455+
treeWalk.addTree(commit.getTree())
456+
treeWalk.setFilter(PathFilter.create(filenameToSearch))
471457

458+
if (treeWalk.next()) {
459+
log.debug("File ${filename} found in branch ${branchName}")
460+
461+
return true
462+
}
463+
}
472464
}
473465
}
474466
}
475-
log.info("File ${filename} not found in repository ${repoPath}")
467+
log.debug("File ${filename} not found in repository ${repoPath}")
476468
return false
477469
}
478470

@@ -481,20 +473,20 @@ class Content extends Feature {
481473
return false
482474
}
483475
try (def git = Git.open(repo)) {
484-
git.tagList().call().any { it.name.endsWith("/" + ref) || it.name == ref }
476+
git.tagList().call().any { it.name.endsWith("/" + ref) || it.name == ref }
485477
}
486478
}
487479

488480
static class RepoCoordinate {
489481
String namespace
490482
String repoName
491-
File newContent
483+
File clonedContentRepo
492484
ContentRepositorySchema repoConfig
493485
boolean refIsTag
494486

495487
@Override
496488
String toString() {
497-
return "RepoCoordinates{ namespace='$namespace', repoName='$repoName', repoConfig.type='${repoConfig.type}', repoConfig.overwriteMode='${repoConfig.overwriteMode}', newContent=$newContent', refIsTag='${refIsTag}' }"
489+
return "RepoCoordinates{ namespace='$namespace', repoName='$repoName', repoConfig.type='${repoConfig.type}', repoConfig.overwriteMode='${repoConfig.overwriteMode}', clonedContentRepo=$clonedContentRepo', refIsTag='${refIsTag}' }"
498490
}
499491

500492
String getFullRepoName() {

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,6 @@ class Jenkins extends Feature {
193193

194194
}
195195

196-
/**
197-
* creates a Jenkinsjob
198-
* @param jobName
199-
* @param repoName
200-
*/
201196
void createJenkinsjob(String namespace, String repoName) {
202197
def credentialId = "scmm-user"
203198
String prefixedNamespace = "${config.application.namePrefix}${namespace}"

src/test/groovy/com/cloudogu/gitops/features/ContentTest.groovy

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ class ContentTest {
439439
"Could not find repo with namespace=${expected.namespace} and repo=${expected.repoName} in ${actualTargetRepos}"
440440
).hasSize(1)
441441

442-
assertThat(actual[0].newContent.absolutePath).isEqualTo(
442+
assertThat(actual[0].clonedContentRepo.absolutePath).isEqualTo(
443443
new File(findRoot(repos), "${expected.namespace}/${expected.repoName}").absolutePath)
444444
}
445445
}
@@ -551,7 +551,7 @@ class ContentTest {
551551
}
552552

553553
@Test
554-
void 'Reset common_repo to repo '() {
554+
void 'Reset common repo to repo '() {
555555
/**
556556
* Prepare Testcase
557557
* using all defined repos -> common/repo is used by nonFolderRepo1 + 2
@@ -614,7 +614,7 @@ class ContentTest {
614614
}
615615

616616
@Test
617-
void 'Update common_repo test '() {
617+
void 'Update common repo test '() {
618618
/**
619619
* Prepare Testcase
620620
* using all defined repos -> common/repo is used by nonFolderRepo1 + 2
@@ -675,7 +675,7 @@ class ContentTest {
675675
}
676676

677677
@Test
678-
void 'init common_repo, expect unchanged repo'() {
678+
void 'init common repo, expect unchanged repo'() {
679679
/**
680680
* Prepare Testcase
681681
* using all defined repos -> common/repo is used by nonFolderRepo1 + 2
@@ -863,7 +863,7 @@ class ContentTest {
863863
}
864864

865865
String findRoot(List<RepoCoordinate> repos) {
866-
def result = new File(repos.get(0).getNewContent().getParent()).getParent()
866+
def result = new File(repos.get(0).getClonedContentRepo().getParent()).getParent()
867867
return result;
868868

869869
}

0 commit comments

Comments
 (0)