From af084491319d4d8a2b650750df29a914e9e9a098 Mon Sep 17 00:00:00 2001 From: White Rabbit Date: Fri, 17 Jan 2025 16:13:40 +0300 Subject: [PATCH] Adding support for `findRef` on remote branches (#24) ### What's done: - `findRef` in jGit is searching only through local branches, that causes problems on CI, fixed --- .github/workflows/build_and_test.yml | 3 + .../com/akuleshov7/vercraft/core/Releases.kt | 55 ++++++++----------- .../kotlin/com/akuleshov7/utils/GitTest.kt | 18 ++++++ gradle.properties | 2 +- .../com/akuleshov7/vercraft/VercraftPlugin.kt | 2 +- 5 files changed, 46 insertions(+), 34 deletions(-) create mode 100644 core/src/test/kotlin/com/akuleshov7/utils/GitTest.kt diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index 4996697..61f591f 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -25,6 +25,8 @@ jobs: with: distribution: 'temurin' java-version: 21 + - name: Fetch all branches + run: git fetch --all - name: Cache konan uses: actions/cache@v4 with: @@ -37,6 +39,7 @@ jobs: with: arguments: | build + --info properties: | org.gradle.caching=true gradle-version: wrapper 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 e54cfb2..227fc48 100644 --- a/core/src/main/kotlin/com/akuleshov7/vercraft/core/Releases.kt +++ b/core/src/main/kotlin/com/akuleshov7/vercraft/core/Releases.kt @@ -5,7 +5,7 @@ import org.apache.logging.log4j.LogManager import org.eclipse.jgit.api.ListBranchCommand.ListMode.REMOTE import org.eclipse.jgit.api.Git import org.eclipse.jgit.api.ListBranchCommand -import org.eclipse.jgit.api.errors.TransportException +import org.eclipse.jgit.lib.Constants import org.eclipse.jgit.lib.Repository internal const val RELEASE_PREFIX = "release" @@ -43,18 +43,20 @@ public class Releases public constructor(private val git: Git, private val confi private val repo: Repository = git.repository - public val mainBranch: Branch = Branch(git, repo.findRef(config.defaultMainBranch)) + public val mainBranch: Branch = findBranch(config.defaultMainBranch) + ?: throw IllegalStateException("${config.defaultMainBranch} branch cannot be found in current git repo. " + + "Please check your fetched branches and fetch-depth (CI platforms usually limit it.") public val releaseBranches: MutableSet = findReleaseBranches() - private val currentCheckoutBranch = repo.branch - ?.let { Branch(git, repo.findRef(it)) } + private val currentCheckoutBranch = findBranch(repo.branch) ?: run { 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. " + - "Trying to resolve branch name using known CI ENV variables: " + - "$GITLAB_BRANCH_REF, $GITHUB_HEAD_REF, $BITBUCKET_BRANCH." + "Vercraft will try to resolve branch name using known CI ENV variables: " + + "$GITLAB_BRANCH_REF, $GITHUB_HEAD_REF, $BITBUCKET_BRANCH. " + + "You can also set it explicitly by $VERCRAFT_BRANCH." ) val branchName = config.checkoutBranch @@ -66,7 +68,7 @@ public class Releases public constructor(private val git: Git, private val confi logger.warn( "$ERROR_PREFIX following variables are not defined in current env" + "$GITLAB_BRANCH_REF, $GITHUB_HEAD_REF, $BITBUCKET_BRANCH" + - "Please pass the branch name which you are trying to process now explicitly " + + "Please pass the branch name which you are trying to process (check-out) now explicitly " + "to VerCraft by setting ENV variable \$VERCRAFT_BRANCH. " ) throw NullPointerException( @@ -190,31 +192,20 @@ public class Releases public constructor(private val git: Git, private val confi .map { ReleaseBranch(Branch(git, it), config) } .toHashSet() - // === TODO: unused logic, which should be unified with the default gradle cmd tasks - // check [[MakeReleaseTask]] - private fun pushBranch(newVersion: SemVer) { - try { - git.push() - .setCredentialsProvider(LocalCredentialsProvider) - .add("release/$newVersion") - .call() - - logger.warn("+ Pushed a branch [release/$newVersion]") - } catch (e: TransportException) { - logger.warn("$ERROR_PREFIX Not able to push branch to remote repository <${e.message}>, please do it manually.") - } - } - - private fun pushTag(newVersion: SemVer) { - try { - git.push() - .setCredentialsProvider(LocalCredentialsProvider) - .add("refs/tags/v$newVersion") - .call() + /** + * It appeared that standard findRef is only checking local branches + * + */ + public fun findBranch(branch: String?): Branch? { + if (branch == null) return null + + val foundBranch = repo.findRef("${Constants.R_HEADS}$branch") + ?: repo.findRef("${Constants.R_REMOTES}${config.remote}/$branch") + ?: run { + logger.error("$ERROR_PREFIX Cannot find branch ref <$branch> in current repository. ") + return null + } - logger.warn("+ Pushed a tag v$newVersion [Release $newVersion]") - } catch (e: TransportException) { - logger.warn("$ERROR_PREFIX Not able to push tag to remote repository <${e.message}>, please do it manually.") - } + return Branch(git, foundBranch) } } diff --git a/core/src/test/kotlin/com/akuleshov7/utils/GitTest.kt b/core/src/test/kotlin/com/akuleshov7/utils/GitTest.kt new file mode 100644 index 0000000..695aff9 --- /dev/null +++ b/core/src/test/kotlin/com/akuleshov7/utils/GitTest.kt @@ -0,0 +1,18 @@ +package com.akuleshov7.utils + +import com.akuleshov7.vercraft.core.DefaultConfig +import com.akuleshov7.vercraft.core.Releases +import org.eclipse.jgit.api.Git +import java.io.File +import kotlin.test.Test + +class GitTest { + @Test + fun smokeTest() { + Git.open(File("../")).use { git -> + val releases = Releases(git, DefaultConfig) + val resultedVer = releases.version.calc() + println(">>++ VerCrafted: $resultedVer") + } + } +} \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index c4b4a43..d08e9f3 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ kotlin.code.style=official group=com.akuleshov7.vercraft -version=0.1.0 +version=0.2.0 diff --git a/plugin-gradle/src/main/kotlin/com/akuleshov7/vercraft/VercraftPlugin.kt b/plugin-gradle/src/main/kotlin/com/akuleshov7/vercraft/VercraftPlugin.kt index 7063619..ab50bf8 100644 --- a/plugin-gradle/src/main/kotlin/com/akuleshov7/vercraft/VercraftPlugin.kt +++ b/plugin-gradle/src/main/kotlin/com/akuleshov7/vercraft/VercraftPlugin.kt @@ -40,7 +40,7 @@ class VercraftPlugin @Inject constructor( // === fetching project runGitCommand( - listOf("git", "fetch", "origin", "--prune", "--tags"), + listOf("git", "fetch", extension.config.get().remote, "--prune", "--tags"), "Unable to fetch project from the remote.", execOperations, project,