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

Add tagRelease task to root #299

Merged
merged 1 commit into from
Feb 13, 2024
Merged
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
111 changes: 111 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.io.ByteArrayOutputStream

plugins {
alias(libs.plugins.composeDesktop) apply false
`jewel-linting`
Expand All @@ -23,4 +25,113 @@ tasks {
}

register("check") { dependsOn(mergeSarifReports) }

register("tagRelease") {
dependsOn("check")

description = "Tags main branch and releases branches with provided tag name"
group = "release"

doFirst {
val tagName = (project.property("tagName") as String?)
?.takeIf { it.isNotBlank() }
?: throw GradleException("Please provide a tag name using -PtagName=[value]")

val stdOut = ByteArrayOutputStream()

// Check we're on the main branch
logger.info("Checking current branch is main...")
exec {
commandLine = listOf("git", "rev-parse", "--abbrev-ref", "HEAD")
standardOutput = stdOut
}.assertNormalExitValue()

val currentBranch = stdOut.use { it.toString() }.trim()
if (currentBranch != "main") {
throw GradleException("This task must only be run on the main branch")
}

// Check tag doesn't already exist
logger.info("Checking current branch is main...")
exec {
commandLine = listOf("git", "tag")
standardOutput = stdOut
}.assertNormalExitValue()

if (stdOut.toString().trim().lines().any { it == tagName }) {
throw GradleException("The tag $tagName already exists!")
}

// Get the current HEAD hash
logger.info("Getting HEAD hash...")
stdOut.reset()
exec {
commandLine = listOf("git", "rev-parse", "HEAD")
standardOutput = stdOut
}.assertNormalExitValue()

val currentHead = stdOut.use { it.toString() }.trim()

// Enumerate the release branches
logger.info("Enumerating release branches...")
stdOut.reset()
exec {
commandLine = listOf("git", "branch")
standardOutput = stdOut
}.assertNormalExitValue()

val releaseBranches = stdOut.use { it.toString() }
.lines()
.filter { it.trim().startsWith("releases/") }
.map { it.trim().removePrefix("releases/") }

logger.lifecycle("Release branches: ${releaseBranches.joinToString { "releases/$it" }}")

// Check all release branches have gotten the latest from main
logger.info("Validating release branches...")
for (branch in releaseBranches) {
stdOut.reset()
exec {
commandLine = listOf("git", "merge-base", "main", "releases/$branch")
standardOutput = stdOut
}.assertNormalExitValue()

val mergeBase = stdOut.use { it.toString() }.trim()
if (mergeBase != currentHead) {
throw GradleException("Branch releases/$branch is not up-to-date with main!")
}
}

// Tag main branch
logger.lifecycle("Tagging head of main branch as $tagName...")
exec {
commandLine = listOf("git", "tag", tagName)
}.assertNormalExitValue()

// Tag release branches
for (branch in releaseBranches) {
val branchTagName = "$tagName-$branch"
logger.lifecycle("Tagging head of branch releases/$branch as $branchTagName...")
stdOut.reset()

logger.info("Getting branch head commit...")
exec {
commandLine = listOf("git", "rev-parse", "releases/$branch")
standardOutput = stdOut
}.assertNormalExitValue()

val branchHead = stdOut.use { it.toString() }.trim()
logger.info("HEAD of releases/$branch is $branchHead")

logger.info("Tagging commit ${branchHead.take(7)} as $branchTagName")
stdOut.reset()
exec {
commandLine = listOf("git", "tag", branchTagName, branchHead)
standardOutput = stdOut
}.assertNormalExitValue()
}

logger.info("All done!")
}
}
}