Skip to content

Commit

Permalink
Workflow step for setting unsuccessful message
Browse files Browse the repository at this point in the history
Add a workflow step that allows setting the unsuccessful message from a
workflow build.  config.jelly is currently missing.

Dependency to the workflow plugin is optional.

Implements a limited part of [JENKINS-26103].
  • Loading branch information
teemumurtola committed Feb 9, 2016
1 parent 963b25f commit 427d422
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 6 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Expand Up @@ -102,6 +102,12 @@
<version>1.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>1.4</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
Expand Down
Expand Up @@ -86,6 +86,20 @@ public static ToGerritRunListener getInstance() {
return listeners.get(0);
}

/**
* Records the failure message for the given build.
*
* @param r the build that caused the failure.
* @param failureMessage the failure message
*/
public void setBuildFailureMessage(@Nonnull Run r, @Nonnull String failureMessage) {
GerritCause cause = getCause(r);
if (cause != null) {
cleanUpGerritCauses(cause, r);
memory.setEntryFailureMessage(cause.getEvent(), r, failureMessage);
}
}

@Override
public synchronized void onCompleted(@Nonnull Run r, @Nonnull TaskListener listener) {
GerritCause cause = getCause(r);
Expand All @@ -110,7 +124,9 @@ public synchronized void onCompleted(@Nonnull Run r, @Nonnull TaskListener liste
// Attempt to record the failure message, if applicable
String failureMessage = this.obtainFailureMessage(event, r, listener);
logger.info("Obtained failure message: {}", failureMessage);
memory.setEntryFailureMessage(event, r, failureMessage);
if (failureMessage != null) {
memory.setEntryFailureMessage(event, r, failureMessage);
}
} catch (IOException e) {
listener.error("[gerrit-trigger] Unable to read failure message from the workspace.");
logger.warn("IOException while obtaining failure message for build: "
Expand Down
@@ -0,0 +1,121 @@
/*
* The MIT License
*
* Copyright (c) 2016 Teemu Murtola.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.sonyericsson.hudson.plugins.gerrit.trigger.workflow;

import com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.ToGerritRunListener;
import hudson.Extension;
import hudson.Util;
import hudson.model.Run;
import javax.annotation.CheckForNull;
import javax.inject.Inject;
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractSynchronousStepExecution;
import org.jenkinsci.plugins.workflow.steps.StepContextParameter;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

/**
* Allows altering the Gerrit review posted at the end of build during the build.
* @author Teemu Murtola &lt;teemu.murtola@gmail.com&gt
*/
public class GerritReviewStep extends AbstractStepImpl {

private String unsuccessfulMessage;

/**
* Constructor.
*
* There are no mandatory parameters to the step.
*/
@DataBoundConstructor
public GerritReviewStep() {
}

/**
* Gets the unsuccessful message for a step.
* @return the message.
*/
@CheckForNull
public String getUnsuccessfulMessage() {
return unsuccessfulMessage;
}

/**
* Sets additional information to post for a failed build.
* @param unsuccessfulMessage Additional information to post for a failed build.
*/
@DataBoundSetter
public void setUnsuccessfulMessage(String unsuccessfulMessage) {
this.unsuccessfulMessage = Util.fixEmptyAndTrim(unsuccessfulMessage);
}

/**
* Executes the GerritReviewStep.
*/
public static class Execution extends AbstractSynchronousStepExecution<Void> {

@StepContextParameter
private transient Run build;

@Inject
private transient GerritReviewStep step;

@Override
protected Void run() throws Exception {
ToGerritRunListener listener = ToGerritRunListener.getInstance();
String unsuccessfulMessage = step.getUnsuccessfulMessage();
if (unsuccessfulMessage != null) {
listener.setBuildFailureMessage(build, unsuccessfulMessage);
}
return null;
}

private static final long serialVersionUID = 1L;
}

/**
* Adds the step as a workflow extension.
*/
@Extension(optional = true)
public static class DescriptorImpl extends AbstractStepDescriptorImpl {

/**
* Constructor.
*/
public DescriptorImpl() {
super(Execution.class);
}

@Override
public String getFunctionName() {
return "gerritReview";
}

@Override
public String getDisplayName() {
return "Set Gerrit review";
}
}
}
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License
Copyright (c) 2016 Teemu Murtola.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:entry field="unsuccessfulMessage" title="${%Unsuccessful message}">
<f:textarea />
</f:entry>
</j:jelly>
@@ -0,0 +1,3 @@
<div>
Additional details to post back to Gerrit if the build is not successful.
</div>
@@ -0,0 +1,12 @@
<div>
<p>
Allows influencing how the build results are reported back to Gerrit.
All input values are optional. If the step occurs multiple times in
a workflow, the last value set for each parameter is used.
</p>
<p>
If the build is not triggered by Gerrit Trigger, or if the trigger is
configured to not report back to Gerrit when the build finishes,
this step does nothing.
</p>
</div>
Expand Up @@ -108,6 +108,48 @@ public void testTriggerWorkflow() throws Exception {
}
}

/**
* Tests setting a failure message using gerritReview from a workflow job.
* @throws Exception if there is one.
*/
@Test
public void testWorkflowStepSetsUnsuccessfulMessage() 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 unsuccessfulMessage: 'myMessage'\n"
+ " currentBuild.setResult('FAILURE')\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,
// and the message contains the custom message...
JSONObject verifiedMessage = gerritServer.waitForNextVerified();
// System.out.println(gerritServer.lastContent);
String message = verifiedMessage.getString("message");
Assert.assertTrue(message.startsWith("Build Failed"));
Assert.assertTrue(message.contains("myMessage"));
JSONObject labels = verifiedMessage.getJSONObject("labels");
assertEquals(0, labels.getInt("Verified"));
} finally {
gerritServer.stop();
}
}

/**
* Tests a {@link JenkinsRule#configRoundtrip(hudson.model.Job)} on the workflow job.
*
Expand Down Expand Up @@ -141,12 +183,24 @@ public void testConfigRoundTrip() throws Exception {
* @throws IOException if so
*/
private WorkflowJob createWorkflowJob(PatchsetCreated event) throws IOException {
WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "WFJob");
job.setDefinition(new CpsFlowDefinition(""
return createWorkflowJob(event, ""
+ "node {\n"
+ " stage 'Build'\n "
+ " sh \"echo Gerrit trigger: ${GERRIT_EVENT_TYPE}\"\n "
+ "}\n"));
+ " stage 'Build'\n"
+ " sh \"echo Gerrit trigger: ${GERRIT_EVENT_TYPE}\"\n"
+ "}\n");
}

/**
* Creates a {@link WorkflowJob} with a configured {@link GerritTrigger} and given workflow DSL script.
*
* @param event the event to trigger on.
* @param script the workflow DSL script.
* @return the job
* @throws IOException if so
*/
private WorkflowJob createWorkflowJob(PatchsetCreated event, String script) throws IOException {
WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "WFJob");
job.setDefinition(new CpsFlowDefinition(script));

GerritTrigger trigger = Setup.createDefaultTrigger(job);
trigger.setGerritProjects(Collections.singletonList(
Expand Down

0 comments on commit 427d422

Please sign in to comment.