Fix group label link references
There was an issue with updating group label links when transferring a project (#44158 (closed)) which caused that also group label links for other projects in the same group were updated (https://gitlab.com/gitlab-org/gitlab-ce/issues/44158#note_67743241).
To give an example, supposing there is following hierarchy:
- group1
- project1
- issue1 referencing grouplabel1
- project2
- issue2 referencing grouplabel1
- grouplabel1
- group2
Then after transferring project1
to group2
, its label references would be:
- group1
- project2
- issue2 referencing group2/project1/projectlabel1 (mistakenly updated label reference)
- grouplabel1
- group2
- project1
- projectlabel1 (copy of grouplabel1, created on project transfer)
- issue1 referencing projectlabel1
IOW label reference was updated also for issue
in project2
.
The issue itself was fixed by !18320 (merged). But because the issue was present for a while there may be many inconsistent references on any deployment. For example on gitlab.com
we currently have >600 issues referencing ~bug label in project bizops/bizops
(because bizops
project was transferred from gitlab-org
group):
> Issue.joins(:label_links).where(label_links: {label_id: 3547268}, project_id: 13083).count
=> 6422
To consolidate group label links we might create a background migration which fixes references of labels pointing to other groups:
for each group label link for all issues and MRs in all projects:
if label link references issue's (or MR's) project or any of ancestor group, then it's OK - skip it
if label link references another project or not-ancestor group, then:
if there is a label with the same name and color in any of issue's (or MR's) ancestor group:
update label link reference to point to the same ancestor's group label