From 6c5266c9cadf500ec8a166066bf4452801cc2545 Mon Sep 17 00:00:00 2001
From: 6543 <6543@obermui.de>
Date: Fri, 4 Sep 2020 03:36:56 +0200
Subject: [PATCH] [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 <techknowlogick@gitea.io>
---
 models/issue.go                 |  5 +++++
 models/migrations/migrations.go |  2 ++
 models/migrations/v148.go       | 14 ++++++++++++++
 routers/repo/issue.go           |  6 ++++--
 4 files changed, 25 insertions(+), 2 deletions(-)
 create mode 100644 models/migrations/v148.go

diff --git a/models/issue.go b/models/issue.go
index 2912f7e8ef..81652771ba 100644
--- a/models/issue.go
+++ b/models/issue.go
@@ -1978,6 +1978,11 @@ func deleteIssuesByRepoID(sess Engine, repoID int64) (attachmentPaths []string,
 		return
 	}
 
+	if _, err = sess.In("dependent_issue_id", deleteCond).
+		Delete(&Comment{}); err != nil {
+		return
+	}
+
 	var attachments []*Attachment
 	if err = sess.In("issue_id", deleteCond).
 		Find(&attachments); err != nil {
diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go
index 721b045fdc..6d27934f6d 100644
--- a/models/migrations/migrations.go
+++ b/models/migrations/migrations.go
@@ -228,6 +228,8 @@ var migrations = []Migration{
 	NewMigration("Add projects info to repository table", addProjectsInfo),
 	// v147 -> v148
 	NewMigration("create review for 0 review id code comments", createReviewsForCodeComments),
+	// v148 -> v149
+	NewMigration("remove issue dependency comments who refer to non existing issues", purgeInvalidDependenciesComments),
 }
 
 // GetCurrentDBVersion returns the current db version
diff --git a/models/migrations/v148.go b/models/migrations/v148.go
new file mode 100644
index 0000000000..35d17f5b2c
--- /dev/null
+++ b/models/migrations/v148.go
@@ -0,0 +1,14 @@
+// Copyright 2020 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package migrations
+
+import (
+	"xorm.io/xorm"
+)
+
+func purgeInvalidDependenciesComments(x *xorm.Engine) error {
+	_, err := x.Exec("DELETE FROM comment WHERE dependent_issue_id != 0 AND dependent_issue_id NOT IN (SELECT id FROM issue)")
+	return err
+}
diff --git a/routers/repo/issue.go b/routers/repo/issue.go
index 4bbc355027..4c745ed5d7 100644
--- a/routers/repo/issue.go
+++ b/routers/repo/issue.go
@@ -1079,8 +1079,10 @@ func ViewIssue(ctx *context.Context) {
 			}
 		} else if comment.Type == models.CommentTypeRemoveDependency || comment.Type == models.CommentTypeAddDependency {
 			if err = comment.LoadDepIssueDetails(); err != nil {
-				ctx.ServerError("LoadDepIssueDetails", err)
-				return
+				if !models.IsErrIssueNotExist(err) {
+					ctx.ServerError("LoadDepIssueDetails", err)
+					return
+				}
 			}
 		} else if comment.Type == models.CommentTypeCode || comment.Type == models.CommentTypeReview {
 			comment.RenderedContent = string(markdown.Render([]byte(comment.Content), ctx.Repo.RepoLink,