Skip to content

Commit

Permalink
Hotfix for VerCraft CI usage (#19)
Browse files Browse the repository at this point in the history
### What's done:
- All CI platforms do not checkout particular branch, but provide a variable, which contains the name of a particular branch;
- So VerCraft tried to get the checked-out branch, but was not able to do it;
- Now VerCraft uses this variable and also has `VERCRAFT_BRANCH_REF` for the branch name to be explicit set;
  • Loading branch information
orchestr7 authored Jan 14, 2025
1 parent 8830d28 commit 4146336
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
5 changes: 5 additions & 0 deletions core/src/main/kotlin/com/akuleshov7/vercraft/core/Branch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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_HEAD_REF: String = "GITHUB_HEAD_REF"
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<RevCommit> = git.log().add(ref.objectId).call().toList()

Expand Down
36 changes: 35 additions & 1 deletion core/src/main/kotlin/com/akuleshov7/vercraft/core/Releases.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,41 @@ public class Releases public constructor(private val git: Git) {

public val releaseBranches: MutableSet<ReleaseBranch> = findReleaseBranches()

private val currentCheckoutBranch = Branch(git, repo.findRef(repo.branch))
private val currentCheckoutBranch = repo.branch
?.let { Branch(git, repo.findRef(it)) }
?: 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_REF."
)

val branchName = System.getenv(GITLAB_BRANCH_REF)
?: System.getenv(GITHUB_HEAD_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_HEAD_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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down

0 comments on commit 4146336

Please sign in to comment.