Skip to content
Permalink
Browse files

Merge pull request #274 from teemumurtola/workflow-steps

Partial implementation for JENKINS-26103
  • Loading branch information
rsandell committed Mar 16, 2016
2 parents 64d7d8a + 3b1146d commit b7ab5892c183e6ca91ce00325f009d9f1571a8b8
@@ -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>
@@ -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);

@@ -624,7 +626,7 @@ private String createBuildsStats(MemoryImprint memoryImprint, TaskListener liste
unsuccessfulMessage = entry.getUnsuccessfulMessage();

if (null != unsuccessfulMessage && !unsuccessfulMessage.isEmpty()) {
logger.trace("Using unsuccessful message from file.");
logger.trace("Using unsuccessful message.");
str.append(" <<<\n");
str.append(unsuccessfulMessage.trim());
str.append("\n>>>");
@@ -86,6 +86,34 @@ 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 unsuccessful message for the given build.
*
* @param r the build that caused the failure.
* @param unsuccessfulMessage the unsuccessful message
*/
public void setBuildUnsuccessfulMessage(@Nonnull Run r, @Nonnull String unsuccessfulMessage) {
GerritCause cause = getCause(r);
if (cause != null) {
cleanUpGerritCauses(cause, r);
memory.setEntryUnsuccessfulMessage(cause.getEvent(), r, unsuccessfulMessage);
}
}

@Override
public synchronized void onCompleted(@Nonnull Run r, @Nonnull TaskListener listener) {
GerritCause cause = getCause(r);
@@ -107,17 +135,19 @@ public synchronized void onCompleted(@Nonnull Run r, @Nonnull TaskListener liste
Result result = r.getResult();
if (result != null && result.isWorseThan(Result.SUCCESS)) {
try {
// 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);
// Attempt to record the unsuccessful message, if applicable
String failureMessage = this.obtainUnsuccessfulMessage(event, r, listener);
logger.info("Obtained unsuccessful message: {}", failureMessage);
if (failureMessage != null) {
memory.setEntryUnsuccessfulMessage(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: "
listener.error("[gerrit-trigger] Unable to read unsuccessful message from the workspace.");
logger.warn("IOException while obtaining unsuccessful message for build: "
+ r.getDisplayName(), e);
} catch (InterruptedException e) {
listener.error("[gerrit-trigger] Unable to read failure message from the workspace.");
logger.warn("InterruptedException while obtaining failure message for build: "
listener.error("[gerrit-trigger] Unable to read unsuccessful message from the workspace.");
logger.warn("InterruptedException while obtaining unsuccessful message for build: "
+ r.getDisplayName(), e);
}
}
@@ -393,7 +423,7 @@ protected String getExpandedContent(FilePath path, EnvVars envVars) throws IOExc
}

/**
* Attempt to obtain the failure message for a build.
* Attempt to obtain the unsuccessful message for a build.
*
* @param event The event that triggered this build
* @param build The build being executed
@@ -402,9 +432,9 @@ protected String getExpandedContent(FilePath path, EnvVars envVars) throws IOExc
* @throws IOException In case of an error communicating with the {@link FilePath} or {@link EnvVars Environment}
* @throws InterruptedException If interrupted while working with the {@link FilePath} or {@link EnvVars Environment}
*/
private String obtainFailureMessage(@Nullable GerritTriggeredEvent event,
@Nonnull Run build,
@Nullable TaskListener listener)
private String obtainUnsuccessfulMessage(@Nullable GerritTriggeredEvent event,
@Nonnull Run build,
@Nullable TaskListener listener)
throws IOException, InterruptedException {
Job project = build.getParent();
String content = null;
@@ -414,7 +444,7 @@ private String obtainFailureMessage(@Nullable GerritTriggeredEvent event,
// trigger will be null in unit tests
if (trigger != null) {
String filepath = trigger.getBuildUnsuccessfulFilepath();
logger.debug("Looking for failure message in file glob: {}", filepath);
logger.debug("Looking for unsuccessful message in file glob: {}", filepath);


if (filepath != null && !filepath.isEmpty()) {
@@ -438,7 +468,7 @@ private String obtainFailureMessage(@Nullable GerritTriggeredEvent event,
// Use the first match
FilePath path = matches[0];
content = this.getExpandedContent(path, envVars);
logger.info("Obtained failure message from file: {}", content);
logger.info("Obtained unsuccessful message from file: {}", content);
}
} else {
logger.warn("Unable to find matching workspace files for job {}, type {}",
@@ -386,25 +386,58 @@ public synchronized boolean isBuilding(GerritTriggeredEvent event) {
}

/**
* Records the failure message for the given build.
* Records a custom URL for the given build.
*
* @param event the event.
* @param r the build that caused the failure.
* @param failureMessage the failure message
* @param event the event.
* @param r the build that caused the failure.
* @param customUrl the URL.
*/
public void setEntryFailureMessage(GerritTriggeredEvent event, Run r, String failureMessage) {
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 unsuccessful message for {}: {}", event, failureMessage);
entry.setUnsuccessfulMessage(failureMessage);
logger.trace("Recording custom URL for {}: {}", event, customUrl);
entry.setCustomUrl(customUrl);
}
}
}

/**
* Records the unsuccessful message for the given build.
*
* @param event the event.
* @param r the build that caused the failure.
* @param unsuccessfulMessage the unsuccessful message
*/
public void setEntryUnsuccessfulMessage(GerritTriggeredEvent event, Run r, String unsuccessfulMessage) {
MemoryImprint pb = getMemoryImprint(event);

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

if (entry != null) {
logger.trace("Recording unsuccessful message for {}: {}", event, unsuccessfulMessage);
entry.setUnsuccessfulMessage(unsuccessfulMessage);
}
}
}

/**
* Records the failure message for the given build.
*
* @param event the event.
* @param r the build that caused the failure.
* @param failureMessage the failure message
* @deprecated Use {@link #setEntryUnsuccessfulMessage}
*/
@Deprecated
public void setEntryFailureMessage(GerritTriggeredEvent event, Run r, String failureMessage) {
setEntryUnsuccessfulMessage(event, r, failureMessage);
}

/**
* A holder for all builds triggered by one event.
*/
@@ -752,6 +785,7 @@ public synchronized boolean wereAllBuildsNotBuilt() {
private String project;
private String build;
private boolean buildCompleted;
private String customUrl;
private String unsuccessfulMessage;

/**
@@ -821,6 +855,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.
*
@@ -0,0 +1,144 @@
/*
* 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 SetGerritReviewStep extends AbstractStepImpl {

private String customUrl;
private String unsuccessfulMessage;

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

/**
* 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.
*/
@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 SetGerritReviewStep.
*/
public static class Execution extends AbstractSynchronousStepExecution<Void> {

@StepContextParameter
private transient Run build;

@Inject
private transient SetGerritReviewStep step;

@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.setBuildUnsuccessfulMessage(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 "setGerritReview";
}

@Override
public String getDisplayName() {
return "Set Gerrit review";
}
}
}

0 comments on commit b7ab589

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