@@ -13,6 +13,7 @@ import (
13
13
"strconv"
14
14
"strings"
15
15
16
+ admin_model "code.gitea.io/gitea/models/admin"
16
17
"code.gitea.io/gitea/models/db"
17
18
"code.gitea.io/gitea/models/issues"
18
19
"code.gitea.io/gitea/models/perm"
@@ -24,6 +25,7 @@ import (
24
25
"code.gitea.io/gitea/modules/log"
25
26
"code.gitea.io/gitea/modules/references"
26
27
"code.gitea.io/gitea/modules/setting"
28
+ "code.gitea.io/gitea/modules/storage"
27
29
api "code.gitea.io/gitea/modules/structs"
28
30
"code.gitea.io/gitea/modules/timeutil"
29
31
"code.gitea.io/gitea/modules/util"
@@ -1990,6 +1992,118 @@ func UpdateIssueDeadline(issue *Issue, deadlineUnix timeutil.TimeStamp, doer *us
1990
1992
return committer .Commit ()
1991
1993
}
1992
1994
1995
+ // DeleteIssue deletes the issue
1996
+ func DeleteIssue (issue * Issue ) error {
1997
+ ctx , committer , err := db .TxContext ()
1998
+ if err != nil {
1999
+ return err
2000
+ }
2001
+ defer committer .Close ()
2002
+
2003
+ if err := deleteIssue (ctx , issue ); err != nil {
2004
+ return err
2005
+ }
2006
+
2007
+ return committer .Commit ()
2008
+ }
2009
+
2010
+ func deleteInIssue (e db.Engine , issueID int64 , beans ... interface {}) error {
2011
+ for _ , bean := range beans {
2012
+ if _ , err := e .In ("issue_id" , issueID ).Delete (bean ); err != nil {
2013
+ return err
2014
+ }
2015
+ }
2016
+ return nil
2017
+ }
2018
+
2019
+ func deleteIssue (ctx context.Context , issue * Issue ) error {
2020
+ e := db .GetEngine (ctx )
2021
+ if _ , err := e .ID (issue .ID ).NoAutoCondition ().Delete (issue ); err != nil {
2022
+ return err
2023
+ }
2024
+
2025
+ if issue .IsPull {
2026
+ if _ , err := e .ID (issue .RepoID ).Decr ("num_pulls" ).Update (new (repo_model.Repository )); err != nil {
2027
+ return err
2028
+ }
2029
+ if issue .IsClosed {
2030
+ if _ , err := e .ID (issue .RepoID ).Decr ("num_closed_pulls" ).Update (new (repo_model.Repository )); err != nil {
2031
+ return err
2032
+ }
2033
+ }
2034
+ } else {
2035
+ if _ , err := e .ID (issue .RepoID ).Decr ("num_issues" ).Update (new (repo_model.Repository )); err != nil {
2036
+ return err
2037
+ }
2038
+ if issue .IsClosed {
2039
+ if _ , err := e .ID (issue .RepoID ).Decr ("num_closed_issues" ).Update (new (repo_model.Repository )); err != nil {
2040
+ return err
2041
+ }
2042
+ }
2043
+ }
2044
+
2045
+ // delete actions assigned to this issue
2046
+ var comments []int64
2047
+ if err := e .Table (new (Comment )).In ("issue_id" , issue .ID ).Cols ("id" ).Find (& comments ); err != nil {
2048
+ return err
2049
+ }
2050
+ for i := range comments {
2051
+ if _ , err := e .Where ("comment_id = ?" , comments [i ]).Delete (& Action {}); err != nil {
2052
+ return err
2053
+ }
2054
+ }
2055
+ if _ , err := e .Table ("action" ).Where ("repo_id = ?" , issue .RepoID ).In ("op_type" , ActionCreateIssue , ActionCreatePullRequest ).
2056
+ Where ("content LIKE ?" , strconv .FormatInt (issue .ID , 10 )+ "|%" ).Delete (& Action {}); err != nil {
2057
+ return err
2058
+ }
2059
+
2060
+ // find attachments related to this issue and remove them
2061
+ var attachments []* repo_model.Attachment
2062
+ if err := e .In ("issue_id" , issue .ID ).Find (& attachments ); err != nil {
2063
+ return err
2064
+ }
2065
+
2066
+ for i := range attachments {
2067
+ admin_model .RemoveStorageWithNotice (ctx , storage .Attachments , "Delete issue attachment" , attachments [i ].RelativePath ())
2068
+ }
2069
+
2070
+ // delete all database data still assigned to this issue
2071
+ if err := deleteInIssue (e , issue .ID ,
2072
+ & issues.ContentHistory {},
2073
+ & Comment {},
2074
+ & IssueLabel {},
2075
+ & IssueDependency {},
2076
+ & IssueAssignees {},
2077
+ & IssueUser {},
2078
+ & Reaction {},
2079
+ & IssueWatch {},
2080
+ & Stopwatch {},
2081
+ & TrackedTime {},
2082
+ & ProjectIssue {},
2083
+ & repo_model.Attachment {},
2084
+ & PullRequest {},
2085
+ ); err != nil {
2086
+ return err
2087
+ }
2088
+
2089
+ // References to this issue in other issues
2090
+ if _ , err := e .In ("ref_issue_id" , issue .ID ).Delete (& Comment {}); err != nil {
2091
+ return err
2092
+ }
2093
+
2094
+ // Delete dependencies for issues in other repositories
2095
+ if _ , err := e .In ("dependency_id" , issue .ID ).Delete (& IssueDependency {}); err != nil {
2096
+ return err
2097
+ }
2098
+
2099
+ // delete from dependent issues
2100
+ if _ , err := e .In ("dependent_issue_id" , issue .ID ).Delete (& Comment {}); err != nil {
2101
+ return err
2102
+ }
2103
+
2104
+ return nil
2105
+ }
2106
+
1993
2107
// DependencyInfo represents high level information about an issue which is a dependency of another issue.
1994
2108
type DependencyInfo struct {
1995
2109
Issue `xorm:"extends"`
0 commit comments