API: Wrong template used when template_name is a substring of another template name
## Summary When creating a project via the API with `use_custom_template: true` and `template_name`, the wrong template is used if the specified template name is a substring of another template's name. This appears to be a regression of [#9146](https://gitlab.com/gitlab-org/gitlab/-/issues/9146). ## Steps to reproduce 1. Create a group or subgroup with custom project templates containing at least two templates where one template name is a substring of another (e.g., "workload" and "python-workload"). In the example below, this is group ID 123458. 2. In each template project, add one file with a unique name. 1. For example: In "workload", add `workload.py`; in "python-workload", add `python-workload.py` 4. Use the REST API to create a project with the following parameters: - `use_custom_template: true` - `template_name: "workload"` (the project with the shorter name) - `group_with_project_templates_id: <group_id>` Example curl command: ```bash curl --request POST \ --url "https://gitlab.com/api/v4/projects" \ --header "PRIVATE-TOKEN: <token>" \ --header "Content-Type: application/json" \ --data '{ "name": "my-new-project", "namespace_id": 123456, "use_custom_template": true, "group_with_project_templates_id": 123458, "template_name": "workload" }' ``` ## Current behavior The API creates a project using the wrong template. In the example above, when requesting the "workload" template, the "python-workload" template is used instead, resulting in the project containing "python-workload.py" instead of "workload.py". ## Expected behavior The API should create a project using the exact template specified by `template_name`. The template selection should use exact matching, not substring matching. ## Example Given a group with two templates: - "workload" (contains: `workload.py`) - "python-workload" (contains: `python-workload.py`) When creating a project with `template_name: "workload"`, the resulting project should contain `workload.py`, not `python-workload.py`. ## Related issues - [#9146](https://gitlab.com/gitlab-org/gitlab/-/issues/9146) - Original issue with similar behavior in the UI - [#37004](https://gitlab.com/gitlab-org/gitlab/-/issues/37004) - API: create project for user with `use_custom_template` and `template_name` fails ## Possible root cause The template selection logic likely uses substring matching or sorting that doesn't guarantee exact matches are selected first. This could be in the `available_custom_project_templates` method or the template finder logic.
issue