Skip to content

Commit

Permalink
[JENKINS-74795] Job created via REST API attaches to default view (#9947
Browse files Browse the repository at this point in the history
)

Co-authored-by: Mark Waite <[email protected]>
  • Loading branch information
basil and MarkEWaite authored Nov 8, 2024
1 parent ac63b7f commit 8298257
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
3 changes: 2 additions & 1 deletion core/src/main/java/hudson/model/ModifiableItemGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -50,7 +51,7 @@ public interface ModifiableItemGroup<T extends Item> extends ItemGroup<T> {
* The request format follows that of {@code &lt;n:form xmlns:n="/lib/form">}.
*
*/
@StaplerNotDispatchable
@RequirePOST
default T doCreateItem(StaplerRequest2 req, StaplerResponse2 rsp) throws IOException, ServletException {
if (ReflectionUtils.isOverridden(
ModifiableItemGroup.class,
Expand Down
53 changes: 53 additions & 0 deletions test/src/test/java/hudson/jobs/CreateItemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -142,4 +149,50 @@ 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("<project/>");
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));
}
}

0 comments on commit 8298257

Please sign in to comment.