You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

v176.go 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package v1_14 //nolint
  4. import (
  5. "xorm.io/xorm"
  6. )
  7. // RemoveInvalidLabels looks through the database to look for comments and issue_labels
  8. // that refer to labels do not belong to the repository or organization that repository
  9. // that the issue is in
  10. func RemoveInvalidLabels(x *xorm.Engine) error {
  11. type Comment struct {
  12. ID int64 `xorm:"pk autoincr"`
  13. Type int `xorm:"INDEX"`
  14. IssueID int64 `xorm:"INDEX"`
  15. LabelID int64
  16. }
  17. type Issue struct {
  18. ID int64 `xorm:"pk autoincr"`
  19. RepoID int64 `xorm:"INDEX UNIQUE(repo_index)"`
  20. Index int64 `xorm:"UNIQUE(repo_index)"` // Index in one repository.
  21. }
  22. type Repository struct {
  23. ID int64 `xorm:"pk autoincr"`
  24. OwnerID int64 `xorm:"UNIQUE(s) index"`
  25. LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
  26. }
  27. type Label struct {
  28. ID int64 `xorm:"pk autoincr"`
  29. RepoID int64 `xorm:"INDEX"`
  30. OrgID int64 `xorm:"INDEX"`
  31. }
  32. type IssueLabel struct {
  33. ID int64 `xorm:"pk autoincr"`
  34. IssueID int64 `xorm:"UNIQUE(s)"`
  35. LabelID int64 `xorm:"UNIQUE(s)"`
  36. }
  37. if err := x.Sync(new(Comment), new(Issue), new(Repository), new(Label), new(IssueLabel)); err != nil {
  38. return err
  39. }
  40. if _, err := x.Exec(`DELETE FROM issue_label WHERE issue_label.id IN (
  41. SELECT il_too.id FROM (
  42. SELECT il_too_too.id
  43. FROM issue_label AS il_too_too
  44. INNER JOIN label ON il_too_too.label_id = label.id
  45. INNER JOIN issue on issue.id = il_too_too.issue_id
  46. INNER JOIN repository on repository.id = issue.repo_id
  47. WHERE
  48. (label.org_id = 0 AND issue.repo_id != label.repo_id) OR (label.repo_id = 0 AND label.org_id != repository.owner_id)
  49. ) AS il_too )`); err != nil {
  50. return err
  51. }
  52. if _, err := x.Exec(`DELETE FROM comment WHERE comment.id IN (
  53. SELECT il_too.id FROM (
  54. SELECT com.id
  55. FROM comment AS com
  56. INNER JOIN label ON com.label_id = label.id
  57. INNER JOIN issue on issue.id = com.issue_id
  58. INNER JOIN repository on repository.id = issue.repo_id
  59. WHERE
  60. com.type = ? AND ((label.org_id = 0 AND issue.repo_id != label.repo_id) OR (label.repo_id = 0 AND label.org_id != repository.owner_id))
  61. ) AS il_too)`, 7); err != nil {
  62. return err
  63. }
  64. return nil
  65. }