Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail if invalid spdx is passed to allow #314

Merged
merged 3 commits into from
Mar 29, 2024
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
37 changes: 31 additions & 6 deletions src/main/kotlin/app/cash/licensee/pluginExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ package app.cash.licensee
import app.cash.licensee.LicenseeExtension.AllowDependencyOptions
import app.cash.licensee.LicenseeExtension.IgnoreDependencyOptions
import java.util.Optional
import javax.inject.Inject
import org.gradle.api.Action
import org.gradle.api.Named
import org.gradle.api.NamedDomainObjectCollection
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.artifacts.Dependency
import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.provider.SetProperty
import org.gradle.api.provider.ProviderFactory

@Suppress("unused") // Public API for Gradle build scripts.
interface LicenseeExtension {
Expand Down Expand Up @@ -245,8 +247,19 @@ internal abstract class IgnoredCoordinate : Named {
abstract val ignoredDatas: MapProperty<String, IgnoredData>
}

internal abstract class MutableLicenseeExtension : LicenseeExtension {
internal abstract val allowedIdentifiers: SetProperty<String>
internal abstract class SpdxId(private val spdxId: String) : Named {
override fun getName(): String = spdxId
init {
requireNotNull(SpdxLicenses.embedded.findByIdentifier(spdxId)) {
"$name is not a valid SPDX id."
}
Comment on lines +253 to +255
Copy link
Collaborator

@JakeWharton JakeWharton Mar 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this could have been put in the old allow before adding to the set to accomplish the same thing. Is there another advantage of all the other changes that I'm missing? Is it secretly working towards #313?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it secretly working towards #313?

Yes, this is the only reason. As you can see in the commits I added the provider overload for allow but realized all functions could benefit of Providers so I removed it here.
We could also add the provider overload back and add more in #313.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer them separate, but we can leave the infrastructure in place here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW I don't plan to implement #313 today and it needs some design first, in case you want to publish another release.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do

}
}

internal abstract class MutableLicenseeExtension @Inject constructor(
private val providers: ProviderFactory,
) : LicenseeExtension {
internal abstract val allowedIdentifiers: NamedDomainObjectContainer<SpdxId>
internal abstract val allowedUrls: MapProperty<String, Optional<String>>
internal abstract val allowedDependencies: MapProperty<DependencyCoordinates, Optional<String>>
internal abstract val ignoredGroupIds: MapProperty<String, IgnoredData>
Expand All @@ -273,9 +286,11 @@ internal abstract class MutableLicenseeExtension : LicenseeExtension {
}

fun toLicenseValidationConfig(): Provider<ValidationConfig> {
return allowedIdentifiers.zip(allowedUrls, allowedDependencies) { allowedIdentifiers, allowedUrls, allowedDependencies ->
return allowedIdentifiers.elements(providers) {
it.name
}.zip(allowedUrls, allowedDependencies) { allowedIdentifiers, allowedUrls, allowedDependencies ->
ValidationConfig(
allowedIdentifiers.toSet(),
allowedIdentifiers,
allowedUrls.mapValues {
it.value.orElse(null)
},
Expand All @@ -287,7 +302,7 @@ internal abstract class MutableLicenseeExtension : LicenseeExtension {
}

override fun allow(spdxId: String) {
allowedIdentifiers.add(spdxId)
allowedIdentifiers.register(spdxId)
}

override fun allowUrl(url: String, options: Action<LicenseeExtension.AllowUrlOptions>) {
Expand Down Expand Up @@ -397,3 +412,13 @@ private fun <T> NamedDomainObjectContainer<T>.configure(name: String, config: Ac
register(name, config)
}
}

// https://github.com/gradle/gradle/issues/28043
inline fun <reified T : Any, R> NamedDomainObjectCollection<T>.elements(
providers: ProviderFactory,
crossinline transform: (T) -> R,
): Provider<out Set<R>> {
return providers.provider {
mapTo(mutableSetOf(), transform)
}
}
8 changes: 8 additions & 0 deletions src/test/fixtures/allow-with-invalid-spdx/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
plugins {
id("java-library")
alias(libs.plugins.licensee)
}

licensee {
allow("ASDF")
}
7 changes: 7 additions & 0 deletions src/test/fixtures/allow-with-invalid-spdx/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pluginManagement {
includeBuild("../../test-build-logic")
}

plugins {
id("licenseeTests")
}
10 changes: 10 additions & 0 deletions src/test/kotlin/app/cash/licensee/LicenseePluginFixtureTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,16 @@ class LicenseePluginFixtureTest {
)
}

@Test fun invalidSpdxFails(
@TestParameter("allow-with-invalid-spdx") fixtureName: String,
) {
val fixtureDir = File(fixturesDir, fixtureName)
val result = createRunner(fixtureDir).buildAndFail()
assertThat(result.output).contains(
"ASDF is not a valid SPDX id.",
)
}

@Test fun pluginMissingOnRootFails(
@TestParameter("plugin-missing-on-root-fails") fixtureName: String,
) {
Expand Down