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-72988] validate displayname against items in the same ItemGroup #9152

Merged
merged 7 commits into from
Sep 9, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@
}

@Restricted(NoExternalUse.class)
public FormValidation doCheckDisplayNameOrNull(@AncestorInPath TopLevelItem item, @QueryParameter String value) {

Check warning on line 295 in core/src/main/java/hudson/model/TopLevelItemDescriptor.java

View check run for this annotation

ci.jenkins.io / Java Compiler

spotless:check

NORMAL: Note: Generating hudson/model/TopLevelItemDescriptor/doCheckDisplayNameOrNull.stapler

Check warning on line 295 in core/src/main/java/hudson/model/TopLevelItemDescriptor.java

View check run for this annotation

ci.jenkins.io / Java Compiler

NORMAL: Note: Generating hudson/model/TopLevelItemDescriptor/doCheckDisplayNameOrNull.stapler

Check warning on line 295 in core/src/main/java/hudson/model/TopLevelItemDescriptor.java

View check run for this annotation

ci.jenkins.io / Java Compiler

compiler:testCompile

NORMAL: Note: Generating hudson/model/TopLevelItemDescriptor/doCheckDisplayNameOrNull.stapler
return Jenkins.get().doCheckDisplayName(value, item.getName());
return Jenkins.get().checkDisplayName(value, item);
}
}
41 changes: 35 additions & 6 deletions core/src/main/java/jenkins/model/Jenkins.java
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@
* @author Kohsuke Kawaguchi
*/
@ExportedBean
public class Jenkins extends AbstractCIBase implements DirectlyModifiableTopLevelItemGroup, StaplerProxy, StaplerFallback,

Check warning on line 346 in core/src/main/java/jenkins/model/Jenkins.java

View check run for this annotation

ci.jenkins.io / Java Compiler

compiler:testCompile

NORMAL: Note: Generating jenkins/model/Jenkins.javadoc
ModifiableViewGroup, AccessControlled, DescriptorByNameOwner,
ModelObjectWithContextMenu, ModelObjectWithChildren, OnMaster, Loadable {
private final transient Queue queue;
Expand Down Expand Up @@ -2388,7 +2388,7 @@
return false;
}

public FormValidation doCheckNumExecutors(@QueryParameter String value) {

Check warning on line 2391 in core/src/main/java/jenkins/model/Jenkins.java

View check run for this annotation

ci.jenkins.io / Java Compiler

compiler:testCompile

NORMAL: Note: Generating jenkins/model/Jenkins/DescriptorImpl/doCheckNumExecutors.stapler
return FormValidation.validateNonNegativeInteger(value);
}

