Skip to content

Commit

Permalink
Merge pull request #3 from DealerDotCom/feature/reload-jobs
Browse files Browse the repository at this point in the history
Reload job config when adding or deleting promotions.
  • Loading branch information
denschu committed Oct 30, 2015
2 parents 594e793 + 2bcc7c6 commit 670bceb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
<version>1.0-groovy-2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>3.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jenkinsci.plugins.jobdsl.promotions;
import groovy.lang.Closure;
import hudson.Extension;
import hudson.model.AbstractItem;
import hudson.model.Item;
import hudson.model.Items;
import hudson.util.IOUtils;
Expand Down Expand Up @@ -37,11 +38,15 @@ public Object promotions(Runnable closure, DslEnvironment dslEnvironment) {

@Override
public void notifyItemCreated(Item item, DslEnvironment dslEnvironment) {
notifyItemCreated(item, dslEnvironment, false);
}

public void notifyItemCreated(Item item, DslEnvironment dslEnvironment, boolean update) {
LOGGER.log(Level.INFO, String.format("Creating promotions for %s", item.getName()));
PromotionsContextHelper contextHelper = (PromotionsContextHelper) dslEnvironment.get("helper");
@SuppressWarnings("unchecked")
List<String> names = (List<String>) dslEnvironment.get("names");
if (names != null) {
if (names != null && names.size() > 0) {
for (String name : names) {
String xml = contextHelper.getSubXml(name);
File dir = new File(item.getRootDir(), "promotions/" + name);
Expand All @@ -51,13 +56,23 @@ public void notifyItemCreated(Item item, DslEnvironment dslEnvironment) {
InputStream in = new ByteArrayInputStream(xml.getBytes("UTF-8"));
IOUtils.copy(in, configXml);
LOGGER.log(Level.INFO, String.format("Added promotion with name %s for %s", name, item.getName()));
update = true;
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Error handling extension code", e);
} catch (IOException e) {
throw new IllegalStateException("Error handling extension code", e);
}
}
}

// Only update if a promotion was actually added, updated, or removed.
if(update) {
try {
((AbstractItem) item).doReload();
} catch(Exception e) {
throw new IllegalStateException("Unable to cast item to AbstractItem and reload config", e);
}
}
}

@Override
Expand All @@ -66,6 +81,7 @@ public void notifyItemUpdated(Item item, DslEnvironment dslEnvironment) {
@SuppressWarnings("unchecked")
List<String> newPromotions = (List<String>) dslEnvironment.get("names");
File dir = new File(item.getRootDir(), "promotions/");
boolean update = false;
//Delete removed promotions
if (newPromotions != null && dir != null){
File[] files = dir.listFiles();
Expand All @@ -74,12 +90,14 @@ public void notifyItemUpdated(Item item, DslEnvironment dslEnvironment) {
if (!newPromotions.contains(promotion.getName())){
promotion.delete();
LOGGER.log(Level.INFO, String.format("Deleted promotion with name %s for %s", promotion.getName(), item.getName()));
update = true;
}
}
}
}

//Delegate to create-method
this.notifyItemCreated(item, dslEnvironment);
this.notifyItemCreated(item, dslEnvironment, update);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ class PromotionsExtensionPointSpec extends Specification {
}
}
}, dslEnvironment)
FreeStyleProject item = new FreeStyleProject(Jenkins.getInstance(), 'freestyle')
FreeStyleProject item = Spy(FreeStyleProject, constructorArgs: [Jenkins.getInstance(), 'freestyle']) {
1 * doReload() >> {}
}
extensionPoint.notifyItemCreated(item, dslEnvironment)

then:
Expand Down

0 comments on commit 670bceb

Please sign in to comment.