diff --git a/build.gradle.kts b/build.gradle.kts index ddf7013..d239557 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,5 +4,9 @@ plugins { kotlin("jvm") version ("2.1.0") java id("com.akuleshov7.buildutils.publishing-configuration") - id("com.akuleshov7.vercraft.plugin-gradle") version("0.0.2") + // id("com.akuleshov7.vercraft.plugin-gradle") version("0.0.2") } + +println("=== ${System.getenv("GITHUB_HEAD_REF")}") +println("=== ${System.getenv("GITHUB_REF_NAME")}") +println("=== ${System.getenv("GITHUB_REF")}") diff --git a/core/src/main/kotlin/com/akuleshov7/vercraft/core/Branch.kt b/core/src/main/kotlin/com/akuleshov7/vercraft/core/Branch.kt index 7d72f3c..09a2c2b 100644 --- a/core/src/main/kotlin/com/akuleshov7/vercraft/core/Branch.kt +++ b/core/src/main/kotlin/com/akuleshov7/vercraft/core/Branch.kt @@ -4,6 +4,11 @@ import org.eclipse.jgit.api.Git import org.eclipse.jgit.lib.Ref import org.eclipse.jgit.revwalk.RevCommit +public const val GITLAB_BRANCH_REF: String = "CI_COMMIT_REF_NAME" +public const val GITHUB_BRANCH_REF: String = "GITHUB_REF_NAME" +public const val BITBUCKET_BRANCH_REF: String = "BITBUCKET_BRANCH" +public const val VERCRAFT_BRANCH_REF: String = "VERCRAFT_BRANCH" + public class Branch(git: Git, public val ref: Ref) { public val gitLog: List = git.log().add(ref.objectId).call().toList() diff --git a/core/src/main/kotlin/com/akuleshov7/vercraft/core/Releases.kt b/core/src/main/kotlin/com/akuleshov7/vercraft/core/Releases.kt index 9687e28..f6006bf 100644 --- a/core/src/main/kotlin/com/akuleshov7/vercraft/core/Releases.kt +++ b/core/src/main/kotlin/com/akuleshov7/vercraft/core/Releases.kt @@ -58,10 +58,34 @@ public class Releases public constructor(private val git: Git) { logger.warn( "$ERROR_PREFIX your current HEAD is detached (no branch is checked out). " + "Usually this happens on CI platforms, which check out particular commit. " + - "Please pass the branch which you are trying to process to VerCraftю" + "Trying to resolve branch name using known CI ENV variables: " + + "$GITLAB_BRANCH_REF, $GITHUB_BRANCH_REF, $BITBUCKET_BRANCH_REF." ) - throw NullPointerException("Current checked out branch is null (looks like your current HEAD is detached)") + val branchName = System.getenv(GITLAB_BRANCH_REF) + ?: System.getenv(GITHUB_BRANCH_REF) + ?: System.getenv(BITBUCKET_BRANCH_REF) + ?: System.getenv(VERCRAFT_BRANCH_REF) + ?: run { + logger.warn( + "$ERROR_PREFIX following variables are not defined in current env" + + "$GITLAB_BRANCH_REF, $GITHUB_BRANCH_REF, $BITBUCKET_BRANCH_REF" + + "Please pass the branch name which you are trying to process now explicitly " + + "to VerCraft by setting ENV variable \$VERCRAFT_BRANCH_REF. " + ) + throw NullPointerException( + "Current HEAD is detached and CI env variables with the branch name are not set, so" + + "not able to determine the original branch name." + ) + } + + Branch( + git, + git.branchList() + .setListMode(REMOTE) + .call() + .first { it.name.endsWith(branchName) } + ) } public val version: VersionCalculator = VersionCalculator(git, this, currentCheckoutBranch) diff --git a/core/src/main/kotlin/com/akuleshov7/vercraft/core/VersionCalculator.kt b/core/src/main/kotlin/com/akuleshov7/vercraft/core/VersionCalculator.kt index 6545fb3..dac716f 100644 --- a/core/src/main/kotlin/com/akuleshov7/vercraft/core/VersionCalculator.kt +++ b/core/src/main/kotlin/com/akuleshov7/vercraft/core/VersionCalculator.kt @@ -127,7 +127,9 @@ public class VersionCalculator( dateFormat.timeZone = TimeZone.getDefault() val formattedDate = dateFormat.format(Date(commit.commitTime * 1000L)) - return SemVer(NO_MAJOR, NO_MINOR, distance + 1).setPrefix("$formattedDate-$branch") + return SemVer(NO_MAJOR, NO_MINOR, distance + 1) + .setPrefix("$formattedDate-$branch") + .setPostFix(commit.name.substring(0, 5)) } }