Skip to content
Permalink
Browse files

Allow setting custom url in workflow

Extend the gerritReview workflow step to also allow setting the URL to
post.  Use the existing mechanism (BuildMemory) for storing the value
while the build is running.

Implements a limited part of [JENKINS-26103].
  • Loading branch information
teemumurtola committed Feb 8, 2016
1 parent 427d422 commit 3e31f724712d5a6f43605f18f601ca0d18d63423
@@ -590,10 +590,12 @@ private String createBuildsStats(MemoryImprint memoryImprint, TaskListener liste
*/
str.append("\n\n");

if (trigger.getCustomUrl() == null || trigger.getCustomUrl().isEmpty()) {
str.append(rootUrl).append(build.getUrl());
} else {
if (entry.getCustomUrl() != null && !entry.getCustomUrl().isEmpty()) {
str.append(expandParameters(entry.getCustomUrl(), build, listener, parameters));
} else if (trigger.getCustomUrl() != null && !trigger.getCustomUrl().isEmpty()) {
str.append(expandParameters(trigger.getCustomUrl(), build, listener, parameters));
} else {
str.append(rootUrl).append(build.getUrl());
}
str.append(MESSAGE_DELIMITER);

@@ -86,6 +86,20 @@ public static ToGerritRunListener getInstance() {
return listeners.get(0);
}

/**
* Records a custom URL for the given build.
*
* @param r the build.
* @param customUrl the URL.
*/
public void setBuildCustomUrl(@Nonnull Run r, @Nonnull String customUrl) {
GerritCause cause = getCause(r);
if (cause != null) {
cleanUpGerritCauses(cause, r);
memory.setEntryCustomUrl(cause.getEvent(), r, customUrl);
}
}

/**
* Records the failure message for the given build.
*
@@ -385,6 +385,26 @@ public synchronized boolean isBuilding(GerritTriggeredEvent event) {
}
}

/**
* Records a custom URL for the given build.
*
* @param event the event.
* @param r the build that caused the failure.
* @param customUrl the URL.
*/
public void setEntryCustomUrl(GerritTriggeredEvent event, Run r, String customUrl) {
MemoryImprint pb = getMemoryImprint(event);

if (pb != null) {
Entry entry = pb.getEntry(r.getParent());

if (entry != null) {
logger.info("Recording custom URL for {}: {}", event, customUrl);
entry.setCustomUrl(customUrl);
}
}
}

/**
* Records the failure message for the given build.
*
@@ -752,6 +772,7 @@ public synchronized boolean wereAllBuildsNotBuilt() {
private String project;
private String build;
private boolean buildCompleted;
private String customUrl;
private String unsuccessfulMessage;

/**
@@ -821,6 +842,24 @@ private void setBuild(Run build) {
}
}

/**
* Sets the URL to post for an entry.
*
* @param customUrl the URL.
*/
private void setCustomUrl(String customUrl) {
this.customUrl = customUrl;
}

/**
* Gets the URL to post for an entry.
*
* @return the URL.
*/
public String getCustomUrl() {
return this.customUrl;
}

/**
* Sets the unsuccessful message for an entry.
*
@@ -42,6 +42,7 @@
*/
public class GerritReviewStep extends AbstractStepImpl {

private String customUrl;
private String unsuccessfulMessage;

/**
@@ -53,6 +54,24 @@
public GerritReviewStep() {
}

/**
* Gets the custom URL for a step.
* @return the URL.
*/
@CheckForNull
public String getCustomUrl() {
return customUrl;
}

/**
* Sets a custom URL to post for a build.
* @param customUrl the URL to post.
*/
@DataBoundSetter
public void setCustomUrl(String customUrl) {
this.customUrl = Util.fixEmptyAndTrim(customUrl);
}

/**
* Gets the unsuccessful message for a step.
* @return the message.
@@ -85,6 +104,10 @@ public void setUnsuccessfulMessage(String unsuccessfulMessage) {
@Override
protected Void run() throws Exception {
ToGerritRunListener listener = ToGerritRunListener.getInstance();
String customUrl = step.getCustomUrl();
if (customUrl != null) {
listener.setBuildCustomUrl(build, customUrl);
}
String unsuccessfulMessage = step.getUnsuccessfulMessage();
if (unsuccessfulMessage != null) {
listener.setBuildFailureMessage(build, unsuccessfulMessage);
@@ -25,6 +25,9 @@ THE SOFTWARE.

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:entry field="customUrl" title="${%URL to post}">
<f:textbox />
</f:entry>
<f:entry field="unsuccessfulMessage" title="${%Unsuccessful message}">
<f:textarea />
</f:entry>
@@ -0,0 +1,5 @@
<div>
Custom URL for a message to send back to Gerrit.
If empty, URL to post from Gerrit Trigger configuration will be used.
Same expansion is done as for the URL to post configuration option.
</div>
@@ -108,6 +108,46 @@ public void testTriggerWorkflow() throws Exception {
}
}

/**
* Tests setting a custom URL using gerritReview from a workflow job.
* @throws Exception if there is one.
*/
@Test
public void testWorkflowStepSetsCustomUrl() throws Exception {
jenkinsRule.jenkins.setCrumbIssuer(null);
MockGerritServer gerritServer = MockGerritServer.get(jenkinsRule);

gerritServer.start();
try {
PatchsetCreated event = Setup.createPatchsetCreated(gerritServer.getName());

WorkflowJob job = createWorkflowJob(event, ""
+ "node {\n"
+ " stage 'Build'\n"
+ " gerritReview customUrl: 'myCustomUrl'\n"
+ "}\n");

PluginImpl.getHandler_().post(event);

// Now wait for the Gerrit server to trigger the workflow build in Jenkins...
TestUtils.waitForBuilds(job, 1);
WorkflowRun run = job.getBuilds().iterator().next();
jenkinsRule.assertLogContains("Set Gerrit review", run);

// Workflow build was triggered successfully. Now lets check make sure the
// gerrit plugin sent a verified notification back to the Gerrit Server...
JSONObject verifiedMessage = gerritServer.waitForNextVerified();
// System.out.println(gerritServer.lastContent);
String message = verifiedMessage.getString("message");
Assert.assertTrue(message.startsWith("Build Successful"));
Assert.assertTrue(message.contains("myCustomUrl"));
JSONObject labels = verifiedMessage.getJSONObject("labels");
assertEquals(1, labels.getInt("Verified"));
} finally {
gerritServer.stop();
}
}

/**
* Tests setting a failure message using gerritReview from a workflow job.
* @throws Exception if there is one.

0 comments on commit 3e31f72

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