Skip to content

Commit

Permalink
[JENKINS-19146] Make work with BuildTrigger.
Browse files Browse the repository at this point in the history
  • Loading branch information
ikedam committed Aug 9, 2013
1 parent b1f7464 commit 7148295
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 6 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -29,8 +29,8 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<!-- tried 1.409 to get to latest before LTS, but has mvn 3 dependency :-( -->
<version>1.408</version>
<!-- DependencyGraph is final in Jenkins < 1.425 -->
<version>1.425</version>
</parent>

<artifactId>flexible-publish</artifactId>
Expand Down
@@ -0,0 +1,113 @@
/*
* The MIT License
*
* Copyright (c) 2013 IKEDA Yasuyuki
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jenkins_ci.plugins.flexible_publish;

import java.util.List;
import java.util.Set;

import org.jenkins_ci.plugins.run_condition.RunCondition;

import hudson.model.AbstractProject;
import hudson.model.DependencyGraph;

/**
* Wraps {@link DependencyGraph} and append {@link RunCondition} to {@link Dependency}.
*
* Methods other than addDependency are just calling methods of wrapped {@link DependencyGraph}
*/
public class ConditionalDependencyGraphWrapper extends DependencyGraph
{
private DependencyGraph graph;
private RunCondition condition;

public ConditionalDependencyGraphWrapper(DependencyGraph graph, RunCondition condition) {
this.graph = graph;
this.condition = condition;
}

/**
* Add dependency. {@link RunCondition} will be attached.
*
* @see hudson.model.DependencyGraph#addDependency(hudson.model.DependencyGraph.Dependency)
*/
@Override
public void addDependency(Dependency dep) {
graph.addDependency(new ConditionalDependencyWrapper(dep, condition));
}

@Override
public void build() {
graph.build();
}

@Override
public int compare(AbstractProject o1, AbstractProject o2) {
return graph.compare(o1, o2);
}

@Override
public <T> T getComputationalData(Class<T> key) {
return graph.getComputationalData(key);
}

@Override
public List<AbstractProject> getDownstream(AbstractProject p) {
return graph.getDownstream(p);
}

@Override
public List<Dependency> getDownstreamDependencies(AbstractProject p) {
return graph.getDownstreamDependencies(p);
}

@Override
public Set<AbstractProject> getTransitiveDownstream(AbstractProject src) {
return graph.getTransitiveDownstream(src);
}

@Override
public Set<AbstractProject> getTransitiveUpstream(AbstractProject src) {
return graph.getTransitiveUpstream(src);
}

@Override
public List<AbstractProject> getUpstream(AbstractProject p) {
return graph.getUpstream(p);
}

@Override
public List<Dependency> getUpstreamDependencies(AbstractProject p) {
return graph.getUpstreamDependencies(p);
}

@Override
public boolean hasIndirectDependencies(AbstractProject src, AbstractProject dst) {
return graph.hasIndirectDependencies(src, dst);
}

@Override
public <T> void putComputationalData(Class<T> key, T value) {
graph.putComputationalData(key, value);
}
}
@@ -0,0 +1,118 @@
/*
* The MIT License
*
* Copyright (c) 2013 IKEDA Yasuyuki
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jenkins_ci.plugins.flexible_publish;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jenkins_ci.plugins.run_condition.RunCondition;

import hudson.model.DependencyGraph.Dependency;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.BuildListener;
import hudson.model.StreamBuildListener;
import hudson.model.TaskListener;
import hudson.util.NullStream;

/**
* Wraps {@link Dependency} and evaluates {@link RunCondition} when the dependency is triggered.
*/
public class ConditionalDependencyWrapper extends Dependency {
private static Logger LOGGER = Logger.getLogger(ConditionalDependencyWrapper.class.getName());
private Dependency dep;
private RunCondition condition;

public ConditionalDependencyWrapper(Dependency dep, RunCondition condition) {
super(dep.getUpstreamProject(), dep.getDownstreamProject());
this.dep = dep;
this.condition = condition;
}

/**
* Determines whether the downstream project should be launched.
*
* {@link RunCondition} is evaluated.
*
* @see hudson.model.DependencyGraph.Dependency#shouldTriggerBuild(hudson.model.AbstractBuild, hudson.model.TaskListener, java.util.List)
*/
@Override
public boolean shouldTriggerBuild(AbstractBuild build,
TaskListener listener, List<Action> actions) {
BuildListener buildListener = null;
if (listener instanceof BuildListener) {
buildListener = (BuildListener)listener;
} else {
// Usually listener is instance of BuildListener,
// So there may be no case entering this path.
// If there's that case, BuildLister wrapping TaskListener should be written.
LOGGER.warning("There is no BuildListener, and logs from RunCondition won't be recorded.");
buildListener = new StreamBuildListener(new NullStream());
}

try {
if (condition.runPerform(build, buildListener)) {
return dep.shouldTriggerBuild(build, listener, actions);
} else {
return false;
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Failed to evaluate condition", e);
return false;
}
}

@Override
public boolean equals(Object obj) {
if (obj == null || getClass() != obj.getClass()) {
return false;
}

ConditionalDependencyWrapper d = (ConditionalDependencyWrapper)obj;

return dep.equals(d.dep) && condition.equals(d.condition);
}

@Override
public int hashCode() {
return dep.hashCode() * 23 + condition.hashCode();
}

@Override
public AbstractProject getDownstreamProject() {
return dep.getDownstreamProject();
}

@Override
public AbstractProject getUpstreamProject() {
return dep.getUpstreamProject();
}

@Override
public boolean pointsItself() {
return dep.pointsItself();
}
}
Expand Up @@ -31,6 +31,8 @@
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.BuildListener;
import hudson.model.DependecyDeclarer;
import hudson.model.DependencyGraph;
import hudson.model.Describable;
import hudson.model.Descriptor;
import hudson.model.Hudson;
Expand All @@ -46,7 +48,7 @@
import java.util.Collections;
import java.util.List;

public class ConditionalPublisher implements Describable<ConditionalPublisher> {
public class ConditionalPublisher implements Describable<ConditionalPublisher>, DependecyDeclarer {

private final RunCondition condition;
private final BuildStep publisher;
Expand Down Expand Up @@ -131,4 +133,11 @@ public Descriptor<? extends BuildStep> getDefaultPublisher() {

}

public void buildDependencyGraph(AbstractProject owner, DependencyGraph graph) {
if (publisher instanceof DependecyDeclarer) {
((DependecyDeclarer)publisher).buildDependencyGraph(owner,
new ConditionalDependencyGraphWrapper(graph, condition));
}
}

}
Expand Up @@ -41,8 +41,6 @@
public class DefaultPublisherDescriptorLister implements PublisherDescriptorLister {

public static final List<String> EXCLUSIONS = Arrays.asList(
"hudson.tasks.BuildTrigger.DescriptorImpl",
"hudson.plugins.parameterizedtrigger.BuildTrigger.DescriptorImpl",
"org.jenkins_ci.plugins.flexible_publish.FlexiblePublisher.FlexiblePublisherDescriptor"
);

Expand Down
Expand Up @@ -31,6 +31,8 @@
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.DependecyDeclarer;
import hudson.model.DependencyGraph;
import hudson.model.Descriptor;
import hudson.model.Hudson;
import hudson.model.Result;
Expand All @@ -49,7 +51,7 @@
import java.util.List;
import java.util.Set;

public class FlexiblePublisher extends Recorder {
public class FlexiblePublisher extends Recorder implements DependecyDeclarer{

public static final String PROMOTION_JOB_TYPE = "hudson.plugins.promoted_builds.PromotionProcess";

Expand Down Expand Up @@ -159,4 +161,10 @@ public Object readResolve() {

}

public void buildDependencyGraph(AbstractProject owner, DependencyGraph graph) {
for(ConditionalPublisher publisher: publishers) {
publisher.buildDependencyGraph(owner, graph);
}
}

}

0 comments on commit 7148295

Please sign in to comment.