forked from kevadesu/forgejo
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:
parent
fb8166c6c6
commit
719bddcd76
301 changed files with 3193 additions and 2919 deletions
|
@ -14,6 +14,7 @@ import (
|
|||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/models/login"
|
||||
"code.gitea.io/gitea/models/perm"
|
||||
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/models/webhook"
|
||||
|
@ -34,7 +35,7 @@ func ToEmail(email *user_model.EmailAddress) *api.Email {
|
|||
}
|
||||
|
||||
// ToBranch convert a git.Commit and git.Branch to an api.Branch
|
||||
func ToBranch(repo *models.Repository, b *git.Branch, c *git.Commit, bp *models.ProtectedBranch, user *user_model.User, isRepoAdmin bool) (*api.Branch, error) {
|
||||
func ToBranch(repo *repo_model.Repository, b *git.Branch, c *git.Commit, bp *models.ProtectedBranch, user *user_model.User, isRepoAdmin bool) (*api.Branch, error) {
|
||||
if bp == nil {
|
||||
var hasPerm bool
|
||||
var err error
|
||||
|
@ -76,7 +77,7 @@ func ToBranch(repo *models.Repository, b *git.Branch, c *git.Commit, bp *models.
|
|||
return nil, err
|
||||
}
|
||||
branch.UserCanPush = bp.CanUserPush(user.ID)
|
||||
branch.UserCanMerge = bp.IsUserMergeWhitelisted(user.ID, permission)
|
||||
branch.UserCanMerge = models.IsUserMergeWhitelisted(bp, user.ID, permission)
|
||||
}
|
||||
|
||||
return branch, nil
|
||||
|
@ -138,7 +139,7 @@ func ToBranchProtection(bp *models.ProtectedBranch) *api.BranchProtection {
|
|||
}
|
||||
|
||||
// ToTag convert a git.Tag to an api.Tag
|
||||
func ToTag(repo *models.Repository, t *git.Tag) *api.Tag {
|
||||
func ToTag(repo *repo_model.Repository, t *git.Tag) *api.Tag {
|
||||
return &api.Tag{
|
||||
Name: t.Name,
|
||||
Message: strings.TrimSpace(t.Message),
|
||||
|
@ -310,7 +311,7 @@ func ToTeam(team *models.Team) *api.Team {
|
|||
}
|
||||
|
||||
// ToAnnotatedTag convert git.Tag to api.AnnotatedTag
|
||||
func ToAnnotatedTag(repo *models.Repository, t *git.Tag, c *git.Commit) *api.AnnotatedTag {
|
||||
func ToAnnotatedTag(repo *repo_model.Repository, t *git.Tag, c *git.Commit) *api.AnnotatedTag {
|
||||
return &api.AnnotatedTag{
|
||||
Tag: t.Name,
|
||||
SHA: t.ID.String(),
|
||||
|
@ -323,7 +324,7 @@ func ToAnnotatedTag(repo *models.Repository, t *git.Tag, c *git.Commit) *api.Ann
|
|||
}
|
||||
|
||||
// ToAnnotatedTagObject convert a git.Commit to an api.AnnotatedTagObject
|
||||
func ToAnnotatedTagObject(repo *models.Repository, commit *git.Commit) *api.AnnotatedTagObject {
|
||||
func ToAnnotatedTagObject(repo *repo_model.Repository, commit *git.Commit) *api.AnnotatedTagObject {
|
||||
return &api.AnnotatedTagObject{
|
||||
SHA: commit.ID.String(),
|
||||
Type: string(git.ObjectCommit),
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
"net/url"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
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/log"
|
||||
|
@ -28,7 +28,7 @@ func ToCommitUser(sig *git.Signature) *api.CommitUser {
|
|||
}
|
||||
|
||||
// ToCommitMeta convert a git.Tag to an api.CommitMeta
|
||||
func ToCommitMeta(repo *models.Repository, tag *git.Tag) *api.CommitMeta {
|
||||
func ToCommitMeta(repo *repo_model.Repository, tag *git.Tag) *api.CommitMeta {
|
||||
return &api.CommitMeta{
|
||||
SHA: tag.Object.String(),
|
||||
URL: util.URLJoin(repo.APIURL(), "git/commits", tag.ID.String()),
|
||||
|
@ -37,7 +37,7 @@ func ToCommitMeta(repo *models.Repository, tag *git.Tag) *api.CommitMeta {
|
|||
}
|
||||
|
||||
// ToPayloadCommit convert a git.Commit to api.PayloadCommit
|
||||
func ToPayloadCommit(repo *models.Repository, c *git.Commit) *api.PayloadCommit {
|
||||
func ToPayloadCommit(repo *repo_model.Repository, c *git.Commit) *api.PayloadCommit {
|
||||
authorUsername := ""
|
||||
if author, err := user_model.GetUserByEmail(c.Author.Email); err == nil {
|
||||
authorUsername = author.Name
|
||||
|
@ -72,7 +72,7 @@ func ToPayloadCommit(repo *models.Repository, c *git.Commit) *api.PayloadCommit
|
|||
}
|
||||
|
||||
// ToCommit convert a git.Commit to api.Commit
|
||||
func ToCommit(repo *models.Repository, commit *git.Commit, userCache map[string]*user_model.User) (*api.Commit, error) {
|
||||
func ToCommit(repo *repo_model.Repository, commit *git.Commit, userCache map[string]*user_model.User) (*api.Commit, error) {
|
||||
|
||||
var apiAuthor, apiCommitter *api.User
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
|
@ -19,7 +19,7 @@ import (
|
|||
|
||||
func TestToCommitMeta(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
headRepo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
|
||||
headRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
|
||||
sha1, _ := git.NewIDFromString("0000000000000000000000000000000000000000")
|
||||
signature := &git.Signature{Name: "Test Signature", Email: "test@email.com", When: time.Unix(0, 0)}
|
||||
tag := &git.Tag{
|
||||
|
|
|
@ -10,6 +10,8 @@ import (
|
|||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
@ -30,7 +32,7 @@ func ToAPIIssue(issue *models.Issue) *api.Issue {
|
|||
if err := issue.LoadRepo(); err != nil {
|
||||
return &api.Issue{}
|
||||
}
|
||||
if err := issue.Repo.GetOwner(); err != nil {
|
||||
if err := issue.Repo.GetOwner(db.DefaultContext); err != nil {
|
||||
return &api.Issue{}
|
||||
}
|
||||
|
||||
|
@ -129,10 +131,10 @@ func ToStopWatches(sws []*models.Stopwatch) (api.StopWatches, error) {
|
|||
result := api.StopWatches(make([]api.StopWatch, 0, len(sws)))
|
||||
|
||||
issueCache := make(map[int64]*models.Issue)
|
||||
repoCache := make(map[int64]*models.Repository)
|
||||
repoCache := make(map[int64]*repo_model.Repository)
|
||||
var (
|
||||
issue *models.Issue
|
||||
repo *models.Repository
|
||||
repo *repo_model.Repository
|
||||
ok bool
|
||||
err error
|
||||
)
|
||||
|
@ -147,7 +149,7 @@ func ToStopWatches(sws []*models.Stopwatch) (api.StopWatches, error) {
|
|||
}
|
||||
repo, ok = repoCache[issue.RepoID]
|
||||
if !ok {
|
||||
repo, err = models.GetRepositoryByID(issue.RepoID)
|
||||
repo, err = repo_model.GetRepositoryByID(issue.RepoID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -176,7 +178,7 @@ func ToTrackedTimeList(tl models.TrackedTimeList) api.TrackedTimeList {
|
|||
}
|
||||
|
||||
// ToLabel converts Label to API format
|
||||
func ToLabel(label *models.Label, repo *models.Repository, org *user_model.User) *api.Label {
|
||||
func ToLabel(label *models.Label, repo *repo_model.Repository, org *user_model.User) *api.Label {
|
||||
result := &api.Label{
|
||||
ID: label.ID,
|
||||
Name: label.Name,
|
||||
|
@ -203,7 +205,7 @@ func ToLabel(label *models.Label, repo *models.Repository, org *user_model.User)
|
|||
}
|
||||
|
||||
// ToLabelList converts list of Label to API format
|
||||
func ToLabelList(labels []*models.Label, repo *models.Repository, org *user_model.User) []*api.Label {
|
||||
func ToLabelList(labels []*models.Label, repo *repo_model.Repository, org *user_model.User) []*api.Label {
|
||||
result := make([]*api.Label, len(labels))
|
||||
for i := range labels {
|
||||
result[i] = ToLabel(labels[i], repo, org)
|
||||
|
|
|
@ -10,6 +10,7 @@ import (
|
|||
"time"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
|
@ -21,7 +22,7 @@ import (
|
|||
func TestLabel_ToLabel(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
label := unittest.AssertExistsAndLoadBean(t, &models.Label{ID: 1}).(*models.Label)
|
||||
repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: label.RepoID}).(*models.Repository)
|
||||
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: label.RepoID}).(*repo_model.Repository)
|
||||
assert.Equal(t, &api.Label{
|
||||
ID: label.ID,
|
||||
Name: label.Name,
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/models/perm"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
"code.gitea.io/gitea/modules/structs"
|
||||
|
||||
|
@ -18,7 +19,7 @@ import (
|
|||
func TestPullRequest_APIFormat(t *testing.T) {
|
||||
//with HeadRepo
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
headRepo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
|
||||
headRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
|
||||
pr := unittest.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 1}).(*models.PullRequest)
|
||||
assert.NoError(t, pr.LoadAttributes())
|
||||
assert.NoError(t, pr.LoadIssue())
|
||||
|
|
|
@ -6,17 +6,19 @@ package convert
|
|||
|
||||
import (
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/models/perm"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
unit_model "code.gitea.io/gitea/models/unit"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
)
|
||||
|
||||
// ToRepo converts a Repository to api.Repository
|
||||
func ToRepo(repo *models.Repository, mode perm.AccessMode) *api.Repository {
|
||||
func ToRepo(repo *repo_model.Repository, mode perm.AccessMode) *api.Repository {
|
||||
return innerToRepo(repo, mode, false)
|
||||
}
|
||||
|
||||
func innerToRepo(repo *models.Repository, mode perm.AccessMode, isParent bool) *api.Repository {
|
||||
func innerToRepo(repo *repo_model.Repository, mode perm.AccessMode, isParent bool) *api.Repository {
|
||||
var parent *api.Repository
|
||||
|
||||
cloneLink := repo.CloneLink()
|
||||
|
@ -73,7 +75,7 @@ func innerToRepo(repo *models.Repository, mode perm.AccessMode, isParent bool) *
|
|||
allowRebase := false
|
||||
allowRebaseMerge := false
|
||||
allowSquash := false
|
||||
defaultMergeStyle := models.MergeStyleMerge
|
||||
defaultMergeStyle := repo_model.MergeStyleMerge
|
||||
if unit, err := repo.GetUnit(unit_model.TypePullRequests); err == nil {
|
||||
config := unit.PullRequestsConfig()
|
||||
hasPullRequests = true
|
||||
|
@ -89,7 +91,7 @@ func innerToRepo(repo *models.Repository, mode perm.AccessMode, isParent bool) *
|
|||
hasProjects = true
|
||||
}
|
||||
|
||||
if err := repo.GetOwner(); err != nil {
|
||||
if err := repo.GetOwner(db.DefaultContext); err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -97,7 +99,9 @@ func innerToRepo(repo *models.Repository, mode perm.AccessMode, isParent bool) *
|
|||
|
||||
mirrorInterval := ""
|
||||
if repo.IsMirror {
|
||||
if err := repo.GetMirror(); err == nil {
|
||||
var err error
|
||||
repo.Mirror, err = repo_model.GetMirrorByRepoID(repo.ID)
|
||||
if err == nil {
|
||||
mirrorInterval = repo.Mirror.Interval.String()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ package convert
|
|||
import (
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
|
@ -49,7 +49,7 @@ func ToWikiCommitList(commits []*git.Commit, total int64) *api.WikiCommitList {
|
|||
}
|
||||
|
||||
// ToWikiPageMetaData converts meta information to a WikiPageMetaData
|
||||
func ToWikiPageMetaData(title string, lastCommit *git.Commit, repo *models.Repository) *api.WikiPageMetaData {
|
||||
func ToWikiPageMetaData(title string, lastCommit *git.Commit, repo *repo_model.Repository) *api.WikiPageMetaData {
|
||||
suburl := wiki_service.NameToSubURL(title)
|
||||
return &api.WikiPageMetaData{
|
||||
Title: title,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue