Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix for VerCraft CI usage #19

Merged
merged 4 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading