Skip to content

Commit

Permalink
Add support for running promotions on inheritance projects (squashed)
Browse files Browse the repository at this point in the history
  • Loading branch information
J-cztery committed Nov 21, 2015
1 parent 2c7603e commit 63e1bfc
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 17 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@
<version>1.10</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>hudson.plugins</groupId>
<artifactId>project-inheritance</artifactId>
<version>1.5.3</version>
<optional>true</optional>
</dependency>
</dependencies>

<build>
Expand Down
46 changes: 29 additions & 17 deletions src/main/java/hudson/plugins/promoted_builds/JobPropertyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,18 @@ public final class JobPropertyImpl extends JobProperty<AbstractProject<?,?>> imp
* These {@link PromotionProcess}es are active.
*/
private final Set<String> activeProcessNames = new HashSet<String>();

// /**
// * Names of the processes that are configured.
// * Used to construct {@link #processes}.
// */
// private final List<String> names = new ArrayList<String>();

/**
* Programmatic construction.
*/
public JobPropertyImpl(AbstractProject<?,?> owner) throws Descriptor.FormException, IOException {
this.owner = owner;
init();
}

public JobPropertyImpl(JobPropertyImpl other, AbstractProject<?,?> owner) throws Descriptor.FormException, IOException {
this.owner = owner;
this.activeProcessNames.addAll(other.activeProcessNames);
loadAllProcesses(other.getRootDir());
}
private JobPropertyImpl(StaplerRequest req, JSONObject json) throws Descriptor.FormException, IOException {
// a hack to get the owning AbstractProject.
// this is needed here so that we can load items
Expand Down Expand Up @@ -112,14 +109,25 @@ private JobPropertyImpl(StaplerRequest req, JSONObject json) throws Descriptor.F
}
init();
}
private void loadAllProcesses(File rootDir) throws IOException {
File[] subdirs = rootDir.listFiles(new FileFilter() {
public boolean accept(File child) {
return child.isDirectory();
}
});

loadProcesses(subdirs);
}
private void init() throws IOException {
// load inactive processes
File[] subdirs = getRootDir().listFiles(new FileFilter() {
public boolean accept(File child) {
return child.isDirectory() && !isActiveProcessNameIgnoreCase(child.getName());
}
});
loadProcesses(subdirs);
}
private void loadProcesses(File[] subdirs) throws IOException {
if(subdirs!=null) {
for (File subdir : subdirs) {
try {
Expand Down Expand Up @@ -179,7 +187,7 @@ protected synchronized void setOwner(AbstractProject<?,?> owner) {
// so use this as the initialization opportunity.
// CopyListener is also using setOwner to re-init after copying config from another job.
processes = new ArrayList<PromotionProcess>(ItemGroupMixIn.<String,PromotionProcess>loadChildren(
this,getRootDir(),ItemGroupMixIn.KEYED_BY_NAME).values());
this,getRootDir(),ItemGroupMixIn.KEYED_BY_NAME).values());
try {
buildActiveProcess();
} catch (IOException e) {
Expand All @@ -201,7 +209,11 @@ private void buildActiveProcess() throws IOException {
// ensure that the name casing matches what's given in the activeProcessName
// this is because in case insensitive file system, we may end up resolving
// to a directory name that differs only in their case.
p.renameTo(getActiveProcessName(p.getName()));
String processName = p.getName();
String activeProcessName = getActiveProcessName(processName);
if (!activeProcessName.equals(processName)){
p.renameTo(activeProcessName);
}
}
}

Expand Down Expand Up @@ -352,16 +364,16 @@ public Action getJobAction(AbstractProject<?,?> job) {

@Extension
public static final class DescriptorImpl extends JobPropertyDescriptor {

public DescriptorImpl() {
super();
}
super();
}

public DescriptorImpl(Class<? extends JobProperty<?>> clazz) {
super(clazz);
}
public DescriptorImpl(Class<? extends JobProperty<?>> clazz) {
super(clazz);
}

public String getDisplayName() {
public String getDisplayName() {
return "Promote Builds When...";
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

package hudson.plugins.promoted_builds.inheritance;

import org.apache.log4j.Logger;

import hudson.Extension;
import hudson.model.JobProperty;

import hudson.plugins.project_inheritance.projects.InheritanceProject;
import hudson.plugins.project_inheritance.projects.inheritance.InheritanceSelector;

import hudson.plugins.promoted_builds.JobPropertyImpl;

/**
*
* @author Jacek Tomaka
* @since TODO
*/
@Extension(optional=true)
public class JobPropertyImplSelector extends InheritanceSelector<JobProperty<?>> {
private static final long serialVersionUID = 1L;
private static final Logger logger = Logger.getLogger(JobPropertyImplSelector.class);

@Override
public boolean isApplicableFor(Class<?> clazz){
return JobProperty.class.isAssignableFrom(clazz);
}

@Override
public InheritanceSelector.MODE getModeFor(Class<?> clazz){
if (JobPropertyImpl.class.isAssignableFrom(clazz)) return MODE.USE_LAST;
return MODE.NOT_RESPONSIBLE;
}

@Override
public String getObjectIdentifier(JobProperty<?> obj){
if ( obj!=null && JobPropertyImpl.class.getName().equals(obj.getClass().getName())){
return JobPropertyImplSelector.class.getName();
}
return null;
}

@Override
public JobPropertyImpl merge(JobProperty<?> prior, JobProperty<?> latter, InheritanceProject caller){
return null;
}

@Override
public JobProperty<?> handleSingleton(JobProperty<?> jobProperty, InheritanceProject caller){
if (jobProperty == null || caller == null) return jobProperty;
if (caller.isAbstract) return jobProperty;

if (!JobPropertyImpl.class.isAssignableFrom(jobProperty.getClass())) return jobProperty;


JobPropertyImpl jobPropertyImpl = (JobPropertyImpl)jobProperty;

try {
JobPropertyImpl newJobProperty = new JobPropertyImpl(jobPropertyImpl, caller);
return newJobProperty;
} catch (Exception ex){
logger.error("Error during hacking up JobPropertyImpl", ex );
}
return jobProperty;
}
}

0 comments on commit 63e1bfc

Please sign in to comment.