Skip to content
Permalink
Browse files

Add option for selecting encoded string for multiline text parameters…

… or not.
  • Loading branch information...
rinrinne committed Apr 14, 2014
1 parent 5e0de02 commit cb2858cc62295877c16745b30e1c27a1eb0cf4b4
@@ -138,6 +138,7 @@
private boolean noNameAndEmailParameters;
private String dependencyJobsNames;
//private List<AbstractProject> dependencyJobs;
private boolean readableMessage;
private String buildStartMessage;
private String buildFailureMessage;
private String buildSuccessfulMessage;
@@ -195,6 +196,7 @@
* @param delayedApproval Delayed Approval on or off.
* @param escapeQuotes EscapeQuotes on or off.
* @param noNameAndEmailParameters Whether to create parameters containing name and email
* @param readableMessage Human readable message or not.
* @param dependencyJobsNames The list of jobs on which this job depends
* @param buildStartMessage Message to write to Gerrit when a build begins
* @param buildSuccessfulMessage Message to write to Gerrit when a build succeeds
@@ -230,6 +232,7 @@ public GerritTrigger(
boolean delayedApproval,
boolean escapeQuotes,
boolean noNameAndEmailParameters,
boolean readableMessage,
String dependencyJobsNames,
String buildStartMessage,
String buildSuccessfulMessage,
@@ -260,6 +263,7 @@ public GerritTrigger(
this.delayedApproval = delayedApproval;
this.escapeQuotes = escapeQuotes;
this.noNameAndEmailParameters = noNameAndEmailParameters;
this.readableMessage = readableMessage;
this.dependencyJobsNames = dependencyJobsNames;
this.buildStartMessage = buildStartMessage;
this.buildSuccessfulMessage = buildSuccessfulMessage;
@@ -1338,6 +1342,28 @@ public void setNoNameAndEmailParameters(boolean noNameAndEmailParameters) {
this.noNameAndEmailParameters = noNameAndEmailParameters;
}

/**
* If readableMessage is on or off. When this is set on this plugin will create parameters
* for multiline text, e.g. commit message, as human readable message. When this is set off,
* it will be encoded.
*
* @return true if readableMessage is on.
*/
public boolean isReadableMessage() {
return readableMessage;
}

/**
* Sets readableMessage to on or off. When this is set on this plugin will create parameters
* for multiline text, e.g. commit message, as human readable message. when this is set off,
* it will be encoded.
*
* @param readableMessage is true if human readable message is set.
*/
public void setReadableMessage(boolean readableMessage) {
this.readableMessage = readableMessage;
}

/**
* The message to show users when a build starts, if custom messages are enabled.
*
@@ -41,9 +41,11 @@
import hudson.model.StringParameterValue;
import hudson.model.TextParameterValue;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Constructor;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@@ -276,7 +278,7 @@ public void setOrCreateStringParameterValue(List<ParameterValue> parameters, Str
}

/**
* Creates a {@link hudson.model.StringParameterValue} and adds it to the provided list.
* Creates a {@link hudson.model.TextParameterValue} and adds it to the provided list.
* If the parameter with the same name already exists in the list it will be replaced by the new parameter,
* but its description will be used, unless the parameter type is something else than a StringParameterValue.
*
@@ -311,11 +313,13 @@ public static void setOrCreateParameters(GerritTriggeredEvent gerritEvent, Abstr

boolean noNameAndEmailParameters = false;
boolean escapeQuotes = false;
boolean readableMessage = false;
if (project != null) {
GerritTrigger trigger = GerritTrigger.getTrigger(project);
if (trigger != null) {
noNameAndEmailParameters = trigger.isNoNameAndEmailParameters();
escapeQuotes = trigger.isEscapeQuotes();
readableMessage = trigger.isReadableMessage();
}
}

@@ -362,8 +366,18 @@ public static void setOrCreateParameters(GerritTriggeredEvent gerritEvent, Abstr

String commitMessage = event.getChange().getCommitMessage();
if (commitMessage != null) {
GERRIT_CHANGE_COMMIT_MESSAGE.setOrCreateTextParameterValue(
parameters, commitMessage, escapeQuotes);
if (readableMessage) {
GERRIT_CHANGE_COMMIT_MESSAGE.setOrCreateTextParameterValue(
parameters, commitMessage, escapeQuotes);
} else {
try {
byte[] encodedBytes = Base64.encodeBase64(commitMessage.getBytes("UTF-8"));
GERRIT_CHANGE_COMMIT_MESSAGE.setOrCreateStringParameterValue(
parameters, new String(encodedBytes), escapeQuotes);
} catch (UnsupportedEncodingException uee) {
logger.error("Failed to encode commit message as Base64: ", uee);
}
}
}
GERRIT_CHANGE_URL.setOrCreateStringParameterValue(
parameters, url, escapeQuotes);
@@ -16,6 +16,10 @@
help="/plugin/gerrit-trigger/trigger/help-EscapeQuotes.html">
<f:checkbox name="escapeQuotes" default="true" checked="${it.escapeQuotes}"/>
</f:entry>
<f:entry title="${%Commit message as human readable}" field="readableMessage"
help="/plugin/gerrit-trigger/trigger/help-ReadableMessage.html">
<f:checkbox name="readableMessage" default="false" checked="${it.readableMessage}"/>
</f:entry>
<f:entry title="${%Do not Pass Compound 'Name And Email' Parameters}" field="noNameAndEmailParameters"
help="/plugin/gerrit-trigger/trigger/help-NoNameAndEmailParameters.html">
<f:checkbox name="noNameAndEmailParameters" default="false" checked="${it.noNameAndEmailParameters}"/>
@@ -0,0 +1,6 @@
<p>
If parameters regarding multiline text, e.g. commit message, should be as human readable or not.
</p>
<p>
If false, those parameters are Base64 encoded to keep environment variables clean.
</p>
@@ -283,7 +283,8 @@ public void testInitializeTriggerOnEvents() {
AbstractProject project = PowerMockito.mock(AbstractProject.class);
when(project.getFullName()).thenReturn("MockedProject");
GerritTrigger trigger = new GerritTrigger(null, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
true, false, true, false, "", "", "", "", "", "", "", null, null, null, null, false, false, "");
true, false, true, false, false, "", "", "", "", "", "", "", null, null, null,
null, false, false, "");
trigger = spy(trigger);
Object triggerOnEvents = Whitebox.getInternalState(trigger, "triggerOnEvents");

@@ -1555,8 +1556,8 @@ public void shouldReturnGlobalSlavesWhenConfigured() {
public void shouldReturnSlaveSelectedInJobWhenConfigured() {
ReplicationConfig replicationConfigMock = setupReplicationConfigMock();
GerritTrigger gerritTrigger = new GerritTrigger(null, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, true, false, true,
false, "", "", "", "", "", "", "", null, PluginImpl.DEFAULT_SERVER_NAME, "slaveUUID", null, false, false,
"");
false, false, "", "", "", "", "", "", "", null, PluginImpl.DEFAULT_SERVER_NAME, "slaveUUID", null,
false, false, "");

when(replicationConfigMock.isEnableReplication()).thenReturn(true);
when(replicationConfigMock.isEnableSlaveSelectionInJobs()).thenReturn(true);
@@ -1576,8 +1577,8 @@ public void shouldReturnSlaveSelectedInJobWhenConfigured() {
public void shouldReturnDefaultSlaveWhenJobConfiguredSlaveDoesNotExist() {
ReplicationConfig replicationConfigMock = setupReplicationConfigMock();
GerritTrigger gerritTrigger = new GerritTrigger(null, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, true, false, true,
false, "", "", "", "", "", "", "", null, PluginImpl.DEFAULT_SERVER_NAME, "slaveUUID", null, false, false,
"");
false, false, "", "", "", "", "", "", "", null, PluginImpl.DEFAULT_SERVER_NAME, "slaveUUID", null,
false, false, "");

// Replication is configured at job level but slave and default no longer exist.
when(replicationConfigMock.isEnableReplication()).thenReturn(true);
@@ -91,8 +91,8 @@ public static FreeStyleProject createGerritTriggeredJob(HudsonTestCase base,
Collections.singletonList(new Branch(CompareType.ANT, "**")), null, null, null));
p.addTrigger(new GerritTrigger(projects, null,
null, null, null, null, null, null, null, null, null, null,
false, false, true, false, null, null, null, null, null, null, null, null,
serverName, null, null, false, false, null));
false, false, true, false, false, null, null, null, null, null, null, null,
null, serverName, null, null, false, false, null));
base.submit(base.createWebClient().getPage(p, "configure").getFormByName("config"));
return p;
}
@@ -120,8 +120,8 @@ public static FreeStyleProject createGerritDynamicTriggeredJob(HudsonTestCase ba
String filepath = uri.toURL().toString();
GerritTrigger trigger = new GerritTrigger(projects, null,
null, null, null, null, null, null, null, null, null, null, false, true,
false, false, null, null, null, null, null, null, null, null, PluginImpl.DEFAULT_SERVER_NAME,
null, list, true, false, filepath);
false, false, false, null, null, null, null, null, null, null, null,
PluginImpl.DEFAULT_SERVER_NAME, null, list, true, false, filepath);
p.addTrigger(trigger);
base.submit(base.createWebClient().getPage(p, "configure").getFormByName("config"));
return p;
@@ -163,8 +163,8 @@ public static FreeStyleProject createGerritTriggeredJobForCommentAdded(HudsonTes
list.add(event);
p.addTrigger(new GerritTrigger(projects, null,
null, null, null, null, null, null, null, null, null, null,
false, false, true, false, null, null, null, null, null, null, null, null,
serverName, null, list, false, false, null));
false, false, true, false, false, null, null, null, null, null, null, null,
null, serverName, null, list, false, false, null));
base.submit(base.createWebClient().getPage(p, "configure").getFormByName("config"));
return p;
}
@@ -409,8 +409,8 @@ public static GerritTrigger createDefaultTrigger(AbstractProject project) {
triggerOnEvents.add(pluginEvent);

GerritTrigger trigger = new GerritTrigger(null, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
true, false, true, false, "", "", "", "", "", "", "", null, PluginImpl.DEFAULT_SERVER_NAME,
null, triggerOnEvents, false, false, "");
true, false, true, false, false, "", "", "", "", "", "", "", null,
PluginImpl.DEFAULT_SERVER_NAME, null, triggerOnEvents, false, false, "");

if (project != null) {
trigger.start(project, true);

0 comments on commit cb2858c

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