Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,15 @@ This plugin is split into two: one for `settings` and the other for `project`. D
}

plugins {
id 'io.github.adelinosousa.gradle.plugins.settings.gh-cli-auth' version '1.0.5'
id 'io.github.adelinosousa.gradle.plugins.settings.gh-cli-auth' version '1.1.0'
}
```

If you need to consume the token in your own settings plugin, you can access it via gradle extra properties:

```shell
if (settings.gradle.extra.has("gh-cli-auth-token")) {
val ghToken = settings.gradle.extra["gh-cli-auth-token"] as String
}
```

Expand All @@ -54,11 +62,11 @@ This plugin is split into two: one for `settings` and the other for `project`. D
# build.gradle

plugins {
id 'io.github.adelinosousa.gradle.plugins.project.gh-cli-auth' version '1.0.5'
id 'io.github.adelinosousa.gradle.plugins.project.gh-cli-auth' version '1.1.0'
}
```

This plugin also exposes a `ghCliAuth` extension to access the token, if needed:
This plugin exposes a `ghCliAuth` extension to access the token, if needed:

```shell
# build.gradle
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
org.gradle.configuration-cache=true
gradle.publish.version=1.0.6
gradle.publish.version=1.1.0
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,30 @@ class GhCliAuthProjectPluginFunctionalTest {
.withGradleVersion("8.14.2")
.build()

// Verify the result
assertTrue(result.output.contains("Registering Maven GitHub repository for organization: test-org"))
}

@Test fun `runs plugin with custom env variable name`() {
settingsFile.writeText("")
buildFile.writeText("""
plugins {
id('io.github.adelinosousa.gradle.plugins.project.gh-cli-auth')
}
""".trimIndent())
propertiesFile.writeText("""
gh.cli.auth.github.org=test-org
gh.cli.auth.env.name=CUSTOM_ENV_VAR
""".trimIndent())

val result = GradleRunner.create()
.forwardOutput()
.withPluginClasspath()
.withProjectDir(projectDir)
.withArguments("--stacktrace", "--info")
.withGradleVersion("8.14.2")
.withEnvironment(mapOf("CUSTOM_ENV_VAR" to "ghp_exampletoken1234567890"))
.build()

assertTrue(result.output.contains("Registering Maven GitHub repository for organization: test-org"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class GhCliAuthSettingsPluginFunctionalTest {
.withGradleVersion("8.14.2")
.build()

// Verify the result
assertTrue(result.output.contains("Registering Maven GitHub repository for organization: test-org"))
}

Expand Down Expand Up @@ -68,7 +67,6 @@ class GhCliAuthSettingsPluginFunctionalTest {
.withGradleVersion("8.14.2")
.build()

// Verify the result
assertTrue(result.output.contains("Adding Google repository"))
}

Expand Down Expand Up @@ -104,7 +102,70 @@ class GhCliAuthSettingsPluginFunctionalTest {
.withGradleVersion("8.14.2")
.build()

// Verify the result
assertTrue(result.output.contains("Adding Gradle Plugin Portal repository"))
}

@Test fun `runs plugin with custom env variable name`() {
settingsFile.writeText(
"""
plugins {
id('io.github.adelinosousa.gradle.plugins.settings.gh-cli-auth')
}
""".trimIndent()
)

buildFile.writeText("")
propertiesFile.writeText("""
gh.cli.auth.github.org=test-org
gh.cli.auth.env.name=CUSTOM_ENV_VAR
""".trimIndent())

val result = GradleRunner.create()
.forwardOutput()
.withPluginClasspath()
.withProjectDir(projectDir)
.withArguments("--stacktrace", "--info")
.withGradleVersion("8.14.2")
.withEnvironment(mapOf("CUSTOM_ENV_VAR" to "ghp_exampletoken1234567890"))
.build()

assertTrue(result.output.contains("Registering Maven GitHub repository for organization: test-org"))
}

@Test fun `plugin shares token with other settings plugins`() {
val tokenName = """gh-cli-auth-token"""
val expectedToken = "ghp_exampletoken1234567890"

settingsFile.writeText(
"""
plugins {
id('io.github.adelinosousa.gradle.plugins.settings.gh-cli-auth')
}

// a simple log to verify that the token is accessible from other setting plugins
gradle.settingsEvaluated {
def ghToken = gradle.ext.get("$tokenName")
println("$tokenName: ${'$'}ghToken")
}
""".trimIndent()
)

buildFile.writeText("")

propertiesFile.writeText("""
gh.cli.auth.github.org=test-org
gh.cli.auth.env.name=CUSTOM_ENV_VAR
""".trimIndent())

val result = GradleRunner.create()
.forwardOutput()
.withPluginClasspath()
.withProjectDir(projectDir)
.withArguments("--stacktrace", "--info")
.withGradleVersion("8.14.2")
.withEnvironment(mapOf("CUSTOM_ENV_VAR" to expectedToken))
.build()

assertTrue(result.output.contains("$tokenName: $expectedToken"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package io.github.adelinosousa.gradle.plugins
internal object Config {
internal const val GITHUB_ORG: String = "gh.cli.auth.github.org"
internal const val ENV_PROPERTY_NAME: String = "gh.cli.auth.env.name"
internal const val EXTRA_TOKEN_NAME: String = "gh-cli-auth-token"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package io.github.adelinosousa.gradle.plugins

import org.gradle.api.Plugin
import org.gradle.api.artifacts.dsl.RepositoryHandler
import org.gradle.api.artifacts.repositories.MavenArtifactRepository
import org.gradle.api.initialization.Settings
import org.gradle.api.logging.Logging
import org.gradle.kotlin.dsl.extra
import java.net.URI

internal class GhCliAuthSettingsPlugin : Plugin<Settings> {
Expand All @@ -22,6 +22,8 @@ internal class GhCliAuthSettingsPlugin : Plugin<Settings> {

val repoCredentials = Environment.getEnvCredentials(gitEnvTokenName) ?: getGhCliCredentials(settings)
if (repoCredentials.isValid()) {
// Set the token to share with other settings plugins
settings.gradle.extra.set(Config.EXTRA_TOKEN_NAME, repoCredentials.token)
settings.pluginManagement.repositories.addRepositoriesWithDefaults(githubOrg, repoCredentials)
settings.dependencyResolutionManagement.repositories.addRepositoriesWithDefaults(githubOrg, repoCredentials)
} else {
Expand Down