Modify label promote logic to avoid label duplicate when group label already exists
The following discussion from !37148 (merged) should be addressed:
I see there's a test failure in the PromoteService in the case that the project label is being promoted and a group label with that title already exists.
We actually handle this scenario in
Labels::PromoteService albeit in an odd way that doesn't work when this constraint is added.
I remember adding the doc for this in https://docs.gitlab.com/ee/user/project/labels.html#promote-a-project-label-to-a-group-label:
If a group label with the same title exists, it will also be merged.
This is the relevant section of the code in
new_label = clone_label_to_group_label(label) label_ids_for_merge(new_label).find_in_batches(batch_size: BATCH_SIZE) do |batched_ids| update_old_label_relations(new_label, batched_ids) destroy_project_labels(batched_ids) end
clone_label_to_group_labelcreates a group label with the same name
label_ids_for_merge(new_label)would include that old group label with the same name
- the block would then update all objects to reference this new label
destroy_project_labelsdeletes the old group label with the same name (The method name is not accurate
I think we could be smarter here and not create a duplicate label if we know that a group label of the same name already exists. We should just update the references to that existing label.
That way we also won't need to update other objects assigned to that old group label.