From 0d2942e18e2a1551a0ff1cf09df8098bc71b60e0 Mon Sep 17 00:00:00 2001 From: emrebdr Date: Sat, 3 Aug 2024 02:20:39 +0300 Subject: [PATCH 1/6] fix unreachable project issues after changing repository ownership --- models/project/issue.go | 13 +++++++++++++ models/project/project.go | 6 ++++++ services/repository/transfer.go | 17 +++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/models/project/issue.go b/models/project/issue.go index 3361b533b972c..8d1f7f8a89d04 100644 --- a/models/project/issue.go +++ b/models/project/issue.go @@ -141,3 +141,16 @@ func (c *Column) moveIssuesToAnotherColumn(ctx context.Context, newColumn *Colum return nil }) } + +// DeleteAllProjectIssueByIssueIDsAndProjectIDs delete all project's issues by issue's and project's ids +func DeleteAllProjectIssueByIssueIDsAndProjectIDs(ctx context.Context, issueIDs, projectIDs []int64) error { + for _, id := range projectIDs { + _, err := db.GetEngine(ctx).Where("project_id=?", id).In("issue_id", issueIDs).Delete(&ProjectIssue{}) + + if err != nil { + return err + } + } + + return nil +} diff --git a/models/project/project.go b/models/project/project.go index 8cebf34b5ee6c..9c8b9d8a45d42 100644 --- a/models/project/project.go +++ b/models/project/project.go @@ -296,6 +296,12 @@ func GetProjectForRepoByID(ctx context.Context, repoID, id int64) (*Project, err return p, nil } +// GetAllProjectsIDsByOwnerId returns the all projects ids it owns +func GetAllProjectsIDsByOwnerId(ctx context.Context, ownerId int64) ([]int64, error) { + projects := make([]int64, 0) + return projects, db.GetEngine(ctx).Table(&Project{}).Where("owner_id=?", ownerId).Cols("id").Find(&projects) +} + // UpdateProject updates project properties func UpdateProject(ctx context.Context, p *Project) error { if !IsCardTypeValid(p.CardType) { diff --git a/services/repository/transfer.go b/services/repository/transfer.go index 9e0ff7ae1405a..b29aad01e3df0 100644 --- a/services/repository/transfer.go +++ b/services/repository/transfer.go @@ -15,6 +15,7 @@ import ( "code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/perm" access_model "code.gitea.io/gitea/models/perm/access" + project_model "code.gitea.io/gitea/models/project" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/log" @@ -177,6 +178,22 @@ func transferOwnership(ctx context.Context, doer *user_model.User, newOwnerName } } + // Remove project's issues that belong to old organization's projects + if oldOwner.IsOrganization() { + projects, err := project_model.GetAllProjectsIDsByOwnerId(ctx, oldOwner.ID) + if err != nil { + return fmt.Errorf("Unable to find old org projects: %w", err) + } + issues, err := issues_model.GetIssueIDsByRepoID(ctx, repo.ID) + if err != nil { + return fmt.Errorf("Unable to find repo's issues: %w", err) + } + err = project_model.DeleteAllProjectIssueByIssueIDsAndProjectIDs(ctx, issues, projects) + if err != nil { + return fmt.Errorf("Unable to delete project's issues: %w", err) + } + } + if newOwner.IsOrganization() { teams, err := organization.FindOrgTeams(ctx, newOwner.ID) if err != nil { From 90c7cc73102d16a849e0bf4ed972bb5b09bcd33e Mon Sep 17 00:00:00 2001 From: emrebdr Date: Sat, 3 Aug 2024 02:34:38 +0300 Subject: [PATCH 2/6] fix linter --- models/project/issue.go | 1 - models/project/project.go | 6 +++--- services/repository/transfer.go | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/models/project/issue.go b/models/project/issue.go index 8d1f7f8a89d04..dc2bf85006d80 100644 --- a/models/project/issue.go +++ b/models/project/issue.go @@ -146,7 +146,6 @@ func (c *Column) moveIssuesToAnotherColumn(ctx context.Context, newColumn *Colum func DeleteAllProjectIssueByIssueIDsAndProjectIDs(ctx context.Context, issueIDs, projectIDs []int64) error { for _, id := range projectIDs { _, err := db.GetEngine(ctx).Where("project_id=?", id).In("issue_id", issueIDs).Delete(&ProjectIssue{}) - if err != nil { return err } diff --git a/models/project/project.go b/models/project/project.go index 9c8b9d8a45d42..e8540d17f0b85 100644 --- a/models/project/project.go +++ b/models/project/project.go @@ -296,10 +296,10 @@ func GetProjectForRepoByID(ctx context.Context, repoID, id int64) (*Project, err return p, nil } -// GetAllProjectsIDsByOwnerId returns the all projects ids it owns -func GetAllProjectsIDsByOwnerId(ctx context.Context, ownerId int64) ([]int64, error) { +// GetAllProjectsIDsByOwnerID returns the all projects ids it owns +func GetAllProjectsIDsByOwnerID(ctx context.Context, ownerID int64) ([]int64, error) { projects := make([]int64, 0) - return projects, db.GetEngine(ctx).Table(&Project{}).Where("owner_id=?", ownerId).Cols("id").Find(&projects) + return projects, db.GetEngine(ctx).Table(&Project{}).Where("owner_id=?", ownerID).Cols("id").Find(&projects) } // UpdateProject updates project properties diff --git a/services/repository/transfer.go b/services/repository/transfer.go index b29aad01e3df0..19d80a6b1300e 100644 --- a/services/repository/transfer.go +++ b/services/repository/transfer.go @@ -180,7 +180,7 @@ func transferOwnership(ctx context.Context, doer *user_model.User, newOwnerName // Remove project's issues that belong to old organization's projects if oldOwner.IsOrganization() { - projects, err := project_model.GetAllProjectsIDsByOwnerId(ctx, oldOwner.ID) + projects, err := project_model.GetAllProjectsIDsByOwnerID(ctx, oldOwner.ID) if err != nil { return fmt.Errorf("Unable to find old org projects: %w", err) } From 9091a9b6be6aed09d0ad3de7770ec38d29d288bf Mon Sep 17 00:00:00 2001 From: emrebdr Date: Sat, 3 Aug 2024 13:58:59 +0300 Subject: [PATCH 3/6] fix delete for only if project owner is organization --- models/project/project.go | 4 ++-- services/repository/transfer.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/models/project/project.go b/models/project/project.go index e8540d17f0b85..050ccf44e02e4 100644 --- a/models/project/project.go +++ b/models/project/project.go @@ -297,9 +297,9 @@ func GetProjectForRepoByID(ctx context.Context, repoID, id int64) (*Project, err } // GetAllProjectsIDsByOwnerID returns the all projects ids it owns -func GetAllProjectsIDsByOwnerID(ctx context.Context, ownerID int64) ([]int64, error) { +func GetAllProjectsIDsByOwnerIDAndType(ctx context.Context, ownerID int64, projectType Type) ([]int64, error) { projects := make([]int64, 0) - return projects, db.GetEngine(ctx).Table(&Project{}).Where("owner_id=?", ownerID).Cols("id").Find(&projects) + return projects, db.GetEngine(ctx).Table(&Project{}).Where("owner_id=? AND type=?", ownerID, projectType).Cols("id").Find(&projects) } // UpdateProject updates project properties diff --git a/services/repository/transfer.go b/services/repository/transfer.go index 19d80a6b1300e..f48653072a813 100644 --- a/services/repository/transfer.go +++ b/services/repository/transfer.go @@ -180,7 +180,7 @@ func transferOwnership(ctx context.Context, doer *user_model.User, newOwnerName // Remove project's issues that belong to old organization's projects if oldOwner.IsOrganization() { - projects, err := project_model.GetAllProjectsIDsByOwnerID(ctx, oldOwner.ID) + projects, err := project_model.GetAllProjectsIDsByOwnerIDAndType(ctx, oldOwner.ID, project_model.TypeOrganization) if err != nil { return fmt.Errorf("Unable to find old org projects: %w", err) } From 1a4c9c5fbe16ce2ed15fda51ebacef841a93a366 Mon Sep 17 00:00:00 2001 From: emrebdr Date: Tue, 6 Aug 2024 22:53:05 +0300 Subject: [PATCH 4/6] added popup warning message --- options/locale/locale_en-US.ini | 1 + templates/repo/settings/options.tmpl | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 92f955c78a7ec..6ed5f79038b54 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -2184,6 +2184,7 @@ settings.transfer_in_progress = There is currently an ongoing transfer. Please c settings.transfer_notices_1 = - You will lose access to the repository if you transfer it to an individual user. settings.transfer_notices_2 = - You will keep access to the repository if you transfer it to an organization that you (co-)own. settings.transfer_notices_3 = - If the repository is private and is transferred to an individual user, this action makes sure that the user does have at least read permission (and changes permissions if necessary). +settings.transfer_notices_4 = - If you transfer your repository to another organization or individual, you will lose the links between the repository's issues and the organization's project board. settings.transfer_owner = New Owner settings.transfer_perform = Perform Transfer settings.transfer_started = This repository has been marked for transfer and awaits confirmation from "%s" diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index 4f98133df3679..952da54ec4bac 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -950,7 +950,8 @@
{{ctx.Locale.Tr "repo.settings.transfer_notices_1"}}
{{ctx.Locale.Tr "repo.settings.transfer_notices_2"}}
- {{ctx.Locale.Tr "repo.settings.transfer_notices_3"}} + {{ctx.Locale.Tr "repo.settings.transfer_notices_3"}}
+ {{ctx.Locale.Tr "repo.settings.transfer_notices_4"}}
{{.CsrfTokenHtml}} From 0ddbc17e82dd2da8916405f5f947d1d47c48f181 Mon Sep 17 00:00:00 2001 From: emrebdr Date: Tue, 6 Aug 2024 23:09:39 +0300 Subject: [PATCH 5/6] changed warning message --- options/locale/locale_en-US.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 6ed5f79038b54..3edd63f8fb52d 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -2184,7 +2184,7 @@ settings.transfer_in_progress = There is currently an ongoing transfer. Please c settings.transfer_notices_1 = - You will lose access to the repository if you transfer it to an individual user. settings.transfer_notices_2 = - You will keep access to the repository if you transfer it to an organization that you (co-)own. settings.transfer_notices_3 = - If the repository is private and is transferred to an individual user, this action makes sure that the user does have at least read permission (and changes permissions if necessary). -settings.transfer_notices_4 = - If you transfer your repository to another organization or individual, you will lose the links between the repository's issues and the organization's project board. +settings.transfer_notices_4 = - If the repository belongs to an organization, and you transfer it to another organization or individual, you will lose the links between the repository's issues and the organization's project board. settings.transfer_owner = New Owner settings.transfer_perform = Perform Transfer settings.transfer_started = This repository has been marked for transfer and awaits confirmation from "%s" From 65a2d5a1423e9d0441f6e0c961bdfb253debbdc9 Mon Sep 17 00:00:00 2001 From: Edip Emre Bodur Date: Mon, 12 Aug 2024 23:18:23 +0300 Subject: [PATCH 6/6] Update models/project/issue.go Co-authored-by: Jason Song --- models/project/issue.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/models/project/issue.go b/models/project/issue.go index dc2bf85006d80..3e7ee831a6436 100644 --- a/models/project/issue.go +++ b/models/project/issue.go @@ -144,12 +144,6 @@ func (c *Column) moveIssuesToAnotherColumn(ctx context.Context, newColumn *Colum // DeleteAllProjectIssueByIssueIDsAndProjectIDs delete all project's issues by issue's and project's ids func DeleteAllProjectIssueByIssueIDsAndProjectIDs(ctx context.Context, issueIDs, projectIDs []int64) error { - for _, id := range projectIDs { - _, err := db.GetEngine(ctx).Where("project_id=?", id).In("issue_id", issueIDs).Delete(&ProjectIssue{}) - if err != nil { - return err - } - } - - return nil + _, err := db.GetEngine(ctx).In("project_id", projectIDs).In("issue_id", issueIDs).Delete(&ProjectIssue{}) + return err }