From fe290d757de8e0d24fb2e47f520b57545f999bda Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Fri, 8 Jul 2011 11:28:46 -0700 Subject: [PATCH] added AbstractBuild.getRootBuild() that matches up with AbstractProject.getRootProject() --- .../main/java/hudson/matrix/MatrixRun.java | 9 +++++++++ .../main/java/hudson/model/AbstractBuild.java | 18 ++++++++++++++++++ .../java/hudson/model/AbstractProject.java | 19 ++++++++++++++----- .../main/java/hudson/maven/MavenBuild.java | 8 ++++++++ 4 files changed, 49 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/hudson/matrix/MatrixRun.java b/core/src/main/java/hudson/matrix/MatrixRun.java index 0a43ae4e8e..42d9614034 100644 --- a/core/src/main/java/hudson/matrix/MatrixRun.java +++ b/core/src/main/java/hudson/matrix/MatrixRun.java @@ -24,6 +24,7 @@ package hudson.matrix; import hudson.FilePath; +import hudson.model.AbstractBuild; import hudson.slaves.WorkspaceList; import hudson.slaves.WorkspaceList.Lease; import static hudson.matrix.MatrixConfiguration.useShortWorkspaceName; @@ -85,6 +86,14 @@ public class MatrixRun extends Build { return getParent().getParent().getBuildByNumber(getNumber()); } + /** + * The same as {@link #getParentBuild()}. + */ + @Override + public AbstractBuild getRootBuild() { + return getParentBuild(); + } + @Override public String getDisplayName() { StaplerRequest req = Stapler.getCurrentRequest(); diff --git a/core/src/main/java/hudson/model/AbstractBuild.java b/core/src/main/java/hudson/model/AbstractBuild.java index 0254e59c3e..505b2d9416 100644 --- a/core/src/main/java/hudson/model/AbstractBuild.java +++ b/core/src/main/java/hudson/model/AbstractBuild.java @@ -185,6 +185,24 @@ public abstract class AbstractBuild

,R extends Abs return builtOn; } + /** + * Gets the nearest ancestor {@link AbstractBuild} that belongs to + * {@linkplain AbstractProject#getRootProject() the root project of getProject()} that + * dominates/governs/encompasses this build. + * + *

+ * Some projects (such as matrix projects, Maven projects, or promotion processes) form a tree of jobs, + * and still in some of them, builds of child projects are related/tied to that of the parent project. + * In such a case, this method returns the governing build. + * + * @return never null. In the worst case the build dominates itself. + * @since 1.421 + * @see AbstractProject#getRootProject() + */ + public AbstractBuild getRootBuild() { + return this; + } + /** * Used to render the side panel "Back to project" link. * diff --git a/core/src/main/java/hudson/model/AbstractProject.java b/core/src/main/java/hudson/model/AbstractProject.java index b9c70791a5..5d972dbc55 100644 --- a/core/src/main/java/hudson/model/AbstractProject.java +++ b/core/src/main/java/hudson/model/AbstractProject.java @@ -368,15 +368,24 @@ public abstract class AbstractProject

,R extends A } /** - * Returns the root project value. + * Gets the nearest ancestor {@link TopLevelItem} that's also an {@link AbstractProject}. * - * @return the root project value. + *

+ * Some projects (such as matrix projects, Maven projects, or promotion processes) form a tree of jobs + * that acts as a single unit. This method can be used to find the top most dominating job that + * covers such a tree. + * + * @return never null. + * @see AbstractBuild#getRootBuild() */ - public AbstractProject getRootProject() { - if (this.getParent() instanceof Jenkins) { + public AbstractProject getRootProject() { + if (this instanceof TopLevelItem) { return this; } else { - return ((AbstractProject) this.getParent()).getRootProject(); + ItemGroup p = this.getParent(); + if (p instanceof AbstractProject) + return ((AbstractProject) p).getRootProject(); + return this; } } diff --git a/maven-plugin/src/main/java/hudson/maven/MavenBuild.java b/maven-plugin/src/main/java/hudson/maven/MavenBuild.java index a17200d669..f5c0859cc1 100644 --- a/maven-plugin/src/main/java/hudson/maven/MavenBuild.java +++ b/maven-plugin/src/main/java/hudson/maven/MavenBuild.java @@ -178,6 +178,14 @@ public class MavenBuild extends AbstractMavenBuild { return getParent().getParent().getBuildByNumber(getNumber()); } + /** + * The same as {@link #getParentBuild()}. + */ + @Override + public AbstractBuild getRootBuild() { + return getParentBuild(); + } + /** * Gets the "governing" {@link MavenModuleSet} that has set * the workspace for this build. -- GitLab