Skip to content

Commit

Permalink
ci: Validate Maven version on publish (#1949)
Browse files Browse the repository at this point in the history
* ci: Validate Gradle version on publish

* use composite build plugin to validate version
  • Loading branch information
piotradamczyk5 authored May 24, 2021
1 parent 60dd609 commit 32244bf
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 3 deletions.
2 changes: 1 addition & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
repositories {
jcenter()
mavenCentral()
}

plugins {
Expand Down
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/Plugins.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ object Plugins {
const val MAVEN_PUBLISH = "maven-publish"
const val NEXUS_STAGING = "io.codearte.nexus-staging"
const val BEN_MANES_PLUGIN = "com.github.ben-manes.versions"
const val MAVEN_VERSION_CHECK = "maven-version-check"
object Kotlin {
const val PLUGIN_JVM = "jvm"
const val PLUGIN_SERIALIZATION = "plugin.serialization"
Expand Down
20 changes: 20 additions & 0 deletions maven_version_check/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
repositories {
mavenCentral()
}

plugins {
`kotlin-dsl`
`java-gradle-plugin`
}

gradlePlugin {
plugins.register("maven-version-check") {
id = "maven-version-check"
implementationClass = "com.github.flank.gradle.MavenVersionCheck"
}
}

dependencies {
testImplementation("junit:junit:4.13.2") // unfortunately it does not work with buildSrc Dependencies
}

1 change: 1 addition & 0 deletions maven_version_check/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = "mavenVersionCheck"
27 changes: 27 additions & 0 deletions maven_version_check/src/main/kotlin/MavenVersionCheck.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.github.flank.gradle

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.PublishException
import org.gradle.api.publish.maven.tasks.PublishToMavenRepository
import org.gradle.internal.impldep.org.jetbrains.annotations.VisibleForTesting
import org.gradle.kotlin.dsl.withType

class MavenVersionCheck : Plugin<Project> {
override fun apply(project: Project) {
project.tasks.withType<PublishToMavenRepository>().configureEach {
doFirst {
assertValidVersion(publication.version)
}
}
}

@VisibleForTesting
internal fun assertValidVersion(version: String) {
val validSnapshots = arrayOf("master-SNAPSHOT", "local-SNAPSHOT")
val versionRegex = "2\\d\\.\\d{2}\\.\\d{1,2}".toRegex()
if (version !in validSnapshots && !versionRegex.matches(version)) {
throw PublishException("Maven version is not valid!")
}
}
}
40 changes: 40 additions & 0 deletions maven_version_check/src/test/kotlin/MavenVersionCheckTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.github.flank.gradle

import org.junit.Assert.assertTrue
import org.junit.Test

class MavenVersionCheckTest {

@Test
fun `Should properly validate maven version for snapshots`() {
with(MavenVersionCheck()) {
assertValidVersion("master-SNAPSHOT")
assertValidVersion("local-SNAPSHOT")
}
}

@Test
fun `Should properly validate maven version for not snapshots`() {
with(MavenVersionCheck()) {
assertValidVersion("21.02.0")
assertValidVersion("22.01.1")
assertValidVersion("23.11.11")
}
}

@Test
fun `Should throw exception for not proper maven version`() {
with(MavenVersionCheck()) {
assertTrue(runCatching { assertValidVersion("master_SNAPSHOT") }.isFailure)
assertTrue(runCatching { assertValidVersion("local_version") }.isFailure)
assertTrue(runCatching { assertValidVersion("version") }.isFailure)
assertTrue(runCatching { assertValidVersion("test") }.isFailure)
assertTrue(runCatching { assertValidVersion("snapshot") }.isFailure)
assertTrue(runCatching { assertValidVersion("2113") }.isFailure)
assertTrue(runCatching { assertValidVersion("2020.12.21") }.isFailure)
assertTrue(runCatching { assertValidVersion("21.2.1") }.isFailure)
assertTrue(runCatching { assertValidVersion("v21.02.01") }.isFailure)
assertTrue(runCatching { assertValidVersion("19.02.1") }.isFailure)
}
}
}
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import org.gradle.kotlin.dsl.support.serviceOf

rootProject.name = "flank"

includeBuild("maven_version_check")

include(
":test_runner",
":firebase_apis:test_api",
Expand Down
3 changes: 1 addition & 2 deletions test_runner/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import groovy.util.Node
import groovy.util.NodeList
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.io.ByteArrayOutputStream
import java.nio.file.Paths

plugins {
application
Expand All @@ -14,6 +12,7 @@ plugins {
id(Plugins.MAVEN_PUBLISH)
signing
id(Plugins.PLUGIN_SHADOW_JAR) version Versions.SHADOW
id(Plugins.MAVEN_VERSION_CHECK)
}

val artifactID = "flank"
Expand Down

0 comments on commit 32244bf

Please sign in to comment.