1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 18:28:37 +00:00

Move attachment into models/repo/ (#17650)

* Move attachment into models/repo/

* Fix test

* Fix bug
This commit is contained in:
Lunny Xiao
2021-11-19 21:39:57 +08:00
committed by GitHub
parent 7a03473159
commit fc3d082609
39 changed files with 477 additions and 414 deletions

View File

@@ -25,6 +25,7 @@ import (
admin_model "code.gitea.io/gitea/models/admin"
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/lfs"
@@ -1485,7 +1486,7 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
}
}
attachments := make([]*Attachment, 0, 20)
attachments := make([]*repo_model.Attachment, 0, 20)
if err = sess.Join("INNER", "`release`", "`release`.id = `attachment`.release_id").
Where("`release`.repo_id = ?", repoID).
Find(&attachments); err != nil {
@@ -1620,7 +1621,7 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
}
// Get all attachments with both issue_id and release_id are zero
var newAttachments []*Attachment
var newAttachments []*repo_model.Attachment
if err := sess.Where(builder.Eq{
"repo_id": repo.ID,
"issue_id": 0,
@@ -1634,7 +1635,7 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
newAttachmentPaths = append(newAttachmentPaths, attach.RelativePath())
}
if _, err := sess.Where("repo_id=?", repo.ID).Delete(new(Attachment)); err != nil {
if _, err := sess.Where("repo_id=?", repo.ID).Delete(new(repo_model.Attachment)); err != nil {
return err
}
@@ -2191,3 +2192,27 @@ func IterateRepository(f func(repo *Repository) error) error {
}
}
}
// LinkedRepository returns the linked repo if any
func LinkedRepository(a *repo_model.Attachment) (*Repository, unit.Type, error) {
if a.IssueID != 0 {
iss, err := GetIssueByID(a.IssueID)
if err != nil {
return nil, unit.TypeIssues, err
}
repo, err := GetRepositoryByID(iss.RepoID)
unitType := unit.TypeIssues
if iss.IsPull {
unitType = unit.TypePullRequests
}
return repo, unitType, err
} else if a.ReleaseID != 0 {
rel, err := GetReleaseByID(a.ReleaseID)
if err != nil {
return nil, unit.TypeReleases, err
}
repo, err := GetRepositoryByID(rel.RepoID)
return repo, unit.TypeReleases, err
}
return nil, -1, nil
}