Skip to content

Commit

Permalink
Merge pull request #9956 from krisstern/chore/stable-2.479/backportin…
Browse files Browse the repository at this point in the history
…g-2.479.2-1

Backporting 2.479.2 LTS
  • Loading branch information
basil authored Nov 10, 2024
2 parents 0f951d2 + cbebc8d commit 077613d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
2 changes: 1 addition & 1 deletion bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ THE SOFTWARE.
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.20</version>
<version>1.4.21</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
Expand Down
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
2 changes: 1 addition & 1 deletion core/src/main/java/jenkins/util/SetContextClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public final class SetContextClassLoader implements AutoCloseable {
* @since 2.362
*/
public SetContextClassLoader() {
this(StackWalker.getInstance().getCallerClass());
this(StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE).getCallerClass());
}

/**
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 077613d

Please sign in to comment.