Skip to content

Commit 751cfb8

Browse files
authored
Fix bug that release attachment files not deleted when deleting repository (#9322)
* Fix bug that release attachment files not deleted when deleting repository * improve code * add quote * improve code
1 parent aceb108 commit 751cfb8

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

models/repo.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,6 +1992,17 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
19921992
}
19931993
}
19941994

1995+
attachments := make([]*Attachment, 0, 20)
1996+
if err = sess.Join("INNER", "`release`", "`release`.id = `attachment`.release_id").
1997+
Where("`release`.repo_id = ?", repoID).
1998+
Find(&attachments); err != nil {
1999+
return err
2000+
}
2001+
releaseAttachments := make([]string, 0, len(attachments))
2002+
for i := 0; i < len(attachments); i++ {
2003+
releaseAttachments = append(releaseAttachments, attachments[i].LocalPath())
2004+
}
2005+
19952006
if err = deleteBeans(sess,
19962007
&Access{RepoID: repo.ID},
19972008
&Action{RepoID: repo.ID},
@@ -2042,13 +2053,13 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
20422053
return err
20432054
}
20442055

2045-
attachmentPaths := make([]string, 0, 20)
2046-
attachments := make([]*Attachment, 0, len(attachmentPaths))
2056+
attachments = attachments[:0]
20472057
if err = sess.Join("INNER", "issue", "issue.id = attachment.issue_id").
20482058
Where("issue.repo_id = ?", repoID).
20492059
Find(&attachments); err != nil {
20502060
return err
20512061
}
2062+
attachmentPaths := make([]string, 0, len(attachments))
20522063
for j := range attachments {
20532064
attachmentPaths = append(attachmentPaths, attachments[j].LocalPath())
20542065
}
@@ -2085,11 +2096,6 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
20852096
return err
20862097
}
20872098

2088-
// Remove attachment files.
2089-
for i := range attachmentPaths {
2090-
removeAllWithNotice(sess, "Delete attachment", attachmentPaths[i])
2091-
}
2092-
20932099
// Remove LFS objects
20942100
var lfsObjects []*LFSMetaObject
20952101
if err = sess.Where("repository_id=?", repoID).Find(&lfsObjects); err != nil {
@@ -2129,6 +2135,21 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
21292135
return fmt.Errorf("Commit: %v", err)
21302136
}
21312137

2138+
sess.Close()
2139+
2140+
// We should always delete the files after the database transaction succeed. If
2141+
// we delete the file but the database rollback, the repository will be borken.
2142+
2143+
// Remove issue attachment files.
2144+
for i := range attachmentPaths {
2145+
removeAllWithNotice(x, "Delete issue attachment", attachmentPaths[i])
2146+
}
2147+
2148+
// Remove release attachment files.
2149+
for i := range releaseAttachments {
2150+
removeAllWithNotice(x, "Delete release attachment", releaseAttachments[i])
2151+
}
2152+
21322153
if len(repo.Avatar) > 0 {
21332154
avatarPath := repo.CustomAvatarPath()
21342155
if com.IsExist(avatarPath) {

0 commit comments

Comments
 (0)