Skip to content
Permalink
Browse files

Merge pull request #2760 from Vlatombe/JENKINS-41128

[FIXED JENKINS-41128] createItem in View not working when posting xml
  • Loading branch information...
daniel-beck committed Feb 26, 2017
2 parents 2a03dda + 84d9244 commit 8f8b058548a4b912d6a9e6fa1a4a0873a70598f7
@@ -319,16 +319,26 @@ public boolean isAddToCurrentView() {
}
}

private boolean needToAddToCurrentView(StaplerRequest req) throws ServletException {
String json = req.getParameter("json");
if (json != null && json.length() > 0) {
// Submitted via UI
JSONObject form = req.getSubmittedForm();
return form.has("addToCurrentView") && form.getBoolean("addToCurrentView");
} else {
// Submitted via API
return true;
}
}

@Override
@RequirePOST
public Item doCreateItem(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
JSONObject form = req.getSubmittedForm();
boolean addToCurrentView = form.has("addToCurrentView") && form.getBoolean("addToCurrentView");
ItemGroup<? extends TopLevelItem> ig = getOwnerItemGroup();
if (ig instanceof ModifiableItemGroup) {
TopLevelItem item = ((ModifiableItemGroup<? extends TopLevelItem>)ig).doCreateItem(req, rsp);
if (item!=null) {
if (addToCurrentView) {
if (needToAddToCurrentView(req)) {
synchronized (this) {
jobNames.add(item.getRelativeNameFrom(getOwnerItemGroup()));
}
@@ -37,27 +37,41 @@
import hudson.security.Permission;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

import org.acegisecurity.Authentication;

import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;

import org.apache.commons.io.IOUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsRule.WebClient;
import org.jvnet.hudson.test.MockFolder;
import org.jvnet.hudson.test.recipes.LocalData;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.xml.sax.SAXException;

public class ListViewTest {

@Rule public JenkinsRule j = new JenkinsRule();

@Rule public TestName testName = new TestName();

@Issue("JENKINS-15309")
@LocalData
@Test public void nullJobNames() throws Exception {
@@ -225,6 +239,26 @@ private void checkLinkFromItemExistsAndIsValid(Item item, ItemGroup ig, Item top
}
assertEquals(Collections.singletonList(p), v.getItems());
}

@Issue("JENKINS-41128")
@Test public void addJobUsingAPI() throws Exception {
ListView v = new ListView("view", j.jenkins);
j.jenkins.addView(v);
StaplerRequest req = mock(StaplerRequest.class);
StaplerResponse rsp = mock(StaplerResponse.class);

String configXml = IOUtils.toString(getClass().getResourceAsStream(String.format("%s/%s/config.xml", getClass().getSimpleName(), testName.getMethodName())), "UTF-8");

when(req.getMethod()).thenReturn("POST");
when(req.getParameter("name")).thenReturn("job1");
when(req.getInputStream()).thenReturn(new Stream(IOUtils.toInputStream(configXml)));
when(req.getContentType()).thenReturn("application/xml");
v.doCreateItem(req, rsp);
List<TopLevelItem> items = v.getItems();
assertEquals(1, items.size());
assertEquals("job1", items.get(0).getName());
}

private static class AllButViewsAuthorizationStrategy extends AuthorizationStrategy {
@Override public ACL getRootACL() {
return UNSECURED.getRootACL();
@@ -241,4 +275,29 @@ private void checkLinkFromItemExistsAndIsValid(Item item, ItemGroup ig, Item top
}
}

private static class Stream extends ServletInputStream {
private final InputStream inner;

public Stream(final InputStream inner) {
this.inner = inner;
}

@Override
public int read() throws IOException {
return inner.read();
}
@Override
public boolean isFinished() {
throw new UnsupportedOperationException();
}
@Override
public boolean isReady() {
throw new UnsupportedOperationException();
}
@Override
public void setReadListener(ReadListener readListener) {
throw new UnsupportedOperationException();
}
}

}
@@ -0,0 +1,16 @@
<?xml version='1.0' encoding='UTF-8'?>
<project>
<description></description>
<keepDependencies>false</keepDependencies>
<properties/>
<scm class="hudson.scm.NullSCM"/>
<canRoam>true</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<triggers/>
<concurrentBuild>false</concurrentBuild>
<builders/>
<publishers/>
<buildWrappers/>
</project>

0 comments on commit 8f8b058

Please sign in to comment.
You can’t perform that action at this time.