Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-31999] added Pipeline compatibility for report publishers
- replaced AbstractBuild with Run
- replaced BuildListener with TaskListener
- added new DataBoundConstructor for mandatory parameters
- added DataBoundSetters for optional parameters with defaults
- implied project actions by implementing
SimpleBuildStep.LastBuildAction
  • Loading branch information
cpoenisch committed May 23, 2016
1 parent c883004 commit 19baf57
Show file tree
Hide file tree
Showing 35 changed files with 579 additions and 416 deletions.
Expand Up @@ -29,7 +29,7 @@
*/
package de.tracetronic.jenkins.plugins.ecutest.report;

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

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -117,7 +117,7 @@ protected VirtualFile getArchiveTargetDir(final File rootDir) {
* if serving the file failed
*/
public void doDynamic(final StaplerRequest req, final StaplerResponse rsp) throws IOException, ServletException {
final AbstractBuild<?, ?> build = getBuild(req);
final Run<?, ?> build = getBuild(req);
final AbstractReportAction action = getBuildAction(req);
if (build == null || action == null) {
LOGGER.warning(String.format("No build or related action found for url %s", req.getRequestURI()));
Expand All @@ -126,7 +126,7 @@ public void doDynamic(final StaplerRequest req, final StaplerResponse rsp) throw
}

final boolean isProjectLevel = action.isProjectLevel();
final File rootDir = isProjectLevel ? build.getProject().getRootDir() : build.getRootDir();
final File rootDir = isProjectLevel ? build.getParent().getRootDir() : build.getRootDir();
final File archiveFile = new File(new File(rootDir, getArchiveDir()), getFileName());
if (!archiveFile.exists()) {
LOGGER.warning(String.format("Archive file does not exists: %s for %s", getFileName(),
Expand Down
Expand Up @@ -31,8 +31,8 @@

import hudson.PluginWrapper;
import hudson.model.Action;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Job;
import hudson.model.Run;

import java.io.File;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -82,13 +82,13 @@ public boolean isProjectLevel() {
}

@Override
public AbstractBuild<?, ?> getBuild(final StaplerRequest req) {
final AbstractBuild<?, ?> build = getAnchestorBuild(req);
public Run<?, ?> getBuild(final StaplerRequest req) {
final Run<?, ?> build = getAnchestorBuild(req);
if (build != null) {
return build;
}

final AbstractProject<?, ?> project = getAnchestorProject(req);
final Job<?, ?> project = getAnchestorProject(req);
if (project != null) {
return getLastReportBuild(project);
}
Expand All @@ -109,7 +109,7 @@ protected VirtualFile getArchiveTargetDir(final File rootDir) {
* @return the last build with report artifacts or {@code null} if no proper build exists
*/
@CheckForNull
protected abstract AbstractBuild<?, ?> getLastReportBuild(final AbstractProject<?, ?> project);
protected abstract Run<?, ?> getLastReportBuild(final Job<?, ?> project);

/**
* Gets the icon path inside of this plugin.
Expand Down
Expand Up @@ -32,14 +32,9 @@
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.matrix.MatrixConfiguration;
import hudson.matrix.MatrixProject;
import hudson.model.Action;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.model.AbstractItem;
import hudson.model.AbstractProject;
import hudson.model.Computer;
import hudson.model.Node;
import hudson.model.Run;
Expand All @@ -50,15 +45,14 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import jenkins.model.Jenkins;
import jenkins.tasks.SimpleBuildStep;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundSetter;

import de.tracetronic.jenkins.plugins.ecutest.env.TestEnvInvisibleAction;
import de.tracetronic.jenkins.plugins.ecutest.env.ToolEnvInvisibleAction;
Expand All @@ -75,24 +69,29 @@
* @author Christian Pönisch <christian.poenisch@tracetronic.de>
*/
@SuppressWarnings("unchecked")
public abstract class AbstractReportPublisher extends Recorder {
public abstract class AbstractReportPublisher extends Recorder implements SimpleBuildStep {

private static final Logger LOGGER = Logger.getLogger(AbstractReportPublisher.class.getName());

private final boolean allowMissing;
private final boolean runOnFailed;
private boolean allowMissing;
private boolean runOnFailed;
/**
* {@code Boolean} type is required to retain backward compatibility with default value {@code true}.
*
* @since 1.9
*/
private final Boolean archiving;
private Boolean archiving = true;
/**
* {@code Boolean} type is required to retain backward compatibility with default value {@code true}.
*
* @since 1.9
*/
private final Boolean keepAll;
private Boolean keepAll = true;

/**
* Instantiates a new {@link AbstractReportPublisher}.
*/
public AbstractReportPublisher() {
super();
}

/**
* Instantiates a new {@link AbstractReportPublisher}.
Expand Down Expand Up @@ -172,26 +171,63 @@ public boolean getKeepAll() {
return keepAll;
}

@Override
public abstract Action getProjectAction(final AbstractProject<?, ?> project);
/**
* @param allowMissing
* specifies whether missing reports are allowed
*/
@DataBoundSetter
public void setAllowMissing(final boolean allowMissing) {
this.allowMissing = allowMissing;
}

@Override
public Collection<? extends Action> getProjectActions(final AbstractProject<?, ?> project) {
final ArrayList<Action> actions = new ArrayList<Action>();
actions.add(getProjectAction(project));
if (project instanceof MatrixProject && ((MatrixProject) project).getActiveConfigurations() != null) {
for (final MatrixConfiguration mc : ((MatrixProject) project).getActiveConfigurations()) {
try {
mc.onLoad(mc.getParent(), mc.getName());
} catch (final IOException e) {
LOGGER.log(Level.SEVERE, "Could not reload the matrix configuration");
}
}
}
/**
* @param runOnFailed
* specifies whether this publisher even runs on a failed build
*/
@DataBoundSetter
public void setRunOnFailed(final boolean runOnFailed) {
this.runOnFailed = runOnFailed;
}

return actions;
/**
* @param archiving
* specifies whether archiving artifacts is enabled
*/
@DataBoundSetter
public void setArchiving(final boolean archiving) {
this.archiving = archiving;
}

/**
* @param keepAll
* specifies whether artifacts are archived for all successful builds,
* otherwise only the most recent
*/
@DataBoundSetter
public void setKeepAll(final boolean keepAll) {
this.keepAll = keepAll;
}

// @Override
// public abstract Action getProjectAction(final AbstractProject<?, ?> project);
//
// @Override
// public Collection<? extends Action> getProjectActions(final AbstractProject<?, ?> project) {
// final ArrayList<Action> actions = new ArrayList<Action>();
// actions.add(getProjectAction(project));
// if (project instanceof MatrixProject && ((MatrixProject) project).getActiveConfigurations() != null) {
// for (final MatrixConfiguration mc : ((MatrixProject) project).getActiveConfigurations()) {
// try {
// mc.onLoad(mc.getParent(), mc.getName());
// } catch (final IOException e) {
// LOGGER.log(Level.SEVERE, "Could not reload the matrix configuration");
// }
// }
// }
//
// return actions;
// }

@Override
public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.NONE;
Expand All @@ -202,7 +238,7 @@ public BuildStepMonitor getRequiredMonitorService() {
* is set or if the build is not aborted or failed.
*
* @param result
* the build result
* the run result
* @return {@code true} if the build can continue
*/
protected boolean canContinue(final Result result) {
Expand All @@ -229,7 +265,7 @@ protected boolean canContinue(final Result result) {
* if the build gets interrupted
*/
protected AbstractToolInstallation configureToolInstallation(final String toolName,
final BuildListener listener, final EnvVars envVars) throws IOException, InterruptedException {
final TaskListener listener, final EnvVars envVars) throws IOException, InterruptedException {
AbstractToolInstallation installation = getToolInstallation(toolName, envVars);

if (installation != null) {
Expand Down Expand Up @@ -269,13 +305,13 @@ public ETInstallation getToolInstallation(final String toolName, final EnvVars e
/**
* Gets the workspace directory, either previous ECU-TEST workspace or default one.
*
* @param build
* the build
* @param run
* the run
* @return the workspace directory
*/
protected String getWorkspaceDir(final AbstractBuild<?, ?> build) {
protected String getWorkspaceDir(final Run<?, ?> run) {
String workspaceDir = "";
final ToolEnvInvisibleAction toolEnvAction = build.getAction(ToolEnvInvisibleAction.class);
final ToolEnvInvisibleAction toolEnvAction = run.getAction(ToolEnvInvisibleAction.class);
if (toolEnvAction != null) {
workspaceDir = toolEnvAction.getToolWorkspace();
}
Expand All @@ -285,13 +321,13 @@ protected String getWorkspaceDir(final AbstractBuild<?, ?> build) {
/**
* Gets the settings directory, either previous ECU-TEST settings or default one.
*
* @param build
* the build
* @param run
* the run
* @return the settings directory
*/
protected String getSettingsDir(final AbstractBuild<?, ?> build) {
protected String getSettingsDir(final Run<?, ?> run) {
String settingsDir = "";
final ToolEnvInvisibleAction toolEnvAction = build.getAction(ToolEnvInvisibleAction.class);
final ToolEnvInvisibleAction toolEnvAction = run.getAction(ToolEnvInvisibleAction.class);
if (toolEnvAction != null) {
settingsDir = toolEnvAction.getToolSettings();
}
Expand All @@ -301,12 +337,12 @@ protected String getSettingsDir(final AbstractBuild<?, ?> build) {
/**
* Gets the archive target.
*
* @param build
* the build
* @param run
* the run
* @return the archive target
*/
protected FilePath getArchiveTarget(final AbstractBuild<?, ?> build) {
return new FilePath(isKeepAll() ? getBuildArchiveDir(build) : getProjectArchiveDir(build.getParent()));
protected FilePath getArchiveTarget(final Run<?, ?> run) {
return new FilePath(isKeepAll() ? getBuildArchiveDir(run) : getProjectArchiveDir(run.getParent()));
}

/**
Expand Down Expand Up @@ -342,8 +378,8 @@ private File getBuildArchiveDir(final Run<?, ?> run) {
* Builds a list of report files for report generation.
* Includes the report files generated during separate sub-project execution.
*
* @param build
* the build
* @param run
* the run
* @param launcher
* the launcher
* @return the list of report files
Expand All @@ -352,10 +388,10 @@ private File getBuildArchiveDir(final Run<?, ?> run) {
* @throws InterruptedException
* if the build gets interrupted
*/
protected List<FilePath> getReportFiles(final AbstractBuild<?, ?> build, final Launcher launcher)
protected List<FilePath> getReportFiles(final Run<?, ?> run, final Launcher launcher)
throws IOException, InterruptedException {
final List<FilePath> reportFiles = new ArrayList<FilePath>();
final List<TestEnvInvisibleAction> testEnvActions = build.getActions(TestEnvInvisibleAction.class);
final List<TestEnvInvisibleAction> testEnvActions = run.getActions(TestEnvInvisibleAction.class);
for (final TestEnvInvisibleAction testEnvAction : testEnvActions) {
final FilePath testReportDir = new FilePath(launcher.getChannel(), testEnvAction.getTestReportDir());
if (testReportDir.exists()) {
Expand All @@ -369,17 +405,17 @@ protected List<FilePath> getReportFiles(final AbstractBuild<?, ?> build, final L
/**
* Removes the report actions from all previous builds which published at project level.
*
* @param build
* the build
* @param run
* the run
* @param clazz
* the report action class to remove
* @throws IOException
* signals that an I/O exception has occurred
*/
@SuppressWarnings("deprecation")
public static void removePreviousReports(final AbstractBuild<?, ?> build,
public static void removePreviousReports(final Run<?, ?> run,
final Class<? extends AbstractReportAction> clazz) throws IOException {
AbstractBuild<?, ?> prevBuild = build.getPreviousBuild();
Run<?, ?> prevBuild = run.getPreviousBuild();
while (prevBuild != null) {
final AbstractReportAction buildAction = prevBuild.getAction(clazz);
if (buildAction != null && buildAction.isProjectLevel()) {
Expand Down

0 comments on commit 19baf57

Please sign in to comment.