Skip to content

Commit

Permalink
feat(pipeline): Improved pipeline implementation to be pipeline-first
Browse files Browse the repository at this point in the history
  • Loading branch information
rottebds committed May 9, 2019
1 parent a59ef81 commit d4638a4
Show file tree
Hide file tree
Showing 21 changed files with 517 additions and 666 deletions.
5 changes: 2 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ plugins {
}

group = 'com.blackducksoftware.integration'
version = '2.0.2-SNAPSHOT'
version = '2.1.0-SNAPSHOT'
description = 'Synopsys Detect Plugin for Jenkins'

apply plugin: 'com.blackducksoftware.integration.solution'
Expand Down Expand Up @@ -48,8 +48,7 @@ jenkinsPlugin {
}

dependencies {
implementation 'com.blackducksoftware.integration:blackduck-common:42.5.2'
implementation 'com.blackducksoftware.integration:integration-common:17.0.2'
implementation 'com.blackducksoftware.integration:blackduck-common:42.6.0'
implementation 'com.synopsys.integration:polaris-common:0.12.0'
implementation 'org.jvnet.localizer:localizer:1.24'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package com.synopsys.integration.jenkins.detect;
package com.synopsys.integration.jenkins;

import java.net.MalformedURLException;
import java.net.URL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package com.synopsys.integration.jenkins.detect.tools;
package com.synopsys.integration.jenkins.detect;

import java.io.File;
import java.io.FileOutputStream;
Expand All @@ -30,7 +30,7 @@
import org.apache.commons.lang3.StringUtils;

import com.synopsys.integration.exception.IntegrationException;
import com.synopsys.integration.jenkins.detect.JenkinsProxyHelper;
import com.synopsys.integration.jenkins.JenkinsProxyHelper;
import com.synopsys.integration.log.IntLogger;
import com.synopsys.integration.rest.client.IntHttpClient;
import com.synopsys.integration.rest.request.Request;
Expand Down Expand Up @@ -82,6 +82,7 @@ private File prepareScriptDownloadDirectory() throws IntegrationException {

private void downloadScriptTo(final String url, final File file) throws IntegrationException, IOException {
final IntHttpClient intHttpClient = new IntHttpClient(logger, 120, true, JenkinsProxyHelper.getProxyInfoFromJenkins(url));
file.createNewFile();

final Request request = new Request.Builder().uri(url).build();
try (final Response response = intHttpClient.execute(request); final FileOutputStream fileOutputStream = new FileOutputStream(file)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@

import hudson.model.TaskListener;

public class JenkinsDetectLogger extends IntLogger implements Serializable {
public class DetectJenkinsLogger extends IntLogger implements Serializable {
private static final long serialVersionUID = -685871863395350470L;

private final TaskListener jenkinsLogger;

private LogLevel level = LogLevel.INFO;

public JenkinsDetectLogger(final TaskListener jenkinsLogger) {
public DetectJenkinsLogger(final TaskListener jenkinsLogger) {
this.jenkinsLogger = jenkinsLogger;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.synopsys.integration.jenkins.detect;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.SystemUtils;

import com.synopsys.integration.log.LogLevel;

public class JavaExecutableManager {
private final DetectJenkinsLogger logger;
private final Map<String, String> environmentVariables;

public JavaExecutableManager(final DetectJenkinsLogger logger, final Map<String, String> environmentVariables) {
this.logger = logger;
this.environmentVariables = environmentVariables;
}

public String calculateJavaExecutablePath(final String javaHome) throws IOException {
String javaExecutablePath = "java";
if (javaHome != null) {
File java = new File(javaHome);
java = new File(java, "bin");
if (SystemUtils.IS_OS_WINDOWS) {
java = new File(java, "java.exe");
} else {
java = new File(java, "java");
}
javaExecutablePath = java.getCanonicalPath();
}
return javaExecutablePath;
}

public void logJavaVersion() {
logger.debug("PATH: " + environmentVariables.get("PATH"));
if (LogLevel.DEBUG == logger.getLogLevel()) {
try {
logger.info("Java version: ");
final ProcessBuilder processBuilder = new ProcessBuilder(Arrays.asList("java", "-version"));
processBuilder.environment().putAll(environmentVariables);
final Process process = processBuilder.start();
process.waitFor();
IOUtils.copy(process.getErrorStream(), logger.getJenkinsListener().getLogger());
IOUtils.copy(process.getInputStream(), logger.getJenkinsListener().getLogger());
} catch (final InterruptedException | IOException e) {
logger.debug("Error printing the JAVA version: " + e.getMessage(), e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
*/
package com.synopsys.integration.jenkins.detect.exception;

public class DetectJenkinsException extends Exception {
import com.synopsys.integration.exception.IntegrationException;

public class DetectJenkinsException extends IntegrationException {
private static final long serialVersionUID = -1172941819259598247L;

public DetectJenkinsException() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
import com.synopsys.integration.blackduck.configuration.BlackDuckServerConfigBuilder;
import com.synopsys.integration.builder.Buildable;
import com.synopsys.integration.builder.IntegrationBuilder;
import com.synopsys.integration.jenkins.detect.JenkinsProxyHelper;
import com.synopsys.integration.jenkins.JenkinsProxyHelper;
import com.synopsys.integration.jenkins.detect.SynopsysCredentialsHelper;
import com.synopsys.integration.log.LogLevel;
import com.synopsys.integration.log.PrintStreamIntLogger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,17 @@
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import com.synopsys.integration.jenkins.detect.JenkinsDetectLogger;
import com.synopsys.integration.jenkins.detect.DetectJenkinsLogger;
import com.synopsys.integration.jenkins.detect.exception.DetectJenkinsException;
import com.synopsys.integration.jenkins.detect.steps.CreateDetectEnvironmentStep;
import com.synopsys.integration.jenkins.detect.steps.CreateDetectRunnerStep;
import com.synopsys.integration.jenkins.detect.steps.remote.DetectRemoteRunner;
import com.synopsys.integration.jenkins.detect.steps.remote.DetectResponse;
import com.synopsys.integration.jenkins.detect.tools.DummyToolInstaller;
import com.synopsys.integration.util.IntEnvironmentVariables;
import com.synopsys.integration.jenkins.detect.substeps.DetectJenkinsSubStepCoordinator;

import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.Computer;
import hudson.model.Node;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.remoting.VirtualChannel;

public class DetectPipelineStep extends Step implements Serializable {
public static final String DISPLAY_NAME = "Synopsys Detect";
Expand Down Expand Up @@ -110,40 +104,34 @@ public class Execution extends SynchronousNonBlockingStepExecution {
private static final long serialVersionUID = -5807577350749324767L;
private transient TaskListener listener;
private transient EnvVars envVars;
private transient Computer computer;
private transient FilePath workspace;
private transient Launcher launcher;

protected Execution(@Nonnull final StepContext context) throws InterruptedException, IOException {
super(context);
listener = context.get(TaskListener.class);
envVars = context.get(EnvVars.class);
computer = context.get(Computer.class);
workspace = context.get(FilePath.class);
launcher = context.get(Launcher.class);
}

@Override
protected Integer run() throws Exception {
final JenkinsDetectLogger logger = new JenkinsDetectLogger(listener);
final CreateDetectEnvironmentStep createDetectEnvironmentStep = new CreateDetectEnvironmentStep(logger);
final IntEnvironmentVariables intEnvironmentVariables = createDetectEnvironmentStep.setDetectEnvironment(envVars);

final CreateDetectRunnerStep createDetectRunnerStep = new CreateDetectRunnerStep(logger);
final Node node = computer.getNode();
final String remoteWorkspacePath = workspace.getRemote();
final String remoteToolsDirectory = new DummyToolInstaller().getToolDir(node).getRemote();
final DetectRemoteRunner detectRemoteRunner = createDetectRunnerStep.createAppropriateDetectRemoteRunner(intEnvironmentVariables, detectProperties, null, remoteWorkspacePath, remoteToolsDirectory);

final VirtualChannel caller = node.getChannel();
final DetectResponse detectResponse = caller.call(detectRemoteRunner);

if (detectResponse.getExitCode() > 0 && returnStatus) {
logger.error("Detect failed with exit code: " + detectResponse.getExitCode());
} else if (detectResponse.getExitCode() > 0) {
throw new DetectJenkinsException("Detect failed with exit code: " + detectResponse.getExitCode());
} else if (null != detectResponse.getException()) {
throw new DetectJenkinsException("Detect encountered an exception", detectResponse.getException());
final DetectJenkinsLogger logger = new DetectJenkinsLogger(listener);

final DetectJenkinsSubStepCoordinator detectJenkinsSubStepCoordinator = new DetectJenkinsSubStepCoordinator(logger, workspace, envVars, null, launcher, listener, detectProperties);
final int exitCode = detectJenkinsSubStepCoordinator.runDetect();

if (exitCode > 0) {
final String errorMsg = "Detect failed with exit code " + exitCode;
if (returnStatus) {
logger.error(errorMsg);
} else {
throw new DetectJenkinsException(errorMsg);
}
}
return detectResponse.getExitCode();

return exitCode;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,26 @@

import java.io.IOException;
import java.io.Serializable;
import java.util.function.Consumer;

import javax.annotation.Nonnull;

import org.kohsuke.stapler.DataBoundConstructor;

import com.synopsys.integration.exception.IntegrationException;
import com.synopsys.integration.jenkins.detect.JenkinsDetectLogger;
import com.synopsys.integration.jenkins.detect.DetectJenkinsLogger;
import com.synopsys.integration.jenkins.detect.exception.DetectJenkinsException;
import com.synopsys.integration.jenkins.detect.steps.CreateDetectEnvironmentStep;
import com.synopsys.integration.jenkins.detect.steps.CreateDetectRunnerStep;
import com.synopsys.integration.jenkins.detect.steps.remote.DetectRemoteRunner;
import com.synopsys.integration.jenkins.detect.steps.remote.DetectResponse;
import com.synopsys.integration.jenkins.detect.tools.DummyToolInstaller;
import com.synopsys.integration.util.IntEnvironmentVariables;
import com.synopsys.integration.jenkins.detect.substeps.DetectJenkinsSubStepCoordinator;

import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.JDK;
import hudson.model.Node;
import hudson.model.Result;
import hudson.remoting.VirtualChannel;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher;
Expand Down Expand Up @@ -80,30 +75,37 @@ public DescriptorImpl getDescriptor() {
// Freestyle
@Override
public boolean perform(final AbstractBuild<?, ?> build, final Launcher launcher, final BuildListener listener) throws InterruptedException, IOException {
final JenkinsDetectLogger logger = new JenkinsDetectLogger(listener);
final DetectJenkinsLogger logger = new DetectJenkinsLogger(listener);

try {
final CreateDetectEnvironmentStep createDetectEnvironmentStep = new CreateDetectEnvironmentStep(logger);
final IntEnvironmentVariables intEnvironmentVariables = createDetectEnvironmentStep.setDetectEnvironment(build.getEnvironment(listener));
final FilePath workspace = build.getWorkspace();
if (workspace == null) {
throw new DetectJenkinsException("Detect cannot be executed when the workspace is null");
}

final CreateDetectRunnerStep createDetectRunnerStep = new CreateDetectRunnerStep(logger);
final Node node = build.getBuiltOn();
final String javaHome = getJavaHome(build, node, listener);
final String remoteWorkspacePath = build.getWorkspace().getRemote();
final String remoteToolsDirectory = new DummyToolInstaller().getToolDir(node).getRemote();
final DetectRemoteRunner detectRemoteRunner = createDetectRunnerStep.createAppropriateDetectRemoteRunner(intEnvironmentVariables, detectProperties, javaHome, remoteWorkspacePath, remoteToolsDirectory);
final EnvVars envVars = build.getEnvironment(listener);

final VirtualChannel caller = node.getChannel();
final DetectResponse detectResponse = caller.call(detectRemoteRunner);
final DetectJenkinsSubStepCoordinator detectJenkinsSubStepCoordinator = new DetectJenkinsSubStepCoordinator(logger, workspace, envVars, getJavaHome(build, node, listener), launcher, listener, detectProperties);
final int exitCode = detectJenkinsSubStepCoordinator.runDetect();

if (detectResponse.getExitCode() > 0) {
logger.error("Detect failed with exit code: " + detectResponse.getExitCode());
if (exitCode > 0) {
logger.error("Detect failed with exit code " + exitCode);
build.setResult(Result.FAILURE);
} else if (null != detectResponse.getException()) {
throw new DetectJenkinsException("Detect encountered an exception", detectResponse.getException());
}
} catch (final Exception e) {
setBuildStatusFromException(logger, e, build::setResult);
if (e instanceof InterruptedException) {
logger.error("Detect thread was interrupted", e);
build.setResult(Result.ABORTED);
Thread.currentThread().interrupt();
} else if (e instanceof IntegrationException) {
logger.error(e.getMessage());
logger.debug(e.getMessage(), e);
build.setResult(Result.UNSTABLE);
} else {
logger.error(e.getMessage(), e);
build.setResult(Result.UNSTABLE);
}
}
return true;
}
Expand All @@ -118,21 +120,6 @@ private String getJavaHome(final AbstractBuild<?, ?> build, final Node node, fin
return nodeJdk.getHome();
}

private void setBuildStatusFromException(final JenkinsDetectLogger logger, final Exception exception, final Consumer<Result> resultConsumer) {
if (exception instanceof InterruptedException) {
logger.error("Detect thread was interrupted", exception);
resultConsumer.accept(Result.ABORTED);
Thread.currentThread().interrupt();
} else if (exception instanceof IntegrationException) {
logger.error(exception.getMessage());
logger.debug(exception.getMessage(), exception);
resultConsumer.accept(Result.UNSTABLE);
} else {
logger.error(exception.getMessage(), exception);
resultConsumer.accept(Result.UNSTABLE);
}
}

@Extension
public static final class DescriptorImpl extends BuildStepDescriptor<Publisher> implements Serializable {
private static final long serialVersionUID = 9059602791947799261L;
Expand Down

0 comments on commit d4638a4

Please sign in to comment.