Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FIXED JENKINS-41128] createItem in View not working when posting xml
  • Loading branch information
Vlatombe committed Feb 21, 2017
1 parent 0e67ad4 commit 84d9244
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
16 changes: 13 additions & 3 deletions core/src/main/java/hudson/model/ListView.java
Expand Up @@ -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()));
}
Expand Down
59 changes: 59 additions & 0 deletions test/src/test/java/hudson/model/ListViewTest.java
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand All @@ -241,4 +275,29 @@ private static class AllButViewsAuthorizationStrategy extends AuthorizationStrat
}
}

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 84d9244

Please sign in to comment.