diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2022-06-13 17:37:59 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-13 17:37:59 +0800 |
commit | 1a9821f57a0293db3adc0eab8aff08ca5fa1026c (patch) | |
tree | 3c3d02813eb63c0d0827ef6d9745f6dcdd2636cb /services/issue | |
parent | 3708ca8e2849ca7e36e6bd15ec6935a2a2d81e55 (diff) | |
download | gitea-1a9821f57a0293db3adc0eab8aff08ca5fa1026c.tar.gz gitea-1a9821f57a0293db3adc0eab8aff08ca5fa1026c.zip |
Move issues related files into models/issues (#19931)
* Move access and repo permission to models/perm/access
* fix test
* fix git test
* Move functions sequence
* Some improvements per @KN4CK3R and @delvh
* Move issues related code to models/issues
* Move some issues related sub package
* Merge
* Fix test
* Fix test
* Fix test
* Fix test
* Rename some files
Diffstat (limited to 'services/issue')
-rw-r--r-- | services/issue/assignee.go | 56 | ||||
-rw-r--r-- | services/issue/assignee_test.go | 6 | ||||
-rw-r--r-- | services/issue/commit.go | 15 | ||||
-rw-r--r-- | services/issue/commit_test.go | 57 | ||||
-rw-r--r-- | services/issue/content.go | 6 | ||||
-rw-r--r-- | services/issue/issue.go | 108 | ||||
-rw-r--r-- | services/issue/issue_test.go | 61 | ||||
-rw-r--r-- | services/issue/label.go | 32 | ||||
-rw-r--r-- | services/issue/label_test.go | 16 | ||||
-rw-r--r-- | services/issue/milestone.go | 13 | ||||
-rw-r--r-- | services/issue/milestone_test.go | 9 | ||||
-rw-r--r-- | services/issue/status.go | 14 |
12 files changed, 264 insertions, 129 deletions
diff --git a/services/issue/assignee.go b/services/issue/assignee.go index 8cad03351c..7c00f472dd 100644 --- a/services/issue/assignee.go +++ b/services/issue/assignee.go @@ -7,8 +7,8 @@ package issue import ( "context" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + issues_model "code.gitea.io/gitea/models/issues" "code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/perm" access_model "code.gitea.io/gitea/models/perm/access" @@ -19,7 +19,7 @@ import ( ) // DeleteNotPassedAssignee deletes all assignees who aren't passed via the "assignees" array -func DeleteNotPassedAssignee(issue *models.Issue, doer *user_model.User, assignees []*user_model.User) (err error) { +func DeleteNotPassedAssignee(issue *issues_model.Issue, doer *user_model.User, assignees []*user_model.User) (err error) { var found bool oriAssignes := make([]*user_model.User, len(issue.Assignees)) _ = copy(oriAssignes, issue.Assignees) @@ -45,8 +45,8 @@ func DeleteNotPassedAssignee(issue *models.Issue, doer *user_model.User, assigne } // ToggleAssignee changes a user between assigned and not assigned for this issue, and make issue comment for it. -func ToggleAssignee(issue *models.Issue, doer *user_model.User, assigneeID int64) (removed bool, comment *models.Comment, err error) { - removed, comment, err = models.ToggleIssueAssignee(issue, doer, assigneeID) +func ToggleAssignee(issue *issues_model.Issue, doer *user_model.User, assigneeID int64) (removed bool, comment *issues_model.Comment, err error) { + removed, comment, err = issues_model.ToggleIssueAssignee(issue, doer, assigneeID) if err != nil { return } @@ -63,11 +63,11 @@ func ToggleAssignee(issue *models.Issue, doer *user_model.User, assigneeID int64 } // ReviewRequest add or remove a review request from a user for this PR, and make comment for it. -func ReviewRequest(issue *models.Issue, doer, reviewer *user_model.User, isAdd bool) (comment *models.Comment, err error) { +func ReviewRequest(issue *issues_model.Issue, doer, reviewer *user_model.User, isAdd bool) (comment *issues_model.Comment, err error) { if isAdd { - comment, err = models.AddReviewRequest(issue, reviewer, doer) + comment, err = issues_model.AddReviewRequest(issue, reviewer, doer) } else { - comment, err = models.RemoveReviewRequest(issue, reviewer, doer) + comment, err = issues_model.RemoveReviewRequest(issue, reviewer, doer) } if err != nil { @@ -82,16 +82,16 @@ func ReviewRequest(issue *models.Issue, doer, reviewer *user_model.User, isAdd b } // IsValidReviewRequest Check permission for ReviewRequest -func IsValidReviewRequest(ctx context.Context, reviewer, doer *user_model.User, isAdd bool, issue *models.Issue, permDoer *access_model.Permission) error { +func IsValidReviewRequest(ctx context.Context, reviewer, doer *user_model.User, isAdd bool, issue *issues_model.Issue, permDoer *access_model.Permission) error { if reviewer.IsOrganization() { - return models.ErrNotValidReviewRequest{ + return issues_model.ErrNotValidReviewRequest{ Reason: "Organization can't be added as reviewer", UserID: doer.ID, RepoID: issue.Repo.ID, } } if doer.IsOrganization() { - return models.ErrNotValidReviewRequest{ + return issues_model.ErrNotValidReviewRequest{ Reason: "Organization can't be doer to add reviewer", UserID: doer.ID, RepoID: issue.Repo.ID, @@ -111,8 +111,8 @@ func IsValidReviewRequest(ctx context.Context, reviewer, doer *user_model.User, } } - lastreview, err := models.GetReviewByIssueIDAndUserID(ctx, issue.ID, reviewer.ID) - if err != nil && !models.IsErrReviewNotExist(err) { + lastreview, err := issues_model.GetReviewByIssueIDAndUserID(ctx, issue.ID, reviewer.ID) + if err != nil && !issues_model.IsErrReviewNotExist(err) { return err } @@ -120,25 +120,25 @@ func IsValidReviewRequest(ctx context.Context, reviewer, doer *user_model.User, if isAdd { pemResult = permReviewer.CanAccessAny(perm.AccessModeRead, unit.TypePullRequests) if !pemResult { - return models.ErrNotValidReviewRequest{ + return issues_model.ErrNotValidReviewRequest{ Reason: "Reviewer can't read", UserID: doer.ID, RepoID: issue.Repo.ID, } } - if doer.ID == issue.PosterID && issue.OriginalAuthorID == 0 && lastreview != nil && lastreview.Type != models.ReviewTypeRequest { + if doer.ID == issue.PosterID && issue.OriginalAuthorID == 0 && lastreview != nil && lastreview.Type != issues_model.ReviewTypeRequest { return nil } pemResult = permDoer.CanAccessAny(perm.AccessModeWrite, unit.TypePullRequests) if !pemResult { - pemResult, err = models.IsOfficialReviewer(ctx, issue, doer) + pemResult, err = issues_model.IsOfficialReviewer(ctx, issue, doer) if err != nil { return err } if !pemResult { - return models.ErrNotValidReviewRequest{ + return issues_model.ErrNotValidReviewRequest{ Reason: "Doer can't choose reviewer", UserID: doer.ID, RepoID: issue.Repo.ID, @@ -147,20 +147,20 @@ func IsValidReviewRequest(ctx context.Context, reviewer, doer *user_model.User, } if reviewer.ID == issue.PosterID && issue.OriginalAuthorID == 0 { - return models.ErrNotValidReviewRequest{ + return issues_model.ErrNotValidReviewRequest{ Reason: "poster of pr can't be reviewer", UserID: doer.ID, RepoID: issue.Repo.ID, } } } else { - if lastreview != nil && lastreview.Type == models.ReviewTypeRequest && lastreview.ReviewerID == doer.ID { + if lastreview != nil && lastreview.Type == issues_model.ReviewTypeRequest && lastreview.ReviewerID == doer.ID { return nil } pemResult = permDoer.IsAdmin() if !pemResult { - return models.ErrNotValidReviewRequest{ + return issues_model.ErrNotValidReviewRequest{ Reason: "Doer is not admin", UserID: doer.ID, RepoID: issue.Repo.ID, @@ -172,9 +172,9 @@ func IsValidReviewRequest(ctx context.Context, reviewer, doer *user_model.User, } // IsValidTeamReviewRequest Check permission for ReviewRequest Team -func IsValidTeamReviewRequest(ctx context.Context, reviewer *organization.Team, doer *user_model.User, isAdd bool, issue *models.Issue) error { +func IsValidTeamReviewRequest(ctx context.Context, reviewer *organization.Team, doer *user_model.User, isAdd bool, issue *issues_model.Issue) error { if doer.IsOrganization() { - return models.ErrNotValidReviewRequest{ + return issues_model.ErrNotValidReviewRequest{ Reason: "Organization can't be doer to add reviewer", UserID: doer.ID, RepoID: issue.Repo.ID, @@ -192,7 +192,7 @@ func IsValidTeamReviewRequest(ctx context.Context, reviewer *organization.Team, hasTeam := organization.HasTeamRepo(ctx, reviewer.OrgID, reviewer.ID, issue.RepoID) if !hasTeam { - return models.ErrNotValidReviewRequest{ + return issues_model.ErrNotValidReviewRequest{ Reason: "Reviewing team can't read repo", UserID: doer.ID, RepoID: issue.Repo.ID, @@ -202,13 +202,13 @@ func IsValidTeamReviewRequest(ctx context.Context, reviewer *organization.Team, doerCanWrite := permission.CanAccessAny(perm.AccessModeWrite, unit.TypePullRequests) if !doerCanWrite { - official, err := models.IsOfficialReviewer(ctx, issue, doer) + official, err := issues_model.IsOfficialReviewer(ctx, issue, doer) if err != nil { log.Error("Unable to Check if IsOfficialReviewer for %-v in %-v#%d", doer, issue.Repo, issue.Index) return err } if !official { - return models.ErrNotValidReviewRequest{ + return issues_model.ErrNotValidReviewRequest{ Reason: "Doer can't choose reviewer", UserID: doer.ID, RepoID: issue.Repo.ID, @@ -216,7 +216,7 @@ func IsValidTeamReviewRequest(ctx context.Context, reviewer *organization.Team, } } } else if !permission.IsAdmin() { - return models.ErrNotValidReviewRequest{ + return issues_model.ErrNotValidReviewRequest{ Reason: "Only admin users can remove team requests. Doer is not admin", UserID: doer.ID, RepoID: issue.Repo.ID, @@ -227,11 +227,11 @@ func IsValidTeamReviewRequest(ctx context.Context, reviewer *organization.Team, } // TeamReviewRequest add or remove a review request from a team for this PR, and make comment for it. -func TeamReviewRequest(issue *models.Issue, doer *user_model.User, reviewer *organization.Team, isAdd bool) (comment *models.Comment, err error) { +func TeamReviewRequest(issue *issues_model.Issue, doer *user_model.User, reviewer *organization.Team, isAdd bool) (comment *issues_model.Comment, err error) { if isAdd { - comment, err = models.AddTeamReviewRequest(issue, reviewer, doer) + comment, err = issues_model.AddTeamReviewRequest(issue, reviewer, doer) } else { - comment, err = models.RemoveTeamReviewRequest(issue, reviewer, doer) + comment, err = issues_model.RemoveTeamReviewRequest(issue, reviewer, doer) } if err != nil { diff --git a/services/issue/assignee_test.go b/services/issue/assignee_test.go index ff4d7029eb..5c8b822499 100644 --- a/services/issue/assignee_test.go +++ b/services/issue/assignee_test.go @@ -7,8 +7,8 @@ package issue import ( "testing" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + issues_model "code.gitea.io/gitea/models/issues" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" @@ -19,7 +19,7 @@ func TestDeleteNotPassedAssignee(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) // Fake issue with assignees - issue, err := models.GetIssueWithAttrsByID(1) + issue, err := issues_model.GetIssueWithAttrsByID(1) assert.NoError(t, err) assert.EqualValues(t, 1, len(issue.Assignees)) @@ -27,7 +27,7 @@ func TestDeleteNotPassedAssignee(t *testing.T) { assert.NoError(t, err) // Check if he got removed - isAssigned, err := models.IsUserAssignedToIssue(db.DefaultContext, issue, user1) + isAssigned, err := issues_model.IsUserAssignedToIssue(db.DefaultContext, issue, user1) assert.NoError(t, err) assert.True(t, isAssigned) diff --git a/services/issue/commit.go b/services/issue/commit.go index 5140eebed1..1053a81162 100644 --- a/services/issue/commit.go +++ b/services/issue/commit.go @@ -15,6 +15,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + issues_model "code.gitea.io/gitea/models/issues" access_model "code.gitea.io/gitea/models/perm/access" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" @@ -76,22 +77,22 @@ func timeLogToAmount(str string) int64 { return a } -func issueAddTime(issue *models.Issue, doer *user_model.User, time time.Time, timeLog string) error { +func issueAddTime(issue *issues_model.Issue, doer *user_model.User, time time.Time, timeLog string) error { amount := timeLogToAmount(timeLog) if amount == 0 { return nil } - _, err := models.AddTime(doer, issue, amount, time) + _, err := issues_model.AddTime(doer, issue, amount, time) return err } // getIssueFromRef returns the issue referenced by a ref. Returns a nil *Issue // if the provided ref references a non-existent issue. -func getIssueFromRef(repo *repo_model.Repository, index int64) (*models.Issue, error) { - issue, err := models.GetIssueByIndex(repo.ID, index) +func getIssueFromRef(repo *repo_model.Repository, index int64) (*issues_model.Issue, error) { + issue, err := issues_model.GetIssueByIndex(repo.ID, index) if err != nil { - if models.IsErrIssueNotExist(err) { + if issues_model.IsErrIssueNotExist(err) { return nil, nil } return nil, err @@ -112,7 +113,7 @@ func UpdateIssuesCommit(doer *user_model.User, repo *repo_model.Repository, comm refMarked := make(map[markKey]bool) var refRepo *repo_model.Repository - var refIssue *models.Issue + var refIssue *issues_model.Issue var err error for _, ref := range references.FindAllIssueReferences(c.Message) { @@ -153,7 +154,7 @@ func UpdateIssuesCommit(doer *user_model.User, repo *repo_model.Repository, comm } message := fmt.Sprintf(`<a href="%s/commit/%s">%s</a>`, html.EscapeString(repo.Link()), html.EscapeString(url.PathEscape(c.Sha1)), html.EscapeString(strings.SplitN(c.Message, "\n", 2)[0])) - if err = models.CreateRefComment(doer, refRepo, refIssue, message, c.Sha1); err != nil { + if err = issues_model.CreateRefComment(doer, refRepo, refIssue, message, c.Sha1); err != nil { return err } diff --git a/services/issue/commit_test.go b/services/issue/commit_test.go index 37283a7890..ce3f913627 100644 --- a/services/issue/commit_test.go +++ b/services/issue/commit_test.go @@ -8,6 +8,7 @@ import ( "testing" "code.gitea.io/gitea/models" + issues_model "code.gitea.io/gitea/models/issues" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" @@ -50,16 +51,16 @@ func TestUpdateIssuesCommit(t *testing.T) { repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository) repo.Owner = user - commentBean := &models.Comment{ - Type: models.CommentTypeCommitRef, + commentBean := &issues_model.Comment{ + Type: issues_model.CommentTypeCommitRef, CommitSHA: "abcdef1", PosterID: user.ID, IssueID: 1, } - issueBean := &models.Issue{RepoID: repo.ID, Index: 4} + issueBean := &issues_model.Issue{RepoID: repo.ID, Index: 4} unittest.AssertNotExistsBean(t, commentBean) - unittest.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 2}, "is_closed=1") + unittest.AssertNotExistsBean(t, &issues_model.Issue{RepoID: repo.ID, Index: 2}, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch)) unittest.AssertExistsAndLoadBean(t, commentBean) unittest.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") @@ -77,16 +78,16 @@ func TestUpdateIssuesCommit(t *testing.T) { }, } repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}).(*repo_model.Repository) - commentBean = &models.Comment{ - Type: models.CommentTypeCommitRef, + commentBean = &issues_model.Comment{ + Type: issues_model.CommentTypeCommitRef, CommitSHA: "abcdef1", PosterID: user.ID, IssueID: 6, } - issueBean = &models.Issue{RepoID: repo.ID, Index: 1} + issueBean = &issues_model.Issue{RepoID: repo.ID, Index: 1} unittest.AssertNotExistsBean(t, commentBean) - unittest.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 1}, "is_closed=1") + unittest.AssertNotExistsBean(t, &issues_model.Issue{RepoID: repo.ID, Index: 1}, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, "non-existing-branch")) unittest.AssertExistsAndLoadBean(t, commentBean) unittest.AssertNotExistsBean(t, issueBean, "is_closed=1") @@ -103,16 +104,16 @@ func TestUpdateIssuesCommit(t *testing.T) { }, } repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}).(*repo_model.Repository) - commentBean = &models.Comment{ - Type: models.CommentTypeCommitRef, + commentBean = &issues_model.Comment{ + Type: issues_model.CommentTypeCommitRef, CommitSHA: "abcdef3", PosterID: user.ID, IssueID: 6, } - issueBean = &models.Issue{RepoID: repo.ID, Index: 1} + issueBean = &issues_model.Issue{RepoID: repo.ID, Index: 1} unittest.AssertNotExistsBean(t, commentBean) - unittest.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 1}, "is_closed=1") + unittest.AssertNotExistsBean(t, &issues_model.Issue{RepoID: repo.ID, Index: 1}, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch)) unittest.AssertExistsAndLoadBean(t, commentBean) unittest.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") @@ -136,9 +137,9 @@ func TestUpdateIssuesCommit_Colon(t *testing.T) { repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository) repo.Owner = user - issueBean := &models.Issue{RepoID: repo.ID, Index: 4} + issueBean := &issues_model.Issue{RepoID: repo.ID, Index: 4} - unittest.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 2}, "is_closed=1") + unittest.AssertNotExistsBean(t, &issues_model.Issue{RepoID: repo.ID, Index: 2}, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch)) unittest.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") unittest.CheckConsistencyFor(t, &models.Action{}) @@ -161,14 +162,14 @@ func TestUpdateIssuesCommit_Issue5957(t *testing.T) { } repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}).(*repo_model.Repository) - commentBean := &models.Comment{ - Type: models.CommentTypeCommitRef, + commentBean := &issues_model.Comment{ + Type: issues_model.CommentTypeCommitRef, CommitSHA: "abcdef1", PosterID: user.ID, IssueID: 7, } - issueBean := &models.Issue{RepoID: repo.ID, Index: 2, ID: 7} + issueBean := &issues_model.Issue{RepoID: repo.ID, Index: 2, ID: 7} unittest.AssertNotExistsBean(t, commentBean) unittest.AssertNotExistsBean(t, issueBean, "is_closed=1") @@ -196,14 +197,14 @@ func TestUpdateIssuesCommit_AnotherRepo(t *testing.T) { } repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}).(*repo_model.Repository) - commentBean := &models.Comment{ - Type: models.CommentTypeCommitRef, + commentBean := &issues_model.Comment{ + Type: issues_model.CommentTypeCommitRef, CommitSHA: "abcdef1", PosterID: user.ID, IssueID: 1, } - issueBean := &models.Issue{RepoID: 1, Index: 1, ID: 1} + issueBean := &issues_model.Issue{RepoID: 1, Index: 1, ID: 1} unittest.AssertNotExistsBean(t, commentBean) unittest.AssertNotExistsBean(t, issueBean, "is_closed=1") @@ -231,14 +232,14 @@ func TestUpdateIssuesCommit_AnotherRepo_FullAddress(t *testing.T) { } repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}).(*repo_model.Repository) - commentBean := &models.Comment{ - Type: models.CommentTypeCommitRef, + commentBean := &issues_model.Comment{ + Type: issues_model.CommentTypeCommitRef, CommitSHA: "abcdef1", PosterID: user.ID, IssueID: 1, } - issueBean := &models.Issue{RepoID: 1, Index: 1, ID: 1} + issueBean := &issues_model.Issue{RepoID: 1, Index: 1, ID: 1} unittest.AssertNotExistsBean(t, commentBean) unittest.AssertNotExistsBean(t, issueBean, "is_closed=1") @@ -274,20 +275,20 @@ func TestUpdateIssuesCommit_AnotherRepoNoPermission(t *testing.T) { } repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 6}).(*repo_model.Repository) - commentBean := &models.Comment{ - Type: models.CommentTypeCommitRef, + commentBean := &issues_model.Comment{ + Type: issues_model.CommentTypeCommitRef, CommitSHA: "abcdef3", PosterID: user.ID, IssueID: 6, } - commentBean2 := &models.Comment{ - Type: models.CommentTypeCommitRef, + commentBean2 := &issues_model.Comment{ + Type: issues_model.CommentTypeCommitRef, CommitSHA: "abcdef4", PosterID: user.ID, IssueID: 6, } - issueBean := &models.Issue{RepoID: 3, Index: 1, ID: 6} + issueBean := &issues_model.Issue{RepoID: 3, Index: 1, ID: 6} unittest.AssertNotExistsBean(t, commentBean) unittest.AssertNotExistsBean(t, commentBean2) diff --git a/services/issue/content.go b/services/issue/content.go index a60878479b..6f493892f4 100644 --- a/services/issue/content.go +++ b/services/issue/content.go @@ -5,16 +5,16 @@ package issue import ( - "code.gitea.io/gitea/models" + issues_model "code.gitea.io/gitea/models/issues" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/notification" ) // ChangeContent changes issue content, as the given user. -func ChangeContent(issue *models.Issue, doer *user_model.User, content string) (err error) { +func ChangeContent(issue *issues_model.Issue, doer *user_model.User, content string) (err error) { oldContent := issue.Content - if err := models.ChangeIssueContent(issue, doer, content); err != nil { + if err := issues_model.ChangeIssueContent(issue, doer, content); err != nil { return err } diff --git a/services/issue/issue.go b/services/issue/issue.go index 78a486727a..ded281e209 100644 --- a/services/issue/issue.go +++ b/services/issue/issue.go @@ -8,18 +8,22 @@ import ( "fmt" "code.gitea.io/gitea/models" + admin_model "code.gitea.io/gitea/models/admin" "code.gitea.io/gitea/models/db" + issues_model "code.gitea.io/gitea/models/issues" access_model "code.gitea.io/gitea/models/perm/access" + project_model "code.gitea.io/gitea/models/project" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/notification" + "code.gitea.io/gitea/modules/storage" "code.gitea.io/gitea/modules/util" ) // NewIssue creates new issue with labels for repository. -func NewIssue(repo *repo_model.Repository, issue *models.Issue, labelIDs []int64, uuids []string, assigneeIDs []int64) error { - if err := models.NewIssue(repo, issue, labelIDs, uuids); err != nil { +func NewIssue(repo *repo_model.Repository, issue *issues_model.Issue, labelIDs []int64, uuids []string, assigneeIDs []int64) error { + if err := issues_model.NewIssue(repo, issue, labelIDs, uuids); err != nil { return err } @@ -29,7 +33,7 @@ func NewIssue(repo *repo_model.Repository, issue *models.Issue, labelIDs []int64 } } - mentions, err := models.FindAndUpdateIssueMentions(db.DefaultContext, issue, issue.Poster, issue.Content) + mentions, err := issues_model.FindAndUpdateIssueMentions(db.DefaultContext, issue, issue.Poster, issue.Content) if err != nil { return err } @@ -46,11 +50,11 @@ func NewIssue(repo *repo_model.Repository, issue *models.Issue, labelIDs []int64 } // ChangeTitle changes the title of this issue, as the given user. -func ChangeTitle(issue *models.Issue, doer *user_model.User, title string) (err error) { +func ChangeTitle(issue *issues_model.Issue, doer *user_model.User, title string) (err error) { oldTitle := issue.Title issue.Title = title - if err = models.ChangeIssueTitle(issue, doer, oldTitle); err != nil { + if err = issues_model.ChangeIssueTitle(issue, doer, oldTitle); err != nil { return } @@ -60,11 +64,11 @@ func ChangeTitle(issue *models.Issue, doer *user_model.User, title string) (err } // ChangeIssueRef changes the branch of this issue, as the given user. -func ChangeIssueRef(issue *models.Issue, doer *user_model.User, ref string) error { +func ChangeIssueRef(issue *issues_model.Issue, doer *user_model.User, ref string) error { oldRef := issue.Ref issue.Ref = ref - if err := models.ChangeIssueRef(issue, doer, oldRef); err != nil { + if err := issues_model.ChangeIssueRef(issue, doer, oldRef); err != nil { return err } @@ -79,7 +83,7 @@ func ChangeIssueRef(issue *models.Issue, doer *user_model.User, ref string) erro // "assignees" (array): Logins for Users to assign to this issue. // Pass one or more user logins to replace the set of assignees on this Issue. // Send an empty array ([]) to clear all assignees from the Issue. -func UpdateAssignees(issue *models.Issue, oneAssignee string, multipleAssignees []string, doer *user_model.User) (err error) { +func UpdateAssignees(issue *issues_model.Issue, oneAssignee string, multipleAssignees []string, doer *user_model.User) (err error) { var allNewAssignees []*user_model.User // Keep the old assignee thingy for compatibility reasons @@ -129,9 +133,9 @@ func UpdateAssignees(issue *models.Issue, oneAssignee string, multipleAssignees } // DeleteIssue deletes an issue -func DeleteIssue(doer *user_model.User, gitRepo *git.Repository, issue *models.Issue) error { +func DeleteIssue(doer *user_model.User, gitRepo *git.Repository, issue *issues_model.Issue) error { // load issue before deleting it - if err := issue.LoadAttributes(); err != nil { + if err := issue.LoadAttributes(db.DefaultContext); err != nil { return err } if err := issue.LoadPullRequest(); err != nil { @@ -139,7 +143,7 @@ func DeleteIssue(doer *user_model.User, gitRepo *git.Repository, issue *models.I } // delete entries in database - if err := models.DeleteIssue(issue); err != nil { + if err := deleteIssue(issue); err != nil { return err } @@ -157,14 +161,14 @@ func DeleteIssue(doer *user_model.User, gitRepo *git.Repository, issue *models.I // AddAssigneeIfNotAssigned adds an assignee only if he isn't already assigned to the issue. // Also checks for access of assigned user -func AddAssigneeIfNotAssigned(issue *models.Issue, doer *user_model.User, assigneeID int64) (err error) { +func AddAssigneeIfNotAssigned(issue *issues_model.Issue, doer *user_model.User, assigneeID int64) (err error) { assignee, err := user_model.GetUserByID(assigneeID) if err != nil { return err } // Check if the user is already assigned - isAssigned, err := models.IsUserAssignedToIssue(db.DefaultContext, issue, assignee) + isAssigned, err := issues_model.IsUserAssignedToIssue(db.DefaultContext, issue, assignee) if err != nil { return err } @@ -178,7 +182,7 @@ func AddAssigneeIfNotAssigned(issue *models.Issue, doer *user_model.User, assign return err } if !valid { - return models.ErrUserDoesNotHaveAccessToRepo{UserID: assigneeID, RepoName: issue.Repo.Name} + return repo_model.ErrUserDoesNotHaveAccessToRepo{UserID: assigneeID, RepoName: issue.Repo.Name} } _, _, err = ToggleAssignee(issue, doer, assigneeID) @@ -191,7 +195,7 @@ func AddAssigneeIfNotAssigned(issue *models.Issue, doer *user_model.User, assign // GetRefEndNamesAndURLs retrieves the ref end names (e.g. refs/heads/branch-name -> branch-name) // and their respective URLs. -func GetRefEndNamesAndURLs(issues []*models.Issue, repoLink string) (map[int64]string, map[int64]string) { +func GetRefEndNamesAndURLs(issues []*issues_model.Issue, repoLink string) (map[int64]string, map[int64]string) { issueRefEndNames := make(map[int64]string, len(issues)) issueRefURLs := make(map[int64]string, len(issues)) for _, issue := range issues { @@ -202,3 +206,77 @@ func GetRefEndNamesAndURLs(issues []*models.Issue, repoLink string) (map[int64]s } return issueRefEndNames, issueRefURLs } + +// deleteIssue deletes the issue +func deleteIssue(issue *issues_model.Issue) error { + ctx, committer, err := db.TxContext() + if err != nil { + return err + } + defer committer.Close() + + e := db.GetEngine(ctx) + if _, err := e.ID(issue.ID).NoAutoCondition().Delete(issue); err != nil { + return err + } + + if err := repo_model.UpdateRepoIssueNumbers(ctx, issue.RepoID, issue.IsPull, issue.IsClosed); err != nil { + return err + } + + if err := models.DeleteIssueActions(ctx, issue.RepoID, issue.ID); err != nil { + return err + } + + // find attachments related to this issue and remove them + if err := issue.LoadAttributes(ctx); err != nil { + return err + } + + for i := range issue.Attachments { + admin_model.RemoveStorageWithNotice(ctx, storage.Attachments, "Delete issue attachment", issue.Attachments[i].RelativePath()) + } + + // delete all database data still assigned to this issue + if err := issues_model.DeleteInIssue(ctx, issue.ID, + &issues_model.ContentHistory{}, + &issues_model.Comment{}, + &issues_model.IssueLabel{}, + &issues_model.IssueDependency{}, + &issues_model.IssueAssignees{}, + &issues_model.IssueUser{}, + &models.Notification{}, + &issues_model.Reaction{}, + &issues_model.IssueWatch{}, + &issues_model.Stopwatch{}, + &issues_model.TrackedTime{}, + &project_model.ProjectIssue{}, + &repo_model.Attachment{}, + &issues_model.PullRequest{}, + ); err != nil { + return err + } + + // References to this issue in other issues + if _, err := db.DeleteByBean(ctx, &issues_model.Comment{ + RefIssueID: issue.ID, + }); err != nil { + return err + } + + // Delete dependencies for issues in other repositories + if _, err := db.DeleteByBean(ctx, &issues_model.IssueDependency{ + DependencyID: issue.ID, + }); err != nil { + return err + } + + // delete from dependent issues + if _, err := db.DeleteByBean(ctx, &issues_model.Comment{ + DependentIssueID: issue.ID, + }); err != nil { + return err + } + + return committer.Commit() +} diff --git a/services/issue/issue_test.go b/services/issue/issue_test.go index caae773616..20f3a3296c 100644 --- a/services/issue/issue_test.go +++ b/services/issue/issue_test.go @@ -7,13 +7,17 @@ package issue import ( "testing" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" + issues_model "code.gitea.io/gitea/models/issues" + repo_model "code.gitea.io/gitea/models/repo" + "code.gitea.io/gitea/models/unittest" + user_model "code.gitea.io/gitea/models/user" "github.com/stretchr/testify/assert" ) func TestGetRefEndNamesAndURLs(t *testing.T) { - issues := []*models.Issue{ + issues := []*issues_model.Issue{ {ID: 1, Ref: "refs/heads/branch1"}, {ID: 2, Ref: "refs/tags/tag1"}, {ID: 3, Ref: "c0ffee"}, @@ -28,3 +32,56 @@ func TestGetRefEndNamesAndURLs(t *testing.T) { 3: repoLink + "/src/commit/c0ffee", }, urls) } + +func TestIssue_DeleteIssue(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + issueIDs, err := issues_model.GetIssueIDsByRepoID(db.DefaultContext, 1) + assert.NoError(t, err) + assert.EqualValues(t, 5, len(issueIDs)) + + issue := &issues_model.Issue{ + RepoID: 1, + ID: issueIDs[2], + } + + err = deleteIssue(issue) + assert.NoError(t, err) + issueIDs, err = issues_model.GetIssueIDsByRepoID(db.DefaultContext, 1) + assert.NoError(t, err) + assert.EqualValues(t, 4, len(issueIDs)) + + // check attachment removal + attachments, err := repo_model.GetAttachmentsByIssueID(db.DefaultContext, 4) + assert.NoError(t, err) + issue, err = issues_model.GetIssueByID(db.DefaultContext, 4) + assert.NoError(t, err) + err = deleteIssue(issue) + assert.NoError(t, err) + assert.EqualValues(t, 2, len(attachments)) + for i := range attachments { + attachment, err := repo_model.GetAttachmentByUUID(db.DefaultContext, attachments[i].UUID) + assert.Error(t, err) + assert.True(t, repo_model.IsErrAttachmentNotExist(err)) + assert.Nil(t, attachment) + } + + // check issue dependencies + user, err := user_model.GetUserByID(1) + assert.NoError(t, err) + issue1, err := issues_model.GetIssueByID(db.DefaultContext, 1) + assert.NoError(t, err) + issue2, err := issues_model.GetIssueByID(db.DefaultContext, 2) + assert.NoError(t, err) + err = issues_model.CreateIssueDependency(user, issue1, issue2) + assert.NoError(t, err) + left, err := issues_model.IssueNoDependenciesLeft(db.DefaultContext, issue1) + assert.NoError(t, err) + assert.False(t, left) + + err = deleteIssue(issue2) + assert.NoError(t, err) + left, err = issues_model.IssueNoDependenciesLeft(db.DefaultContext, issue1) + assert.NoError(t, err) + assert.True(t, left) +} diff --git a/services/issue/label.go b/services/issue/label.go index 289466f604..bc5f9b910e 100644 --- a/services/issue/label.go +++ b/services/issue/label.go @@ -5,16 +5,16 @@ package issue import ( - "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + issues_model "code.gitea.io/gitea/models/issues" access_model "code.gitea.io/gitea/models/perm/access" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/notification" ) // ClearLabels clears all of an issue's labels -func ClearLabels(issue *models.Issue, doer *user_model.User) (err error) { - if err = models.ClearIssueLabels(issue, doer); err != nil { +func ClearLabels(issue *issues_model.Issue, doer *user_model.User) (err error) { + if err = issues_model.ClearIssueLabels(issue, doer); err != nil { return } @@ -24,18 +24,18 @@ func ClearLabels(issue *models.Issue, doer *user_model.User) (err error) { } // AddLabel adds a new label to the issue. -func AddLabel(issue *models.Issue, doer *user_model.User, label *models.Label) error { - if err := models.NewIssueLabel(issue, label, doer); err != nil { +func AddLabel(issue *issues_model.Issue, doer *user_model.User, label *issues_model.Label) error { + if err := issues_model.NewIssueLabel(issue, label, doer); err != nil { return err } - notification.NotifyIssueChangeLabels(doer, issue, []*models.Label{label}, nil) + notification.NotifyIssueChangeLabels(doer, issue, []*issues_model.Label{label}, nil) return nil } // AddLabels adds a list of new labels to the issue. -func AddLabels(issue *models.Issue, doer *user_model.User, labels []*models.Label) error { - if err := models.NewIssueLabels(issue, labels, doer); err != nil { +func AddLabels(issue *issues_model.Issue, doer *user_model.User, labels []*issues_model.Label) error { + if err := issues_model.NewIssueLabels(issue, labels, doer); err != nil { return err } @@ -44,7 +44,7 @@ func AddLabels(issue *models.Issue, doer *user_model.User, labels []*models.Labe } // RemoveLabel removes a label from issue by given ID. -func RemoveLabel(issue *models.Issue, doer *user_model.User, label *models.Label) error { +func RemoveLabel(issue *issues_model.Issue, doer *user_model.User, label *issues_model.Label) error { ctx, committer, err := db.TxContext() if err != nil { return err @@ -61,12 +61,12 @@ func RemoveLabel(issue *models.Issue, doer *user_model.User, label *models.Label } if !perm.CanWriteIssuesOrPulls(issue.IsPull) { if label.OrgID > 0 { - return models.ErrOrgLabelNotExist{} + return issues_model.ErrOrgLabelNotExist{} } - return models.ErrRepoLabelNotExist{} + return issues_model.ErrRepoLabelNotExist{} } - if err := models.DeleteIssueLabel(ctx, issue, label, doer); err != nil { + if err := issues_model.DeleteIssueLabel(ctx, issue, label, doer); err != nil { return err } @@ -74,18 +74,18 @@ func RemoveLabel(issue *models.Issue, doer *user_model.User, label *models.Label return err } - notification.NotifyIssueChangeLabels(doer, issue, nil, []*models.Label{label}) + notification.NotifyIssueChangeLabels(doer, issue, nil, []*issues_model.Label{label}) return nil } // ReplaceLabels removes all current labels and add new labels to the issue. -func ReplaceLabels(issue *models.Issue, doer *user_model.User, labels []*models.Label) error { - old, err := models.GetLabelsByIssueID(db.DefaultContext, issue.ID) +func ReplaceLabels(issue *issues_model.Issue, doer *user_model.User, labels []*issues_model.Label) error { + old, err := issues_model.GetLabelsByIssueID(db.DefaultContext, issue.ID) if err != nil { return err } - if err := models.ReplaceIssueLabels(issue, labels, doer); err != nil { + if err := issues_model.ReplaceIssueLabels(issue, labels, doer); err != nil { return err } diff --git a/services/issue/label_test.go b/services/issue/label_test.go index 73e30e894f..120c9ea4f1 100644 --- a/services/issue/label_test.go +++ b/services/issue/label_test.go @@ -7,7 +7,7 @@ package issue import ( "testing" - "code.gitea.io/gitea/models" + issues_model "code.gitea.io/gitea/models/issues" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" @@ -27,15 +27,15 @@ func TestIssue_AddLabels(t *testing.T) { } for _, test := range tests { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: test.issueID}).(*models.Issue) - labels := make([]*models.Label, len(test.labelIDs)) + issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: test.issueID}).(*issues_model.Issue) + labels := make([]*issues_model.Label, len(test.labelIDs)) for i, labelID := range test.labelIDs { - labels[i] = unittest.AssertExistsAndLoadBean(t, &models.Label{ID: labelID}).(*models.Label) + labels[i] = unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: labelID}).(*issues_model.Label) } doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: test.doerID}).(*user_model.User) assert.NoError(t, AddLabels(issue, doer, labels)) for _, labelID := range test.labelIDs { - unittest.AssertExistsAndLoadBean(t, &models.IssueLabel{IssueID: test.issueID, LabelID: labelID}) + unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: test.issueID, LabelID: labelID}) } } } @@ -53,10 +53,10 @@ func TestIssue_AddLabel(t *testing.T) { } for _, test := range tests { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: test.issueID}).(*models.Issue) - label := unittest.AssertExistsAndLoadBean(t, &models.Label{ID: test.labelID}).(*models.Label) + issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: test.issueID}).(*issues_model.Issue) + label := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: test.labelID}).(*issues_model.Label) doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: test.doerID}).(*user_model.User) assert.NoError(t, AddLabel(issue, doer, label)) - unittest.AssertExistsAndLoadBean(t, &models.IssueLabel{IssueID: test.issueID, LabelID: test.labelID}) + unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: test.issueID, LabelID: test.labelID}) } } diff --git a/services/issue/milestone.go b/services/issue/milestone.go index 287f8ae285..af337c3f14 100644 --- a/services/issue/milestone.go +++ b/services/issue/milestone.go @@ -8,15 +8,14 @@ import ( "context" "fmt" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" issues_model "code.gitea.io/gitea/models/issues" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/notification" ) -func changeMilestoneAssign(ctx context.Context, doer *user_model.User, issue *models.Issue, oldMilestoneID int64) error { - if err := models.UpdateIssueCols(ctx, issue, "milestone_id"); err != nil { +func changeMilestoneAssign(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldMilestoneID int64) error { + if err := issues_model.UpdateIssueCols(ctx, issue, "milestone_id"); err != nil { return err } @@ -37,15 +36,15 @@ func changeMilestoneAssign(ctx context.Context, doer *user_model.User, issue *mo return err } - opts := &models.CreateCommentOptions{ - Type: models.CommentTypeMilestone, + opts := &issues_model.CreateCommentOptions{ + Type: issues_model.CommentTypeMilestone, Doer: doer, Repo: issue.Repo, Issue: issue, OldMilestoneID: oldMilestoneID, MilestoneID: issue.MilestoneID, } - if _, err := models.CreateCommentCtx(ctx, opts); err != nil { + if _, err := issues_model.CreateCommentCtx(ctx, opts); err != nil { return err } } @@ -54,7 +53,7 @@ func changeMilestoneAssign(ctx context.Context, doer *user_model.User, issue *mo } // ChangeMilestoneAssign changes assignment of milestone for issue. -func ChangeMilestoneAssign(issue *models.Issue, doer *user_model.User, oldMilestoneID int64) (err error) { +func ChangeMilestoneAssign(issue *issues_model.Issue, doer *user_model.User, oldMilestoneID int64) (err error) { ctx, committer, err := db.TxContext() if err != nil { return err diff --git a/services/issue/milestone_test.go b/services/issue/milestone_test.go index 80e37a8acd..d08b1ae8c7 100644 --- a/services/issue/milestone_test.go +++ b/services/issue/milestone_test.go @@ -7,7 +7,6 @@ package issue import ( "testing" - "code.gitea.io/gitea/models" issues_model "code.gitea.io/gitea/models/issues" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" @@ -17,7 +16,7 @@ import ( func TestChangeMilestoneAssign(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{RepoID: 1}).(*models.Issue) + issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{RepoID: 1}).(*issues_model.Issue) doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User) assert.NotNil(t, issue) assert.NotNil(t, doer) @@ -25,11 +24,11 @@ func TestChangeMilestoneAssign(t *testing.T) { oldMilestoneID := issue.MilestoneID issue.MilestoneID = 2 assert.NoError(t, ChangeMilestoneAssign(issue, doer, oldMilestoneID)) - unittest.AssertExistsAndLoadBean(t, &models.Comment{ + unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ IssueID: issue.ID, - Type: models.CommentTypeMilestone, + Type: issues_model.CommentTypeMilestone, MilestoneID: issue.MilestoneID, OldMilestoneID: oldMilestoneID, }) - unittest.CheckConsistencyFor(t, &issues_model.Milestone{}, &models.Issue{}) + unittest.CheckConsistencyFor(t, &issues_model.Milestone{}, &issues_model.Issue{}) } diff --git a/services/issue/status.go b/services/issue/status.go index d2b4fc303e..0da5c88762 100644 --- a/services/issue/status.go +++ b/services/issue/status.go @@ -7,25 +7,25 @@ package issue import ( "context" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + issues_model "code.gitea.io/gitea/models/issues" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification" ) // ChangeStatus changes issue status to open or closed. -func ChangeStatus(issue *models.Issue, doer *user_model.User, closed bool) error { +func ChangeStatus(issue *issues_model.Issue, doer *user_model.User, closed bool) error { return changeStatusCtx(db.DefaultContext, issue, doer, closed) } // changeStatusCtx changes issue status to open or closed. // TODO: if context is not db.DefaultContext we get a deadlock!!! -func changeStatusCtx(ctx context.Context, issue *models.Issue, doer *user_model.User, closed bool) error { - comment, err := models.ChangeIssueStatus(ctx, issue, doer, closed) +func changeStatusCtx(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, closed bool) error { + comment, err := issues_model.ChangeIssueStatus(ctx, issue, doer, closed) if err != nil { - if models.IsErrDependenciesLeft(err) && closed { - if err := models.FinishIssueStopwatchIfPossible(ctx, doer, issue); err != nil { + if issues_model.IsErrDependenciesLeft(err) && closed { + if err := issues_model.FinishIssueStopwatchIfPossible(ctx, doer, issue); err != nil { log.Error("Unable to stop stopwatch for issue[%d]#%d: %v", issue.ID, issue.Index, err) } } @@ -33,7 +33,7 @@ func changeStatusCtx(ctx context.Context, issue *models.Issue, doer *user_model. } if closed { - if err := models.FinishIssueStopwatchIfPossible(ctx, doer, issue); err != nil { + if err := issues_model.FinishIssueStopwatchIfPossible(ctx, doer, issue); err != nil { return err } } |