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-68602] parent name for ProjectNamingStrategy #6598

Merged
merged 10 commits into from
Sep 5, 2022
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/model/AbstractItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public HttpResponse doConfirmRename(@QueryParameter String newName) throws IOExc
if (newName.equals(name)) {
return FormValidation.warning(Messages.AbstractItem_NewNameUnchanged());
}
Jenkins.get().getProjectNamingStrategy().checkName(newName);
Jenkins.get().getProjectNamingStrategy().checkName(getParent().getFullName(), newName);
checkIfNameIsUsed(newName);
checkRename(newName);
} catch (Failure e) {
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/hudson/model/ItemGroupMixIn.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public synchronized <T extends TopLevelItem> T copy(T src, String name) throws I
public synchronized TopLevelItem createProjectFromXML(String name, InputStream xml) throws IOException {
acl.checkPermission(Item.CREATE);

Jenkins.get().getProjectNamingStrategy().checkName(name);
Jenkins.get().getProjectNamingStrategy().checkName(parent.getFullName(), name);
Items.verifyItemDoesNotAlreadyExist(parent, name, null);
Jenkins.checkGoodName(name);

Expand Down Expand Up @@ -315,7 +315,7 @@ public synchronized TopLevelItem createProject(TopLevelItemDescriptor type, Stri
type.checkApplicableIn(parent);
acl.getACL().checkCreatePermission(parent, type);

Jenkins.get().getProjectNamingStrategy().checkName(name);
Jenkins.get().getProjectNamingStrategy().checkName(parent.getFullName(), name);
Items.verifyItemDoesNotAlreadyExist(parent, name, null);
Jenkins.checkGoodName(name);

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/model/Job.java
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,7 @@ public synchronized void doConfigSubmit(StaplerRequest req,

final ProjectNamingStrategy namingStrategy = Jenkins.get().getProjectNamingStrategy();
if (namingStrategy.isForceExistingJobs()) {
namingStrategy.checkName(name);
namingStrategy.checkName(getParent().getFullName(), name);
}
FormApply.success(".").generateResponse(req, rsp, null);
} catch (JSONException e) {
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/hudson/model/View.java
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,8 @@ public FormValidation doCheckJobName(@QueryParameter String value) {
try {
Jenkins.checkGoodName(value);
value = value.trim(); // why trim *after* checkGoodName? not sure, but ItemGroupMixIn.createTopLevelItem does the same
Jenkins.get().getProjectNamingStrategy().checkName(value);
ItemGroup<?> parent = getOwner().getItemGroup();
Jenkins.get().getProjectNamingStrategy().checkName(parent.getFullName(), value);
} catch (Failure e) {
return FormValidation.error(e.getMessage());
}
Expand Down
18 changes: 18 additions & 0 deletions core/src/main/java/jenkins/model/ProjectNamingStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,29 @@ public static DescriptorExtensionList<ProjectNamingStrategy, ProjectNamingStrate
* the name given from the UI
* @throws Failure
* if the user has to be informed about an illegal name, forces the user to change the name before submitting. The message of the failure will be presented to the user.
* @deprecated Use {@link #checkName(String, String)}
*/
@Deprecated
public void checkName(String name) throws Failure {
// no op
}

/**
* Called when creating a new job.
*
* @param parentName
* the full name of the parent ItemGroup
* @param name
* the name given from the UI
* @throws Failure
* if the user has to be informed about an illegal name, forces the user to change the name before submitting. The message of the failure will be presented to the user.
*
* @since TODO
*/
public void checkName(String parentName, String name) throws Failure {
checkName(name);
}

/**
* This flag can be used to force existing jobs to be migrated to a new naming strategy - if this method returns true, the naming will be enforced at every config change. If {@code false} is
* returned, only new jobs have to follow the strategy.
Expand Down