Skip to content

Commit c03055c

Browse files
committed
Fix bug
1 parent 6199436 commit c03055c

File tree

3 files changed

+38
-45
lines changed

3 files changed

+38
-45
lines changed

models/issues/pull.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"code.gitea.io/gitea/models/db"
1616
git_model "code.gitea.io/gitea/models/git"
1717
org_model "code.gitea.io/gitea/models/organization"
18-
pull_model "code.gitea.io/gitea/models/pull"
1918
repo_model "code.gitea.io/gitea/models/repo"
2019
user_model "code.gitea.io/gitea/models/user"
2120
"code.gitea.io/gitea/modules/git"
@@ -156,26 +155,6 @@ func init() {
156155
db.RegisterModel(new(PullRequest))
157156
}
158157

159-
// DeletePullsByBaseRepoID deletes all pull requests by the base repository ID
160-
func DeletePullsByBaseRepoID(ctx context.Context, repoID int64) error {
161-
deleteCond := builder.Select("id").From("pull_request").Where(builder.Eq{"pull_request.base_repo_id": repoID})
162-
163-
// Delete scheduled auto merges
164-
if _, err := db.GetEngine(ctx).In("pull_id", deleteCond).
165-
Delete(&pull_model.AutoMerge{}); err != nil {
166-
return err
167-
}
168-
169-
// Delete review states
170-
if _, err := db.GetEngine(ctx).In("pull_id", deleteCond).
171-
Delete(&pull_model.ReviewState{}); err != nil {
172-
return err
173-
}
174-
175-
_, err := db.DeleteByBean(ctx, &PullRequest{BaseRepoID: repoID})
176-
return err
177-
}
178-
179158
func (pr *PullRequest) String() string {
180159
if pr == nil {
181160
return "<PullRequest nil>"

services/issue/issue.go

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
issues_model "code.gitea.io/gitea/models/issues"
1313
access_model "code.gitea.io/gitea/models/perm/access"
1414
project_model "code.gitea.io/gitea/models/project"
15+
pull_model "code.gitea.io/gitea/models/pull"
1516
repo_model "code.gitea.io/gitea/models/repo"
1617
system_model "code.gitea.io/gitea/models/system"
1718
user_model "code.gitea.io/gitea/models/user"
@@ -267,10 +268,6 @@ func deleteIssue(ctx context.Context, issue *issues_model.Issue) ([]string, erro
267268
}
268269
defer committer.Close()
269270

270-
if _, err := db.GetEngine(ctx).ID(issue.ID).NoAutoCondition().Delete(issue); err != nil {
271-
return nil, err
272-
}
273-
274271
// update the total issue numbers
275272
if err := repo_model.UpdateRepoIssueNumbers(ctx, issue.RepoID, issue.IsPull, false); err != nil {
276273
return nil, err
@@ -302,6 +299,13 @@ func deleteIssue(ctx context.Context, issue *issues_model.Issue) ([]string, erro
302299
}
303300

304301
// deference all review comments
302+
if err := issue.LoadRepo(ctx); err != nil {
303+
return nil, err
304+
}
305+
if err := issue.LoadPullRequest(ctx); err != nil {
306+
return nil, err
307+
}
308+
305309
if err := issue.LoadComments(ctx); err != nil {
306310
return nil, err
307311
}
@@ -335,17 +339,8 @@ func deleteIssue(ctx context.Context, issue *issues_model.Issue) ([]string, erro
335339
}
336340

337341
if comment.ReviewID > 0 {
338-
if err := comment.LoadIssue(ctx); err != nil {
339-
return nil, err
340-
}
341-
if err := comment.Issue.LoadRepo(ctx); err != nil {
342-
return nil, err
343-
}
344-
if err := comment.Issue.LoadPullRequest(ctx); err != nil {
345-
return nil, err
346-
}
347-
if err := git.RemoveRef(ctx, comment.Issue.Repo.RepoPath(), issues_model.GetCodeCommentRef(comment.Issue.PullRequest.Index, comment.ID)); err != nil {
348-
log.Error("Unable to remove ref in base repository for PR[%d] Error: %v", comment.Issue.PullRequest.ID, err)
342+
if err := git.RemoveRef(ctx, issue.Repo.RepoPath(), issues_model.GetCodeCommentRef(issue.PullRequest.Index, comment.ID)); err != nil {
343+
log.Error("Unable to remove ref in base repository for PR[%d] Error: %v", issue.PullRequest.ID, err)
349344
// We should not return error here, because the comment has been removed from database.
350345
// users have to delete this ref manually or we should have a synchronize between
351346
// database comment table and git refs.
@@ -364,6 +359,29 @@ func deleteIssue(ctx context.Context, issue *issues_model.Issue) ([]string, erro
364359
}
365360
}
366361

362+
// delete all pull request records
363+
if issue.IsPull {
364+
// Delete scheduled auto merges
365+
if _, err := db.GetEngine(ctx).Where("pull_id=?", issue.PullRequest.ID).
366+
Delete(&pull_model.AutoMerge{}); err != nil {
367+
return nil, err
368+
}
369+
370+
// Delete review states
371+
if _, err := db.GetEngine(ctx).Where("pull_id=?", issue.PullRequest.ID).
372+
Delete(&pull_model.ReviewState{}); err != nil {
373+
return nil, err
374+
}
375+
376+
if _, err := db.GetEngine(ctx).ID(issue.PullRequest.ID).Delete(&issues_model.PullRequest{}); err != nil {
377+
return nil, err
378+
}
379+
}
380+
381+
if _, err := db.GetEngine(ctx).ID(issue.ID).NoAutoCondition().Delete(issue); err != nil {
382+
return nil, err
383+
}
384+
367385
if err := committer.Commit(); err != nil {
368386
return nil, err
369387
}

services/repository/delete.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,6 @@ func DeleteRepositoryDirectly(ctx context.Context, repoID int64, ignoreOrgTeams
9797
}
9898
needRewriteKeysFile := deleted > 0
9999

100-
if err := deleteDBRepository(ctx, repoID); err != nil {
101-
return err
102-
}
103-
104100
if org != nil && org.IsOrganization() {
105101
teams, err := organization.FindOrgTeams(ctx, org.ID)
106102
if err != nil {
@@ -187,11 +183,6 @@ func DeleteRepositoryDirectly(ctx context.Context, repoID int64, ignoreOrgTeams
187183
return err
188184
}
189185

190-
// Delete Pulls and related objects
191-
if err := issues_model.DeletePullsByBaseRepoID(ctx, repoID); err != nil {
192-
return err
193-
}
194-
195186
// Delete Issues and related objects
196187
var attachmentPaths []string
197188
if attachmentPaths, err = issue_service.DeleteIssuesByRepoID(ctx, repoID); err != nil {
@@ -291,6 +282,11 @@ func DeleteRepositoryDirectly(ctx context.Context, repoID int64, ignoreOrgTeams
291282
return err
292283
}
293284

285+
// delete all related database records first before deleting the repository record
286+
if err := deleteDBRepository(ctx, repoID); err != nil {
287+
return err
288+
}
289+
294290
if err = committer.Commit(); err != nil {
295291
return err
296292
}

0 commit comments

Comments
 (0)