Skip to content
Permalink
Browse files

[FIXED JENKINS-28705] Export the tag name for commits with a tag asso…

…ciated.
  • Loading branch information
orrc committed Jun 21, 2015
1 parent a4887b1 commit 64b3ffb427d50d9413a3cb8b652dc067403e9cbc
@@ -7,18 +7,26 @@
/** Exports the message text associated with a git tag used for a build. */
public class GitTagMessageAction implements EnvironmentContributingAction {

/** The name of the environment variable this plugin exports. */
static final String ENV_VAR_NAME = "GIT_TAG_MESSAGE";
/** The name of the environment variable this plugin exports for a git tag message. */
static final String ENV_VAR_NAME_MESSAGE = "GIT_TAG_MESSAGE";

/** The name of the environment variable this plugin exports for a git tag name. */
static final String ENV_VAR_NAME_TAG = "GIT_TAG_NAME";

private final String tagMessage;
private final String tagName;

public GitTagMessageAction(String tagMessage) {
public GitTagMessageAction(String tagName, String tagMessage) {
this.tagMessage = tagMessage;
this.tagName = tagName;
}

public void buildEnvVars(AbstractBuild<?, ?> build, EnvVars env) {
if (tagMessage != null) {
env.put(ENV_VAR_NAME, tagMessage);
env.put(ENV_VAR_NAME_MESSAGE, tagMessage);
}
if (tagName != null) {
env.put(ENV_VAR_NAME_TAG, tagName);
}
}

@@ -13,12 +13,12 @@
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.IOException;
import java.io.PrintStream;
import java.util.logging.Logger;

import static hudson.Util.fixEmpty;
import static hudson.Util.fixEmptyAndTrim;
import static org.jenkinsci.plugins.gittagmessage.GitTagMessageAction.ENV_VAR_NAME;
import static org.jenkinsci.plugins.gittagmessage.GitTagMessageAction.ENV_VAR_NAME_MESSAGE;
import static org.jenkinsci.plugins.gittagmessage.GitTagMessageAction.ENV_VAR_NAME_TAG;

public class GitTagMessageExtension extends GitSCMExtension {

@@ -58,7 +58,7 @@ public void onCheckoutCompleted(GitSCM scm, AbstractBuild<?, ?> build, GitClient
}
}

// Retrieve the tag message for the given tag name, then store it
// Attempt to retrieve the tag message for the discovered tag name
try {
String tagMessage = git.getTagMessage(tagName); // "git tag -l <tag> -n10000"
// Empty or whitespace-only values aren't exported to the environment by Jenkins, so we can trim the message
@@ -67,10 +67,16 @@ public void onCheckoutCompleted(GitSCM scm, AbstractBuild<?, ?> build, GitClient
listener.getLogger().println(Messages.NoTagMessageFound(tagName));
LOGGER.finest(String.format("No tag message could be determined for git tag '%s'.", tagName));
} else {
build.addAction(new GitTagMessageAction(tagMessage));
listener.getLogger().println(Messages.ExportingTagMessage(ENV_VAR_NAME, tagName));
listener.getLogger().println(Messages.ExportingTagMessage(ENV_VAR_NAME_MESSAGE, tagName));
LOGGER.finest(String.format("Exporting tag message '%s' from tag '%s'.", tagMessage, tagName));
}

// Always export the tag name itself
listener.getLogger().println(Messages.ExportingTagName(ENV_VAR_NAME_TAG, tagName));
LOGGER.finest(String.format("Exporting git tag name '%s'", tagName));

// Add the action which will export the variables
build.addAction(new GitTagMessageAction(tagName, tagMessage));
} catch (StringIndexOutOfBoundsException e) {
// git-client currently throws this exception if you ask for the message of a non-existent tag
LOGGER.info(String.format("No tag message exists for '%s'.", tagName));
@@ -1,8 +1,12 @@
<div>
If the revision checked out has a git tag associated with it, and a message
was specified when creating the tag, then that message will be exported during
the build as the <strong><tt>GIT_TAG_MESSAGE</tt></strong> environment
variable.<br/>If no tag message was specified, the commit message will be used.
If the revision checked out has a git tag associated with it, the tag name
will be exported during the build as <strong><tt>GIT_TAG_NAME</tt></strong>.
<br/>
If a message was specified when creating the tag, then that message will be
exported during the build as the <strong><tt>GIT_TAG_MESSAGE</tt></strong>
environment variable.
<br/>
If no tag message was specified, the commit message will be used.
<p/>
If the revision has more than one tag associated with it, only the most recent
tag will be taken into account, <strong>unless</strong> the refspec contains
@@ -1,4 +1,5 @@
DisplayName=Export git tag message as environment variable
NoTagFound=Tag information could not be determined for this revision; no git tag message will be exported
DisplayName=Export git tag and message as environment variables
NoTagFound=Tag information could not be determined for this revision; no git tag info will be exported
NoTagMessageFound=No tag message could be determined for git tag ''{0}''
ExportingTagMessage=Exporting {0} from tag ''{1}''
ExportingTagMessage=Exporting {0} from tag ''{1}''
ExportingTagName=Exporting {0} for tag ''{1}''
@@ -22,6 +22,8 @@
import java.io.IOException;
import java.util.Collections;

import static org.jenkinsci.plugins.gittagmessage.GitTagMessageAction.ENV_VAR_NAME_MESSAGE;
import static org.jenkinsci.plugins.gittagmessage.GitTagMessageAction.ENV_VAR_NAME_TAG;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@@ -50,8 +52,8 @@ public void commitWithoutTagShouldNotExportMessage() throws Exception {
FreeStyleProject job = configureGitTagMessageJob();
buildJobAndAssertSuccess(job);

// Then no git tag message should have been exported
assertBuildEnvironment(job, null);
// Then no git tag information should have been exported
assertBuildEnvironment(job, null, null);
}

@Test
@@ -64,8 +66,8 @@ public void commitWithEmptyTagMessageShouldNotExportMessage() throws Exception {
FreeStyleProject job = configureGitTagMessageJob();
buildJobAndAssertSuccess(job);

// Then a git tag message should not have been exported
assertBuildEnvironment(job, null);
// Then the git tag name message, but no message should have been exported
assertBuildEnvironment(job, "release-1.0", null);
}

@Test
@@ -79,7 +81,7 @@ public void commitWithTagShouldExportMessage() throws Exception {
buildJobAndAssertSuccess(job);

// Then the (trimmed) git tag message should have been exported
assertBuildEnvironment(job, "This is the first release.");
assertBuildEnvironment(job, "release-1.0", "This is the first release.");
}

@Test
@@ -94,8 +96,8 @@ public void commitWithMultipleTagsShouldExportMessage() throws Exception {
FreeStyleProject job = configureGitTagMessageJob();
buildJobAndAssertSuccess(job);

// Then the most recent tag message should have been exported
assertBuildEnvironment(job, "This is the first release.");
// Then the most recent tag info should have been exported
assertBuildEnvironment(job, "release-1.0", "This is the first release.");
}

@Test
@@ -110,12 +112,12 @@ public void jobWithMatchingTagShouldExportThatTagMessage() throws Exception {
FreeStyleProject job = configureGitTagMessageJob("+refs/tags/beta/*:refs/remotes/origin/tags/beta/*", "*/tags/beta/*");
buildJobAndAssertSuccess(job);

// Then the selected tag message should be exported, even although it's not the latest tag
assertBuildEnvironment(job, "Beta #1");
// Then the selected tag info should be exported, even although it's not the latest tag
assertBuildEnvironment(job, "beta/1", "Beta #1");
}

/** Asserts that the most recent build of the given job exported a tag message, or not exported if {@code null}. */
private static void assertBuildEnvironment(FreeStyleProject job, String expectedTagMessage) {
private static void assertBuildEnvironment(FreeStyleProject job, String expectedTagName, String expectedTagMessage) {
EnvVars env = null;
for (Builder b : job.getBuilders()) {
if (b instanceof CaptureEnvironmentBuilder) {
@@ -124,10 +126,16 @@ private static void assertBuildEnvironment(FreeStyleProject job, String expected
}
}

if (expectedTagName == null) {
assertFalse(env.containsKey(ENV_VAR_NAME_TAG));
} else {
assertEquals(expectedTagName, env.get(ENV_VAR_NAME_TAG));
}

if (expectedTagMessage == null) {
assertFalse(env.containsKey(GitTagMessageAction.ENV_VAR_NAME));
assertFalse(env.containsKey(ENV_VAR_NAME_MESSAGE));
} else {
assertEquals(expectedTagMessage, env.get(GitTagMessageAction.ENV_VAR_NAME));
assertEquals(expectedTagMessage, env.get(ENV_VAR_NAME_MESSAGE));
}
}

0 comments on commit 64b3ffb

Please sign in to comment.
You can’t perform that action at this time.