Skip to content
This repository has been archived by the owner on Apr 6, 2022. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'JENKINS-31202' of https://github.com/benjaminfuchs/anal…
…ysis-core-plugin into benjaminfuchs-JENKINS-31202
  • Loading branch information
uhafner committed Oct 22, 2016
2 parents f51e15d + 551bf57 commit 573e167
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 73 deletions.
131 changes: 92 additions & 39 deletions src/main/java/hudson/plugins/analysis/core/AbstractProjectAction.java
Expand Up @@ -19,6 +19,7 @@

import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Job;
import hudson.model.Action;
import hudson.model.Api;
import hudson.model.Run;
Expand All @@ -38,7 +39,7 @@
import hudson.util.Graph;

/**
* A project action displays a link on the side panel of a project. This action
* A job action displays a link on the side panel of a job. This action
* also is responsible to render the historical trend via its associated
* 'floatingBox.jelly' view.
*
Expand All @@ -52,7 +53,7 @@ public abstract class AbstractProjectAction<T extends ResultAction<?>> implement
private static final Logger LOGGER = Logger.getLogger(AbstractProjectAction.class.getName());

/** Project that owns this action. */
private final AbstractProject<?, ?> project;
private final Job<?, ?> owner;
/** The type of the result action. */
private final Class<? extends T> resultActionType;
/** The icon URL of this action: it will be shown as soon as a result is available. */
Expand All @@ -69,8 +70,8 @@ public abstract class AbstractProjectAction<T extends ResultAction<?>> implement
/**
* Creates a new instance of {@link AbstractProjectAction}.
*
* @param project
* the project that owns this action
* @param job
* the job that owns this action
* @param resultActionType
* the type of the result action
* @param name
Expand All @@ -84,16 +85,42 @@ public abstract class AbstractProjectAction<T extends ResultAction<?>> implement
* @param resultUrl
* the URL of the associated build results
*/
public AbstractProjectAction(final AbstractProject<?, ?> project, final Class<? extends T> resultActionType,
public AbstractProjectAction(final Job<?, ?> job, final Class<? extends T> resultActionType,
final Localizable name, final Localizable trendName, final String pluginUrl, final String iconUrl, final String resultUrl) {
this.project = project;
this.owner = job;
this.resultActionType = resultActionType;
this.name = name;
this.trendName = trendName;
this.pluginUrl = pluginUrl;
this.iconUrl = iconUrl;
this.resultUrl = resultUrl;
}

/**
* Creates a new instance of {@link AbstractProjectAction}.
*
* @param project
* the project that owns this action
* @param resultActionType
* the type of the result action
* @param name
* the human readable name of this action
* @param trendName
* the human readable name of the trend graph
* @param pluginUrl
* the URL of the associated plug-in
* @param iconUrl
* the icon to show
* @param resultUrl
* the URL of the associated build results
* @deprecated use
* {@link #AbstractProjectAction(Job, Class, Localizable, Localizable, String, String, String)}
*/
@Deprecated
public AbstractProjectAction(final AbstractProject<?, ?> project, final Class<? extends T> resultActionType,
final Localizable name, final Localizable trendName, final String pluginUrl, final String iconUrl, final String resultUrl) {
this((Job<?, ?>) project, resultActionType, name, trendName, pluginUrl, iconUrl, resultUrl);
}

/**
* Gets the remote API for this action.
Expand Down Expand Up @@ -128,17 +155,30 @@ public String getTrendName() {
return asString(trendName);
}

/**
* Returns the owner this action belongs to.
*
* @return the owner
*/
public final Job<?, ?> getOwner() {
return owner;
}

/**
* Returns the project this action belongs to.
*
* @return the project
*
* @deprecated use
* {@link #getOwner()}
*/
@Deprecated
public final AbstractProject<?, ?> getProject() {
return project;
return owner instanceof AbstractProject ? (AbstractProject<?, ?>) owner : null;
}

/**
* Returns the graph configuration view for this project. If the requested
* Returns the graph configuration view for this owner. If the requested
* link is neither the user graph configuration nor the default
* configuration then <code>null</code> is returned.
*
Expand Down Expand Up @@ -181,7 +221,7 @@ public Object getTrendDetails() {
* @return the details
*/
public Object getTrendDetails(final StaplerRequest request, final StaplerResponse response) {
return new TrendDetails(getProject(), getTrendGraph(request, response, "../../"), getTrendGraphId());
return new TrendDetails(getOwner(), getTrendGraph(request, response, "../../"), getTrendGraphId());
}

/**
Expand Down Expand Up @@ -283,7 +323,7 @@ public boolean canShowEnableTrendLink(final StaplerRequest request) {
* @return a view to configure the trend graph for the current user
*/
protected GraphConfigurationView createUserConfiguration(final StaplerRequest request) {
return new UserGraphConfigurationView(createConfiguration(), getProject(),
return new UserGraphConfigurationView(createConfiguration(), getOwner(),
getUrlName(), request.getCookies(), createBuildHistory());
}

Expand All @@ -293,7 +333,7 @@ protected GraphConfigurationView createUserConfiguration(final StaplerRequest re
* @return a view to configure the trend graph defaults
*/
protected GraphConfigurationView createDefaultConfiguration() {
return new DefaultGraphConfigurationView(createConfiguration(), getProject(),
return new DefaultGraphConfigurationView(createConfiguration(), getOwner(),
getUrlName(), createBuildHistory());
}

Expand All @@ -303,12 +343,12 @@ protected GraphConfigurationView createDefaultConfiguration() {
* @return build history
*/
protected BuildHistory createBuildHistory() {
AbstractBuild<?, ?> lastFinishedBuild = getLastFinishedBuild();
if (lastFinishedBuild == null) {
Run<?, ?> lastFinishedRun = getLastFinishedRun();
if (lastFinishedRun == null) {
return new NullBuildHistory();
}
else {
return new BuildHistory((Run<?, ?>) lastFinishedBuild, resultActionType, false, false);
return new BuildHistory(lastFinishedRun, resultActionType, false, false);
}
}

Expand Down Expand Up @@ -358,10 +398,10 @@ protected GraphConfiguration createConfiguration(final List<BuildResultGraph> av
}

/**
* Returns the icon URL for the side-panel in the project screen. If there
* Returns the icon URL for the side-panel in the owner screen. If there
* is no valid result yet, then <code>null</code> is returned.
*
* @return the icon URL for the side-panel in the project screen
* @return the icon URL for the side-panel in the owner screen
*/
@Override
public String getIconFileName() {
Expand All @@ -378,7 +418,7 @@ public final String getUrlName() {
}

/**
* Returns whether this project has a valid result action attached.
* Returns whether this owner has a valid result action attached.
*
* @return <code>true</code> if the results are valid
*/
Expand All @@ -394,43 +434,56 @@ public final boolean hasValidResults() {
*/
@CheckForNull
public ResultAction<?> getLastAction() {
AbstractBuild<?, ?> lastBuild = getLastFinishedBuild();
if (lastBuild == null) {
Run<?, ?> lastRun = getLastFinishedRun();
if (lastRun == null) {
return null;
}
else {
return getResultAction(lastBuild);
return getResultAction(lastRun);
}
}

/**
* Returns the result action for the specified build.
*
* @param lastBuild
* @param lastRun
* the build to get the action for
* @return the action or <code>null</code> if there is no such action
*/
@CheckForNull
protected T getResultAction(final AbstractBuild<?, ?> lastBuild) {
return lastBuild.getAction(resultActionType);
protected T getResultAction(final Run<?, ?> lastRun) {
return lastRun.getAction(resultActionType);
}

/**
* Returns the last finished build.
* Returns the last finished run.
*
* @return the last finished build or <code>null</code> if there is no
* such build
* @return the last finished run or <code>null</code> if there is no
* such run
*/
@CheckForNull @Exported
public AbstractBuild<?, ?> getLastFinishedBuild() {
if (project == null) {
public Run<?, ?> getLastFinishedRun() {
if (owner == null) {
return null;
}
AbstractBuild<?, ?> lastBuild = project.getLastBuild();
while (lastBuild != null && (lastBuild.isBuilding() || getResultAction(lastBuild) == null)) {
lastBuild = lastBuild.getPreviousBuild();
Run<?, ?> lastRun = owner.getLastBuild();
while (lastRun != null && (lastRun.isBuilding() || getResultAction(lastRun) == null)) {
lastRun = lastRun.getPreviousBuild();
}
return lastBuild;
return lastRun;
}

/**
* Returns the last finished build.
*
* @return the last finished build or <code>null</code> if there is no
* such build
* @deprecated use
* {@link #getLastFinishedRun()}
*/
@Deprecated @CheckForNull @Exported
public AbstractBuild<?, ?> getLastFinishedBuild() {
return (AbstractBuild<?, ?>) getLastFinishedRun();
}

/**
Expand All @@ -444,14 +497,14 @@ protected T getResultAction(final AbstractBuild<?, ?> lastBuild) {
* in case of an error
*/
public void doIndex(final StaplerRequest request, final StaplerResponse response) throws IOException {
AbstractBuild<?, ?> build = getLastFinishedBuild();
if (build != null) {
response.sendRedirect2(String.format("../%d/%s", build.getNumber(), resultUrl));
Run<?, ?> lastRun = getLastFinishedRun();
if (lastRun != null) {
response.sendRedirect2(String.format("../%d/%s", lastRun.getNumber(), resultUrl));
}
}

/**
* Creates a new instance of <code>AbstractProjectAction</code>.
* Creates a new instance of {@link AbstractProjectAction}.
*
* @param project
* the project that owns this action
Expand All @@ -460,10 +513,10 @@ public void doIndex(final StaplerRequest request, final StaplerResponse response
* @param plugin
* the plug-in that owns this action
* @deprecated use
* {@link #AbstractProjectAction(AbstractProject, Class, Localizable, Localizable, String, String, String)}
* {@link #AbstractProjectAction(Job, Class, Localizable, Localizable, String, String, String)}
*/
@Deprecated
public AbstractProjectAction(final AbstractProject<?, ?> project, final Class<? extends T> resultActionType, final PluginDescriptor plugin) {
this(project, resultActionType, null, null, plugin.getPluginName(), plugin.getIconUrl(), plugin.getPluginResultUrlName());
this((Job<?, ?>) project, resultActionType, null, null, plugin.getPluginName(), plugin.getIconUrl(), plugin.getPluginResultUrlName());
}
}
Expand Up @@ -8,6 +8,7 @@
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

import hudson.model.Job;
import hudson.model.AbstractProject;

import hudson.plugins.analysis.Messages;
Expand All @@ -19,6 +20,28 @@
public class DefaultGraphConfigurationView extends GraphConfigurationView {
private final String url;

/**
* Creates a new instance of {@link DefaultGraphConfigurationView}.
*
* @param configuration
* the graph configuration
* @param job
* the owning job to configure the graphs for
* @param pluginName
* The URL of the job action (there might be a one to many mapping to this defaults view)
* @param buildHistory
* the build history for this job
* @param url
* The URL of this view
*/
public DefaultGraphConfigurationView(final GraphConfiguration configuration, final Job<?, ?> job,
final String pluginName, final BuildHistory buildHistory, final String url) {
super(configuration, job, pluginName, buildHistory);
this.url = url;

configuration.initializeFromFile(createDefaultsFile(job, pluginName));
}

/**
* Creates a new instance of {@link DefaultGraphConfigurationView}.
*
Expand All @@ -32,15 +55,33 @@ public class DefaultGraphConfigurationView extends GraphConfigurationView {
* the build history for this project
* @param url
* The URL of this view
* @deprecated use
* {@link #DefaultGraphConfigurationView(GraphConfiguration, Job, String, BuildHistory, String)}
*/
@Deprecated
public DefaultGraphConfigurationView(final GraphConfiguration configuration, final AbstractProject<?, ?> project,
final String pluginName, final BuildHistory buildHistory, final String url) {
super(configuration, project, pluginName, buildHistory);
this.url = url;

configuration.initializeFromFile(createDefaultsFile(project, pluginName));
this(configuration, (Job<?, ?>) project, pluginName, buildHistory, url);
}


/**
* Creates a new instance of {@link DefaultGraphConfigurationView}.
*
* @param configuration
* the graph configuration
* @param job
* the owning job to configure the graphs for
* @param pluginName
* The name of the plug-in.
* @param buildHistory
* the build history for this job
*/
public DefaultGraphConfigurationView(final GraphConfiguration configuration, final Job<?, ?> job,
final String pluginName, final BuildHistory buildHistory) {
this(configuration, job, pluginName, buildHistory,
job.getAbsoluteUrl() + pluginName + "/configureDefaults");
}

/**
* Creates a new instance of {@link DefaultGraphConfigurationView}.
*
Expand All @@ -52,10 +93,13 @@ public DefaultGraphConfigurationView(final GraphConfiguration configuration, fin
* The name of the plug-in.
* @param buildHistory
* the build history for this project
* @deprecated use
* {@link #AbstractProjectAction(GraphConfiguration, Job, String, BuildHistory)}
*/
@Deprecated
public DefaultGraphConfigurationView(final GraphConfiguration configuration, final AbstractProject<?, ?> project,
final String pluginName, final BuildHistory buildHistory) {
this(configuration, project, pluginName, buildHistory,
this(configuration, (Job<?, ?>) project, pluginName, buildHistory,
project.getAbsoluteUrl() + pluginName + "/configureDefaults");
}

Expand Down

0 comments on commit 573e167

Please sign in to comment.