Skip to content

Commit cd4cf63

Browse files
basilkrisstern
authored andcommitted
[JENKINS-74795] Job created via REST API attaches to default view (#9947)
Co-authored-by: Mark Waite <[email protected]> (cherry picked from commit 8298257)
1 parent 5c2bcc5 commit cd4cf63

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

core/src/main/java/hudson/model/ModifiableItemGroup.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.kohsuke.stapler.StaplerRequest2;
3434
import org.kohsuke.stapler.StaplerResponse;
3535
import org.kohsuke.stapler.StaplerResponse2;
36+
import org.kohsuke.stapler.interceptor.RequirePOST;
3637

3738
/**
3839
* {@link ItemGroup} that is a general purpose container, which allows users and the rest of the program
@@ -50,7 +51,7 @@ public interface ModifiableItemGroup<T extends Item> extends ItemGroup<T> {
5051
* The request format follows that of {@code &lt;n:form xmlns:n="/lib/form">}.
5152
*
5253
*/
53-
@StaplerNotDispatchable
54+
@RequirePOST
5455
default T doCreateItem(StaplerRequest2 req, StaplerResponse2 rsp) throws IOException, ServletException {
5556
if (ReflectionUtils.isOverridden(
5657
ModifiableItemGroup.class,

test/src/test/java/hudson/jobs/CreateItemTest.java

+53
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,25 @@
2525
package hudson.jobs;
2626

2727
import static org.hamcrest.MatcherAssert.assertThat;
28+
import static org.hamcrest.Matchers.containsInAnyOrder;
2829
import static org.hamcrest.Matchers.nullValue;
2930
import static org.junit.Assert.assertEquals;
31+
import static org.junit.Assert.assertFalse;
3032
import static org.junit.Assert.assertNotNull;
33+
import static org.junit.Assert.assertTrue;
3134

3235
import hudson.model.Failure;
3336
import hudson.model.FreeStyleProject;
3437
import hudson.model.Item;
3538
import hudson.model.ItemGroup;
39+
import hudson.model.ListView;
40+
import hudson.model.User;
3641
import hudson.model.listeners.ItemListener;
3742
import java.net.HttpURLConnection;
3843
import java.net.URI;
3944
import java.net.URL;
4045
import java.text.MessageFormat;
46+
import jenkins.model.Jenkins;
4147
import org.htmlunit.HttpMethod;
4248
import org.htmlunit.Page;
4349
import org.htmlunit.WebRequest;
@@ -46,6 +52,7 @@
4652
import org.junit.Test;
4753
import org.jvnet.hudson.test.Issue;
4854
import org.jvnet.hudson.test.JenkinsRule;
55+
import org.jvnet.hudson.test.MockAuthorizationStrategy;
4956
import org.jvnet.hudson.test.MockFolder;
5057
import org.jvnet.hudson.test.TestExtension;
5158

@@ -142,4 +149,50 @@ public void onCheckCopy(Item src, ItemGroup parent) throws Failure {
142149
}
143150
}
144151

152+
@Issue("JENKINS-74795")
153+
@Test
154+
public void testCreateItemDoesNotPopulateDefaultView() throws Exception {
155+
// Create a view that only displays jobs that start with 'a-'
156+
FreeStyleProject aJob = rule.createFreeStyleProject("a-freestyle-job");
157+
ListView aView = new ListView("a-view");
158+
aView.setIncludeRegex("a-.*");
159+
rule.jenkins.addView(aView);
160+
assertThat(aView.getItems(), containsInAnyOrder(aJob));
161+
assertFalse(aView.isDefault()); // Not yet the default view
162+
163+
// Create a view that only displays jobs that start with 'b-'
164+
FreeStyleProject bJob = rule.createFreeStyleProject("b-freestyle-job");
165+
ListView bView = new ListView("b-view");
166+
bView.setIncludeRegex("b-.*");
167+
rule.jenkins.addView(bView);
168+
assertThat(bView.getItems(), containsInAnyOrder(bJob));
169+
assertFalse(bView.isDefault()); // Not the default view
170+
171+
// Make the a-view the default
172+
rule.jenkins.setPrimaryView(aView);
173+
assertTrue(aView.isDefault()); // Now a-view is the default view
174+
175+
// Use createItem API to create a new job
176+
User user = User.getById("user", true);
177+
rule.jenkins.setSecurityRealm(rule.createDummySecurityRealm());
178+
rule.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy()
179+
.grant(Jenkins.READ, Item.CREATE)
180+
.everywhere()
181+
.to(user.getId()));
182+
String b2JobName = "b-freestyle-job-2";
183+
try (JenkinsRule.WebClient wc = rule.createWebClient()) {
184+
wc.login(user.getId());
185+
WebRequest request = new WebRequest(wc.createCrumbedUrl("createItem?name=" + b2JobName), HttpMethod.POST);
186+
request.setAdditionalHeader("Content-Type", "application/xml");
187+
request.setRequestBody("<project/>");
188+
wc.getPage(request);
189+
}
190+
FreeStyleProject b2Job = rule.jenkins.getItemByFullName(b2JobName, FreeStyleProject.class);
191+
assertThat(bView.getItems(), containsInAnyOrder(bJob, b2Job));
192+
assertFalse(bView.isDefault());
193+
194+
// Confirm new job is not visible in default view
195+
assertTrue(aView.isDefault()); // a-view is still the default view
196+
assertThat(aView.getItems(), containsInAnyOrder(aJob));
197+
}
145198
}

0 commit comments

Comments
 (0)