diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-11-19 21:39:57 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-19 21:39:57 +0800 |
commit | fc3d0826096943b979717ed46c9a4cfd86e06106 (patch) | |
tree | 3143882ccf4dea3a8bf2a0de9c8da9a4efec26ce /models/repo.go | |
parent | 7a0347315995b25bcb2dca4786504fb699b5f004 (diff) | |
download | gitea-fc3d0826096943b979717ed46c9a4cfd86e06106.tar.gz gitea-fc3d0826096943b979717ed46c9a4cfd86e06106.zip |
Move attachment into models/repo/ (#17650)
* Move attachment into models/repo/
* Fix test
* Fix bug
Diffstat (limited to 'models/repo.go')
-rw-r--r-- | models/repo.go | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/models/repo.go b/models/repo.go index 3877502926..5921441afa 100644 --- a/models/repo.go +++ b/models/repo.go @@ -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 +} |