Skip to content

Commit

Permalink
Gradle Kotlin DSL Repository Resolution Fix
Browse files Browse the repository at this point in the history
The Kotlin DSL had issues with fetching and uploading dependencies,
within #14 we provided a workaround. During the investigation we
realized that the whole repository resolution was buggy for Kotlin DSL.

With this changes we are adding tests for fetching dependencies,
as well as for uploading.
We achieved this by implementing the Action interface directly within the RepositoryActionHandler.
Furthermore we removed the deprecated `upload` method with the `owner`.

Closes: #14, #15
  • Loading branch information
Simon Schrottner committed Oct 11, 2021
1 parent a2580e8 commit 5eb3635
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 107 deletions.
48 changes: 15 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,49 +171,31 @@ Be aware that this has to be a projectId - you are not able to upload to groups!

#### Groovy DSL

For adding a repository to the maven-publish repositories please use following methods.

- with an `Owner` and without the need to manually passing the return value to the `maven`-configuration. The `owner`
represents a reference/delegate to the repositories object, hence that we will apply the configuration automatically.

```groovy
publishing {
repositories {
gitLab.upload(owner, projectId)
gitLab.upload(owner, projectId) {
name = "custom name"
tokenSelektor = "" // a name of a configured token
tokenSelectors = [] // a list of configured tokens, which will be checked based on their order in this set
}
}
}
```
- without an `Owner` and passing the value to the `maven`-configuration
```groovy
publishing {
repositories {
maven gitLab.upload(projectId)
maven gitLab.upload(projectId) {
name = "custom name"
tokenSelektor = "" // a name of a configured token
tokenSelectors = [] // a list of configured tokens, which will be checked based on their order in this set
}
For adding a repository to the maven-publish repositories please use following method.

```groovy
publishing {
repositories {
maven gitLab.upload(projectId)
maven gitLab.upload(projectId) {
name = "custom name"
tokenSelektor = "" // a name of a configured token
tokenSelectors = [] // a list of configured tokens, which will be checked based on their order in this set
}
}
```
}
```

#### Kotlin DSL

```kotlin
publishing {
repositories {
val gitLab = the<GitlabRepositoriesExtension>()
gitLab.upload(this, projectId) {
maven(gitLab.upload(projectId) {
name.set("GitLab")
tokenSelector.set("testToken")
}
})
}
}
```
Expand Down Expand Up @@ -280,7 +262,7 @@ repositories {
publishing {
repositories {
maven gitLab.upload(owner, "ID")
maven gitLab.upload("ID")
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ class GitlabRepositoriesPluginFunctionalKotlinTests {
)
void "uploadTest"() {
def testFile = TestFileUtils.getTestResource(new File(projectDir, 'test.xml'), 'test.xml')
// TODO: INSPECT why this is throwing a strange error when applied. some asserts are deactivated due to this.
//settingsGradle = TestFileUtils.getTestResource(new File(projectDir, SETTINGS_GRADLE), SETTINGS_GRADLE)
settingsGradle = TestFileUtils.getTestResource(new File(projectDir, SETTINGS_GRADLE), SETTINGS_GRADLE)
buildGradle = TestFileUtils.getTestResource(new File(projectDir, BUILD_GRADLE), 'build-upload.gradle.kts')

def uploadResult = runTest("publishTestPublicationToGitLabRepository", "-i", "-s")
Expand All @@ -153,18 +152,18 @@ class GitlabRepositoriesPluginFunctionalKotlinTests {
.contains("BUILD SUCCESSFUL")
.containsSubsequence(
"added Job-Token: jobToken",
// "added Private-Token: tokenIgnoredNoValue",
// "added Deploy-Token: token0",
// "added Deploy-Token: token1",
// "Settings evaluated",
"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 $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'",
)
.doesNotContain("Maven Repository $repoPrefix-ignoredNoValue is using '")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ 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
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable
import org.junit.jupiter.api.io.TempDir
import org.slf4j.Logger
import org.slf4j.LoggerFactory

@Disabled
class GitlabRepositoriesPluginFunctionalTests {

private static final Logger logger = LoggerFactory.getLogger(GitlabRepositoriesPluginFunctionalTests.class)
Expand Down
14 changes: 7 additions & 7 deletions src/functionalTest/resources/build-upload.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ publishing {
repositories {
val existingId: String by project
val gitLab = the<GitlabRepositoriesExtension>()
// maven(gitLab.upload("$existingId"))
// maven(gitLab.upload("specialToken") { tokenSelector.set("token0") })
// maven(gitLab.upload("specialToken1") { tokenSelector.set("token1") })
// maven(gitLab.upload("specialTokenSelection") { tokenSelectors.addAll("jobToken", "token1") })
// maven(gitLab.upload("ignoredNoValue") { tokenSelector.set("tokenIgnoredNoValue") })
gitLab.upload(this, "24974077") {
maven(gitLab.upload("$existingId"))
maven(gitLab.upload("specialToken") { tokenSelector.set("token0") })
maven(gitLab.upload("specialToken1") { tokenSelector.set("token1") })
maven(gitLab.upload("specialTokenSelection") { tokenSelectors.addAll("jobToken", "token1") })
maven(gitLab.upload("ignoredNoValue") { tokenSelector.set("tokenIgnoredNoValue") })
maven(gitLab.upload("24974077") {
name.set("GitLab")
tokenSelector.set("testToken")
}
})
}

publications {
Expand Down
8 changes: 8 additions & 0 deletions src/functionalTest/resources/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,11 @@ repositories {
}
}
}

configurations {
testing
}

dependencies {
testing "at.schrottner.test.gitlab-repositories:test-file:test-SNAPSHOT@xml"
}
12 changes: 12 additions & 0 deletions src/functionalTest/resources/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import at.schrottner.gradle.auths.*
import at.schrottner.gradle.*
import org.gradle.api.artifacts.repositories.MavenArtifactRepository

buildscript {
val pluginClasspath: String by project
Expand Down Expand Up @@ -27,6 +28,10 @@ configure<GitlabRepositoriesExtension> {
key = "tokenAdded"
value = "test"
})
token(PrivateToken::class.javaObjectType, {
key = "downloadToken"
value = System.getenv("TEST_UPLOAD_TOKEN")
})
}

repositories {
Expand All @@ -35,6 +40,7 @@ repositories {
val renamedId: String by project
val gitLab = the<GitlabRepositoriesExtension>()

maven(gitLab.project("24974077") { tokenSelector.set("downloadToken") })
maven(gitLab.group("$existingId"))
maven(gitLab.project("$existingId"))
maven(gitLab.group("$renamedId") { name.set("group-renamed") })
Expand All @@ -48,3 +54,9 @@ repositories {
maven(gitLab.group("ignoredNoValue") { tokenSelector.set("tokenIgnoredNoValue") })
maven(gitLab.project("ignoredNoValue") { tokenSelector.set("tokenIgnoredNoValue") })
}

val testing by configurations.creating

dependencies {
testing("at.schrottner.test.gitlab-repositories:test-file:test-SNAPSHOT@xml")
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,22 @@ import org.gradle.api.Project
import org.gradle.api.artifacts.dsl.RepositoryHandler
import org.gradle.api.artifacts.repositories.MavenArtifactRepository
import org.gradle.api.initialization.Settings
import org.gradle.api.internal.artifacts.dsl.DefaultRepositoryHandler
import org.gradle.api.model.ObjectFactory
import org.slf4j.Logger
import org.slf4j.LoggerFactory


/**
* 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 ObjectFactory objects
private final RepositoryActionHandler handler

String baseUrl = "gitlab.com"
boolean applyToProject = true
Expand All @@ -35,13 +33,11 @@ class GitlabRepositoriesExtension {

GitlabRepositoriesExtension(Settings settings, ObjectFactory objects) {
this.objects = objects
handler = new RepositoryActionHandler(this)
setup()
}

GitlabRepositoriesExtension(Project project, ObjectFactory objects, GitlabRepositoriesExtension parent = null) {
this.objects = objects
handler = new RepositoryActionHandler(this)
if (parent) {
this.baseUrl = parent.baseUrl
}
Expand Down Expand Up @@ -87,20 +83,11 @@ class GitlabRepositoriesExtension {
* available during CI builds etc. and this might cause on wanted side-effects if it is not resolving to a
* usable endpoint
*
*
* @param delegate
* @param id
* @param configAction
* @return
*/
def upload(def delegate, String projectId, Action<? super RepositoryConfiguration> configAction = null) {
def internal = upload(projectId, configAction)
if (projectId)
delegate?.maven(internal)
internal
}

def upload(String projectId, Action<? super RepositoryConfiguration> configAction = null) {
Action<MavenArtifactRepository> upload(String projectId, Action<? super RepositoryConfiguration> configAction = null) {
RepositoryConfiguration repositoryConfiguration = generateRepositoryConfiguration(projectId, GitLabEntityType.PROJECT)
mavenInternal(repositoryConfiguration, configAction)
}
Expand All @@ -115,21 +102,21 @@ class GitlabRepositoriesExtension {
* @deprecated use{@link #group(java.lang.String, org.gradle.api.Action)} or {@link #project(java.lang.String, org.gradle.api.Action)}
*/
@Deprecated
def maven(String id, Action<? super RepositoryConfiguration> configAction = null) {
Action<MavenArtifactRepository> maven(String id, Action<? super RepositoryConfiguration> configAction = null) {
group(id, configAction)
}

def group(String id, Action<? super RepositoryConfiguration> configAction = null) {
def repositoryConfiguration = generateRepositoryConfiguration(id, GitLabEntityType.GROUP)
Action<MavenArtifactRepository> group(String id, Action<? super RepositoryConfiguration> configAction = null) {
RepositoryConfiguration repositoryConfiguration = generateRepositoryConfiguration(id, GitLabEntityType.GROUP)
mavenInternal(repositoryConfiguration, configAction)
}

def project(String id, Action<? super RepositoryConfiguration> configAction = null) {
def repositoryConfiguration = generateRepositoryConfiguration(id, GitLabEntityType.PROJECT)
Action<MavenArtifactRepository> project(String id, Action<? super RepositoryConfiguration> configAction = null) {
RepositoryConfiguration repositoryConfiguration = generateRepositoryConfiguration(id, GitLabEntityType.PROJECT)
mavenInternal(repositoryConfiguration, configAction)
}

def mavenInternal(RepositoryConfiguration repositoryConfiguration,
Action<MavenArtifactRepository> mavenInternal(RepositoryConfiguration repositoryConfiguration,
Action<? super RepositoryConfiguration> configAction = null) {

if (!repositoryConfiguration.id) {
Expand All @@ -138,7 +125,7 @@ class GitlabRepositoriesExtension {

configAction?.execute(repositoryConfiguration)

Action<MavenArtifactRepository> artifactRepo = handler.generate(repositoryConfiguration)
Action<MavenArtifactRepository> artifactRepo = new RepositoryActionHandler(this, repositoryConfiguration)

artifactActionStorage.add artifactRepo

Expand Down
Loading

0 comments on commit 5eb3635

Please sign in to comment.