From aea0a3dd17dfd4ccff60c297bd22ea1068f2b766 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Tue, 5 Nov 2024 12:52:10 -0800 Subject: [PATCH 1/2] [JENKINS-74795] Job created via REST API attaches to default view Co-authored-by: Mark Waite --- .../hudson/model/ModifiableItemGroup.java | 3 +- .../test/java/hudson/jobs/CreateItemTest.java | 57 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/hudson/model/ModifiableItemGroup.java b/core/src/main/java/hudson/model/ModifiableItemGroup.java index 35e41305afb5..d0bcda31187c 100644 --- a/core/src/main/java/hudson/model/ModifiableItemGroup.java +++ b/core/src/main/java/hudson/model/ModifiableItemGroup.java @@ -33,6 +33,7 @@ import org.kohsuke.stapler.StaplerRequest2; import org.kohsuke.stapler.StaplerResponse; import org.kohsuke.stapler.StaplerResponse2; +import org.kohsuke.stapler.interceptor.RequirePOST; /** * {@link ItemGroup} that is a general purpose container, which allows users and the rest of the program @@ -50,7 +51,7 @@ public interface ModifiableItemGroup extends ItemGroup { * The request format follows that of {@code <n:form xmlns:n="/lib/form">}. * */ - @StaplerNotDispatchable + @RequirePOST default T doCreateItem(StaplerRequest2 req, StaplerResponse2 rsp) throws IOException, ServletException { if (ReflectionUtils.isOverridden( ModifiableItemGroup.class, diff --git a/test/src/test/java/hudson/jobs/CreateItemTest.java b/test/src/test/java/hudson/jobs/CreateItemTest.java index df8c577e30dc..1f0dcd52ee57 100644 --- a/test/src/test/java/hudson/jobs/CreateItemTest.java +++ b/test/src/test/java/hudson/jobs/CreateItemTest.java @@ -25,19 +25,25 @@ package hudson.jobs; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.nullValue; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import hudson.model.Failure; import hudson.model.FreeStyleProject; import hudson.model.Item; import hudson.model.ItemGroup; +import hudson.model.ListView; +import hudson.model.User; import hudson.model.listeners.ItemListener; import java.net.HttpURLConnection; import java.net.URI; import java.net.URL; import java.text.MessageFormat; +import jenkins.model.Jenkins; import org.htmlunit.HttpMethod; import org.htmlunit.Page; import org.htmlunit.WebRequest; @@ -46,6 +52,7 @@ import org.junit.Test; import org.jvnet.hudson.test.Issue; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.MockAuthorizationStrategy; import org.jvnet.hudson.test.MockFolder; import org.jvnet.hudson.test.TestExtension; @@ -142,4 +149,54 @@ public void onCheckCopy(Item src, ItemGroup parent) throws Failure { } } + @Issue("JENKINS-74795") + @Test + public void testCreateItemDoesNotPopulateDefaultView() throws Exception { + // Create a view that only displays jobs that start with 'a-' + FreeStyleProject aJob = rule.createFreeStyleProject("a-freestyle-job"); + ListView aView = new ListView("a-view"); + aView.setIncludeRegex("a-.*"); + rule.jenkins.addView(aView); + assertThat(aView.getItems(), containsInAnyOrder(aJob)); + assertFalse(aView.isDefault()); // Not yet the default view + + // Create a view that only displays jobs that start with 'b-' + FreeStyleProject bJob = rule.createFreeStyleProject("b-freestyle-job"); + ListView bView = new ListView("b-view"); + bView.setIncludeRegex("b-.*"); + rule.jenkins.addView(bView); + assertThat(bView.getItems(), containsInAnyOrder(bJob)); + assertFalse(bView.isDefault()); // Not the default view + + // Make the a-view the default + rule.jenkins.setPrimaryView(aView); + assertTrue(aView.isDefault()); // Now a-view is the default view + + // Use createItem API to create a new job + User user = User.getById("user", true); + rule.jenkins.setSecurityRealm(rule.createDummySecurityRealm()); + rule.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy() + .grant(Jenkins.READ, Item.CREATE) + .everywhere() + .to(user.getId())); + String b2JobName = "b-freestyle-job-2"; + try (JenkinsRule.WebClient wc = rule.createWebClient()) { + wc.login(user.getId()); + WebRequest request = new WebRequest(wc.createCrumbedUrl("createItem?name=" + b2JobName), HttpMethod.POST); + request.setAdditionalHeader("Content-Type", "application/xml"); + request.setRequestBody(""); + wc.getPage(request); + } + FreeStyleProject b2Job = rule.jenkins.getItemByFullName(b2JobName, FreeStyleProject.class); + assertThat(bView.getItems(), containsInAnyOrder(bJob, b2Job)); + assertFalse(bView.isDefault()); + + // Confirm new job is not visible in default view + assertTrue(aView.isDefault()); // a-view is still the default view + assertThat(aView.getItems(), containsInAnyOrder(aJob)); + + // Confirm new job is visible in non-default view + assertFalse(bView.isDefault()); // b-view is not the default view + assertThat(bView.getItems(), containsInAnyOrder(bJob, b2Job)); + } } From dbeeeb3e8a1de63f127992a03b72aa38e8abfe70 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Thu, 7 Nov 2024 12:42:58 -0800 Subject: [PATCH 2/2] Update test/src/test/java/hudson/jobs/CreateItemTest.java Co-authored-by: Mark Waite --- test/src/test/java/hudson/jobs/CreateItemTest.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/src/test/java/hudson/jobs/CreateItemTest.java b/test/src/test/java/hudson/jobs/CreateItemTest.java index 1f0dcd52ee57..284200abdef8 100644 --- a/test/src/test/java/hudson/jobs/CreateItemTest.java +++ b/test/src/test/java/hudson/jobs/CreateItemTest.java @@ -194,9 +194,5 @@ public void testCreateItemDoesNotPopulateDefaultView() throws Exception { // Confirm new job is not visible in default view assertTrue(aView.isDefault()); // a-view is still the default view assertThat(aView.getItems(), containsInAnyOrder(aJob)); - - // Confirm new job is visible in non-default view - assertFalse(bView.isDefault()); // b-view is not the default view - assertThat(bView.getItems(), containsInAnyOrder(bJob, b2Job)); } }