Skip to content

Commit

Permalink
Adding support for findRef on remote branches (#24)
Browse files Browse the repository at this point in the history
### What's done:
- `findRef` in jGit is searching only through local branches, that causes problems on CI, fixed
  • Loading branch information
orchestr7 committed Jan 17, 2025
1 parent 1859730 commit af08449
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 34 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -37,6 +39,7 @@ jobs:
with:
arguments: |
build
--info
properties: |
org.gradle.caching=true
gradle-version: wrapper
Expand Down
55 changes: 23 additions & 32 deletions core/src/main/kotlin/com/akuleshov7/vercraft/core/Releases.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<ReleaseBranch> = 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
Expand All @@ -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(
Expand Down Expand Up @@ -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)
}
}
18 changes: 18 additions & 0 deletions core/src/test/kotlin/com/akuleshov7/utils/GitTest.kt
Original file line number Diff line number Diff line change
@@ -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")
}
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
kotlin.code.style=official
group=com.akuleshov7.vercraft
version=0.1.0
version=0.2.0
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit af08449

Please sign in to comment.