Move repository model into models/repo (#17933)

* Some refactors related repository model

* Move more methods out of repository

* Move repository into models/repo

* Fix test

* Fix test

* some improvements

* Remove unnecessary function
This commit is contained in:
Lunny Xiao 2021-12-10 09:27:50 +08:00 committed by GitHub
parent fb8166c6c6
commit 719bddcd76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
301 changed files with 3193 additions and 2919 deletions

View file

@ -11,6 +11,7 @@ import (
"strconv"
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/log"
@ -63,10 +64,10 @@ type Notification struct {
UpdatedBy int64 `xorm:"INDEX NOT NULL"`
Issue *Issue `xorm:"-"`
Repository *Repository `xorm:"-"`
Comment *Comment `xorm:"-"`
User *user_model.User `xorm:"-"`
Issue *Issue `xorm:"-"`
Repository *repo_model.Repository `xorm:"-"`
Comment *Comment `xorm:"-"`
User *user_model.User `xorm:"-"`
CreatedUnix timeutil.TimeStamp `xorm:"created INDEX NOT NULL"`
UpdatedUnix timeutil.TimeStamp `xorm:"updated INDEX NOT NULL"`
@ -140,7 +141,7 @@ func CountNotifications(opts *FindNotificationOptions) (int64, error) {
}
// CreateRepoTransferNotification creates notification for the user a repository was transferred to
func CreateRepoTransferNotification(doer, newOwner *user_model.User, repo *Repository) error {
func CreateRepoTransferNotification(doer, newOwner *user_model.User, repo *repo_model.Repository) error {
ctx, committer, err := db.TxContext()
if err != nil {
return err
@ -190,14 +191,15 @@ func CreateOrUpdateIssueNotifications(issueID, commentID, notificationAuthorID,
}
defer committer.Close()
if err := createOrUpdateIssueNotifications(db.GetEngine(ctx), issueID, commentID, notificationAuthorID, receiverID); err != nil {
if err := createOrUpdateIssueNotifications(ctx, issueID, commentID, notificationAuthorID, receiverID); err != nil {
return err
}
return committer.Commit()
}
func createOrUpdateIssueNotifications(e db.Engine, issueID, commentID, notificationAuthorID, receiverID int64) error {
func createOrUpdateIssueNotifications(ctx context.Context, issueID, commentID, notificationAuthorID, receiverID int64) error {
e := db.GetEngine(ctx)
// init
var toNotify map[int64]struct{}
notifications, err := getNotificationsByIssueID(e, issueID)
@ -251,7 +253,7 @@ func createOrUpdateIssueNotifications(e db.Engine, issueID, commentID, notificat
}
}
err = issue.loadRepo(e)
err = issue.loadRepo(ctx)
if err != nil {
return err
}
@ -267,10 +269,10 @@ func createOrUpdateIssueNotifications(e db.Engine, issueID, commentID, notificat
return err
}
if issue.IsPull && !issue.Repo.checkUnitUser(e, user, unit.TypePullRequests) {
if issue.IsPull && !checkRepoUnitUser(ctx, issue.Repo, user, unit.TypePullRequests) {
continue
}
if !issue.IsPull && !issue.Repo.checkUnitUser(e, user, unit.TypeIssues) {
if !issue.IsPull && !checkRepoUnitUser(ctx, issue.Repo, user, unit.TypeIssues) {
continue
}
@ -399,7 +401,7 @@ func (n *Notification) LoadAttributes() (err error) {
func (n *Notification) loadAttributes(ctx context.Context) (err error) {
e := db.GetEngine(ctx)
if err = n.loadRepo(e); err != nil {
if err = n.loadRepo(ctx); err != nil {
return
}
if err = n.loadIssue(ctx); err != nil {
@ -414,9 +416,9 @@ func (n *Notification) loadAttributes(ctx context.Context) (err error) {
return
}
func (n *Notification) loadRepo(e db.Engine) (err error) {
func (n *Notification) loadRepo(ctx context.Context) (err error) {
if n.Repository == nil {
n.Repository, err = getRepositoryByID(e, n.RepoID)
n.Repository, err = repo_model.GetRepositoryByIDCtx(ctx, n.RepoID)
if err != nil {
return fmt.Errorf("getRepositoryByID [%d]: %v", n.RepoID, err)
}
@ -462,8 +464,8 @@ func (n *Notification) loadUser(e db.Engine) (err error) {
}
// GetRepo returns the repo of the notification
func (n *Notification) GetRepo() (*Repository, error) {
return n.Repository, n.loadRepo(db.GetEngine(db.DefaultContext))
func (n *Notification) GetRepo() (*repo_model.Repository, error) {
return n.Repository, n.loadRepo(db.DefaultContext)
}
// GetIssue returns the issue of the notification
@ -526,7 +528,7 @@ func (nl NotificationList) LoadRepos() (RepositoryList, []int, error) {
}
repoIDs := nl.getPendingRepoIDs()
repos := make(map[int64]*Repository, len(repoIDs))
repos := make(map[int64]*repo_model.Repository, len(repoIDs))
left := len(repoIDs)
for left > 0 {
limit := defaultMaxInSize
@ -535,13 +537,13 @@ func (nl NotificationList) LoadRepos() (RepositoryList, []int, error) {
}
rows, err := db.GetEngine(db.DefaultContext).
In("id", repoIDs[:limit]).
Rows(new(Repository))
Rows(new(repo_model.Repository))
if err != nil {
return nil, nil, err
}
for rows.Next() {
var repo Repository
var repo repo_model.Repository
err = rows.Scan(&repo)
if err != nil {
rows.Close()