From f9d1d8f7efed2666377381272790c8f9f2c718b7 Mon Sep 17 00:00:00 2001 From: Simon Schrottner Date: Fri, 19 Mar 2021 11:36:59 +0100 Subject: [PATCH] Reworked Structure (#5) BREAKING CHANGE --- build.gradle | 22 +- license-header.spotless | 9 + ...sitoriesPluginFunctionalKotlinTests.groovy | 59 ++--- ...abRepositoriesPluginFunctionalTests.groovy | 222 ++++++++++++------ .../at/schrottner/gradle/TestFileUtils.groovy | 9 + .../resources/build-upload.gradle | 18 +- .../resources/build-withoutApplying.gradle | 37 +++ src/functionalTest/resources/build.gradle | 30 +-- src/functionalTest/resources/settings.gradle | 30 +-- .../resources/subprojectTest/build.gradle | 31 +++ .../resources/subprojectTest/settings.gradle | 29 +++ .../subprojectTest/subproject1/build.gradle | 32 +++ .../subprojectTest/subproject2/build.gradle | 31 +++ .../groovy/at/schrottner/gradle/Config.groovy | 16 ++ .../schrottner/gradle/GitLabEntityType.groovy | 28 +++ .../gradle/GitlabRepositoriesExtension.groovy | 182 +++++--------- .../gradle/GitlabRepositoriesPlugin.groovy | 63 +++-- .../at/schrottner/gradle/LogHandler.groovy | 17 -- .../at/schrottner/gradle/MavenConfig.groovy | 30 --- .../gradle/RepositoryActionHandler.groovy | 119 ++++++++++ .../gradle/RepositoryConfiguration.groovy | 36 +++ .../gradle/auths/DeployToken.groovy | 9 + .../schrottner/gradle/auths/JobToken.groovy | 9 + .../gradle/auths/PrivateToken.groovy | 9 + .../at/schrottner/gradle/auths/Token.groovy | 13 + .../mavenConfigs/GroupMavenConfig.groovy | 11 - .../mavenConfigs/ProjectMavenConfig.groovy | 16 -- 27 files changed, 759 insertions(+), 358 deletions(-) create mode 100644 license-header.spotless create mode 100644 src/functionalTest/resources/build-withoutApplying.gradle create mode 100644 src/functionalTest/resources/subprojectTest/build.gradle create mode 100644 src/functionalTest/resources/subprojectTest/settings.gradle create mode 100644 src/functionalTest/resources/subprojectTest/subproject1/build.gradle create mode 100644 src/functionalTest/resources/subprojectTest/subproject2/build.gradle create mode 100644 src/main/groovy/at/schrottner/gradle/Config.groovy create mode 100644 src/main/groovy/at/schrottner/gradle/GitLabEntityType.groovy delete mode 100644 src/main/groovy/at/schrottner/gradle/LogHandler.groovy delete mode 100644 src/main/groovy/at/schrottner/gradle/MavenConfig.groovy create mode 100644 src/main/groovy/at/schrottner/gradle/RepositoryActionHandler.groovy create mode 100644 src/main/groovy/at/schrottner/gradle/RepositoryConfiguration.groovy delete mode 100644 src/main/groovy/at/schrottner/gradle/mavenConfigs/GroupMavenConfig.groovy delete mode 100644 src/main/groovy/at/schrottner/gradle/mavenConfigs/ProjectMavenConfig.groovy diff --git a/build.gradle b/build.gradle index 7bc4cc7..d07deba 100644 --- a/build.gradle +++ b/build.gradle @@ -14,6 +14,21 @@ plugins { version = "${gitVersion()}".replaceFirst(~/^v/, '') +spotless { + groovy { + importOrder() // standard import order + + excludeJava() + greclipse() + + licenseHeaderFile file("license-header.spotless") // or licenseHeaderFile + } + groovyGradle { + target '*.gradle' // default target of groovyGradle + greclipse() + } +} + repositories { mavenCentral() } @@ -66,7 +81,11 @@ gradlePlugin { pluginBundle { website = 'http://schrottner.at/gradle-gitlab-repositories/' vcsUrl = 'https://github.com/aepfli/gradle-gitlab-repositories.git' - tags = ['GitLab', 'repositories', 'maven'] + tags = [ + 'GitLab', + 'repositories', + 'maven' + ] } test { @@ -78,7 +97,6 @@ test { sourceSets { functionalTest { - } } diff --git a/license-header.spotless b/license-header.spotless new file mode 100644 index 0000000..f66896b --- /dev/null +++ b/license-header.spotless @@ -0,0 +1,9 @@ +/* + * Copyright 2016-2021 the original author or authors. + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v2.0 which + * accompanies this distribution and is available at + * + * http://www.eclipse.org/legal/epl-v20.html + */ \ No newline at end of file diff --git a/src/functionalTest/groovy/at/schrottner/gradle/GitlabRepositoriesPluginFunctionalKotlinTests.groovy b/src/functionalTest/groovy/at/schrottner/gradle/GitlabRepositoriesPluginFunctionalKotlinTests.groovy index da6cdde..0361128 100644 --- a/src/functionalTest/groovy/at/schrottner/gradle/GitlabRepositoriesPluginFunctionalKotlinTests.groovy +++ b/src/functionalTest/groovy/at/schrottner/gradle/GitlabRepositoriesPluginFunctionalKotlinTests.groovy @@ -1,22 +1,28 @@ /* - * This Groovy source file was generated by the Gradle "init" task. + * Copyright 2016-2021 the original author or authors. + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v2.0 which + * accompanies this distribution and is available at + * + * http://www.eclipse.org/legal/epl-v20.html */ package at.schrottner.gradle +import static org.assertj.core.api.Assertions.assertThat + import org.gradle.testkit.runner.BuildResult import org.gradle.testkit.runner.GradleRunner import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import org.junit.jupiter.api.io.TempDir -import static org.assertj.core.api.Assertions.assertThat - class GitlabRepositoriesPluginFunctionalKotlinTests { private pluginClasspath = getClass().classLoader.findResource("plugin-classpath.txt") - .readLines() - .collect { it.replace("\\\\", "\\\\\\\\") } // escape backslashes in Windows paths - .collect { "\"$it\"" } - .join(", ") + .readLines() + .collect { it.replace("\\\\", "\\\\\\\\") } // escape backslashes in Windows paths + .collect { "\"$it\"" } + .join(", ") File projectDir File settingsGradle @@ -50,9 +56,9 @@ class GitlabRepositoriesPluginFunctionalKotlinTests { assertThat(result.output) .contains("BUILD SUCCESSFUL") .containsSubsequence( - "added Job-Token: jobToken", - "added Deploy-Token: token0", - "added Deploy-Token: token1" + "added Job-Token: jobToken", + "added Deploy-Token: token0", + "added Deploy-Token: token1" ) } @@ -73,9 +79,9 @@ class GitlabRepositoriesPluginFunctionalKotlinTests { assertThat(result.output) .contains("BUILD SUCCESSFUL") .containsSubsequence( - "added Job-Token: jobToken", - "added Deploy-Token: token0", - "added Deploy-Token: token1" + "added Job-Token: jobToken", + "added Deploy-Token: token0", + "added Deploy-Token: token1" ) } @@ -101,13 +107,12 @@ class GitlabRepositoriesPluginFunctionalKotlinTests { assertThat(result.output) .contains("BUILD SUCCESSFUL") .containsSubsequence( - "added Job-Token: jobToken", - "added Deploy-Token: token0", - "added Deploy-Token: token1", - "Settings evaluated", - "replaced Job-Token: jobToken", - "replaced Private-Token: token0", - "replaced Private-Token: token1" + "added Job-Token: jobToken", + "added Deploy-Token: token0", + "added Deploy-Token: token1", + "Settings evaluated", + "replaced Private-Token: token0", + "replaced Private-Token: token1" ) } @@ -131,13 +136,13 @@ class GitlabRepositoriesPluginFunctionalKotlinTests { runner.withPluginClasspath() runner.withArguments("gitLabTask", "-i", "-s") runner.withProjectDir(projectDir) -// runner. { -// -// // gradle testkit jacoco support -// File("./build/testkit/testkit-gradle.properties") -// .copyTo(File(projectDir, "gradle.properties")) -// -// } + // runner. { + // + // // gradle testkit jacoco support + // File("./build/testkit/testkit-gradle.properties") + // .copyTo(File(projectDir, "gradle.properties")) + // + // } runner.build() } diff --git a/src/functionalTest/groovy/at/schrottner/gradle/GitlabRepositoriesPluginFunctionalTests.groovy b/src/functionalTest/groovy/at/schrottner/gradle/GitlabRepositoriesPluginFunctionalTests.groovy index 923d725..7a2cbaf 100644 --- a/src/functionalTest/groovy/at/schrottner/gradle/GitlabRepositoriesPluginFunctionalTests.groovy +++ b/src/functionalTest/groovy/at/schrottner/gradle/GitlabRepositoriesPluginFunctionalTests.groovy @@ -1,8 +1,18 @@ /* - * This Groovy source file was generated by the Gradle 'init' task. + * Copyright 2016-2021 the original author or authors. + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v2.0 which + * accompanies this distribution and is available at + * + * http://www.eclipse.org/legal/epl-v20.html */ package at.schrottner.gradle +import static org.assertj.core.api.Assertions.assertThat + +import java.nio.file.Files +import org.gradle.internal.impldep.org.apache.commons.io.FileUtils import org.gradle.testkit.runner.BuildResult import org.gradle.testkit.runner.GradleRunner import org.junit.jupiter.api.BeforeEach @@ -13,10 +23,6 @@ import org.junit.jupiter.api.io.TempDir import org.slf4j.Logger import org.slf4j.LoggerFactory -import java.nio.file.Files - -import static org.assertj.core.api.Assertions.assertThat - class GitlabRepositoriesPluginFunctionalTests { private static final Logger logger = LoggerFactory.getLogger(GitlabRepositoriesPluginFunctionalTests.class) @@ -26,10 +32,10 @@ class GitlabRepositoriesPluginFunctionalTests { private static def renamedId = "123" private static def realms = ["group", "project"] private pluginClasspath = getClass().classLoader.findResource("plugin-classpath.txt") - .readLines() - .collect { it.replace('\\\\', '\\\\\\\\') } // escape backslashes in Windows paths - .collect { "$it" } - .join(",") + .readLines() + .collect { it.replace('\\\\', '\\\\\\\\') } // escape backslashes in Windows paths + .collect { "$it" } + .join(",") File projectDir File settingsGradle @@ -67,19 +73,19 @@ class GitlabRepositoriesPluginFunctionalTests { assertThat(result.output) .contains("BUILD SUCCESSFUL") .containsSubsequence( - "added Job-Token: jobToken", - "added Private-Token: tokenIgnoredNoValue", - "added Deploy-Token: token0", - "added Deploy-Token: token1" + "added Job-Token: jobToken", + "added Private-Token: tokenIgnoredNoValue", + "added Deploy-Token: token0", + "added Deploy-Token: token1" ) .containsSubsequence("Maven Repository $repoPrefix-$existingId is using 'token0'", - "Maven Repository $it-renamed is using 'token0'", - "Maven Repository $repoPrefix-specialToken is using 'token0'", - "Maven Repository $repoPrefix-specialToken1 is using 'token1'", - "Maven Repository $repoPrefix-specialTokenSelection is using 'token1'", - "Maven Repository $repoPrefix-ignoredNoValue was not added, as no token could be applied!" + "Maven Repository $it-renamed is using 'token0'", + "Maven Repository $repoPrefix-specialToken is using 'token0'", + "Maven Repository $repoPrefix-specialToken1 is using 'token1'", + "Maven Repository $repoPrefix-specialTokenSelection is using 'token1'", + "Maven Repository $repoPrefix-ignoredNoValue was not added, as no token could be applied!" ) - .doesNotContain("Maven Repository $repoPrefix-ignoredNoValue is using") + .doesNotContain("Maven Repository $repoPrefix-ignoredNoValue is using '") } } @@ -98,19 +104,19 @@ class GitlabRepositoriesPluginFunctionalTests { assertThat(result.output) .contains("BUILD SUCCESSFUL") .containsSubsequence( - "added Job-Token: jobToken", - "added Private-Token: tokenIgnoredNoValue", - "added Private-Token: token0", - "added Private-Token: token1" + "added Job-Token: jobToken", + "added Private-Token: tokenIgnoredNoValue", + "added Private-Token: token0", + "added Private-Token: token1" ) .containsSubsequence("Maven Repository $repoPrefix-$existingId is using 'token0'", - "Maven Repository $it-renamed is using 'token0'", - "Maven Repository $repoPrefix-specialToken is using 'token0'", - "Maven Repository $repoPrefix-specialToken1 is using 'token1'", - "Maven Repository $repoPrefix-specialTokenSelection is using 'token1'", - "Maven Repository $repoPrefix-ignoredNoValue was not added, as no token could be applied!" + "Maven Repository $it-renamed is using 'token0'", + "Maven Repository $repoPrefix-specialToken is using 'token0'", + "Maven Repository $repoPrefix-specialToken1 is using 'token1'", + "Maven Repository $repoPrefix-specialTokenSelection is using 'token1'", + "Maven Repository $repoPrefix-ignoredNoValue was not added, as no token could be applied!" ) - .doesNotContain("Maven Repository $repoPrefix-ignoredNoValue is using") + .doesNotContain("Maven Repository $repoPrefix-ignoredNoValue is using '") } } @@ -130,82 +136,146 @@ class GitlabRepositoriesPluginFunctionalTests { assertThat(result.output) .contains("BUILD SUCCESSFUL") .containsSubsequence( - "added Job-Token: jobToken", - "added Private-Token: tokenIgnoredNoValue", - "added Deploy-Token: token0", - "added Deploy-Token: token1", - "Settings evaluated", - "added Job-Token: jobToken", - "added Private-Token: tokenIgnoredNoValue", - "added Deploy-Token: token0", - "added Deploy-Token: token1", - "replaced Job-Token: jobToken", - "replaced Private-Token: tokenIgnoredNoValue", - "replaced Private-Token: token0", - "replaced Private-Token: token1", - "added Deploy-Token: tokenAdded" + "added Job-Token: jobToken", + "added Private-Token: tokenIgnoredNoValue", + "added Deploy-Token: token0", + "added Deploy-Token: token1", + "Settings evaluated", + "replaced Private-Token: tokenIgnoredNoValue", + "replaced Private-Token: token0", + "replaced Private-Token: token1", + "added Deploy-Token: tokenAdded" + ) + .containsSubsequence("Maven Repository $repoPrefix-$existingId is using 'token0'", + "Maven Repository $it-renamed is using 'token0'", + "Maven Repository $repoPrefix-specialToken is using 'token0'", + "Maven Repository $repoPrefix-specialToken1 is using 'token1'", + "Maven Repository $repoPrefix-specialTokenSelection is using 'token1'", + "Maven Repository $repoPrefix-ignoredNoValue was not added, as no token could be applied!" + ) + .doesNotContain("Maven Repository $repoPrefix-ignoredNoValue is using '") + } + } + + @Test + void "used in settings and project without applying"() { + //given: + settingsGradle = TestFileUtils.getTestResource(new File(projectDir, SETTINGS_GRADLE), SETTINGS_GRADLE) + buildGradle = TestFileUtils.getTestResource(new File(projectDir, BUILD_GRADLE), "build-withoutApplying.gradle") + + //when: + BuildResult result = runTest() + + //then: + realms.each { + def capitalized = it.capitalize() + def repoPrefix = "GitLab-${capitalized}" + assertThat(result.output) + .contains("BUILD SUCCESSFUL") + .containsSubsequence( + "added Job-Token: jobToken", + "added Private-Token: tokenIgnoredNoValue", + "added Deploy-Token: token0", + "added Deploy-Token: token1", + "Settings evaluated", + "replaced Private-Token: tokenIgnoredNoValue", + "replaced Private-Token: token0", + "replaced Private-Token: token1", + "added Deploy-Token: tokenAdded" ) .containsSubsequence("Maven Repository $repoPrefix-$existingId is using 'token0'", - "Maven Repository $it-renamed is using 'token0'", - "Maven Repository $repoPrefix-specialToken is using 'token0'", - "Maven Repository $repoPrefix-specialToken1 is using 'token1'", - "Maven Repository $repoPrefix-specialTokenSelection is using 'token1'", - "Maven Repository $repoPrefix-ignoredNoValue was not added, as no token could be applied!" + "Maven Repository $it-renamed is using 'token0'", + "Maven Repository $repoPrefix-specialToken is using 'token0'", + "Maven Repository $repoPrefix-specialToken1 is using 'token1'", + "Maven Repository $repoPrefix-specialTokenSelection is using 'token1'", + "Maven Repository $repoPrefix-ignoredNoValue was not added, as no token could be applied!" ) - .doesNotContain("Maven Repository $repoPrefix-ignoredNoValue is using") + .doesNotContain("Maven Repository $repoPrefix-ignoredNoValue is using '") } } @Test @DisabledIfEnvironmentVariable( - named = 'TEST_UPLOAD_TOKEN', - matches = '^$', - disabledReason = 'Upload deactivated due to missing TEST_UPLOAD_TOKEN' + named = 'TEST_UPLOAD_TOKEN', + matches = '^$', + disabledReason = 'Upload deactivated due to missing TEST_UPLOAD_TOKEN' ) void "uploadTest"() { def testFile = TestFileUtils.getTestResource(new File(projectDir, 'test.xml'), 'test.xml') settingsGradle = TestFileUtils.getTestResource(new File(projectDir, SETTINGS_GRADLE), SETTINGS_GRADLE) buildGradle = TestFileUtils.getTestResource(new File(projectDir, BUILD_GRADLE), 'build-upload.gradle') - def uploadRunner = GradleRunner.create() - uploadRunner.forwardOutput() - uploadRunner.withPluginClasspath() - uploadRunner.withArguments("publishTestPublicationToGitLabRepository", "-i", "-s") - uploadRunner.withProjectDir(projectDir) - def uploadResult = uploadRunner.build() + def uploadResult = runTest("publishTestPublicationToGitLabRepository", "-i", "-s") def repoPrefix = "GitLab-Project" assertThat(uploadResult.output) .contains("BUILD SUCCESSFUL") .containsSubsequence( - "added Job-Token: jobToken", - "added Private-Token: tokenIgnoredNoValue", - "added Deploy-Token: token0", - "added Deploy-Token: token1", - "Settings evaluated", - "added Job-Token: jobToken", - "added Private-Token: tokenIgnoredNoValue", - "added Deploy-Token: token0", - "added Deploy-Token: token1", - "added Private-Token: testToken" + "added Job-Token: jobToken", + "added Private-Token: tokenIgnoredNoValue", + "added Deploy-Token: token0", + "added Deploy-Token: token1", + "Settings evaluated", + "added Private-Token: testToken" ) .containsSubsequence("Maven Repository $repoPrefix-$existingId is using 'token0'", - "Maven Repository $repoPrefix-specialToken is using 'token0'", - "Maven Repository $repoPrefix-specialToken1 is using 'token1'", - "Maven Repository $repoPrefix-specialTokenSelection is using 'token1'", - "Maven Repository $repoPrefix-ignoredNoValue was not added, as no token could be applied!", - "Maven Repository GitLab is using 'testToken'", + "Maven Repository $repoPrefix-specialToken is using 'token0'", + "Maven Repository $repoPrefix-specialToken1 is using 'token1'", + "Maven Repository $repoPrefix-specialTokenSelection is using 'token1'", + "Maven Repository $repoPrefix-ignoredNoValue was not added, as no token could be applied!", + "Maven Repository GitLab is using 'testToken'", ) - .doesNotContain("Maven Repository $repoPrefix-ignoredNoValue is using") + .doesNotContain("Maven Repository $repoPrefix-ignoredNoValue is using '") .contains("Publishing to repository 'GitLab'") } + @Test + void "subprojectTest"() { + //given: + FileUtils.copyDirectory(new File(ClassLoader.getSystemClassLoader().getResource('subprojectTest').toURI()), projectDir) + + //when: + BuildResult result = runTest() + assertThat(result.output) + .contains("BUILD SUCCESSFUL") + .containsSubsequence( + "added Job-Token: jobToken", + "added Private-Token: tokenIgnoredNoValue", + "added Deploy-Token: token0", + "added Deploy-Token: token1", + "Settings evaluated", + "Configure project :", + "readding Token from Parent Job-Token: jobToken", + "readding Token from Parent Private-Token: tokenIgnoredNoValue", + "readding Token from Parent Deploy-Token: token0", + "readding Token from Parent Deploy-Token: token1", + "replaced Private-Token: tokenIgnoredNoValue", + "replaced Private-Token: token0", + "replaced Private-Token: token1", + "Configure project :subproject1", + "readding Token from Parent Job-Token: jobToken", + "readding Token from Parent Private-Token: tokenIgnoredNoValue", + "readding Token from Parent Deploy-Token: token0", + "readding Token from Parent Deploy-Token: token1", + "added Deploy-Token: tokenAdded", + "added Deploy-Token: tokenAdded1", + "replaced Deploy-Token: token0", + "Configure project :subproject2", + "readding Token from Parent Job-Token: jobToken", + "readding Token from Parent Private-Token: tokenIgnoredNoValue", + "readding Token from Parent Deploy-Token: token0", + "readding Token from Parent Deploy-Token: token1", + "added Deploy-Token: tokenAdded", + "added Deploy-Token: tokenAdded1", + "replaced Deploy-Token: token0", + ) + } - private BuildResult runTest() { + private BuildResult runTest(String[] args = ["tasks", "-i", "-s"]) { def runner = GradleRunner.create() runner.forwardOutput() runner.withPluginClasspath() - runner.withArguments("tasks", "-i", "-s") + runner.withArguments(args) runner.withProjectDir(projectDir) runner.build() } diff --git a/src/functionalTest/groovy/at/schrottner/gradle/TestFileUtils.groovy b/src/functionalTest/groovy/at/schrottner/gradle/TestFileUtils.groovy index 61c4e3e..7d824c2 100644 --- a/src/functionalTest/groovy/at/schrottner/gradle/TestFileUtils.groovy +++ b/src/functionalTest/groovy/at/schrottner/gradle/TestFileUtils.groovy @@ -1,3 +1,12 @@ +/* + * Copyright 2016-2021 the original author or authors. + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v2.0 which + * accompanies this distribution and is available at + * + * http://www.eclipse.org/legal/epl-v20.html + */ package at.schrottner.gradle class TestFileUtils { diff --git a/src/functionalTest/resources/build-upload.gradle b/src/functionalTest/resources/build-upload.gradle index 58bf9f3..76884b3 100644 --- a/src/functionalTest/resources/build-upload.gradle +++ b/src/functionalTest/resources/build-upload.gradle @@ -3,9 +3,11 @@ buildscript { classpath files(pluginClasspath.split(',')) } } + plugins { id 'maven-publish' } + apply plugin: at.schrottner.gradle.GitlabRepositoriesPlugin gitLab { @@ -14,23 +16,23 @@ gitLab { it.value = System.getenv('TEST_UPLOAD_TOKEN') } } + publishing { repositories { - gitLab.upload(owner, "") - gitLab.upload(owner, "$existingId") - gitLab.upload(owner, "specialToken") { + maven gitLab.upload("$existingId") + maven gitLab.upload("specialToken") { tokenSelector = "token0" } - gitLab.upload(owner, "specialToken1") { + maven gitLab.upload("specialToken1") { tokenSelector = "token1" } - gitLab.upload(owner, "specialTokenSelection") { + maven gitLab.upload("specialTokenSelection") { tokenSelectors = ["jobToken", "token1"] } - gitLab.upload(owner, "ignoredNoValue") { + maven gitLab.upload("ignoredNoValue") { tokenSelector = "tokenIgnoredNoValue" } - gitLab.upload(owner, "24974077") { + maven gitLab.upload("24974077") { name = "GitLab" tokenSelector = "testToken" } @@ -44,4 +46,4 @@ publishing { artifact source: 'test.xml', classifier: 'features' } } -} \ No newline at end of file +} diff --git a/src/functionalTest/resources/build-withoutApplying.gradle b/src/functionalTest/resources/build-withoutApplying.gradle new file mode 100644 index 0000000..85fd701 --- /dev/null +++ b/src/functionalTest/resources/build-withoutApplying.gradle @@ -0,0 +1,37 @@ +gitLab { + token(PrivateToken) { + it.key = 'tokenIgnoredNoValue' + it.value = '' + } + token(PrivateToken) { + it.key = 'token0' + it.value = 'test' + } + token(PrivateToken) { + it.key = 'token1' + it.value = 'test' + } + token(DeployToken) { + it.key = 'tokenAdded' + it.value = 'test' + } +} +repositories { + realms.split(',').each { realm -> + + maven gitLab."$realm"("$existingId") + maven gitLab."$realm"("$renamedId") { name = "$realm-renamed" } + maven gitLab."$realm"("specialToken") { + tokenSelector = "token0" + } + maven gitLab."$realm"("specialToken1") { + tokenSelector = "token1" + } + maven gitLab."$realm"("specialTokenSelection") { + tokenSelectors = ["jobToken", "token1"] + } + maven gitLab."$realm"("ignoredNoValue") { + tokenSelector = "tokenIgnoredNoValue" + } + } +} \ No newline at end of file diff --git a/src/functionalTest/resources/build.gradle b/src/functionalTest/resources/build.gradle index bc89a6d..070bd71 100644 --- a/src/functionalTest/resources/build.gradle +++ b/src/functionalTest/resources/build.gradle @@ -23,20 +23,22 @@ gitLab { it.value = 'test' } } -realms.split(',').each { realm -> +repositories { + realms.split(',').each { realm -> - gitLab."$realm"("$existingId") - gitLab."$realm"("$renamedId") { name = "$realm-renamed" } - gitLab."$realm"("specialToken") { - tokenSelector = "token0" - } - gitLab."$realm"("specialToken1") { - tokenSelector = "token1" - } - gitLab."$realm"("specialTokenSelection") { - tokenSelectors = ["jobToken", "token1"] - } - gitLab."$realm"("ignoredNoValue") { - tokenSelector = "tokenIgnoredNoValue" + maven gitLab."$realm"("$existingId") + maven gitLab."$realm"("$renamedId") { name = "$realm-renamed" } + maven gitLab."$realm"("specialToken") { + tokenSelector = "token0" + } + maven gitLab."$realm"("specialToken1") { + tokenSelector = "token1" + } + maven gitLab."$realm"("specialTokenSelection") { + tokenSelectors = ["jobToken", "token1"] + } + maven gitLab."$realm"("ignoredNoValue") { + tokenSelector = "tokenIgnoredNoValue" + } } } diff --git a/src/functionalTest/resources/settings.gradle b/src/functionalTest/resources/settings.gradle index 1faf0ec..778bdb4 100644 --- a/src/functionalTest/resources/settings.gradle +++ b/src/functionalTest/resources/settings.gradle @@ -20,20 +20,22 @@ gitLab { } } -realms.split(',').each { realm -> +pluginManagement.repositories { + realms.split(',').each { realm -> - gitLab."$realm"("$existingId") - gitLab."$realm"("$renamedId") { name = "$realm-renamed" } - gitLab."$realm"("specialToken") { - tokenSelector = "token0" - } - gitLab."$realm"("specialToken1") { - tokenSelector = "token1" - } - gitLab."$realm"("specialTokenSelection") { - tokenSelectors = ["jobToken", "token1"] - } - gitLab."$realm"("ignoredNoValue") { - tokenSelector = "tokenIgnoredNoValue" + maven gitLab."$realm"("$existingId") + maven gitLab."$realm"("$renamedId") { name = "$realm-renamed" } + maven gitLab."$realm"("specialToken") { + tokenSelector = "token0" + } + maven gitLab."$realm"("specialToken1") { + tokenSelector = "token1" + } + maven gitLab."$realm"("specialTokenSelection") { + tokenSelectors = ["jobToken", "token1"] + } + maven gitLab."$realm"("ignoredNoValue") { + tokenSelector = "tokenIgnoredNoValue" + } } } diff --git a/src/functionalTest/resources/subprojectTest/build.gradle b/src/functionalTest/resources/subprojectTest/build.gradle new file mode 100644 index 0000000..60a8991 --- /dev/null +++ b/src/functionalTest/resources/subprojectTest/build.gradle @@ -0,0 +1,31 @@ +buildscript { + dependencies { + classpath files(pluginClasspath.split(',')) + } +} +apply plugin: at.schrottner.gradle.GitlabRepositoriesPlugin + +gitLab { + token(PrivateToken) { + it.key = 'tokenIgnoredNoValue' + it.value = '' + } + token(PrivateToken) { + it.key = 'token0' + it.value = 'test' + } + token(PrivateToken) { + it.key = 'token1' + it.value = 'test' + } + token(DeployToken) { + it.key = 'tokenAdded' + it.value = 'test' + } +} +repositories { + realms.split(',').each { realm -> + + maven gitLab."$realm"("$existingId") + } +} diff --git a/src/functionalTest/resources/subprojectTest/settings.gradle b/src/functionalTest/resources/subprojectTest/settings.gradle new file mode 100644 index 0000000..6efa6fe --- /dev/null +++ b/src/functionalTest/resources/subprojectTest/settings.gradle @@ -0,0 +1,29 @@ +buildscript { + dependencies { + classpath files(pluginClasspath.split(',')) + } +} +apply plugin: at.schrottner.gradle.GitlabRepositoriesPlugin + +gitLab { + token(PrivateToken) { + it.key = 'tokenIgnoredNoValue' + it.value = '' + } + token(DeployToken) { + it.key = 'token0' + it.value = 'test' + } + token(DeployToken) { + it.key = 'token1' + it.value = 'test' + } +} + +pluginManagement.repositories { + realms.split(',').each { realm -> + maven gitLab."$realm"("$existingId") + } +} + +include 'subproject1', 'subproject2' \ No newline at end of file diff --git a/src/functionalTest/resources/subprojectTest/subproject1/build.gradle b/src/functionalTest/resources/subprojectTest/subproject1/build.gradle new file mode 100644 index 0000000..a09d101 --- /dev/null +++ b/src/functionalTest/resources/subprojectTest/subproject1/build.gradle @@ -0,0 +1,32 @@ +buildscript { + dependencies { + classpath files(pluginClasspath.split(',')) + } +} +apply plugin: at.schrottner.gradle.GitlabRepositoriesPlugin + +gitLab { + token(DeployToken) { + it.key = 'tokenAdded' + it.value = '' + } + token(DeployToken) { + it.key = 'tokenAdded1' + it.value = 'project1' + } + token(DeployToken) { + it.key = 'token0' + it.value = 'test' + } +} + +repositories { + realms.split(',').each { realm -> + maven gitLab."$realm"("tokentest") { + tokenSelector = "tokenAdded" + } + maven gitLab."$realm"("tokentest1") { + tokenSelector = "tokenAdded1" + } + } +} diff --git a/src/functionalTest/resources/subprojectTest/subproject2/build.gradle b/src/functionalTest/resources/subprojectTest/subproject2/build.gradle new file mode 100644 index 0000000..e7dbe2a --- /dev/null +++ b/src/functionalTest/resources/subprojectTest/subproject2/build.gradle @@ -0,0 +1,31 @@ +buildscript { + dependencies { + classpath files(pluginClasspath.split(',')) + } +} +apply plugin: at.schrottner.gradle.GitlabRepositoriesPlugin + +gitLab { + token(DeployToken) { + it.key = 'tokenAdded' + it.value = 'test' + } + token(DeployToken) { + it.key = 'tokenAdded1' + it.value = '' + } + token(DeployToken) { + it.key = 'token0' + it.value = 'test' + } +} +repositories { + realms.split(',').each { realm -> + maven gitLab."$realm"("tokentest") { + tokenSelector = "tokenAdded" + } + maven gitLab."$realm"("tokentest1") { + tokenSelector = "tokenAdded1" + } + } +} diff --git a/src/main/groovy/at/schrottner/gradle/Config.groovy b/src/main/groovy/at/schrottner/gradle/Config.groovy new file mode 100644 index 0000000..b48f9f7 --- /dev/null +++ b/src/main/groovy/at/schrottner/gradle/Config.groovy @@ -0,0 +1,16 @@ +/* + * Copyright 2016-2021 the original author or authors. + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v2.0 which + * accompanies this distribution and is available at + * + * http://www.eclipse.org/legal/epl-v20.html + */ +package at.schrottner.gradle + +class Config { + public static final String LOG_PREFIX = "GitLab Repositories ::" + public static final String GROUP = "Group" + public static final String PROJECT = "Project" +} diff --git a/src/main/groovy/at/schrottner/gradle/GitLabEntityType.groovy b/src/main/groovy/at/schrottner/gradle/GitLabEntityType.groovy new file mode 100644 index 0000000..e0b95da --- /dev/null +++ b/src/main/groovy/at/schrottner/gradle/GitLabEntityType.groovy @@ -0,0 +1,28 @@ +/* + * Copyright 2016-2021 the original author or authors. + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v2.0 which + * accompanies this distribution and is available at + * + * http://www.eclipse.org/legal/epl-v20.html + */ +package at.schrottner.gradle + +enum GitLabEntityType { + GROUP("Group", "groups"), + PROJECT("Project", "projects") + + String name + String endpoint + + GitLabEntityType(String name, String endpoint) { + this.name = name + this.endpoint = endpoint + } + + @Override + String toString() { + return name + } +} \ No newline at end of file diff --git a/src/main/groovy/at/schrottner/gradle/GitlabRepositoriesExtension.groovy b/src/main/groovy/at/schrottner/gradle/GitlabRepositoriesExtension.groovy index 336da24..8caa570 100644 --- a/src/main/groovy/at/schrottner/gradle/GitlabRepositoriesExtension.groovy +++ b/src/main/groovy/at/schrottner/gradle/GitlabRepositoriesExtension.groovy @@ -2,191 +2,129 @@ package at.schrottner.gradle import at.schrottner.gradle.auths.JobToken import at.schrottner.gradle.auths.Token -import at.schrottner.gradle.mavenConfigs.GroupMavenConfig -import at.schrottner.gradle.mavenConfigs.ProjectMavenConfig +import groovy.transform.CompileStatic import org.gradle.api.Action import org.gradle.api.Project import org.gradle.api.artifacts.dsl.RepositoryHandler -import org.gradle.api.artifacts.repositories.AuthenticationContainer import org.gradle.api.artifacts.repositories.MavenArtifactRepository -import org.gradle.api.credentials.HttpHeaderCredentials import org.gradle.api.initialization.Settings -import org.gradle.authentication.http.HttpHeaderAuthentication +import org.gradle.api.model.ObjectFactory +import org.slf4j.Logger import org.slf4j.LoggerFactory -public class GitlabRepositoriesExtension implements LogHandler { + +/** + * GitLabRepositoriesExtension is the main entry point to configure the plugin + * + * It provides additional methods to automatically add repositories based on GitLab Groups + * or Projects. + */ +@CompileStatic +class GitlabRepositoriesExtension { + + private static final Logger logger = LoggerFactory.getLogger(RepositoryHandler) public static final String NAME = "gitLab" private final RepositoryHandler repositories - private int afterPosition + private final ObjectFactory objects + private final RepositoryActionHandler handler String baseUrl = "gitlab.com" - String afterRepository = 'MavenLocal' boolean applyToProject = true Map tokens = [:] - public static final def artifacts = [:] + public final List> artifactActionStorage = [] - GitlabRepositoriesExtension(Settings settings) { - this.logPrefix = "Settings" - this.logger = LoggerFactory.getLogger(GitlabRepositoriesExtension.class) + GitlabRepositoriesExtension(Settings settings, ObjectFactory objects) { + this.objects = objects this.repositories = settings.pluginManagement.repositories + handler = new RepositoryActionHandler(this) setup() } - GitlabRepositoriesExtension(Project project) { - this.logPrefix = "Project ($project.name)" - this.logger = project.logger + GitlabRepositoriesExtension(Project project, ObjectFactory objects, GitlabRepositoriesExtension parent = null) { + this.objects = objects this.repositories = project.repositories - migrateSettingsTokens(project) - setup() + handler = new RepositoryActionHandler(this) + if (parent) { + this.baseUrl = parent.baseUrl + } + if (!migrateSettingsTokens(project)) { + setup() + } } - private void migrateSettingsTokens(Project project) { + private boolean migrateSettingsTokens(Project project) { if (project.extensions.extraProperties.has('gitLabTokens')) { def passedOnTokens = (project.extensions.extraProperties['gitLabTokens'] ?: [:]) as Map passedOnTokens.each { key, value -> - token(Class.forName(value.getClass().name) as Class) { - it.key = value['key'] - it.value = value['value'] - } + def token = (Class.forName(value.getClass().name) as Class).newInstance() + token.key = key + token.value = value['value'] + logger.info("$Config.LOG_PREFIX readding Token from Parent $token.name: $token.key") + tokens.put(token.key, token) } + return true } + return false } void setup() { - logger.info("$logPrefix initializing") + logger.info("$Config.LOG_PREFIX initializing") token(JobToken, { it.key = 'jobToken' it.value = System.getenv("CI_JOB_TOKEN") }) - afterPosition = repositories.indexOf(repositories.findByName(afterRepository)) } void token(Class tokenClass, Action action) { def token = tokenClass.newInstance(); action.execute(token) - logger.info("$logPrefix ${tokens.containsKey(token.key) ? "replaced" : "added"} $token.name: $token.key") + logger.info("$Config.LOG_PREFIX ${tokens.containsKey(token.key) ? "replaced" : "added"} $token.name: $token.key") tokens.put(token.key, token) } - void setAfterRepository(String afterRepository) { - this.afterRepository = afterRepository - afterPosition = repositories.indexOf(repositories.findByName(afterRepository)) + def upload(String id, Action configAction = null) { + RepositoryConfiguration repositoryConfiguration = generateRepositoryConfiguration(id, GitLabEntityType.PROJECT) + mavenInternal(repositoryConfiguration, configAction) } - MavenArtifactRepository upload(def delegate, String id, Action configAction = null) { - mavenInternal(new ProjectMavenConfig(id), configAction) { repo -> - delegate.maven(repo) - } + private RepositoryConfiguration generateRepositoryConfiguration(String id, GitLabEntityType entityType) { + RepositoryConfiguration repositoryConfiguration = objects.newInstance(RepositoryConfiguration.class, id, entityType) + repositoryConfiguration } /** * @deprecated use{@link #group(java.lang.String, org.gradle.api.Action)} or {@link #project(java.lang.String, org.gradle.api.Action)} */ @Deprecated - MavenArtifactRepository maven(String id, Action configAction = null) { + def maven(String id, Action configAction = null) { group(id, configAction) } - MavenArtifactRepository group(String id, Action configAction = null) { - mavenInternal(new GroupMavenConfig(id), configAction) + def group(String id, Action configAction = null) { + def repositoryConfiguration = generateRepositoryConfiguration(id, GitLabEntityType.GROUP) + mavenInternal(repositoryConfiguration, configAction) } - MavenArtifactRepository project(String id, Action configAction = null) { - - mavenInternal(new ProjectMavenConfig(id), configAction) + def project(String id, Action configAction = null) { + def repositoryConfiguration = generateRepositoryConfiguration(id, GitLabEntityType.PROJECT) + mavenInternal(repositoryConfiguration, configAction) } + def mavenInternal(RepositoryConfiguration repositoryConfiguration, + Action configAction = null) { - MavenArtifactRepository mavenInternal(MavenConfig mavenConfig, - Action configAction = null, - Closure action = null) { - - if (!mavenConfig.id) { - logger.info("$logPrefix: No ID provided nothing will happen here :)") - return null + if (!repositoryConfiguration.id) { + logger.info("$Config.LOG_PREFIX: No ID provided - project will be added anyways, but will not be used") } - configAction?.execute(mavenConfig) - - Map applicableTokens = getApplicableTokens(mavenConfig) - - def artifactRepo = generateMavenArtifactRepositoryAction( - mavenConfig, - applicableTokens) - - if (artifactRepo) - if (action) - action(artifactRepo) - else - applyMaven(artifactRepo) - } - - private MavenArtifactRepository applyMaven(Action artifactRepo) { - def repo = repositories.maven(artifactRepo) - - repositories.remove(repo) + configAction?.execute(repositoryConfiguration) - repositories.add(++afterPosition, repo) - repo - } + Action artifactRepo = handler.generate(repositoryConfiguration) - private Map getApplicableTokens(MavenConfig mavenConfig) { - Map applicableTokens = (mavenConfig.tokenSelectors ?: tokens.keySet()).collectEntries { - def token = tokens.get(it) - if (token) - [{ it }: token] - } - logger.debug("$logPrefix: Maven Repository with $mavenConfig.name will try to use following tokens ${applicableTokens.keySet()}") + artifactActionStorage.add artifactRepo - applicableTokens - } - - private Action generateMavenArtifactRepositoryAction(mavenConfig, Map tokens) { - Token token = tokens.values().find { token -> - token.value - } - if (token) { - logger.info("$logPrefix: Maven Repository $mavenConfig.name is using '${token.key}'") - def artifactRepo = new Action() { - @Override - void execute(MavenArtifactRepository mvn) { - mvn.url = mavenConfig.buildUrl(baseUrl) - mvn.name = mavenConfig.name - - mvn.credentials(HttpHeaderCredentials) { - it.name = token.name - it.value = token.value - } - mvn.authentication(new Action() { - @Override - void execute(AuthenticationContainer authentications) { - authentications.create('header', HttpHeaderAuthentication) - } - }) - } - } - - artifacts[mavenConfig] = artifactRepo - return artifactRepo - - } else { - logger.error("$logPrefix Maven Repository $mavenConfig.name was not added, as no token could be applied!\n\t" + - "\n\t" + - "#################################################################################### \n\t" + - "#################################################################################### \n\t" + - "#################################################################################### \n\t" + - "Currently you have configured following tokens, but non seem to resolve to an value: \n\t" + - "\t- ${mavenConfig.tokenSelectors.join("\n\t\t- ")} \n\t" + - "\n\t" + - " Please verify your configuration - Thank you! \n\t" + - "\n\t" + - "#################################################################################### \n\t" + - "#################################################################################### \n\t" + - "#################################################################################### \n\t" + - "") - return null - } + return artifactRepo } } \ No newline at end of file diff --git a/src/main/groovy/at/schrottner/gradle/GitlabRepositoriesPlugin.groovy b/src/main/groovy/at/schrottner/gradle/GitlabRepositoriesPlugin.groovy index 083244a..bdc4377 100644 --- a/src/main/groovy/at/schrottner/gradle/GitlabRepositoriesPlugin.groovy +++ b/src/main/groovy/at/schrottner/gradle/GitlabRepositoriesPlugin.groovy @@ -7,16 +7,23 @@ import org.gradle.api.DefaultTask import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.initialization.Settings +import org.gradle.api.model.ObjectFactory import org.gradle.api.plugins.ExtensionAware -import org.gradle.api.plugins.ExtraPropertiesExtension +import org.gradle.internal.extensibility.DefaultExtraPropertiesExtension + +import javax.inject.Inject class GitlabRepositoriesPlugin implements Plugin { - void apply(ExtensionAware extensionAware) { + ObjectFactory objects - extensionAware.extensions.extraProperties.set('DeployToken', DeployToken.class) - extensionAware.extensions.extraProperties.set('PrivateToken', PrivateToken.class) - extensionAware.extensions.extraProperties.set('JobToken', JobToken.class) + @Inject + GitlabRepositoriesPlugin(ObjectFactory objectFactory) { + this.objects = objectFactory + } + + void apply(ExtensionAware extensionAware) { + addProps(extensionAware) if (extensionAware instanceof Project) { apply(extensionAware) @@ -25,15 +32,34 @@ class GitlabRepositoriesPlugin implements Plugin { } } + void addProps(ExtensionAware extensionAware) { + extensionAware.extensions.extraProperties.set('DeployToken', DeployToken.class) + extensionAware.extensions.extraProperties.set('PrivateToken', PrivateToken.class) + extensionAware.extensions.extraProperties.set('JobToken', JobToken.class) + } + void apply(Settings extensionAware) { - GitlabRepositoriesExtension extension = extensionAware.extensions.create(GitlabRepositoriesExtension.NAME, GitlabRepositoriesExtension, (Settings) extensionAware) - extensionAware.gradle.beforeProject { project -> - if (extension.applyToProject) - applyProjects(project) - } - extensionAware.gradle.beforeProject { project -> - def ext = project.extensions.findByName(ExtraPropertiesExtension.EXTENSION_NAME) + GitlabRepositoriesExtension extension = extensionAware.extensions.create( + GitlabRepositoriesExtension.NAME, + GitlabRepositoriesExtension, + extensionAware, + objects + ) + + extensionAware.gradle.beforeProject { Project project -> + def ext = project.extensions.findByName(DefaultExtraPropertiesExtension.EXTENSION_NAME) ext.gitLabTokens = extension.tokens + if (extension.applyToProject) { + applyProjects(extension, project) + addProps(project) + project.extensions.create( + GitlabRepositoriesExtension.NAME, + GitlabRepositoriesExtension, + project, + objects, + extension + ) + } } } @@ -42,7 +68,8 @@ class GitlabRepositoriesPlugin implements Plugin { extensionAware.extensions.create( GitlabRepositoriesExtension.NAME, GitlabRepositoriesExtension, - (Project) extensionAware + extensionAware, + objects ) def task = extensionAware.tasks.maybeCreate('gitLabTask', DefaultTask) @@ -54,14 +81,8 @@ class GitlabRepositoriesPlugin implements Plugin { } } - private static void remapTokens(GitlabRepositoriesExtension extension, pExt) { - extension.tokens.each { - pExt.tokens.put it.key, it.value - } - } - - private void applyProjects(project) { - GitlabRepositoriesExtension.artifacts.each { key, value -> + private void applyProjects(extension, project) { + extension.artifactActionStorage.each { value -> project.repositories.maven value } } diff --git a/src/main/groovy/at/schrottner/gradle/LogHandler.groovy b/src/main/groovy/at/schrottner/gradle/LogHandler.groovy deleted file mode 100644 index 0fae350..0000000 --- a/src/main/groovy/at/schrottner/gradle/LogHandler.groovy +++ /dev/null @@ -1,17 +0,0 @@ -package at.schrottner.gradle - -import org.slf4j.LoggerFactory - -trait LogHandler { - public static final String LOG_PREFIX = "GitLab Repositories" - static org.slf4j.Logger logger - String logPrefix - - def getLogger() { - logger ?: LoggerFactory.getLogger(GitlabRepositoriesExtension.class) - } - - def getLogPrefix() { - "$LOG_PREFIX :: $logPrefix ::" - } -} \ No newline at end of file diff --git a/src/main/groovy/at/schrottner/gradle/MavenConfig.groovy b/src/main/groovy/at/schrottner/gradle/MavenConfig.groovy deleted file mode 100644 index 10af7fb..0000000 --- a/src/main/groovy/at/schrottner/gradle/MavenConfig.groovy +++ /dev/null @@ -1,30 +0,0 @@ -package at.schrottner.gradle; - -import java.util.Set; - -trait MavenConfig implements LogHandler { - public static final String REPOSITORY_PREFIX = "GitLab" - String tokenSelector - Set tokenSelectors - String name - String id - String endpoint - - String getName() { - name ? "$name" : "$REPOSITORY_PREFIX-${(endpoint - 's').capitalize()}-$id" - } - - Set getTokenSelectors() { - if (tokenSelector) { - logger.info("$logPrefix: Maven Repository $name is using Single Token Selector '$tokenSelector' - other tokens will be ignored") - - [tokenSelector].toSet() - } else { - tokenSelectors - } - } - - String buildUrl(String baseUrl) { - "https://$baseUrl/api/v4/$endpoint/$id/-/packages/maven" - } -} \ No newline at end of file diff --git a/src/main/groovy/at/schrottner/gradle/RepositoryActionHandler.groovy b/src/main/groovy/at/schrottner/gradle/RepositoryActionHandler.groovy new file mode 100644 index 0000000..1e63b6b --- /dev/null +++ b/src/main/groovy/at/schrottner/gradle/RepositoryActionHandler.groovy @@ -0,0 +1,119 @@ +package at.schrottner.gradle + +import at.schrottner.gradle.auths.Token +import org.gradle.api.Action +import org.gradle.api.artifacts.repositories.AuthenticationContainer +import org.gradle.api.artifacts.repositories.MavenArtifactRepository +import org.gradle.api.credentials.HttpHeaderCredentials +import org.gradle.authentication.http.HttpHeaderAuthentication +import org.slf4j.Logger +import org.slf4j.LoggerFactory + +class RepositoryActionHandler { + + public static final String REPOSITORY_PREFIX = "GitLab" + private static final Logger logger = LoggerFactory.getLogger(RepositoryActionHandler) + private String baseUrl + private Map tokens + + RepositoryActionHandler(GitlabRepositoriesExtension extension) { + this.baseUrl = extension.baseUrl + this.tokens = extension.tokens + } + + Action generate(RepositoryConfiguration repositoryConfiguration) { + Token token + Set tokenList + (token, tokenList) = computeTokenInformation(repositoryConfiguration) + + if (!token) { + handleInapplicableTokenCase(repositoryConfiguration, tokenList) + } else { + logger.info("${logPrefix(repositoryConfiguration)} is using '${token.key}' '${token.value}'") + } + Action artifactRepo = generateArtifactRepositoryAction(repositoryConfiguration, token) + return artifactRepo + } + + private List computeTokenInformation(RepositoryConfiguration repositoryConfiguration) { + Token token + Set tokenList + if (repositoryConfiguration.tokenSelector.getOrNull()) { + logger.info("${logPrefix(repositoryConfiguration)} is using Single Token Selector '${repositoryConfiguration.tokenSelector.get()}' " + + "- other tokens will be ignored") + + def t = tokens.get(repositoryConfiguration.tokenSelector.get()) + token = t?.value ? t : null + tokenList = [repositoryConfiguration.tokenSelector.get()] + } else if (repositoryConfiguration.tokenSelectors.getOrNull()) { + token = repositoryConfiguration.tokenSelectors.get().findResult { + def t = tokens.get(it) + return t?.value ? t : null + } + tokenList = repositoryConfiguration.tokenSelectors.get() + } else { + token = tokens.values().find { + it.value + } + tokenList = tokens.keySet() + } + [token, tokenList] + } + + private String logPrefix(RepositoryConfiguration repositoryConfiguration) { + "$Config.LOG_PREFIX Maven Repository ${buildName(repositoryConfiguration)}" + } + + private Action generateArtifactRepositoryAction(RepositoryConfiguration repositoryConfiguration, Token token) { + new Action() { + @Override + void execute(MavenArtifactRepository mvn) { + mvn.url = buildUrl(repositoryConfiguration) + mvn.name = buildName(repositoryConfiguration) + + mvn.credentials(HttpHeaderCredentials) { + it.name = token?.name + it.value = token?.value + } + mvn.authentication(new Action() { + @Override + void execute(AuthenticationContainer authentications) { + authentications.create('header', HttpHeaderAuthentication) + } + }) + } + } + } + + private String buildName(RepositoryConfiguration repositoryConfiguration) { + return repositoryConfiguration.name.getOrElse("$REPOSITORY_PREFIX-${repositoryConfiguration.entityType}-${repositoryConfiguration.id}") + } + + private String buildUrl(RepositoryConfiguration repositoryConfiguration) { + switch (repositoryConfiguration.entityType) { + case GitLabEntityType.PROJECT: + "https://$baseUrl/api/v4/${GitLabEntityType.PROJECT.endpoint}/${repositoryConfiguration.id}/packages/maven" + break + case Config.GROUP: + "https://$baseUrl/api/v4/${GitLabEntityType.GROUP.endpoint}/${repositoryConfiguration.id}/-/packages/maven" + break + } + } + + private void handleInapplicableTokenCase(RepositoryConfiguration repositoryConfiguration, Set applicableTokens) { + logger.error("${logPrefix(repositoryConfiguration)} was not added, as no token could be applied!\n\t" + + "\n\t" + + "#################################################################################### \n\t" + + "#################################################################################### \n\t" + + "#################################################################################### \n\t" + + "Currently you have configured following tokens, but non seem to resolve to an value: \n\t" + + "\t- ${applicableTokens.join("\n\t\t- ")} \n\t" + + "\n\t" + + " Please verify your configuration - Thank you! \n\t" + + "\n\t" + + "#################################################################################### \n\t" + + "#################################################################################### \n\t" + + "#################################################################################### \n\t" + + "") + } +} diff --git a/src/main/groovy/at/schrottner/gradle/RepositoryConfiguration.groovy b/src/main/groovy/at/schrottner/gradle/RepositoryConfiguration.groovy new file mode 100644 index 0000000..5d25f6f --- /dev/null +++ b/src/main/groovy/at/schrottner/gradle/RepositoryConfiguration.groovy @@ -0,0 +1,36 @@ +package at.schrottner.gradle + +import org.gradle.api.Action +import org.gradle.api.artifacts.repositories.RepositoryContentDescriptor +import org.gradle.api.internal.CollectionCallbackActionDecorator +import org.gradle.api.internal.artifacts.BaseRepositoryFactory +import org.gradle.api.internal.artifacts.dsl.DefaultRepositoryHandler +import org.gradle.api.provider.Property +import org.gradle.api.provider.SetProperty +import org.gradle.internal.reflect.Instantiator + +import javax.inject.Inject + +/** + * Representation of the actual configuration done within the gradle definition + * + * TODO: + * - add ContentFiltering + */ +abstract class RepositoryConfiguration { + String id + GitLabEntityType entityType + + @Inject + RepositoryConfiguration(String id, GitLabEntityType entityType) { + this.id = id + this.entityType = entityType + } + + abstract public Property getTokenSelector() + + abstract public SetProperty getTokenSelectors() + + abstract public Property getName() + +} \ No newline at end of file diff --git a/src/main/groovy/at/schrottner/gradle/auths/DeployToken.groovy b/src/main/groovy/at/schrottner/gradle/auths/DeployToken.groovy index ea6fc1a..75d4beb 100644 --- a/src/main/groovy/at/schrottner/gradle/auths/DeployToken.groovy +++ b/src/main/groovy/at/schrottner/gradle/auths/DeployToken.groovy @@ -1,3 +1,12 @@ +/* + * Copyright 2016-2021 the original author or authors. + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v2.0 which + * accompanies this distribution and is available at + * + * http://www.eclipse.org/legal/epl-v20.html + */ package at.schrottner.gradle.auths class DeployToken extends Token { diff --git a/src/main/groovy/at/schrottner/gradle/auths/JobToken.groovy b/src/main/groovy/at/schrottner/gradle/auths/JobToken.groovy index 1cd29d1..62bd79e 100644 --- a/src/main/groovy/at/schrottner/gradle/auths/JobToken.groovy +++ b/src/main/groovy/at/schrottner/gradle/auths/JobToken.groovy @@ -1,3 +1,12 @@ +/* + * Copyright 2016-2021 the original author or authors. + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v2.0 which + * accompanies this distribution and is available at + * + * http://www.eclipse.org/legal/epl-v20.html + */ package at.schrottner.gradle.auths class JobToken extends Token { diff --git a/src/main/groovy/at/schrottner/gradle/auths/PrivateToken.groovy b/src/main/groovy/at/schrottner/gradle/auths/PrivateToken.groovy index 6af4a33..5209e2e 100644 --- a/src/main/groovy/at/schrottner/gradle/auths/PrivateToken.groovy +++ b/src/main/groovy/at/schrottner/gradle/auths/PrivateToken.groovy @@ -1,3 +1,12 @@ +/* + * Copyright 2016-2021 the original author or authors. + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v2.0 which + * accompanies this distribution and is available at + * + * http://www.eclipse.org/legal/epl-v20.html + */ package at.schrottner.gradle.auths class PrivateToken extends Token { diff --git a/src/main/groovy/at/schrottner/gradle/auths/Token.groovy b/src/main/groovy/at/schrottner/gradle/auths/Token.groovy index 34750b0..8f44588 100644 --- a/src/main/groovy/at/schrottner/gradle/auths/Token.groovy +++ b/src/main/groovy/at/schrottner/gradle/auths/Token.groovy @@ -1,5 +1,18 @@ +/* + * Copyright 2016-2021 the original author or authors. + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v2.0 which + * accompanies this distribution and is available at + * + * http://www.eclipse.org/legal/epl-v20.html + */ package at.schrottner.gradle.auths +/** + * TODO: + * - rework tokens - this subclassing is ridiculous and should be reflected by a enum + */ public abstract class Token { String name String value diff --git a/src/main/groovy/at/schrottner/gradle/mavenConfigs/GroupMavenConfig.groovy b/src/main/groovy/at/schrottner/gradle/mavenConfigs/GroupMavenConfig.groovy deleted file mode 100644 index 4167c31..0000000 --- a/src/main/groovy/at/schrottner/gradle/mavenConfigs/GroupMavenConfig.groovy +++ /dev/null @@ -1,11 +0,0 @@ -package at.schrottner.gradle.mavenConfigs - -import at.schrottner.gradle.MavenConfig - -class GroupMavenConfig implements MavenConfig { - - GroupMavenConfig(String id) { - this.id = id - this.endpoint = "groups" - } -} \ No newline at end of file diff --git a/src/main/groovy/at/schrottner/gradle/mavenConfigs/ProjectMavenConfig.groovy b/src/main/groovy/at/schrottner/gradle/mavenConfigs/ProjectMavenConfig.groovy deleted file mode 100644 index ac7dff9..0000000 --- a/src/main/groovy/at/schrottner/gradle/mavenConfigs/ProjectMavenConfig.groovy +++ /dev/null @@ -1,16 +0,0 @@ -package at.schrottner.gradle.mavenConfigs - -import at.schrottner.gradle.MavenConfig - -class ProjectMavenConfig implements MavenConfig { - - ProjectMavenConfig(String id) { - this.id = id - this.endpoint = "projects" - } - - @Override - String buildUrl(String baseUrl) { - "https://$baseUrl/api/v4/$endpoint/$id/packages/maven" - } -} \ No newline at end of file