Skip to content

Commit

Permalink
[JENKINS-68602] Parent name for ProjectNamingStrategy (#6598)
Browse files Browse the repository at this point in the history
Co-authored-by: Adrien Lecharpentier <[email protected]>
Co-authored-by: Alexander Brandes <[email protected]>
  • Loading branch information
3 people authored Sep 5, 2022
1 parent 3673959 commit f93ee62
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
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

0 comments on commit f93ee62

Please sign in to comment.