Skip to content

Commit

Permalink
Create release plugin (#48)
Browse files Browse the repository at this point in the history
Fixes #28
  • Loading branch information
philipp94831 authored Mar 4, 2024
1 parent bceceff commit a80994c
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 5 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
bakdata gradle plugins
======================

A collection of small gradle plugin, mostly focused on deployment.
A collection of small Gradle plugins, mostly focused on deployment.

**Sonar** Some defaults for easy integration of sonar on multi-module projects
**Sonatype** is used for uploading to sonatype repos and ultimately publish to Maven Central
- **Sonar** Some defaults for easy integration of sonar on multi-module projects
- **Sonatype** is used for uploading to sonatype repos and ultimately publish to Maven Central
- **Release** adds configurable push behavior for version bumping

## Development

Expand All @@ -25,9 +26,11 @@ buildscript {
dependencies {
classpath("com.bakdata.gradle:sonar:0.0.1-SNAPSHOT")
classpath("com.bakdata.gradle:sonatype:0.0.1-SNAPSHOT")
classpath("com.bakdata.gradle:release:0.0.1-SNAPSHOT")
}
}
apply(plugin = "com.bakdata.sonar")
apply(plugin = "com.bakdata.sonatype")
apply(plugin = "com.bakdata.release")
```
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
id("net.researchgate.release") version "3.0.2"
// eat your own dog food - apply the plugins to this plugin project
id("com.bakdata.sonar") version "1.1.17"
id("com.bakdata.sonatype") version "1.2.1"
id("com.bakdata.sonatype") version "1.2.2"
id("org.hildan.github.changelog") version "1.13.1"
id("org.gradle.kotlin.kotlin-dsl") version "2.1.6" apply false
id("com.gradle.plugin-publish") version "1.2.1" apply false
Expand Down
11 changes: 11 additions & 0 deletions release/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
plugins {
kotlin("jvm")
id("org.jetbrains.dokka") version "1.9.10"
}
apply(plugin = "org.gradle.kotlin.kotlin-dsl")

description = "Configures Gradle Release plugin for usage in CI"

dependencies {
implementation("net.researchgate", "gradle-release", "3.0.2")
}
63 changes: 63 additions & 0 deletions release/src/main/kotlin/com/bakdata/gradle/ReleasePlugin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* The MIT License
*
* Copyright (c) 2024 bakdata GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.bakdata.gradle

import net.researchgate.release.ReleaseExtension
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.configure

class ReleasePlugin : Plugin<Project> {

companion object {
const val DISABLE_PUSH_TO_REMOTE = "release.disablePushToRemote"
const val REQUIRE_BRANCH = "release.requireBranch"
}

override fun apply(rootProject: Project) {
if (rootProject.parent != null) {
throw GradleException("Apply this plugin only to the top-level project.")
}

with(rootProject) {
apply(plugin = "net.researchgate.release")

val disablePushToRemote: String? = project.findProperty(DISABLE_PUSH_TO_REMOTE) as? String
val branch: String? = project.findProperty(REQUIRE_BRANCH) as? String
configure<ReleaseExtension> {
git {
if (disablePushToRemote?.toBoolean() == true) {
pushToRemote.set(false)
}
branch?.also {
requireBranch.set(it)
}
}
}
}
}
}
124 changes: 124 additions & 0 deletions release/src/test/kotlin/com/bakdata/gradle/ReleasePluginTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* The MIT License
*
* Copyright (c) 2024 bakdata GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.bakdata.gradle

import com.bakdata.gradle.ReleasePlugin.Companion.DISABLE_PUSH_TO_REMOTE
import com.bakdata.gradle.ReleasePlugin.Companion.REQUIRE_BRANCH
import net.researchgate.release.ReleaseExtension
import net.researchgate.release.ReleasePlugin
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatCode
import org.assertj.core.api.Condition
import org.assertj.core.api.SoftAssertions
import org.gradle.api.Project
import org.gradle.api.internal.project.DefaultProject
import org.gradle.kotlin.dsl.extra
import org.gradle.kotlin.dsl.findByType
import org.gradle.testfixtures.ProjectBuilder
import org.junit.jupiter.api.Test

internal class ReleasePluginTest {

private fun Project.evaluate() {
(this as DefaultProject).evaluate()
}

@Test
fun testSingleModuleProject() {
val project = ProjectBuilder.builder().build()

assertThatCode {
project.pluginManager.apply("com.bakdata.release")
project.evaluate()
}.doesNotThrowAnyException()

SoftAssertions.assertSoftly { softly ->
softly.assertThat(project.plugins)
.haveExactly(1, Condition({ it is ReleasePlugin }, "Has release plugin"))
softly.assertThat(project.extensions.findByType<ReleaseExtension>()?.git)
.satisfies {
softly.assertThat(it?.pushToRemote?.get()).isEqualTo("origin")
softly.assertThat(it?.requireBranch?.get()).isEqualTo("main")
}
}
}

@Test
fun testDisablePushToRemote() {
val project = ProjectBuilder.builder().build()

assertThatCode {
project.extra.set(DISABLE_PUSH_TO_REMOTE, "false")
project.pluginManager.apply("com.bakdata.release")
project.evaluate()
}.doesNotThrowAnyException()

SoftAssertions.assertSoftly { softly ->
softly.assertThat(project.extensions.findByType<ReleaseExtension>()?.git?.pushToRemote?.get())
.isEqualTo("origin")
}
}

@Test
fun testEnablePushToRemote() {
val project = ProjectBuilder.builder().build()

assertThatCode {
project.extra.set(DISABLE_PUSH_TO_REMOTE, "true")
project.pluginManager.apply("com.bakdata.release")
project.evaluate()
}.doesNotThrowAnyException()

SoftAssertions.assertSoftly { softly ->
softly.assertThat(project.extensions.findByType<ReleaseExtension>()?.git?.pushToRemote?.get())
.isEqualTo(false)
}
}

@Test
fun testRequireBranch() {
val project = ProjectBuilder.builder().build()

assertThatCode {
project.extra.set(REQUIRE_BRANCH, "my-branch")
project.pluginManager.apply("com.bakdata.release")
project.evaluate()
}.doesNotThrowAnyException()

SoftAssertions.assertSoftly { softly ->
softly.assertThat(project.extensions.findByType<ReleaseExtension>()?.git?.requireBranch?.get())
.isEqualTo("my-branch")
}
}

@Test
fun testWrongApplicationInMultiModuleProject() {
val parent = ProjectBuilder.builder().withName("parent").build()
val child1 = ProjectBuilder.builder().withName("child1").withParent(parent).build()

assertThatCode { child1.pluginManager.apply("com.bakdata.release") }
.satisfies { assertThat(it.cause).hasMessageContaining("top-level project") }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
implementation-class=com.bakdata.gradle.ReleasePlugin
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ pluginManagement {

rootProject.name = 'gradle-plugins'

include 'sonar', 'sonatype'
include 'sonar', 'sonatype', 'release'

0 comments on commit a80994c

Please sign in to comment.