From 08b9c3f6642db241777cc38c9ef612a020b8903f Mon Sep 17 00:00:00 2001 From: Austin Shalit Date: Mon, 28 Dec 2020 01:13:57 -0800 Subject: [PATCH] Add GITHUB_REF detection --- build.gradle | 1 + .../versioning/GitVersionProvider.groovy | 88 +++++++++++-------- .../WPILibVersioningPluginTests.groovy | 8 ++ 3 files changed, 61 insertions(+), 36 deletions(-) diff --git a/build.gradle b/build.gradle index d855146..f9be4ea 100644 --- a/build.gradle +++ b/build.gradle @@ -10,6 +10,7 @@ plugins { dependencies { compile 'org.ajoberstar.grgit:grgit-core:3.0.0' testCompile 'junit:junit:4.12' + testCompile 'com.github.stefanbirkner:system-lambda:1.1.1' } repositories { diff --git a/src/main/groovy/edu/wpi/first/wpilib/versioning/GitVersionProvider.groovy b/src/main/groovy/edu/wpi/first/wpilib/versioning/GitVersionProvider.groovy index 6e8c288..7ca850a 100644 --- a/src/main/groovy/edu/wpi/first/wpilib/versioning/GitVersionProvider.groovy +++ b/src/main/groovy/edu/wpi/first/wpilib/versioning/GitVersionProvider.groovy @@ -51,56 +51,72 @@ class GitVersionProvider implements WPILibVersionProvider { } public String getVersion(WPILibVersioningPluginExtension extension, Project project, boolean allTags) { - // Determine the version number and make it available on our plugin extension - def gitDir = getGitDir(project.rootProject.rootDir) - // If no git directory was found, print a message to the console and return an empty string - if (gitDir == null) { - println "No .git was found in $project.rootProject.rootDir, or any parent directories of that directory." - println "No version number generated." - return '' + String tag = null + boolean isDirty = false; + + // Check to see if we are running on CI + if (System.getenv("CI") == "true") { + def githubRef = System.getenv("GITHUB_REF") + if (extension.releaseMode && githubRef.startsWith("refs/tags/")) { + tag = githubRef.replace("refs/tags/", "") + + println "GitHub provided a tag via the GITHUB_REF environment variable: $githubRef" + } } - Grgit git = Grgit.open(currentDir: (Object)gitDir.absolutePath) - // Get the tag given by describe - String tag = git.describe(tags: (Object)allTags) + if (tag == null) { + // We are not on CI or CI failed to provide a tag, need to use git + // Determine the version number and make it available on our plugin extension + def gitDir = getGitDir(project.rootProject.rootDir) + // If no git directory was found, print a message to the console and return an empty string + if (gitDir == null) { + println "No .git was found in $project.rootProject.rootDir, or any parent directories of that directory." + println "No version number generated." + return '' + } - // Get a list of all tags - List tags = git.tag.list() + Grgit git = Grgit.open(currentDir: (Object)gitDir.absolutePath) + // Get the tag given by describe + tag = git.describe(tags: (Object)allTags) - Tag describeTag = null + // Get a list of all tags + List tags = git.tag.list() - if (tag != null && tags != null) { - // Find tag hash that starts with describe - tags.find { Tag tg -> - if (tag.startsWith(tg.name)) { - describeTag = tg - return true + Tag describeTag = null + + if (tag != null && tags != null) { + // Find tag hash that starts with describe + tags.find { Tag tg -> + if (tag.startsWith(tg.name)) { + describeTag = tg + return true + } + return false } - return false } - } - // If we found the tag matching describe - if (describeTag != null) { - String commitId = describeTag.commit.id + // If we found the tag matching describe + if (describeTag != null) { + String commitId = describeTag.commit.id - // Find all tags matching commit - // Sort by date - Tag newestTag = tags.findAll { - it.commit.id.equals(commitId) - }.sort { - it.dateTime - }.last() + // Find all tags matching commit + // Sort by date + Tag newestTag = tags.findAll { + it.commit.id.equals(commitId) + }.sort { + it.dateTime + }.last() + + // Replace describe tag with newest + tag = tag.replace(describeTag.name, newestTag.name) + } - // Replace describe tag with newest - tag = tag.replace(describeTag.name, newestTag.name) + isDirty = !git.status().isClean() } - boolean isDirty = !git.status().isClean() def match = tag =~ versionRegex if (!match.matches()) { - String annotated = allTags ? "" : "annotated" - println "Latest $annotated tag is $tag. This does not match the expected version number pattern." + println "Tag is $tag. This does not match the expected version number pattern." println "No version number was generated." return '' } diff --git a/src/test/groovy/edu/wpi/first/wpilib/versioning/WPILibVersioningPluginTests.groovy b/src/test/groovy/edu/wpi/first/wpilib/versioning/WPILibVersioningPluginTests.groovy index 8dca4c9..f5d7d7e 100644 --- a/src/test/groovy/edu/wpi/first/wpilib/versioning/WPILibVersioningPluginTests.groovy +++ b/src/test/groovy/edu/wpi/first/wpilib/versioning/WPILibVersioningPluginTests.groovy @@ -11,6 +11,7 @@ import java.nio.file.Files import static org.junit.Assert.assertEquals import static org.junit.Assert.assertTrue +import static com.github.stefanbirkner.systemlambda.SystemLambda.withEnvironmentVariable /** * Tests for the wpilib versioning plugin @@ -116,6 +117,13 @@ class WPILibVersioningPluginTests { null, true) } + @Test + public void 'Uses GitHub provided tag instead of git'() { + withEnvironmentVariable("CI", "true") + .and("GITHUB_REF", "refs/tags/v1.0.0-beta-2") + .execute({ -> verifyProjectVersion('v1.0.0-beta-1', null, true, "1.0.0-beta-2") }) + } + static def verifyRegex(String majorVersion, String minorVersion, String qualifier = null, int numCommits = 0, String hash = null) { def strBuilder = new StringBuilder() strBuilder.append('v').append(majorVersion).append(minorVersion)