Skip to content

Commit eb8d5f6

Browse files
authored
Fix bug that release attachment files not deleted when deleting repository (#9322) (#9329)
* Fix bug that release attachment files not deleted when deleting repository * improve code * add quote * improve code
1 parent 9ef148a commit eb8d5f6

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

models/repo.go

+28-7
Original file line numberDiff line numberDiff line change
@@ -1860,6 +1860,17 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
18601860
}
18611861
}
18621862

1863+
attachments := make([]*Attachment, 0, 20)
1864+
if err = sess.Join("INNER", "`release`", "`release`.id = `attachment`.release_id").
1865+
Where("`release`.repo_id = ?", repoID).
1866+
Find(&attachments); err != nil {
1867+
return err
1868+
}
1869+
releaseAttachments := make([]string, 0, len(attachments))
1870+
for i := 0; i < len(attachments); i++ {
1871+
releaseAttachments = append(releaseAttachments, attachments[i].LocalPath())
1872+
}
1873+
18631874
if err = deleteBeans(sess,
18641875
&Access{RepoID: repo.ID},
18651876
&Action{RepoID: repo.ID},
@@ -1910,13 +1921,13 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
19101921
return err
19111922
}
19121923

1913-
attachmentPaths := make([]string, 0, 20)
1914-
attachments := make([]*Attachment, 0, len(attachmentPaths))
1924+
attachments = attachments[:0]
19151925
if err = sess.Join("INNER", "issue", "issue.id = attachment.issue_id").
19161926
Where("issue.repo_id = ?", repoID).
19171927
Find(&attachments); err != nil {
19181928
return err
19191929
}
1930+
attachmentPaths := make([]string, 0, len(attachments))
19201931
for j := range attachments {
19211932
attachmentPaths = append(attachmentPaths, attachments[j].LocalPath())
19221933
}
@@ -1953,11 +1964,6 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
19531964
return err
19541965
}
19551966

1956-
// Remove attachment files.
1957-
for i := range attachmentPaths {
1958-
removeAllWithNotice(sess, "Delete attachment", attachmentPaths[i])
1959-
}
1960-
19611967
// Remove LFS objects
19621968
var lfsObjects []*LFSMetaObject
19631969
if err = sess.Where("repository_id=?", repoID).Find(&lfsObjects); err != nil {
@@ -1997,6 +2003,8 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
19972003
return fmt.Errorf("Commit: %v", err)
19982004
}
19992005

2006+
sess.Close()
2007+
20002008
if org.IsOrganization() {
20012009
if err = PrepareWebhooks(repo, HookEventRepository, &api.RepositoryPayload{
20022010
Action: api.HookRepoDeleted,
@@ -2009,6 +2017,19 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
20092017
go HookQueue.Add(repo.ID)
20102018
}
20112019

2020+
// We should always delete the files after the database transaction succeed. If
2021+
// we delete the file but the database rollback, the repository will be borken.
2022+
2023+
// Remove issue attachment files.
2024+
for i := range attachmentPaths {
2025+
removeAllWithNotice(x, "Delete issue attachment", attachmentPaths[i])
2026+
}
2027+
2028+
// Remove release attachment files.
2029+
for i := range releaseAttachments {
2030+
removeAllWithNotice(x, "Delete release attachment", releaseAttachments[i])
2031+
}
2032+
20122033
if len(repo.Avatar) > 0 {
20132034
avatarPath := repo.CustomAvatarPath()
20142035
if com.IsExist(avatarPath) {

0 commit comments

Comments
 (0)