Expand Down Expand Up @@ -4195,7 +4195,7 @@
* @since 2.414
*/
@RequirePOST
public HttpRedirect doQuietDown(@QueryParameter boolean block,

Check warning on line 4198 in core/src/main/java/jenkins/model/Jenkins.java

View check run for this annotation

ci.jenkins.io / Java Compiler

compiler:testCompile

NORMAL: Note: Generating jenkins/model/Jenkins/doQuietDown.stapler
@QueryParameter int timeout,
@QueryParameter @CheckForNull String message,
@QueryParameter boolean safeRestart) throws InterruptedException, IOException {
Expand Down Expand Up @@ -4633,7 +4633,7 @@
*
* @since 2.414
*/
public HttpResponse doSafeRestart(StaplerRequest req, @QueryParameter("message") String message) throws IOException, ServletException, RestartNotSupportedException {

Check warning on line 4636 in core/src/main/java/jenkins/model/Jenkins.java

View check run for this annotation

ci.jenkins.io / Java Compiler

compiler:testCompile

NORMAL: Note: Generating jenkins/model/Jenkins/doSafeRestart.stapler
checkPermission(MANAGE);
if (req != null && req.getMethod().equals("GET")) {
return HttpResponses.forwardToView(this, "_safeRestart.jelly");
Expand Down Expand Up @@ -4959,7 +4959,7 @@
/**
* If the user chose the default JDK, make sure we got 'java' in PATH.
*/
public FormValidation doDefaultJDKCheck(StaplerRequest request, @QueryParameter String value) {

Check warning on line 4962 in core/src/main/java/jenkins/model/Jenkins.java

View check run for this annotation

ci.jenkins.io / Java Compiler

compiler:testCompile

NORMAL: Note: Generating jenkins/model/Jenkins/doDefaultJDKCheck.stapler
if (!JDK.isDefaultName(value))
// assume the user configured named ones properly in system config ---
// or else system config should have reported form field validation errors.
Expand All @@ -4976,7 +4976,7 @@
* Checks if a top-level view with the given name exists and
* make sure that the name is good as a view name.
*/
public FormValidation doCheckViewName(@QueryParameter String value) {

Check warning on line 4979 in core/src/main/java/jenkins/model/Jenkins.java

View check run for this annotation

ci.jenkins.io / Java Compiler

compiler:testCompile

NORMAL: Note: Generating jenkins/model/Jenkins/doCheckViewName.stapler
checkPermission(View.CREATE);

String name = fixEmpty(value);
Expand All @@ -5002,7 +5002,7 @@
* @deprecated 1.512
*/
@Deprecated
public FormValidation doViewExistsCheck(@QueryParameter String value) {

Check warning on line 5005 in core/src/main/java/jenkins/model/Jenkins.java

View check run for this annotation

ci.jenkins.io / Java Compiler

compiler:testCompile

NORMAL: Note: Generating jenkins/model/Jenkins/doViewExistsCheck.stapler
checkPermission(View.CREATE);

String view = fixEmpty(value);
Expand Down Expand Up @@ -5290,8 +5290,9 @@
* job that the user is configuring though to prevent a validation warning
* if the user sets the displayName to what it currently is.
*/
boolean isDisplayNameUnique(String displayName, String currentJobName) {
Collection<TopLevelItem> itemCollection = items.values();
boolean isDisplayNameUnique(ItemGroup<?> itemGroup, String displayName, String currentJobName) {

Collection<TopLevelItem> itemCollection = (Collection<TopLevelItem>) itemGroup.getItems(t -> t instanceof TopLevelItem);

// if there are a lot of projects, we'll have to store their
// display names in a HashSet or something for a quick check
Expand All @@ -5315,8 +5316,8 @@
* @param name The name to test
* @param currentJobName The name of the job that the user is configuring
*/
boolean isNameUnique(String name, String currentJobName) {
Item item = getItem(name);
boolean isNameUnique(ItemGroup<?> itemGroup, String name, String currentJobName) {
Item item = itemGroup.getItem(name);

if (null == item) {
// the candidate name didn't return any items so the name is unique
Expand All @@ -5338,17 +5339,45 @@
* existing display names or project names
* @param displayName The display name to test
* @param jobName The name of the job the user is configuring
*
* @deprecated use {@link TopLevelItemDescriptor#doCheckDisplayNameOrNull(TopLevelItem, String)}
*/
@Deprecated
public FormValidation doCheckDisplayName(@QueryParameter String displayName,

Check warning on line 5346 in core/src/main/java/jenkins/model/Jenkins.java

View check run for this annotation

ci.jenkins.io / Java Compiler

spotless:check

NORMAL: Note: Generating jenkins/model/Jenkins/doCheckDisplayName.stapler

Check warning on line 5346 in core/src/main/java/jenkins/model/Jenkins.java

View check run for this annotation

ci.jenkins.io / Java Compiler

NORMAL: Note: Generating jenkins/model/Jenkins/doCheckDisplayName.stapler

Check warning on line 5346 in core/src/main/java/jenkins/model/Jenkins.java

View check run for this annotation

ci.jenkins.io / Java Compiler

compiler:testCompile

NORMAL: Note: Generating jenkins/model/Jenkins/doCheckDisplayName.stapler
@QueryParameter String jobName) {
displayName = displayName.trim();

LOGGER.fine(() -> "Current job name is " + jobName);

if (!isNameUnique(displayName, jobName)) {
if (!isNameUnique(this, displayName, jobName)) {

Check warning on line 5352 in core/src/main/java/jenkins/model/Jenkins.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 5352 is only partially covered, one branch is missing
return FormValidation.warning(Messages.Jenkins_CheckDisplayName_NameNotUniqueWarning(displayName));
}
else if (!isDisplayNameUnique(this, displayName, jobName)) {

Check warning on line 5355 in core/src/main/java/jenkins/model/Jenkins.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 5355 is only partially covered, one branch is missing
return FormValidation.warning(Messages.Jenkins_CheckDisplayName_DisplayNameNotUniqueWarning(displayName));

Check warning on line 5356 in core/src/main/java/jenkins/model/Jenkins.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 5356 is not covered by tests
}
else {
return FormValidation.ok();
}
}

/**
* Checks to see if the candidate displayName collides with any
* existing display names or project names in the items parent group
* @param displayName The display name to test
* @param item The item to check for duplicates
*/
@Restricted(NoExternalUse.class)
public FormValidation checkDisplayName(String displayName,
TopLevelItem item) {
displayName = displayName.trim();
String jobName = item.getName();

LOGGER.fine(() -> "Current job name is " + jobName);

if (!isNameUnique(item.getParent(), displayName, jobName)) {
return FormValidation.warning(Messages.Jenkins_CheckDisplayName_NameNotUniqueWarning(displayName));
}
else if (!isDisplayNameUnique(displayName, jobName)) {
else if (!isDisplayNameUnique(item.getParent(), displayName, jobName)) {
return FormValidation.warning(Messages.Jenkins_CheckDisplayName_DisplayNameNotUniqueWarning(displayName));
}
else {
Expand Down
20 changes: 10 additions & 10 deletions test/src/test/java/jenkins/model/JenkinsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ public void testIsDisplayNameUniqueTrue() throws Exception {
p.setDisplayName("displayName");

Jenkins jenkins = Jenkins.get();
assertTrue(jenkins.isDisplayNameUnique("displayName1", curJobName));
assertTrue(jenkins.isDisplayNameUnique(jobName, curJobName));
assertTrue(jenkins.isDisplayNameUnique(jenkins, "displayName1", curJobName));
assertTrue(jenkins.isDisplayNameUnique(jenkins, jobName, curJobName));
}

@Test
Expand All @@ -220,7 +220,7 @@ public void testIsDisplayNameUniqueFalse() throws Exception {
p.setDisplayName(displayName);

Jenkins jenkins = Jenkins.get();
assertFalse(jenkins.isDisplayNameUnique(displayName, curJobName));
assertFalse(jenkins.isDisplayNameUnique(jenkins, displayName, curJobName));
}

@Test
Expand All @@ -233,7 +233,7 @@ public void testIsDisplayNameUniqueSameAsCurrentJob() throws Exception {

Jenkins jenkins = Jenkins.get();
// should be true as we don't test against the current job
assertTrue(jenkins.isDisplayNameUnique(displayName, curJobName));
assertTrue(jenkins.isDisplayNameUnique(jenkins, displayName, curJobName));
}

@Test
Expand All @@ -244,7 +244,7 @@ public void testIsNameUniqueTrue() throws Exception {
j.createFreeStyleProject(jobName);

Jenkins jenkins = Jenkins.get();
assertTrue(jenkins.isNameUnique("jobName1", curJobName));
assertTrue(jenkins.isNameUnique(jenkins, "jobName1", curJobName));
}

@Test
Expand All @@ -255,7 +255,7 @@ public void testIsNameUniqueFalse() throws Exception {
j.createFreeStyleProject(jobName);

Jenkins jenkins = Jenkins.get();
assertFalse(jenkins.isNameUnique(jobName, curJobName));
assertFalse(jenkins.isNameUnique(jenkins, jobName, curJobName));
}

@Test
Expand All @@ -267,7 +267,7 @@ public void testIsNameUniqueSameAsCurrentJob() throws Exception {

Jenkins jenkins = Jenkins.get();
// true because we don't test against the current job
assertTrue(jenkins.isNameUnique(curJobName, curJobName));
assertTrue(jenkins.isNameUnique(jenkins, curJobName, curJobName));
}

@Test
Expand All @@ -281,7 +281,7 @@ public void testDoCheckDisplayNameUnique() throws Exception {
p.setDisplayName("displayName");

Jenkins jenkins = Jenkins.get();
FormValidation v = jenkins.doCheckDisplayName("1displayName", curJobName);
FormValidation v = jenkins.checkDisplayName("1displayName", curProject);
assertEquals(FormValidation.ok(), v);
}

Expand All @@ -297,7 +297,7 @@ public void testDoCheckDisplayNameSameAsDisplayName() throws Exception {
p.setDisplayName(displayName);

Jenkins jenkins = Jenkins.get();
FormValidation v = jenkins.doCheckDisplayName(displayName, curJobName);
FormValidation v = jenkins.checkDisplayName(displayName, curProject);
assertEquals(FormValidation.Kind.WARNING, v.kind);
}

Expand All @@ -313,7 +313,7 @@ public void testDoCheckDisplayNameSameAsJobName() throws Exception {
p.setDisplayName(displayName);

Jenkins jenkins = Jenkins.get();
FormValidation v = jenkins.doCheckDisplayName(jobName, curJobName);
FormValidation v = jenkins.checkDisplayName(jobName, curProject);
assertEquals(FormValidation.Kind.WARNING, v.kind);
}

Expand Down
Loading