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 GITHUB_REF detection #27

Merged
merged 1 commit into from
Dec 29, 2020
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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Tag> 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<Tag> 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 ''
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down