Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-74795] createItem API should not assign new jobs to default view #9933

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/src/main/java/hudson/model/ListView.java
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,8 @@ private boolean needToAddToCurrentView(StaplerRequest2 req) throws ServletExcept
JSONObject form = req.getSubmittedForm();
return form.has("addToCurrentView") && form.getBoolean("addToCurrentView");
} else {
// Submitted via API
return true;
// Submitted via API - JENKINS-74975
return false;
}
}

Expand Down
52 changes: 52 additions & 0 deletions test/src/test/java/hudson/jobs/CreateItemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@
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.listeners.ItemListener;
import java.net.HttpURLConnection;
import java.net.URI;
Expand Down Expand Up @@ -63,6 +67,54 @@ public void setup() {
rule.jenkins.setSecurityRealm(rule.createDummySecurityRealm());
}

@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

// Disable crumbs for easier API testing
rule.jenkins.setCrumbIssuer(null);

// Use createItem API to create a new job by copying existing job
String b2JobName = "b-freestyle-job-2";
URL apiURL = new URI(MessageFormat.format("{0}createItem?mode=copy&from={1}&name={2}",
rule.getURL().toString(), bJob.getName(), b2JobName)).toURL();
WebRequest request = new WebRequest(apiURL, HttpMethod.POST);
deleteContentTypeHeader(request);
Page p = rule.createWebClient()
.withThrowExceptionOnFailingStatusCode(false)
.getPage(request);
assertEquals("Creating job from copy should succeed.",
HttpURLConnection.HTTP_OK,
p.getWebResponse().getStatusCode());
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));
}

@Issue("JENKINS-31235")
@Test
public void testCreateItemFromCopy() throws Exception {
Expand Down
37 changes: 36 additions & 1 deletion test/src/test/java/hudson/model/ItemsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
Expand Down Expand Up @@ -185,12 +187,45 @@ private void overwriteTargetSetUp() throws Exception {
grant(Item.DISCOVER).onPaths("known").to("attacker"));
}

private void createPrimaryView() throws Exception {
// Create a view that only displays jobs that start with 'a-'
ListView aView = new ListView("a-view");
aView.setIncludeRegex("a-.*");
r.jenkins.addView(aView);
assertThat(aView.getItems(), is(empty()));
assertFalse(aView.isDefault()); // Not yet the primary view

// Create a view that only displays jobs that start with 'b-'
ListView bView = new ListView("b-view");
bView.setIncludeRegex("b-.*");
r.jenkins.addView(bView);
assertThat(bView.getItems(), is(empty()));
assertFalse(bView.isDefault()); // Not the primary view

// Make the a-view the primary view
r.jenkins.setPrimaryView(aView);
assertTrue(aView.isDefault()); // Now a-view is the primary view
}

/* JENKINS-74795 notes that new items created through the REST API
* are made visible in the default view with 2.475-2.483.
* They were not made visible in the default view with 2.474 and
* earlier.
*/
private void assertPrimaryViewEmpty() throws Exception {
// Confirm no job is visible in primary view
View view = r.jenkins.getPrimaryView();
assertTrue(view.isDefault());
assertThat(view.getItems(), is(empty()));
}

/** Control cases: if there is no such item yet, nothing is stopping you. */
@Test public void overwriteNonexistentTarget() throws Exception {
overwriteTargetSetUp();
createPrimaryView();
for (OverwriteTactic tactic : OverwriteTactic.values()) {
tactic.run(r, "nonexistent");
System.out.println(tactic + " worked as expected on a nonexistent target");
assertPrimaryViewEmpty();
r.jenkins.getItem("nonexistent").delete();
}
}
Expand Down
Loading