Skip to content

Commit

Permalink
Supports promoted-builds plugin (thanks @juanpablo-santos)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Carter committed Jun 23, 2015
1 parent 4fd1585 commit 6f8e956
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@
<artifactId>matrix-auth</artifactId>
<version>1.2</version>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>promoted-builds</artifactId>
<version>2.21</version>
<optional>true</optional>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import hudson.model.JobProperty;
import hudson.model.JobPropertyDescriptor;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

import java.util.Collection;
Expand All @@ -17,7 +18,6 @@ public class TemplateProperty extends JobProperty<AbstractProject<?, ?>> {
public static Collection<AbstractProject> getImplementations(final String templateFullName) {
Collection<AbstractProject> projects = ProjectUtils.findProjectsWithProperty(TemplateImplementationProperty.class);
return Collections2.filter(projects, new Predicate<AbstractProject>() {
@Override
public boolean apply(AbstractProject abstractProject) {
TemplateImplementationProperty prop = (TemplateImplementationProperty) abstractProject.getProperty(TemplateImplementationProperty.class);
return templateFullName.equals(prop.getTemplateJobName());
Expand All @@ -26,6 +26,10 @@ public boolean apply(AbstractProject abstractProject) {

}

@DataBoundConstructor
public TemplateProperty() {
}

public Collection<AbstractProject> getImplementations() {
return getImplementations(owner.getFullName());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.joelj.jenkins.eztemplates.promotedbuilds;

import hudson.Util;
import hudson.model.AbstractProject;
import hudson.plugins.promoted_builds.JobPropertyImpl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Logger;


/**
* Utility class to handle the promoted builds plugin.
*/
public class PromotedBuildsTemplateUtils {

private static Logger LOG = Logger.getLogger("ez-templates");

/**
* Adds all the promotions from the template project into the implementation one. All existing promotions from the
* implementation project are lost.
*
* @param implementationProject
* @param templateProject
* @throws IOException
*/
public static void addPromotions(AbstractProject implementationProject, AbstractProject templateProject) throws IOException {

JobPropertyImpl promotions = (JobPropertyImpl) implementationProject.getProperty(JobPropertyImpl.class);
if (promotions != null) {
LOG.info(String.format("Merging [%s].", promotions.getFullDisplayName()));

// remove existing promotions on implementationProject, if any
implementationProject.removeProperty(JobPropertyImpl.class);
Util.deleteRecursive(new File(implementationProject.getRootDir(), "promotions"));
promotions.getItems().clear();

// obtain templateProject promotions. Each promotion is stored under a different folder under $JOB/promotions
File templatePromotions = new File(templateProject.getRootDir(), "promotions");
String[] list = templatePromotions.list();
if (list != null) {
for (String promotionDir : list) {
File templatePromotionProcess = new File(templatePromotions, promotionDir);
if (templatePromotionProcess.isDirectory()) {
// for each promotion, create a process from its configuration
promotions.createProcessFromXml(promotionDir, new FileInputStream(new File(templatePromotionProcess, "config.xml")));
}
}
}

// update implementationProject with the resulting promotions
implementationProject.addProperty(promotions);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.joelj.jenkins.eztemplates.TemplateImplementationProperty;
import com.joelj.jenkins.eztemplates.TemplateProperty;
import com.joelj.jenkins.eztemplates.promotedbuilds.PromotedBuildsTemplateUtils;
import hudson.matrix.AxisList;
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixProject;
Expand All @@ -11,6 +12,7 @@
import hudson.util.CopyOnWriteList;
import hudson.security.*;
import hudson.scm.SCM;
import jenkins.model.Jenkins;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
Expand Down Expand Up @@ -146,6 +148,10 @@ public static void handleTemplateImplementationSaved(AbstractProject implementat
implementationProject.addProperty(oldOwnership);
}

if (Jenkins.getInstance().getPlugin("promoted-builds") != null) {
PromotedBuildsTemplateUtils.addPromotions(implementationProject, templateProject);
}

ProjectUtils.silentSave(implementationProject);
}

Expand Down

0 comments on commit 6f8e956

Please sign in to comment.