Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
JENKINS-44084 Pipeline support for electricflow plugin
  • Loading branch information
mcritic committed May 8, 2017
1 parent fc76e1e commit 03eeac3
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 113 deletions.
Expand Up @@ -225,7 +225,8 @@ public String runPipeline(

if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
+ conn.getResponseCode() + " "
+ conn.getResponseMessage());
}

br = new BufferedReader(new InputStreamReader(
Expand Down
Expand Up @@ -12,6 +12,7 @@
import java.io.IOException;
import java.util.List;

import javax.annotation.Nonnull;
import javax.servlet.ServletException;

import org.apache.commons.logging.Log;
Expand All @@ -27,11 +28,15 @@
import net.sf.json.JSONObject;

import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;

import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;

import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
Expand All @@ -41,8 +46,11 @@
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;

import jenkins.tasks.SimpleBuildStep;

public class ElectricFlowPipelinePublisher
extends Recorder
implements SimpleBuildStep
{

//~ Static fields/initializers ---------------------------------------------
Expand All @@ -69,15 +77,76 @@ public class ElectricFlowPipelinePublisher
Launcher launcher,
BuildListener listener)
{
Configuration cred = getDescriptor().getConfigurationByName(
return runPipeline(build, listener, null);
}

@Override public void perform(
@Nonnull Run<?, ?> run,
@Nonnull FilePath filePath,
@Nonnull Launcher launcher,
@Nonnull TaskListener taskListener)
throws InterruptedException, IOException
{
boolean result = runPipeline(run, null, taskListener);

if (!result) {
run.setResult(Result.FAILURE);
}
}

private void expandParameters(
JSONArray parameters,
EnvReplacer env)
{

for (Object jsonObject : parameters) {
JSONObject json = (JSONObject) jsonObject;
String parameterValue = (String) json.get("parameterValue");
String expandValue = env.expandEnv(parameterValue);

json.put("parameterValue", expandValue);
}
}

private void logListener(
BuildListener buildListener,
TaskListener taskListener,
String log)
{

if (buildListener != null) {
buildListener.getLogger()
.println(log);
}
else if (taskListener != null) {
taskListener.getLogger()
.print(log);
}
}

private boolean runPipeline(
Run run,
BuildListener buildListener,
TaskListener taskListener)
{
Configuration cred = getDescriptor().getConfigurationByName(
this.configuration);
String electricFlowUrl = cred.getElectricFlowUrl();
String userName = cred.getElectricFlowUser();
String userPassword = cred.getElectricFlowPassword();

listener.getLogger()
.println("Url: " + electricFlowUrl + ", Project name: "
+ projectName + ", Pipeline name: " + pipelineName);
if (cred == null) {
logListener(buildListener, taskListener,
"Configuration name' " + this.configuration
+ "' doesn't exist. ");

return false;
}

String electricFlowUrl = cred.getElectricFlowUrl();
String userName = cred.getElectricFlowUser();
String userPassword = cred.getElectricFlowPassword();

logListener(buildListener, taskListener,
"Url: " + electricFlowUrl + ", Project name: " + projectName
+ ", Pipeline name: " + pipelineName);

// exp starts here
JSONObject obj = new JSONObject();
Expand Down Expand Up @@ -115,8 +184,8 @@ public class ElectricFlowPipelinePublisher
}

try {
listener.getLogger()
.println("Preparing to run pipeline...");
logListener(buildListener, taskListener,
"Preparing to run pipeline...");

ElectricFlowClient efClient = new ElectricFlowClient(
electricFlowUrl, userName, userPassword);
Expand All @@ -128,26 +197,25 @@ public class ElectricFlowPipelinePublisher
pipelineName);
}
else {
EnvReplacer env = new EnvReplacer(build, listener);
EnvReplacer env = new EnvReplacer(run, taskListener);

expandParameters(parameters, env);
pipelineResult = efClient.runPipeline(projectName, pipelineName,
parameters);
}

String summaryHtml = getSummaryHtml(efClient, pipelineResult, parameters);

SummaryTextAction action = new SummaryTextAction(build,
String summaryHtml = getSummaryHtml(efClient,
pipelineResult, parameters);
SummaryTextAction action = new SummaryTextAction(run,
summaryHtml);

build.addAction(action);
build.save();
listener.getLogger()
.println("Pipeline result: " + pipelineResult);
run.addAction(action);
run.save();
logListener(buildListener, taskListener,
"Pipeline result: " + pipelineResult);
}
catch (Exception e) {
listener.getLogger()
.println(e.getMessage());
logListener(buildListener, taskListener, e.getMessage());
log.error(e.getMessage(), e);

return false;
Expand All @@ -156,78 +224,6 @@ public class ElectricFlowPipelinePublisher
return true;
}

private String getSummaryHtml(ElectricFlowClient efClient, String pipelineResult, JSONArray parameters) {
JSONObject flowRuntime = (JSONObject) JSONObject.fromObject(
pipelineResult)
.get(
"flowRuntime");
String pipelineId = (String) flowRuntime.get("pipelineId");
String flowRuntimeId = (String) flowRuntime.get(
"flowRuntimeId");
String url = efClient.getElectricFlowUrl()
+ "/flow/#pipeline-run/" + pipelineId
+ "/" + flowRuntimeId;


String summaryText = "<h3>ElectricFlow Run Pipeline</h3>" +
"<table cellspacing=\"2\" cellpadding=\"4\"> \n"
+ " <tr>\n"
+ " <td>Pipeline URL:</td>\n"
+ " <td><a href='" + url + "'>" + url + "</a></td> \n"
+ " </tr>\n"
+ " <tr>\n"
+ " <td>Pipeline Name:</td>\n"
+ " <td><a href='" + url + "'>" + pipelineName
+ "</a></td> \n"
+ " </tr>\n"
+ " <tr>\n"
+ " <td>Project Name:</td>\n"
+ " <td>" + projectName + "</td> \n"
+ " </tr>";

if (!parameters.isEmpty()) {
StringBuilder strBuilder = new StringBuilder(summaryText);
strBuilder.append(" <tr>\n"
+ " <td>&nbsp;<b>Parameters</b></td>\n"
+ " <td></td> \n"
+ " </tr>\n");

for (Object jsonObject : parameters) {
JSONObject json = (JSONObject) jsonObject;
String parameterName = (String) json.get(
"parameterName");
String parameterValue = (String) json.get(
"parameterValue");
strBuilder.append(" <tr>\n"
+ " <td>&nbsp;&nbsp;&nbsp;&nbsp;")
.append(parameterName)
.append(":</td>\n"
+ " <td>")
.append(parameterValue)
.append("</td> \n"
+ " </tr>\n");
}
summaryText = strBuilder.toString();
}

summaryText = summaryText + "</table>";
return summaryText;
}

private void expandParameters(
JSONArray parameters,
EnvReplacer env)
{

for (Object jsonObject : parameters) {
JSONObject json = (JSONObject) jsonObject;
String parameterValue = (String) json.get("parameterValue");
String expandValue = env.expandEnv(parameterValue);

json.put("parameterValue", expandValue);
}
}

public JSONArray getAdditionalOption()
{
return additionalOption;
Expand Down Expand Up @@ -278,6 +274,15 @@ private JSONArray getPipelineParameters()
}
}
}

