Skip to content

Commit

Permalink
[FIXED JENKINS-8963] Added REST API to create promotion processes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Aug 25, 2014
1 parent ca0b5e5 commit 98a7966
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/main/java/hudson/plugins/promoted_builds/JobPropertyImpl.java
@@ -1,20 +1,24 @@
package hudson.plugins.promoted_builds;

import hudson.Extension;
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.BuildListener;
import hudson.model.Descriptor;
import hudson.model.Failure;
import hudson.model.Hudson;
import hudson.model.Item;
import hudson.model.ItemGroup;
import hudson.model.ItemGroupMixIn;
import hudson.model.Items;
import hudson.model.Job;
import hudson.model.JobProperty;
import hudson.model.JobPropertyDescriptor;
import hudson.model.listeners.ItemListener;
import hudson.remoting.Callable;
import hudson.util.IOUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

Expand All @@ -25,13 +29,15 @@
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.model.Jenkins;

/**
* Promotion processes defined for a project.
Expand Down Expand Up @@ -236,6 +242,35 @@ public List<PromotionProcess> getActiveItems() {
return activeProcesses;
}

/** @see ItemGroupMixIn#createProjectFromXML */
public PromotionProcess createProcessFromXml(final String name, InputStream xml) throws IOException {
owner.checkPermission(Item.CONFIGURE); // CREATE is ItemGroup-scoped and owner is not an ItemGroup
Jenkins.getInstance().getProjectNamingStrategy().checkName(name);
if (getItem(name) != null) {
throw new IllegalArgumentException(owner.getDisplayName() + " already contains an item '" + name + "'");
}
File configXml = Items.getConfigFile(getRootDirFor(name)).getFile();
File dir = configXml.getParentFile();
dir.mkdirs();
try {
IOUtils.copy(xml, configXml);
PromotionProcess result = Items.whileUpdatingByXml(new Callable<PromotionProcess,IOException>() {
@Override public PromotionProcess call() throws IOException {
setOwner(owner);
return getItem(name);
}
});
if (result == null) {
throw new IOException("failed to load from " + configXml);
}
ItemListener.fireOnCreated(result);
return result;
} catch (IOException e) {
Util.deleteRecursive(dir);
throw e;
}
}

/**
* Gets {@link AbstractProject} that contains us.
*/
Expand Down
Expand Up @@ -5,12 +5,18 @@
import hudson.model.Api;
import hudson.model.PermalinkProjectAction;
import hudson.model.ProminentProjectAction;
import java.io.IOException;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.HttpResponses;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import org.kohsuke.stapler.interceptor.RequirePOST;

/**
* For customizing project top-level GUI.
Expand Down Expand Up @@ -95,6 +101,12 @@ public List<Permalink> getPermalinks() {
return r;
}

@RequirePOST
public HttpResponse doCreateProcess(@QueryParameter String name, StaplerRequest req) throws IOException {
property.createProcessFromXml(name, req.getInputStream());
return HttpResponses.ok();
}

public String getIconFileName() {
return "star.png";
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/hudson/plugins/promoted_builds/RemoteApiTest.java
Expand Up @@ -104,4 +104,21 @@ public class RemoteApiTest {
assertEquals(0, promotion.getActiveItems().size());
}

@Test public void create() throws Exception {
FreeStyleProject p = r.createFreeStyleProject("p");
JobPropertyImpl promotion = new JobPropertyImpl(p);
p.addProperty(promotion);
assertEquals(0, promotion.getItems().size());
assertEquals(0, promotion.getActiveItems().size());
JenkinsRule.WebClient wc = r.createWebClient();
WebRequestSettings req = new WebRequestSettings(new URL(wc.createCrumbedUrl("job/p/promotion/createProcess") + "&name=promo"), HttpMethod.POST);
req.setRequestBody("<hudson.plugins.promoted__builds.PromotionProcess><conditions><hudson.plugins.promoted__builds.conditions.SelfPromotionCondition><evenIfUnstable>true</evenIfUnstable></hudson.plugins.promoted__builds.conditions.SelfPromotionCondition></conditions></hudson.plugins.promoted__builds.PromotionProcess>");
wc.getPage(req);
assertEquals(1, promotion.getItems().size());
assertEquals("not yet in use", 0, promotion.getActiveItems().size());
PromotionProcess proc = promotion.getItem("promo");
assertNotNull(proc);
assertTrue(proc.conditions.get(SelfPromotionCondition.class).isEvenIfUnstable());
}

}

1 comment on commit 98a7966

@shenghuofei
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you give a example?

Please sign in to comment.