Skip to content

Commit

Permalink
Allow setting custom url in workflow
Browse files Browse the repository at this point in the history
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 9, 2016
1 parent 427d422 commit 3e31f72
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 3 deletions.
Expand Up @@ -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);

Expand Down
Expand Up @@ -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.
*
Expand Down
Expand Up @@ -385,6 +385,26 @@ public synchronized List<Run> getBuilds(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.
*
Expand Down Expand Up @@ -752,6 +772,7 @@ public static class Entry {
private String project;
private String build;
private boolean buildCompleted;
private String customUrl;
private String unsuccessfulMessage;

/**
Expand Down Expand Up @@ -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.
*
Expand Down
Expand Up @@ -42,6 +42,7 @@
*/
public class GerritReviewStep extends AbstractStepImpl {

private String customUrl;
private String unsuccessfulMessage;

/**
Expand All @@ -53,6 +54,24 @@ public class GerritReviewStep extends AbstractStepImpl {
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.
Expand Down Expand Up @@ -85,6 +104,10 @@ public static class Execution extends AbstractSynchronousStepExecution<Void> {
@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);
Expand Down
Expand Up @@ -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>
Expand Down
@@ -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>
Expand Up @@ -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.
Expand Down

0 comments on commit 3e31f72

Please sign in to comment.