JSONArray pipelineJsonParameters = (JSONArray) JSONObject
.fromObject(addParam)
.get("parameters");

if (pipelineJsonParameters != null
&& !pipelineJsonParameters.isEmpty()) {
return pipelineJsonParameters;
}
}

return new JSONArray();
Expand All @@ -293,6 +298,66 @@ public String getProjectName()
return BuildStepMonitor.NONE;
}

private String getSummaryHtml(
ElectricFlowClient efClient,
String pipelineResult,
JSONArray parameters)
{
JSONObject flowRuntime = (JSONObject) JSONObject.fromObject(
pipelineResult)
.get("flowRuntime");
String pipelineId = (String) flowRuntime.get("pipelineId");
String flowRuntimeId = (String) flowRuntime.get("flowRuntimeId");
String url = efClient.getElectricFlowUrl()
+ "/flow/#pipeline-run/" + pipelineId
+ "/" + flowRuntimeId;
String summaryText = "<h3>ElectricFlow Run Pipeline</h3>"
+ "<table cellspacing=\"2\" cellpadding=\"4\"> \n"
+ " <tr>\n"
+ " <td>Pipeline URL:</td>\n"
+ " <td><a href='" + url + "'>" + url + "</a></td> \n"
+ " </tr>\n"
+ " <tr>\n"
+ " <td>Pipeline Name:</td>\n"
+ " <td><a href='" + url + "'>" + pipelineName
+ "</a></td> \n"
+ " </tr>\n"
+ " <tr>\n"
+ " <td>Project Name:</td>\n"
+ " <td>" + projectName + "</td> \n"
+ " </tr>";

if (!parameters.isEmpty()) {
StringBuilder strBuilder = new StringBuilder(summaryText);

strBuilder.append(" <tr>\n"
+ " <td>&nbsp;<b>Parameters</b></td>\n"
+ " <td></td> \n"
+ " </tr>\n");

for (Object jsonObject : parameters) {
JSONObject json = (JSONObject) jsonObject;
String parameterName = (String) json.get("parameterName");
String parameterValue = (String) json.get("parameterValue");

strBuilder.append(" <tr>\n"
+ " <td>&nbsp;&nbsp;&nbsp;&nbsp;")
.append(parameterName)
.append(":</td>\n"
+ " <td>")
.append(parameterValue)
.append("</td> \n"
+ " </tr>\n");
}

summaryText = strBuilder.toString();
}

summaryText = summaryText + "</table>";

return summaryText;
}

@DataBoundSetter public void setAdditionalOption(
JSONArray additionalOption)
{
Expand Down Expand Up @@ -335,7 +400,6 @@ public static final class DescriptorImpl

//~ Static fields/initializers -----------------------------------------


private static final Log log = LogFactory.getLog(DescriptorImpl.class);

//~ Constructors -------------------------------------------------------
Expand Down
Expand Up @@ -13,28 +13,28 @@

import hudson.EnvVars;

import hudson.model.AbstractBuild;
import hudson.model.Run;
import hudson.model.TaskListener;

public class EnvReplacer
{

//~ Instance fields --------------------------------------------------------

private TaskListener listener;
private AbstractBuild build;
private EnvVars treeMap;
private TaskListener listener;
private Run run;
private EnvVars treeMap;

//~ Constructors -----------------------------------------------------------

public EnvReplacer(
AbstractBuild build,
TaskListener listener)
Run run,
TaskListener listener)
throws IOException, InterruptedException
{
this.listener = listener;
this.build = build;
this.treeMap = this.build.getEnvironment(this.listener);
this.run = run;
this.treeMap = this.run.getEnvironment(this.listener);
}

//~ Methods ----------------------------------------------------------------
Expand Down

0 comments on commit 03eeac3

Please sign in to comment.