Skip to content

Commit

Permalink
Develop (#10)
Browse files Browse the repository at this point in the history
* fix: update build configuration, code issues fixed and container version fixed
* build: Add latest gradle version for test
* chore: Fix plugin tags for gradle plugin publishing
* feature: change behaviour for buildID
  • Loading branch information
m-raab authored Apr 18, 2023
1 parent 8686837 commit 9cb6cdd
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 58 deletions.
12 changes: 9 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ jobs:
runs-on: ubuntu-latest

steps:
- name: Set up JDK 1.8
uses: actions/setup-java@v1
- name: Set up JDK 8
uses: actions/setup-java@v3
with:
java-version: 1.8
java-version: 8
distribution: temurin
- uses: actions/checkout@v2
with:
fetch-depth: 0
Expand Down Expand Up @@ -53,5 +54,10 @@ jobs:
JAVA_OPTS: "-Xmx1024M -XX:MaxPermSize=512M -XX:ReservedCodeCacheSize=512M"
GRADLE_OPTS: "-Dorg.gradle.daemon=true"
run: ./gradlew test build :publishIntershopMvnPublicationToMavenRepository -s --scan
- name: Publish Unit Test Results
uses: EnricoMi/publish-unit-test-result-action/composite@v2
if: always()
with:
files: build/test-results/**/*.xml
- name: Post Build
run: rm -f $HOME/.gradle/gradle.properties && rm -f $HOME/.docker/config.json
12 changes: 9 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ jobs:
runs-on: ubuntu-latest

steps:
- name: Set up JDK 1.8
uses: actions/setup-java@v1
- name: Set up JDK
uses: actions/setup-java@v3
with:
java-version: 1.8
java-version: 8
distribution: temurin
- uses: actions/checkout@v2
with:
fetch-depth: 0
Expand Down Expand Up @@ -52,5 +53,10 @@ jobs:
JAVA_OPTS: "-Xmx1024M -XX:MaxPermSize=512M -XX:ReservedCodeCacheSize=512M"
GRADLE_OPTS: "-Dorg.gradle.daemon=true"
run: ./gradlew -PrunOnCI=true test build :publishIntershopMvnPublicationToMavenRepository :publishPlugins -s --scan
- name: Publish Unit Test Results
uses: EnricoMi/publish-unit-test-result-action/composite@v2
if: always()
with:
files: build/test-results/**/*.xml
- name: Post Build
run: rm -f $HOME/.gradle/gradle.properties
9 changes: 5 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ plugins {
id("io.gitlab.arturbosch.detekt") version "1.21.0"

// plugin for publishing to Gradle Portal
id("com.gradle.plugin-publish") version "1.0.0"

id("com.gradle.plugin-publish") version "1.2.0"
}

// release configuration
group = "com.intershop.gradle.version"
description = "Gradle SCM version plugin - SCM based version handling for Gradle"
version = "1.7.5"
version = "1.8.0"


val sonatypeUsername: String by project
val sonatypePassword: String? by project
Expand Down Expand Up @@ -89,8 +91,7 @@ detekt {
tasks {
withType<Test>().configureEach {
maxParallelForks = 1

systemProperty("intershop.gradle.versions", "7.2,7.5.1")
systemProperty("intershop.gradle.versions", "7.5.1")
useJUnitPlatform()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ open class GitFlowVersionPlugin: Plugin<Project> {

val ext = extensions.findByType(
VersionExtension::class.java
) ?: extensions.create(VERSION_EXTENSION_NAME, VersionExtension::class.java)
) ?: extensions.create(VERSION_EXTENSION_NAME, VersionExtension::class.java, this)

tasks.register("showVersion", ShowVersion::class.java)
tasks.register("createChangeLog", CreateChangeLog::class.java) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.intershop.gradle.gitflow.extension
import com.intershop.gradle.gitflow.utils.GitVersionService
import com.intershop.release.version.Version
import com.intershop.release.version.VersionType
import org.gradle.api.Project
import org.gradle.api.file.ProjectLayout
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Provider
Expand All @@ -31,7 +32,8 @@ import javax.inject.Inject
* @param layout directory layout
* @param providerFactory provider factory for gradle
*/
open class VersionExtension @Inject constructor(objectFactory: ObjectFactory,
open class VersionExtension @Inject constructor(project: Project,
objectFactory: ObjectFactory,
layout: ProjectLayout,
val providerFactory: ProviderFactory ) {

Expand All @@ -54,10 +56,12 @@ open class VersionExtension @Inject constructor(objectFactory: ObjectFactory,
private val fullbranchProperty = objectFactory.property(Boolean::class.java)

private val versionProperty = objectFactory.property(String::class.java)
private val versionWithIDProperty = objectFactory.property(String::class.java)
private val branchProperty = objectFactory.property(String::class.java)
private val previousVersionProperty = objectFactory.property(String::class.java)
private val containerVersionProperty = objectFactory.property(String::class.java)


init {
defaultVersionProperty.convention("1.0.0")
mainBranchProperty.convention("master")
Expand All @@ -71,6 +75,7 @@ open class VersionExtension @Inject constructor(objectFactory: ObjectFactory,
fullbranchProperty.convention(false)

versionProperty.convention("")
versionWithIDProperty.convention("")
previousVersionProperty.convention("")
containerVersionProperty.convention("")
branchProperty.convention("")
Expand Down Expand Up @@ -236,10 +241,6 @@ open class VersionExtension @Inject constructor(objectFactory: ObjectFactory,
getValueFor("MERGE_BUILD", "mergeBuild", "false")
.lowercase(Locale.getDefault()) == "true"

versionService.isUniqueVersion =
getValueFor("UNIQUE_VERSION", "uniqueVersion", "false")
.lowercase(Locale.getDefault()) == "true"

versionService.sourceBranch = getValueFor("PR_SOURCE_BRANCH", "sourceBranch", "")
versionService.pullRequestID = getValueFor("PR_ID", "pullRequestID", "")

Expand Down Expand Up @@ -288,6 +289,19 @@ open class VersionExtension @Inject constructor(objectFactory: ObjectFactory,
containerVersionProperty.get()
}

val versionWithID: String by lazy {
if(versionWithIDProperty.get().isEmpty()) {
if(versionService.buildID.isNotEmpty()) {
versionWithIDProperty.set(this.versionService.versionWithID)
} else {
project.logger.quiet("A version with ID is requested, but the Gradle property 'buildID'" +
" or the environment 'BUILD_ID' is not set.")
versionWithIDProperty.set(this.versionService.version)
}
}
versionWithIDProperty.get()
}

/**
* This is the previous version string.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ open class ShowVersion: DefaultTask() {
println("-- GitFlow container version is not available!")
}

if(versionExt.versionService.buildID.isNotEmpty()) {
println("-- Version with ID is")
println("-- " + versionExt.versionWithID)
}
if(preVersion.isNotEmpty()) {
println("-- GitFlow previous version is")
println("-- " + versionExt.previousVersion)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,6 @@ class GitVersionService @JvmOverloads constructor(
*/
var isMergeBuild: Boolean = false

/**
* This must be set to true, if the Container version must be always unique.
* Therefore the following environment variables must be set: uniqueContainer, buildID
*/
var isUniqueVersion: Boolean = false

private val changed: Boolean by lazy {
val status = client.status().call()
val rv = status.untracked.size > 0 || status.uncommittedChanges.size > 0 ||
Expand Down Expand Up @@ -204,35 +198,32 @@ class GitVersionService @JvmOverloads constructor(
rv
}

private fun versionFromMainBranch(isContainer: Boolean) : String {
private fun versionFromMainBranch(isContainer: Boolean, uniqueID: String) : String {
val vm = getLatestVersion()
val addMetaData = versionForLocalChanges("", "local")

val uniqueID = if(isUniqueVersion) "id${buildID}-" else ""
val idStr = if(uniqueID.isNotEmpty()) "id${uniqueID}-" else ""

return when {
vm == defaultVersion && addMetaData.isEmpty() && !isContainer ->
vm.setBuildMetadata("${uniqueID}SNAPSHOT").toString()
vm.setBuildMetadata("${idStr}SNAPSHOT").toString()
vm == defaultVersion && addMetaData.isEmpty() && isContainer ->
vm.setBuildMetadata("${uniqueID}latest").toString()
vm.setBuildMetadata("${idStr}latest").toString()
addMetaData.isNotBlank() && !isContainer ->
vm.setBranchMetadata(addMetaData).setBuildMetadata("${uniqueID}SNAPSHOT").toString()
vm.setBranchMetadata(addMetaData).setBuildMetadata("${idStr}SNAPSHOT").toString()
addMetaData.isNotBlank() && isContainer ->
vm.setBranchMetadata(addMetaData).setBuildMetadata("${uniqueID}latest").toString()
vm.setBranchMetadata(addMetaData).setBuildMetadata("${idStr}latest").toString()

else -> vm.toString()
}
}

private fun versionForMergeBranch(isContainer: Boolean): String {
private fun versionForMergeBranch(): String {
if (sourceBranch.isEmpty() && pullRequestID.isEmpty()) {
log.error("The source branch or the pull request ID is not specified," +
"but necessary because this should be a version for a pull request.")
throw GradleException("Missing environment variables for pull request - source branch and ID")
}
if (buildID.isEmpty()) {
log.warn("The build ID is not specified for the version calculation for a pull request")
}
val mBranchName = if (sourceBranch.startsWith(headsPrefix)) {
sourceBranch.substring(headsPrefix.length)
} else {
Expand All @@ -257,20 +248,11 @@ class GitVersionService @JvmOverloads constructor(
}
}

return if(isContainer) {
"${bBranchName}-pr${pullRequestID}-id${buildID}-latest"
} else {
"${bBranchName}-pr${pullRequestID}-id${buildID}-SNAPSHOT"
}
return "${bBranchName}-pr${pullRequestID}"
}

private fun versionFromDevBranch(isContainer: Boolean) : String {
val uniqueID = if(isUniqueVersion) "-id${buildID}" else ""
return if(isContainer) {
"${versionForLocalChanges("dev", "local-dev")}${uniqueID}-latest"
} else {
"${versionForLocalChanges("dev", "local-dev")}${uniqueID}-SNAPSHOT"
}
private fun versionFromDevBranch() : String {
return versionForLocalChanges("dev", "local-dev")
}

private fun versionFromHotfix(isContainer: Boolean) : String {
Expand Down Expand Up @@ -308,6 +290,58 @@ class GitVersionService @JvmOverloads constructor(
}
}

/**
* This is the calculated version of the Git repository.
* If a buildID is specified for special branches a build id is added.
*/

val versionWithID: String by lazy {
val tag = getVersionTagFrom(Constants.HEAD)
var rv: String


if(! localOnly) {
when {
isMergeBuild -> {
rv = "${versionForMergeBranch()}-id${buildID}-SNAPSHOT"
}
tag.isNotBlank() -> {
rv = versionForLocalChanges(tag, "${tag}-local-SNAPSHOT")
}
branch == mainBranch -> {
rv = versionFromMainBranch(false, buildID)
}
branch == developBranch -> {
rv = "${versionFromDevBranch()}-id${buildID}-SNAPSHOT"
}
branch.startsWith("${hotfixPrefix}${separator}") -> {
rv = versionFromHotfix(false)
}
branch.startsWith("${featurePrefix}${separator}") -> {
rv = versionFromFeature(false)
}
branch.startsWith("${releasePrefix}${separator}") -> {
rv = versionFromRelease(false)
}
else -> {
val branches = getBranchListForRef()
rv = when {
branches.contains(mainBranch) -> versionFromMainBranch(false, buildID)
branches.contains(developBranch) -> "${versionFromDevBranch()}-id${buildID}-SNAPSHOT"
branches.contains(releasePrefix) -> {
val bn = branches.first { it.startsWith("${releasePrefix}${separator}") }
getBranchNameForVersion(releasePrefix, bn)
}
else -> "version-id${buildID}-SNAPSHOT"
}
}
}
} else {
rv = "LOCAL"
}
rv
}

/**
* This is the calculated version of the Git repository.
*/
Expand All @@ -319,22 +353,21 @@ class GitVersionService @JvmOverloads constructor(
if(! localOnly) {
when {
isMergeBuild -> {
rv = versionForMergeBranch(false)
rv = "${versionForMergeBranch()}-SNAPSHOT"
}
tag.isNotBlank() -> {
rv = versionForLocalChanges(tag, "${tag}-local-SNAPSHOT")
}
branch == mainBranch -> {
rv = versionFromMainBranch(false)
rv = versionFromMainBranch(false,"")
}
branch == developBranch -> {
rv = versionFromDevBranch(false)
rv = "${versionFromDevBranch()}-SNAPSHOT"
}
branch.startsWith("${hotfixPrefix}${separator}") -> {
rv = versionFromHotfix(false)
}
branch.startsWith("${featurePrefix}${separator}") -> {
// version = feature/local-'branchname'-SNAPSHOT
rv = versionFromFeature(false)
}
branch.startsWith("${releasePrefix}${separator}") -> {
Expand All @@ -343,8 +376,8 @@ class GitVersionService @JvmOverloads constructor(
else -> {
val branches = getBranchListForRef()
rv = when {
branches.contains(mainBranch) -> versionFromMainBranch(false)
branches.contains(developBranch) -> versionFromDevBranch(false)
branches.contains(mainBranch) -> versionFromMainBranch(false, "")
branches.contains(developBranch) -> "${versionFromDevBranch()}-SNAPSHOT"
branches.contains(releasePrefix) -> {
val bn = branches.first { it.startsWith("${releasePrefix}${separator}") }
getBranchNameForVersion(releasePrefix, bn)
Expand All @@ -369,22 +402,21 @@ class GitVersionService @JvmOverloads constructor(
if(! localOnly) {
when {
isMergeBuild -> {
rv = versionForMergeBranch(true)
rv = "${versionForMergeBranch()}-latest"
}
tag.isNotBlank() -> {
rv = versionForLocalChanges(tag, "${tag}-local")
}
branch == mainBranch -> {
rv = versionFromMainBranch(true)
rv = versionFromMainBranch(true, "")
}
branch == developBranch -> {
rv = versionFromDevBranch(true)
rv = "${versionFromDevBranch()}-latest"
}
branch.startsWith("${hotfixPrefix}${separator}") -> {
rv = versionFromHotfix(true)
}
branch.startsWith("${featurePrefix}${separator}") -> {
// version = feature/local-'branchname'-SNAPSHOT
rv = versionFromFeature(true)
}
branch.startsWith("${releasePrefix}${separator}") -> {
Expand All @@ -393,8 +425,8 @@ class GitVersionService @JvmOverloads constructor(
else -> {
val branches = getBranchListForRef()
rv = when {
branches.contains(mainBranch) -> versionFromMainBranch(true)
branches.contains(developBranch) -> versionFromDevBranch(true)
branches.contains(mainBranch) -> versionFromMainBranch(true, "")
branches.contains(developBranch) -> "${versionFromDevBranch()}-latest"
branches.contains(releasePrefix) -> {
val bn = branches.first { it.startsWith("${releasePrefix}${separator}") }
getBranchNameForVersion(releasePrefix, bn)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,6 @@ class PluginIntegrationSpec extends AbstractIntegrationGroovySpec {
then:
result1.task(":showVersion").outcome == TaskOutcome.SUCCESS
result1.output.contains("2.0.0-id456789-SNAPSHOT")
result1.output.contains("2.0.0-id456789-latest")

when:
creator.setBranch("hotfix/team1/12345-message")
Expand Down Expand Up @@ -527,7 +526,6 @@ class PluginIntegrationSpec extends AbstractIntegrationGroovySpec {
then:
result1.task(":showVersion").outcome == TaskOutcome.SUCCESS
result1.output.contains("2.0.0-id963258-SNAPSHOT")
result1.output.contains("2.0.0-id963258-latest")

when:
creator.setBranch("hotfix/team1/12345-message")
Expand Down

0 comments on commit 9cb6cdd

Please sign in to comment.