Skip to content

Commit 6c5266c

Browse files
6543techknowlogick
andauthored
[BugFix] Fix comment broken issue ref dependence (#12651)
* deleteIssuesByRepoID: delete related CommentTypeRemoveDependency & CommentTypeAddDependency comments too * Ignore ErrIssueNotExist on comment.LoadDepIssueDetails() * Add migration * Ignore 'dependent_issue_id = 0' case * exchange as per @lunny Co-authored-by: techknowlogick <[email protected]>
1 parent 42a5e39 commit 6c5266c

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

models/issue.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,6 +1978,11 @@ func deleteIssuesByRepoID(sess Engine, repoID int64) (attachmentPaths []string,
19781978
return
19791979
}
19801980

1981+
if _, err = sess.In("dependent_issue_id", deleteCond).
1982+
Delete(&Comment{}); err != nil {
1983+
return
1984+
}
1985+
19811986
var attachments []*Attachment
19821987
if err = sess.In("issue_id", deleteCond).
19831988
Find(&attachments); err != nil {

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ var migrations = []Migration{
228228
NewMigration("Add projects info to repository table", addProjectsInfo),
229229
// v147 -> v148
230230
NewMigration("create review for 0 review id code comments", createReviewsForCodeComments),
231+
// v148 -> v149
232+
NewMigration("remove issue dependency comments who refer to non existing issues", purgeInvalidDependenciesComments),
231233
}
232234

233235
// GetCurrentDBVersion returns the current db version

models/migrations/v148.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import (
8+
"xorm.io/xorm"
9+
)
10+
11+
func purgeInvalidDependenciesComments(x *xorm.Engine) error {
12+
_, err := x.Exec("DELETE FROM comment WHERE dependent_issue_id != 0 AND dependent_issue_id NOT IN (SELECT id FROM issue)")
13+
return err
14+
}

routers/repo/issue.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,8 +1079,10 @@ func ViewIssue(ctx *context.Context) {
10791079
}
10801080
} else if comment.Type == models.CommentTypeRemoveDependency || comment.Type == models.CommentTypeAddDependency {
10811081
if err = comment.LoadDepIssueDetails(); err != nil {
1082-
ctx.ServerError("LoadDepIssueDetails", err)
1083-
return
1082+
if !models.IsErrIssueNotExist(err) {
1083+
ctx.ServerError("LoadDepIssueDetails", err)
1084+
return
1085+
}
10841086
}
10851087
} else if comment.Type == models.CommentTypeCode || comment.Type == models.CommentTypeReview {
10861088
comment.RenderedContent = string(markdown.Render([]byte(comment.Content), ctx.Repo.RepoLink,

0 commit comments

Comments
 (0)