diff options
Diffstat (limited to 'routers')
50 files changed, 306 insertions, 254 deletions
diff --git a/routers/api/v1/admin/adopt.go b/routers/api/v1/admin/adopt.go index 85262bc836..1c0e237cdb 100644 --- a/routers/api/v1/admin/adopt.go +++ b/routers/api/v1/admin/adopt.go @@ -8,6 +8,7 @@ import ( "net/http" "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/context" "code.gitea.io/gitea/modules/util" @@ -91,12 +92,12 @@ func AdoptRepository(ctx *context.APIContext) { } // check not a repo - has, err := models.IsRepositoryExist(ctxUser, repoName) + has, err := repo_model.IsRepositoryExist(ctxUser, repoName) if err != nil { ctx.InternalServerError(err) return } - isDir, err := util.IsDir(models.RepoPath(ctxUser.Name, repoName)) + isDir, err := util.IsDir(repo_model.RepoPath(ctxUser.Name, repoName)) if err != nil { ctx.InternalServerError(err) return @@ -153,12 +154,12 @@ func DeleteUnadoptedRepository(ctx *context.APIContext) { } // check not a repo - has, err := models.IsRepositoryExist(ctxUser, repoName) + has, err := repo_model.IsRepositoryExist(ctxUser, repoName) if err != nil { ctx.InternalServerError(err) return } - isDir, err := util.IsDir(models.RepoPath(ctxUser.Name, repoName)) + isDir, err := util.IsDir(repo_model.RepoPath(ctxUser.Name, repoName)) if err != nil { ctx.InternalServerError(err) return diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 867803f8dd..1d7d4251db 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -71,6 +71,7 @@ import ( "strings" "code.gitea.io/gitea/models" + 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/context" @@ -157,9 +158,9 @@ func repoAssignment() func(ctx *context.APIContext) { ctx.Repo.Owner = owner // Get repository. - repo, err := models.GetRepositoryByName(owner.ID, repoName) + repo, err := repo_model.GetRepositoryByName(owner.ID, repoName) if err != nil { - if models.IsErrRepoNotExist(err) { + if repo_model.IsErrRepoNotExist(err) { redirectRepoID, err := models.LookupRepoRedirect(owner.ID, repoName) if err == nil { context.RedirectToRepo(ctx.Context, redirectRepoID) diff --git a/routers/api/v1/org/team.go b/routers/api/v1/org/team.go index 371f1dc86d..074d6fd009 100644 --- a/routers/api/v1/org/team.go +++ b/routers/api/v1/org/team.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/perm" + repo_model "code.gitea.io/gitea/models/repo" unit_model "code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" @@ -512,10 +513,10 @@ func GetTeamRepos(ctx *context.APIContext) { } // getRepositoryByParams get repository by a team's organization ID and repo name -func getRepositoryByParams(ctx *context.APIContext) *models.Repository { - repo, err := models.GetRepositoryByName(ctx.Org.Team.OrgID, ctx.Params(":reponame")) +func getRepositoryByParams(ctx *context.APIContext) *repo_model.Repository { + repo, err := repo_model.GetRepositoryByName(ctx.Org.Team.OrgID, ctx.Params(":reponame")) if err != nil { - if models.IsErrRepoNotExist(err) { + if repo_model.IsErrRepoNotExist(err) { ctx.NotFound() } else { ctx.Error(http.StatusInternalServerError, "GetRepositoryByName", err) diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index e511152e57..56266b406a 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -69,7 +69,7 @@ func GetBranch(ctx *context.APIContext) { return } - branchProtection, err := ctx.Repo.Repository.GetBranchProtection(branchName) + branchProtection, err := models.GetProtectedBranchBy(ctx.Repo.Repository.ID, branchName) if err != nil { ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err) return @@ -210,7 +210,7 @@ func CreateBranch(ctx *context.APIContext) { return } - branchProtection, err := ctx.Repo.Repository.GetBranchProtection(branch.Name) + branchProtection, err := models.GetProtectedBranchBy(ctx.Repo.Repository.ID, branch.Name) if err != nil { ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err) return @@ -270,7 +270,7 @@ func ListBranches(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "GetCommit", err) return } - branchProtection, err := ctx.Repo.Repository.GetBranchProtection(branches[i].Name) + branchProtection, err := models.GetProtectedBranchBy(ctx.Repo.Repository.ID, branches[i].Name) if err != nil { ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err) return @@ -354,7 +354,7 @@ func ListBranchProtections(ctx *context.APIContext) { // "$ref": "#/responses/BranchProtectionList" repo := ctx.Repo.Repository - bps, err := repo.GetProtectedBranches() + bps, err := models.GetProtectedBranches(repo.ID) if err != nil { ctx.Error(http.StatusInternalServerError, "GetProtectedBranches", err) return @@ -811,7 +811,7 @@ func DeleteBranchProtection(ctx *context.APIContext) { return } - if err := ctx.Repo.Repository.DeleteProtectedBranch(bp.ID); err != nil { + if err := models.DeleteProtectedBranch(ctx.Repo.Repository.ID, bp.ID); err != nil { ctx.Error(http.StatusInternalServerError, "DeleteProtectedBranch", err) return } diff --git a/routers/api/v1/repo/collaborators.go b/routers/api/v1/repo/collaborators.go index 80794fa444..d49b6357bd 100644 --- a/routers/api/v1/repo/collaborators.go +++ b/routers/api/v1/repo/collaborators.go @@ -9,6 +9,7 @@ import ( "errors" "net/http" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/perm" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" @@ -48,13 +49,13 @@ func ListCollaborators(ctx *context.APIContext) { // "200": // "$ref": "#/responses/UserList" - count, err := ctx.Repo.Repository.CountCollaborators() + count, err := models.CountCollaborators(ctx.Repo.Repository.ID) if err != nil { ctx.InternalServerError(err) return } - collaborators, err := ctx.Repo.Repository.GetCollaborators(utils.GetListOptions(ctx)) + collaborators, err := models.GetCollaborators(ctx.Repo.Repository.ID, utils.GetListOptions(ctx)) if err != nil { ctx.Error(http.StatusInternalServerError, "ListCollaborators", err) return @@ -109,7 +110,7 @@ func IsCollaborator(ctx *context.APIContext) { } return } - isColab, err := ctx.Repo.Repository.IsCollaborator(user.ID) + isColab, err := models.IsCollaborator(ctx.Repo.Repository.ID, user.ID) if err != nil { ctx.Error(http.StatusInternalServerError, "IsCollaborator", err) return @@ -171,13 +172,13 @@ func AddCollaborator(ctx *context.APIContext) { return } - if err := ctx.Repo.Repository.AddCollaborator(collaborator); err != nil { + if err := models.AddCollaborator(ctx.Repo.Repository, collaborator); err != nil { ctx.Error(http.StatusInternalServerError, "AddCollaborator", err) return } if form.Permission != nil { - if err := ctx.Repo.Repository.ChangeCollaborationAccessMode(collaborator.ID, perm.ParseAccessMode(*form.Permission)); err != nil { + if err := models.ChangeCollaborationAccessMode(ctx.Repo.Repository, collaborator.ID, perm.ParseAccessMode(*form.Permission)); err != nil { ctx.Error(http.StatusInternalServerError, "ChangeCollaborationAccessMode", err) return } @@ -225,7 +226,7 @@ func DeleteCollaborator(ctx *context.APIContext) { return } - if err := ctx.Repo.Repository.DeleteCollaboration(collaborator.ID); err != nil { + if err := models.DeleteCollaboration(ctx.Repo.Repository, collaborator.ID); err != nil { ctx.Error(http.StatusInternalServerError, "DeleteCollaboration", err) return } @@ -254,7 +255,7 @@ func GetReviewers(ctx *context.APIContext) { // "200": // "$ref": "#/responses/UserList" - reviewers, err := ctx.Repo.Repository.GetReviewers(ctx.User.ID, 0) + reviewers, err := models.GetReviewers(ctx.Repo.Repository, ctx.User.ID, 0) if err != nil { ctx.Error(http.StatusInternalServerError, "ListCollaborators", err) return @@ -284,7 +285,7 @@ func GetAssignees(ctx *context.APIContext) { // "200": // "$ref": "#/responses/UserList" - assignees, err := ctx.Repo.Repository.GetAssignees() + assignees, err := models.GetRepoAssignees(ctx.Repo.Repository) if err != nil { ctx.Error(http.StatusInternalServerError, "ListCollaborators", err) return diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 9907054b83..117fef08da 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -11,7 +11,7 @@ import ( "net/http" "strconv" - "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/context" "code.gitea.io/gitea/modules/convert" @@ -249,7 +249,7 @@ func DownloadCommitDiffOrPatch(ctx *context.APIContext) { // "$ref": "#/responses/string" // "404": // "$ref": "#/responses/notFound" - repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) + repoPath := repo_model.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) if err := git.GetRawDiff( repoPath, ctx.Params(":sha"), diff --git a/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go index e84f652ed9..98decfcb6f 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -12,6 +12,7 @@ import ( "time" "code.gitea.io/gitea/models" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" @@ -119,7 +120,7 @@ func GetArchive(ctx *context.APIContext) { // "404": // "$ref": "#/responses/notFound" - repoPath := models.RepoPath(ctx.Params(":username"), ctx.Params(":reponame")) + repoPath := repo_model.RepoPath(ctx.Params(":username"), ctx.Params(":reponame")) if ctx.Repo.GitRepo == nil { gitRepo, err := git.OpenRepository(repoPath) if err != nil { diff --git a/routers/api/v1/repo/fork.go b/routers/api/v1/repo/fork.go index 33321e6a02..aa51019ebd 100644 --- a/routers/api/v1/repo/fork.go +++ b/routers/api/v1/repo/fork.go @@ -50,7 +50,7 @@ func ListForks(ctx *context.APIContext) { // "200": // "$ref": "#/responses/RepositoryList" - forks, err := ctx.Repo.Repository.GetForks(utils.GetListOptions(ctx)) + forks, err := models.GetForks(ctx.Repo.Repository, utils.GetListOptions(ctx)) if err != nil { ctx.Error(http.StatusInternalServerError, "GetForks", err) return diff --git a/routers/api/v1/repo/key.go b/routers/api/v1/repo/key.go index ad94da5b2d..03ebcb8f72 100644 --- a/routers/api/v1/repo/key.go +++ b/routers/api/v1/repo/key.go @@ -12,6 +12,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/modules/context" "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/setting" @@ -21,12 +22,12 @@ import ( ) // appendPrivateInformation appends the owner and key type information to api.PublicKey -func appendPrivateInformation(apiKey *api.DeployKey, key *models.DeployKey, repository *models.Repository) (*api.DeployKey, error) { +func appendPrivateInformation(apiKey *api.DeployKey, key *models.DeployKey, repository *repo_model.Repository) (*api.DeployKey, error) { apiKey.ReadOnly = key.Mode == perm.AccessModeRead if repository.ID == key.RepoID { apiKey.Repository = convert.ToRepo(repository, key.Mode) } else { - repo, err := models.GetRepositoryByID(key.RepoID) + repo, err := repo_model.GetRepositoryByID(key.RepoID) if err != nil { return apiKey, err } diff --git a/routers/api/v1/repo/language.go b/routers/api/v1/repo/language.go index c45911ee66..427a8fd6b5 100644 --- a/routers/api/v1/repo/language.go +++ b/routers/api/v1/repo/language.go @@ -9,12 +9,12 @@ import ( "net/http" "strconv" - "code.gitea.io/gitea/models" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" ) -type languageResponse []*models.LanguageStat +type languageResponse []*repo_model.LanguageStat func (l languageResponse) MarshalJSON() ([]byte, error) { var buf bytes.Buffer @@ -68,7 +68,7 @@ func GetLanguages(ctx *context.APIContext) { // "200": // "$ref": "#/responses/LanguageStatistics" - langs, err := ctx.Repo.Repository.GetLanguageStats() + langs, err := repo_model.GetLanguageStats(ctx.Repo.Repository) if err != nil { log.Error("GetLanguageStats failed: %v", err) ctx.InternalServerError(err) diff --git a/routers/api/v1/repo/migrate.go b/routers/api/v1/repo/migrate.go index dba44efb21..108d78ef23 100644 --- a/routers/api/v1/repo/migrate.go +++ b/routers/api/v1/repo/migrate.go @@ -14,6 +14,7 @@ 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" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" @@ -173,7 +174,7 @@ func Migrate(ctx *context.APIContext) { GitServiceType: gitServiceType, IsPrivate: opts.Private, IsMirror: opts.Mirror, - Status: models.RepositoryBeingMigrated, + Status: repo_model.RepositoryBeingMigrated, }) if err != nil { handleMigrateError(ctx, repoOwner, remoteAddr, err) diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index dd415a34af..3e37da24ec 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -14,6 +14,7 @@ import ( "time" "code.gitea.io/gitea/models" + 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/context" @@ -763,10 +764,10 @@ func MergePullRequest(ctx *context.APIContext) { } // handle manually-merged mark - if models.MergeStyle(form.Do) == models.MergeStyleManuallyMerged { + if repo_model.MergeStyle(form.Do) == repo_model.MergeStyleManuallyMerged { if err = pull_service.MergedManually(pr, ctx.User, ctx.Repo.GitRepo, form.MergeCommitID); err != nil { if models.IsErrInvalidMergeStyle(err) { - ctx.Error(http.StatusMethodNotAllowed, "Invalid merge style", fmt.Errorf("%s is not allowed an allowed merge style for this repository", models.MergeStyle(form.Do))) + ctx.Error(http.StatusMethodNotAllowed, "Invalid merge style", fmt.Errorf("%s is not allowed an allowed merge style for this repository", repo_model.MergeStyle(form.Do))) return } if strings.Contains(err.Error(), "Wrong commit ID") { @@ -818,15 +819,15 @@ func MergePullRequest(ctx *context.APIContext) { } if len(form.Do) == 0 { - form.Do = string(models.MergeStyleMerge) + form.Do = string(repo_model.MergeStyleMerge) } message := strings.TrimSpace(form.MergeTitleField) if len(message) == 0 { - if models.MergeStyle(form.Do) == models.MergeStyleMerge { + if repo_model.MergeStyle(form.Do) == repo_model.MergeStyleMerge { message = pr.GetDefaultMergeMessage() } - if models.MergeStyle(form.Do) == models.MergeStyleSquash { + if repo_model.MergeStyle(form.Do) == repo_model.MergeStyleSquash { message = pr.GetDefaultSquashMessage() } } @@ -836,9 +837,9 @@ func MergePullRequest(ctx *context.APIContext) { message += "\n\n" + form.MergeMessageField } - if err := pull_service.Merge(pr, ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil { + if err := pull_service.Merge(pr, ctx.User, ctx.Repo.GitRepo, repo_model.MergeStyle(form.Do), message); err != nil { if models.IsErrInvalidMergeStyle(err) { - ctx.Error(http.StatusMethodNotAllowed, "Invalid merge style", fmt.Errorf("%s is not allowed an allowed merge style for this repository", models.MergeStyle(form.Do))) + ctx.Error(http.StatusMethodNotAllowed, "Invalid merge style", fmt.Errorf("%s is not allowed an allowed merge style for this repository", repo_model.MergeStyle(form.Do))) return } else if models.IsErrMergeConflicts(err) { conflictError := err.(models.ErrMergeConflicts) @@ -901,7 +902,7 @@ func MergePullRequest(ctx *context.APIContext) { ctx.Status(http.StatusOK) } -func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) (*user_model.User, *models.Repository, *git.Repository, *git.CompareInfo, string, string) { +func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) (*user_model.User, *repo_model.Repository, *git.Repository, *git.CompareInfo, string, string) { baseRepo := ctx.Repo.Repository // Get compared branches information @@ -966,7 +967,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) headRepo = ctx.Repo.Repository headGitRepo = ctx.Repo.GitRepo } else { - headGitRepo, err = git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name)) + headGitRepo, err = git.OpenRepository(repo_model.RepoPath(headUser.Name, headRepo.Name)) if err != nil { ctx.Error(http.StatusInternalServerError, "OpenRepository", err) return nil, nil, nil, nil, "", "" @@ -1018,7 +1019,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) return nil, nil, nil, nil, "", "" } - compareInfo, err := headGitRepo.GetCompareInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch, true, false) + compareInfo, err := headGitRepo.GetCompareInfo(repo_model.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch, true, false) if err != nil { headGitRepo.Close() ctx.Error(http.StatusInternalServerError, "GetCompareInfo", err) diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index 2f37e10984..4486e33fe8 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -14,6 +14,7 @@ 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" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" @@ -216,7 +217,7 @@ func Search(ctx *context.APIContext) { results := make([]*api.Repository, len(repos)) for i, repo := range repos { - if err = repo.GetOwner(); err != nil { + if err = repo.GetOwner(db.DefaultContext); err != nil { ctx.JSON(http.StatusInternalServerError, api.SearchError{ OK: false, Error: err.Error(), @@ -256,7 +257,7 @@ func CreateUserRepo(ctx *context.APIContext, owner *user_model.User, opt api.Cre IsPrivate: opt.Private, AutoInit: opt.AutoInit, DefaultBranch: opt.DefaultBranch, - TrustModel: models.ToTrustModel(opt.TrustModel), + TrustModel: repo_model.ToTrustModel(opt.TrustModel), IsTemplate: opt.Template, }) if err != nil { @@ -272,7 +273,7 @@ func CreateUserRepo(ctx *context.APIContext, owner *user_model.User, opt api.Cre } // reload repo from db to get a real state after creation - repo, err = models.GetRepositoryByID(repo.ID) + repo, err = repo_model.GetRepositoryByID(repo.ID) if err != nil { ctx.Error(http.StatusInternalServerError, "GetRepositoryByID", err) } @@ -553,9 +554,9 @@ func GetByID(ctx *context.APIContext) { // "200": // "$ref": "#/responses/Repository" - repo, err := models.GetRepositoryByID(ctx.ParamsInt64(":id")) + repo, err := repo_model.GetRepositoryByID(ctx.ParamsInt64(":id")) if err != nil { - if models.IsErrRepoNotExist(err) { + if repo_model.IsErrRepoNotExist(err) { ctx.NotFound() } else { ctx.Error(http.StatusInternalServerError, "GetRepositoryByID", err) @@ -628,7 +629,7 @@ func Edit(ctx *context.APIContext) { } } - repo, err := models.GetRepositoryByID(ctx.Repo.Repository.ID) + repo, err := repo_model.GetRepositoryByID(ctx.Repo.Repository.ID) if err != nil { ctx.InternalServerError(err) return @@ -738,7 +739,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { owner := ctx.Repo.Owner repo := ctx.Repo.Repository - var units []models.RepoUnit + var units []repo_model.RepoUnit var deleteUnitTypes []unit_model.Type if opts.HasIssues != nil { @@ -755,10 +756,10 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { return err } - units = append(units, models.RepoUnit{ + units = append(units, repo_model.RepoUnit{ RepoID: repo.ID, Type: unit_model.TypeExternalTracker, - Config: &models.ExternalTrackerConfig{ + Config: &repo_model.ExternalTrackerConfig{ ExternalTrackerURL: opts.ExternalTracker.ExternalTrackerURL, ExternalTrackerFormat: opts.ExternalTracker.ExternalTrackerFormat, ExternalTrackerStyle: opts.ExternalTracker.ExternalTrackerStyle, @@ -767,17 +768,17 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeIssues) } else if *opts.HasIssues && opts.ExternalTracker == nil && !unit_model.TypeIssues.UnitGlobalDisabled() { // Default to built-in tracker - var config *models.IssuesConfig + var config *repo_model.IssuesConfig if opts.InternalTracker != nil { - config = &models.IssuesConfig{ + config = &repo_model.IssuesConfig{ EnableTimetracker: opts.InternalTracker.EnableTimeTracker, AllowOnlyContributorsToTrackTime: opts.InternalTracker.AllowOnlyContributorsToTrackTime, EnableDependencies: opts.InternalTracker.EnableIssueDependencies, } } else if unit, err := repo.GetUnit(unit_model.TypeIssues); err != nil { // Unit type doesn't exist so we make a new config file with default values - config = &models.IssuesConfig{ + config = &repo_model.IssuesConfig{ EnableTimetracker: true, AllowOnlyContributorsToTrackTime: true, EnableDependencies: true, @@ -786,7 +787,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { config = unit.IssuesConfig() } - units = append(units, models.RepoUnit{ + units = append(units, repo_model.RepoUnit{ RepoID: repo.ID, Type: unit_model.TypeIssues, Config: config, @@ -811,17 +812,17 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { return err } - units = append(units, models.RepoUnit{ + units = append(units, repo_model.RepoUnit{ RepoID: repo.ID, Type: unit_model.TypeExternalWiki, - Config: &models.ExternalWikiConfig{ + Config: &repo_model.ExternalWikiConfig{ ExternalWikiURL: opts.ExternalWiki.ExternalWikiURL, }, }) deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeWiki) } else if *opts.HasWiki && opts.ExternalWiki == nil && !unit_model.TypeWiki.UnitGlobalDisabled() { - config := &models.UnitConfig{} - units = append(units, models.RepoUnit{ + config := &repo_model.UnitConfig{} + units = append(units, repo_model.RepoUnit{ RepoID: repo.ID, Type: unit_model.TypeWiki, Config: config, @@ -843,10 +844,10 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { // we get the config settings and then set them // if those settings were provided in the opts. unit, err := repo.GetUnit(unit_model.TypePullRequests) - var config *models.PullRequestsConfig + var config *repo_model.PullRequestsConfig if err != nil { // Unit type doesn't exist so we make a new config file with default values - config = &models.PullRequestsConfig{ + config = &repo_model.PullRequestsConfig{ IgnoreWhitespaceConflicts: false, AllowMerge: true, AllowRebase: true, @@ -855,7 +856,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { AllowManualMerge: true, AutodetectManualMerge: false, DefaultDeleteBranchAfterMerge: false, - DefaultMergeStyle: models.MergeStyleMerge, + DefaultMergeStyle: repo_model.MergeStyleMerge, } } else { config = unit.PullRequestsConfig() @@ -886,10 +887,10 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { config.DefaultDeleteBranchAfterMerge = *opts.DefaultDeleteBranchAfterMerge } if opts.DefaultMergeStyle != nil { - config.DefaultMergeStyle = models.MergeStyle(*opts.DefaultMergeStyle) + config.DefaultMergeStyle = repo_model.MergeStyle(*opts.DefaultMergeStyle) } - units = append(units, models.RepoUnit{ + units = append(units, repo_model.RepoUnit{ RepoID: repo.ID, Type: unit_model.TypePullRequests, Config: config, @@ -901,7 +902,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { if opts.HasProjects != nil && !unit_model.TypeProjects.UnitGlobalDisabled() { if *opts.HasProjects { - units = append(units, models.RepoUnit{ + units = append(units, repo_model.RepoUnit{ RepoID: repo.ID, Type: unit_model.TypeProjects, }) @@ -930,14 +931,14 @@ func updateRepoArchivedState(ctx *context.APIContext, opts api.EditRepoOption) e return err } if *opts.Archived { - if err := repo.SetArchiveRepoState(*opts.Archived); err != nil { + if err := models.SetArchiveRepoState(repo, *opts.Archived); err != nil { log.Error("Tried to archive a repo: %s", err) ctx.Error(http.StatusInternalServerError, "ArchiveRepoState", err) return err } log.Trace("Repository was archived: %s/%s", ctx.Repo.Owner.Name, repo.Name) } else { - if err := repo.SetArchiveRepoState(*opts.Archived); err != nil { + if err := models.SetArchiveRepoState(repo, *opts.Archived); err != nil { log.Error("Tried to un-archive a repo: %s", err) ctx.Error(http.StatusInternalServerError, "ArchiveRepoState", err) return err @@ -958,14 +959,16 @@ func updateMirrorInterval(ctx *context.APIContext, opts api.EditRepoOption) erro ctx.Error(http.StatusUnprocessableEntity, err.Error(), err) return err } - if err := repo.GetMirror(); err != nil { + mirror, err := repo_model.GetMirrorByRepoID(repo.ID) + if err != nil { log.Error("Failed to get mirror: %s", err) ctx.Error(http.StatusInternalServerError, "MirrorInterval", err) return err } if interval, err := time.ParseDuration(*opts.MirrorInterval); err == nil { - repo.Mirror.Interval = interval - if err := models.UpdateMirror(repo.Mirror); err != nil { + mirror.Interval = interval + mirror.Repo = repo + if err := repo_model.UpdateMirror(mirror); err != nil { log.Error("Failed to Set Mirror Interval: %s", err) ctx.Error(http.StatusUnprocessableEntity, "MirrorInterval", err) return err @@ -1007,7 +1010,7 @@ func Delete(ctx *context.APIContext) { owner := ctx.Repo.Owner repo := ctx.Repo.Repository - canDelete, err := repo.CanUserDelete(ctx.User) + canDelete, err := models.CanUserDelete(repo, ctx.User) if err != nil { ctx.Error(http.StatusInternalServerError, "CanUserDelete", err) return diff --git a/routers/api/v1/repo/repo_test.go b/routers/api/v1/repo/repo_test.go index 4d4093582b..17b49f7f58 100644 --- a/routers/api/v1/repo/repo_test.go +++ b/routers/api/v1/repo/repo_test.go @@ -8,7 +8,7 @@ import ( "net/http" "testing" - "code.gitea.io/gitea/models" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/context" api "code.gitea.io/gitea/modules/structs" @@ -60,7 +60,7 @@ func TestRepoEdit(t *testing.T) { Edit(apiCtx) assert.EqualValues(t, http.StatusOK, ctx.Resp.Status()) - unittest.AssertExistsAndLoadBean(t, &models.Repository{ + unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ ID: 1, }, unittest.Cond("name = ? AND is_archived = 1", *opts.Name)) } @@ -82,7 +82,7 @@ func TestRepoEditNameChange(t *testing.T) { Edit(apiCtx) assert.EqualValues(t, http.StatusOK, ctx.Resp.Status()) - unittest.AssertExistsAndLoadBean(t, &models.Repository{ + unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ ID: 1, }, unittest.Cond("name = ?", opts.Name)) } diff --git a/routers/api/v1/repo/subscriber.go b/routers/api/v1/repo/subscriber.go index dae92969ba..64a0fc1d5d 100644 --- a/routers/api/v1/repo/subscriber.go +++ b/routers/api/v1/repo/subscriber.go @@ -7,6 +7,7 @@ package repo import ( "net/http" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" api "code.gitea.io/gitea/modules/structs" @@ -43,7 +44,7 @@ func ListSubscribers(ctx *context.APIContext) { // "200": // "$ref": "#/responses/UserList" - subscribers, err := ctx.Repo.Repository.GetWatchers(utils.GetListOptions(ctx)) + subscribers, err := models.GetRepoWatchers(ctx.Repo.Repository.ID, utils.GetListOptions(ctx)) if err != nil { ctx.Error(http.StatusInternalServerError, "GetWatchers", err) return diff --git a/routers/api/v1/repo/teams.go b/routers/api/v1/repo/teams.go index 1348205ec9..024224b5a3 100644 --- a/routers/api/v1/repo/teams.go +++ b/routers/api/v1/repo/teams.go @@ -41,7 +41,7 @@ func ListTeams(ctx *context.APIContext) { return } - teams, err := ctx.Repo.Repository.GetRepoTeams() + teams, err := models.GetRepoTeams(ctx.Repo.Repository) if err != nil { ctx.InternalServerError(err) return diff --git a/routers/api/v1/repo/transfer.go b/routers/api/v1/repo/transfer.go index 0a698383e5..e2a83c70ec 100644 --- a/routers/api/v1/repo/transfer.go +++ b/routers/api/v1/repo/transfer.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/perm" + repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" @@ -112,7 +113,7 @@ func Transfer(ctx *context.APIContext) { return } - if ctx.Repo.Repository.Status == models.RepositoryPendingTransfer { + if ctx.Repo.Repository.Status == repo_model.RepositoryPendingTransfer { log.Trace("Repository transfer initiated: %s -> %s", ctx.Repo.Repository.FullName(), newOwner.Name) ctx.JSON(http.StatusCreated, convert.ToRepo(ctx.Repo.Repository, perm.AccessModeAdmin)) return diff --git a/routers/api/v1/user/repo.go b/routers/api/v1/user/repo.go index 760032460e..3d5c841856 100644 --- a/routers/api/v1/user/repo.go +++ b/routers/api/v1/user/repo.go @@ -8,6 +8,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/perm" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" @@ -118,7 +119,7 @@ func ListMyRepos(ctx *context.APIContext) { results := make([]*api.Repository, len(repos)) for i, repo := range repos { - if err = repo.GetOwner(); err != nil { + if err = repo.GetOwner(db.DefaultContext); err != nil { ctx.Error(http.StatusInternalServerError, "GetOwner", err) return } diff --git a/routers/api/v1/user/watch.go b/routers/api/v1/user/watch.go index 54d5e74bc0..5c7a4d8d89 100644 --- a/routers/api/v1/user/watch.go +++ b/routers/api/v1/user/watch.go @@ -9,6 +9,7 @@ import ( "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/context" "code.gitea.io/gitea/modules/convert" @@ -201,6 +202,6 @@ func Unwatch(ctx *context.APIContext) { } // subscriptionURL returns the URL of the subscription API endpoint of a repo -func subscriptionURL(repo *models.Repository) string { +func subscriptionURL(repo *repo_model.Repository) string { return repo.APIURL() + "/subscription" } diff --git a/routers/private/default_branch.go b/routers/private/default_branch.go index ec6adc4805..974534c219 100644 --- a/routers/private/default_branch.go +++ b/routers/private/default_branch.go @@ -9,7 +9,7 @@ import ( "fmt" "net/http" - "code.gitea.io/gitea/models" + repo_model "code.gitea.io/gitea/models/repo" gitea_context "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" @@ -34,7 +34,7 @@ func SetDefaultBranch(ctx *gitea_context.PrivateContext) { ownerName := ctx.Params(":owner") repoName := ctx.Params(":repo") branch := ctx.Params(":branch") - repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName) + repo, err := repo_model.GetRepositoryByOwnerAndName(ownerName, repoName) if err != nil { log.Error("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err) ctx.JSON(http.StatusInternalServerError, private.Response{ @@ -65,7 +65,7 @@ func SetDefaultBranch(ctx *gitea_context.PrivateContext) { } gitRepo.Close() - if err := repo.UpdateDefaultBranch(); err != nil { + if err := repo_model.UpdateDefaultBranch(repo); err != nil { ctx.JSON(http.StatusInternalServerError, private.Response{ Err: fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err), }) diff --git a/routers/private/hook_post_receive.go b/routers/private/hook_post_receive.go index 0d559250ea..a71f465911 100644 --- a/routers/private/hook_post_receive.go +++ b/routers/private/hook_post_receive.go @@ -11,6 +11,7 @@ import ( "strings" "code.gitea.io/gitea/models" + repo_model "code.gitea.io/gitea/models/repo" gitea_context "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" @@ -35,7 +36,7 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) { repoName := ctx.Params(":repo") // defer getting the repository at this point - as we should only retrieve it if we're going to call update - var repo *models.Repository + var repo *repo_model.Repository updates := make([]*repo_module.PushUpdateOptions, 0, len(opts.OldCommitIDs)) wasEmpty := false @@ -116,7 +117,7 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) { // We have to reload the repo in case its state is changed above repo = nil - var baseRepo *models.Repository + var baseRepo *repo_model.Repository // Now handle the pull request notification trailers for i := range opts.OldCommitIDs { diff --git a/routers/private/hook_pre_receive.go b/routers/private/hook_pre_receive.go index 5ab21d525b..17ccf010ae 100644 --- a/routers/private/hook_pre_receive.go +++ b/routers/private/hook_pre_receive.go @@ -363,7 +363,7 @@ func preReceiveTag(ctx *preReceiveContext, oldCommitID, newCommitID, refFullName if !ctx.gotProtectedTags { var err error - ctx.protectedTags, err = ctx.Repo.Repository.GetProtectedTags() + ctx.protectedTags, err = models.GetProtectedTags(ctx.Repo.Repository.ID) if err != nil { log.Error("Unable to get protected tags for %-v Error: %v", ctx.Repo.Repository, err) ctx.JSON(http.StatusInternalServerError, private.Response{ diff --git a/routers/private/internal_repo.go b/routers/private/internal_repo.go index 60daa1dbea..8d0fd72235 100644 --- a/routers/private/internal_repo.go +++ b/routers/private/internal_repo.go @@ -10,7 +10,7 @@ import ( "fmt" "net/http" - "code.gitea.io/gitea/models" + repo_model "code.gitea.io/gitea/models/repo" gitea_context "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" @@ -68,8 +68,8 @@ func RepoAssignment(ctx *gitea_context.PrivateContext) context.CancelFunc { return cancel } -func loadRepository(ctx *gitea_context.PrivateContext, ownerName, repoName string) *models.Repository { - repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName) +func loadRepository(ctx *gitea_context.PrivateContext, ownerName, repoName string) *repo_model.Repository { + repo, err := repo_model.GetRepositoryByOwnerAndName(ownerName, repoName) if err != nil { log.Error("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err) ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ diff --git a/routers/private/serv.go b/routers/private/serv.go index 9cbd0c2c82..f28d5a7450 100644 --- a/routers/private/serv.go +++ b/routers/private/serv.go @@ -12,6 +12,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/unit" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" @@ -126,9 +127,9 @@ func ServCommand(ctx *context.PrivateContext) { // Now get the Repository and set the results section repoExist := true - repo, err := models.GetRepositoryByName(owner.ID, results.RepoName) + repo, err := repo_model.GetRepositoryByName(owner.ID, results.RepoName) if err != nil { - if models.IsErrRepoNotExist(err) { + if repo_model.IsErrRepoNotExist(err) { repoExist = false for _, verb := range ctx.FormStrings("verb") { if "git-upload-pack" == verb { @@ -374,7 +375,7 @@ func ServCommand(ctx *context.PrivateContext) { if results.IsWiki { // Ensure the wiki is enabled before we allow access to it if _, err := repo.GetUnit(unit.TypeWiki); err != nil { - if models.IsErrUnitTypeNotExist(err) { + if repo_model.IsErrUnitTypeNotExist(err) { ctx.JSON(http.StatusForbidden, private.ErrServCommand{ Results: results, Err: "repository wiki is disabled", diff --git a/routers/web/admin/repos.go b/routers/web/admin/repos.go index 69e522ef7e..7db8b4d337 100644 --- a/routers/web/admin/repos.go +++ b/routers/web/admin/repos.go @@ -11,6 +11,7 @@ import ( "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/base" "code.gitea.io/gitea/modules/context" @@ -41,7 +42,7 @@ func Repos(ctx *context.Context) { // DeleteRepo delete one repository func DeleteRepo(ctx *context.Context) { - repo, err := models.GetRepositoryByID(ctx.FormInt64("id")) + repo, err := repo_model.GetRepositoryByID(ctx.FormInt64("id")) if err != nil { ctx.ServerError("GetRepositoryByID", err) return @@ -134,12 +135,12 @@ func AdoptOrDeleteRepository(ctx *context.Context) { repoName := dirSplit[1] // check not a repo - has, err := models.IsRepositoryExist(ctxUser, repoName) + has, err := repo_model.IsRepositoryExist(ctxUser, repoName) if err != nil { ctx.ServerError("IsRepositoryExist", err) return } - isDir, err := util.IsDir(models.RepoPath(ctxUser.Name, repoName)) + isDir, err := util.IsDir(repo_model.RepoPath(ctxUser.Name, repoName)) if err != nil { ctx.ServerError("IsDir", err) return diff --git a/routers/web/explore/code.go b/routers/web/explore/code.go index 6ff48be0f8..b61f25a96c 100644 --- a/routers/web/explore/code.go +++ b/routers/web/explore/code.go @@ -8,6 +8,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" @@ -69,16 +70,16 @@ func Code(ctx *context.Context) { // if non-admin login user, we need check UnitTypeCode at first if ctx.User != nil && len(repoIDs) > 0 { - repoMaps, err := models.GetRepositoriesMapByIDs(repoIDs) + repoMaps, err := repo_model.GetRepositoriesMapByIDs(repoIDs) if err != nil { ctx.ServerError("SearchResults", err) return } - var rightRepoMap = make(map[int64]*models.Repository, len(repoMaps)) + var rightRepoMap = make(map[int64]*repo_model.Repository, len(repoMaps)) repoIDs = make([]int64, 0, len(repoMaps)) for id, repo := range repoMaps { - if repo.CheckUnitUser(ctx.User, unit.TypeCode) { + if models.CheckRepoUnitUser(repo, ctx.User, unit.TypeCode) { rightRepoMap[id] = repo repoIDs = append(repoIDs, id) } @@ -113,7 +114,7 @@ func Code(ctx *context.Context) { } } - repoMaps, err := models.GetRepositoriesMapByIDs(loadRepoIDs) + repoMaps, err := repo_model.GetRepositoriesMapByIDs(loadRepoIDs) if err != nil { ctx.ServerError("SearchResults", err) return diff --git a/routers/web/explore/repo.go b/routers/web/explore/repo.go index a81386b5fd..0a78e9e29d 100644 --- a/routers/web/explore/repo.go +++ b/routers/web/explore/repo.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/setting" @@ -36,7 +37,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) { } var ( - repos []*models.Repository + repos []*repo_model.Repository count int64 err error orderBy db.SearchOrderBy diff --git a/routers/web/goget.go b/routers/web/goget.go index 8a8e1797dc..6898f0f432 100644 --- a/routers/web/goget.go +++ b/routers/web/goget.go @@ -10,7 +10,7 @@ import ( "path" "strings" - "code.gitea.io/gitea/models" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" @@ -53,7 +53,7 @@ func goGet(ctx *context.Context) { } branchName := setting.Repository.DefaultBranch - repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName) + repo, err := repo_model.GetRepositoryByOwnerAndName(ownerName, repoName) if err == nil && len(repo.DefaultBranch) > 0 { branchName = repo.DefaultBranch } @@ -79,7 +79,7 @@ func goGet(ctx *context.Context) { </html> `, map[string]string{ "GoGetImport": context.ComposeGoGetImport(ownerName, trimmedRepoName), - "CloneLink": models.ComposeHTTPSCloneURL(ownerName, repoName), + "CloneLink": repo_model.ComposeHTTPSCloneURL(ownerName, repoName), "GoDocDirectory": prefix + "{/dir}", "GoDocFile": prefix + "{/dir}/{file}#L{line}", "Insecure": insecure, diff --git a/routers/web/org/home.go b/routers/web/org/home.go index 52f0282b5c..3a42044281 100644 --- a/routers/web/org/home.go +++ b/routers/web/org/home.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/markup" @@ -87,7 +88,7 @@ func Home(ctx *context.Context) { } var ( - repos []*models.Repository + repos []*repo_model.Repository count int64 err error ) diff --git a/routers/web/org/teams.go b/routers/web/org/teams.go index 8d39f26976..40fba5cd09 100644 --- a/routers/web/org/teams.go +++ b/routers/web/org/teams.go @@ -13,6 +13,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/perm" + repo_model "code.gitea.io/gitea/models/repo" unit_model "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" @@ -178,10 +179,10 @@ func TeamsRepoAction(ctx *context.Context) { switch action { case "add": repoName := path.Base(ctx.FormString("repo_name")) - var repo *models.Repository - repo, err = models.GetRepositoryByName(ctx.Org.Organization.ID, repoName) + var repo *repo_model.Repository + repo, err = repo_model.GetRepositoryByName(ctx.Org.Organization.ID, repoName) if err != nil { - if models.IsErrRepoNotExist(err) { + if repo_model.IsErrRepoNotExist(err) { ctx.Flash.Error(ctx.Tr("org.teams.add_nonexistent_repo")) ctx.Redirect(ctx.Org.OrgLink + "/teams/" + url.PathEscape(ctx.Org.Team.LowerName) + "/repositories") return diff --git a/routers/web/repo/blame.go b/routers/web/repo/blame.go index 5786aa1eb2..75246c3acb 100644 --- a/routers/web/repo/blame.go +++ b/routers/web/repo/blame.go @@ -11,7 +11,7 @@ import ( "net/url" "strings" - "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/base" "code.gitea.io/gitea/modules/context" @@ -103,7 +103,7 @@ func RefBlame(ctx *context.Context) { return } - blameReader, err := git.CreateBlameReader(ctx, models.RepoPath(userName, repoName), commitID, fileName) + blameReader, err := git.CreateBlameReader(ctx, repo_model.RepoPath(userName, repoName), commitID, fileName) if err != nil { ctx.NotFound("CreateBlameReader", err) return diff --git a/routers/web/repo/branch.go b/routers/web/repo/branch.go index 9c25180596..f6a475c3c9 100644 --- a/routers/web/repo/branch.go +++ b/routers/web/repo/branch.go @@ -12,6 +12,7 @@ import ( "strings" "code.gitea.io/gitea/models" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" @@ -117,7 +118,7 @@ func RestoreBranchPost(ctx *context.Context) { branchID := ctx.FormInt64("branch_id") branchName := ctx.FormString("name") - deletedBranch, err := ctx.Repo.Repository.GetDeletedBranchByID(branchID) + deletedBranch, err := models.GetDeletedBranchByID(ctx.Repo.Repository.ID, branchID) if err != nil { log.Error("GetDeletedBranchByID: %v", err) ctx.Flash.Error(ctx.Tr("repo.branch.restore_failed", branchName)) @@ -179,13 +180,13 @@ func loadBranches(ctx *context.Context, skip, limit int) ([]*Branch, int) { return nil, 0 } - protectedBranches, err := ctx.Repo.Repository.GetProtectedBranches() + protectedBranches, err := models.GetProtectedBranches(ctx.Repo.Repository.ID) if err != nil { ctx.ServerError("GetProtectedBranches", err) return nil, 0 } - repoIDToRepo := map[int64]*models.Repository{} + repoIDToRepo := map[int64]*repo_model.Repository{} repoIDToRepo[ctx.Repo.Repository.ID] = ctx.Repo.Repository repoIDToGitRepo := map[int64]*git.Repository{} @@ -223,7 +224,7 @@ func loadBranches(ctx *context.Context, skip, limit int) ([]*Branch, int) { } func loadOneBranch(ctx *context.Context, rawBranch *git.Branch, protectedBranches []*models.ProtectedBranch, - repoIDToRepo map[int64]*models.Repository, + repoIDToRepo map[int64]*repo_model.Repository, repoIDToGitRepo map[int64]*git.Repository) *Branch { log.Trace("loadOneBranch: '%s'", rawBranch.Name) @@ -311,7 +312,7 @@ func loadOneBranch(ctx *context.Context, rawBranch *git.Branch, protectedBranche func getDeletedBranches(ctx *context.Context) ([]*Branch, error) { branches := []*Branch{} - deletedBranches, err := ctx.Repo.Repository.GetDeletedBranches() + deletedBranches, err := models.GetDeletedBranches(ctx.Repo.Repository.ID) if err != nil { return branches, err } diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index 993fc1c335..ef21017a31 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -12,6 +12,7 @@ import ( "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/base" "code.gitea.io/gitea/modules/charset" @@ -382,7 +383,7 @@ func RawDiff(ctx *context.Context) { if ctx.Data["PageIsWiki"] != nil { repoPath = ctx.Repo.Repository.WikiPath() } else { - repoPath = models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) + repoPath = repo_model.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) } if err := git.GetRawDiff( repoPath, diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 4cd817a399..21311ba97c 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -17,6 +17,8 @@ import ( "strings" "code.gitea.io/gitea/models" + "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/base" @@ -165,7 +167,7 @@ func setCsvCompareContext(ctx *context.Context) { // CompareInfo represents the collected results from ParseCompareInfo type CompareInfo struct { HeadUser *user_model.User - HeadRepo *models.Repository + HeadRepo *repo_model.Repository HeadGitRepo *git.Repository CompareInfo *git.CompareInfo BaseBranch string @@ -259,16 +261,16 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo { ci.HeadRepo = baseRepo } } else { - ci.HeadRepo, err = models.GetRepositoryByOwnerAndName(headInfosSplit[0], headInfosSplit[1]) + ci.HeadRepo, err = repo_model.GetRepositoryByOwnerAndName(headInfosSplit[0], headInfosSplit[1]) if err != nil { - if models.IsErrRepoNotExist(err) { + if repo_model.IsErrRepoNotExist(err) { ctx.NotFound("GetRepositoryByOwnerAndName", nil) } else { ctx.ServerError("GetRepositoryByOwnerAndName", err) } return nil } - if err := ci.HeadRepo.GetOwner(); err != nil { + if err := ci.HeadRepo.GetOwner(db.DefaultContext); err != nil { if user_model.IsErrUserNotExist(err) { ctx.NotFound("GetUserByName", nil) } else { @@ -320,11 +322,11 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo { // 1. First if the baseRepo is a fork get the "RootRepo" it was // forked from - var rootRepo *models.Repository + var rootRepo *repo_model.Repository if baseRepo.IsFork { err = baseRepo.GetBaseRepo() if err != nil { - if !models.IsErrRepoNotExist(err) { + if !repo_model.IsErrRepoNotExist(err) { ctx.ServerError("Unable to find root repo", err) return nil } @@ -336,7 +338,7 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo { // 2. Now if the current user is not the owner of the baseRepo, // check if they have a fork of the base repo and offer that as // "OwnForkRepo" - var ownForkRepo *models.Repository + var ownForkRepo *repo_model.Repository if ctx.User != nil && baseRepo.OwnerID != ctx.User.ID { repo := models.GetForkedRepo(ctx.User.ID, baseRepo.ID) if repo != nil { @@ -438,7 +440,7 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo { if rootRepo != nil && rootRepo.ID != ci.HeadRepo.ID && rootRepo.ID != baseRepo.ID { - canRead := rootRepo.CheckUnitUser(ctx.User, unit.TypeCode) + canRead := models.CheckRepoUnitUser(rootRepo, ctx.User, unit.TypeCode) if canRead { ctx.Data["RootRepo"] = rootRepo if !fileOnly { @@ -463,7 +465,7 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo { ownForkRepo.ID != ci.HeadRepo.ID && ownForkRepo.ID != baseRepo.ID && (rootRepo == nil || ownForkRepo.ID != rootRepo.ID) { - canRead := ownForkRepo.CheckUnitUser(ctx.User, unit.TypeCode) + canRead := models.CheckRepoUnitUser(ownForkRepo, ctx.User, unit.TypeCode) if canRead { ctx.Data["OwnForkRepo"] = ownForkRepo if !fileOnly { @@ -653,7 +655,7 @@ func PrepareCompareDiff( return false } -func getBranchesAndTagsForRepo(repo *models.Repository) (branches, tags []string, err error) { +func getBranchesAndTagsForRepo(repo *repo_model.Repository) (branches, tags []string, err error) { gitRepo, err := git.OpenRepository(repo.RepoPath()) if err != nil { return nil, nil, err diff --git a/routers/web/repo/download.go b/routers/web/repo/download.go index 2307f736ad..430de24c69 100644 --- a/routers/web/repo/download.go +++ b/routers/web/repo/download.go @@ -6,6 +6,7 @@ package repo import ( + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/httpcache" @@ -38,7 +39,7 @@ func ServeBlobOrLFS(ctx *context.Context, blob *git.Blob) error { pointer, _ := lfs.ReadPointer(dataRc) if pointer.IsValid() { - meta, _ := ctx.Repo.Repository.GetLFSMetaObjectByOid(pointer.Oid) + meta, _ := models.GetLFSMetaObjectByOid(ctx.Repo.Repository.ID, pointer.Oid) if meta == nil { if err = dataRc.Close(); err != nil { log.Error("ServeBlobOrLFS: Close: %v", err) diff --git a/routers/web/repo/http.go b/routers/web/repo/http.go index 3aa8e84f57..3e12cd3267 100644 --- a/routers/web/repo/http.go +++ b/routers/web/repo/http.go @@ -20,8 +20,10 @@ import ( "time" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "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/modules/context" @@ -129,9 +131,9 @@ func httpBase(ctx *context.Context) (h *serviceHandler) { } repoExist := true - repo, err := models.GetRepositoryByName(owner.ID, reponame) + repo, err := repo_model.GetRepositoryByName(owner.ID, reponame) if err != nil { - if models.IsErrRepoNotExist(err) { + if repo_model.IsErrRepoNotExist(err) { if redirectRepoID, err := models.LookupRepoRedirect(owner.ID, reponame); err == nil { context.RedirectToRepo(ctx, redirectRepoID) return @@ -158,7 +160,7 @@ func httpBase(ctx *context.Context) (h *serviceHandler) { // don't allow anonymous pulls if organization is not public if isPublicPull { - if err := repo.GetOwner(); err != nil { + if err := repo.GetOwner(db.DefaultContext); err != nil { ctx.ServerError("GetOwner", err) return } @@ -273,7 +275,7 @@ func httpBase(ctx *context.Context) (h *serviceHandler) { if isWiki { // Ensure the wiki is enabled before we allow access to it if _, err := repo.GetUnit(unit.TypeWiki); err != nil { - if models.IsErrUnitTypeNotExist(err) { + if repo_model.IsErrUnitTypeNotExist(err) { ctx.HandleText(http.StatusForbidden, "repository wiki is disabled") return } @@ -295,9 +297,9 @@ func httpBase(ctx *context.Context) (h *serviceHandler) { r.URL.Path = strings.ToLower(r.URL.Path) // blue: In case some repo name has upper case name - dir := models.RepoPath(username, reponame) + dir := repo_model.RepoPath(username, reponame) if isWiki { - dir = models.RepoPath(username, wikiRepoName) + dir = repo_model.RepoPath(username, wikiRepoName) } return &serviceHandler{cfg, w, r, dir, cfg.Env} diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index 398aa26cc4..df977d2c13 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -272,7 +272,7 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption uti ctx.Data["CommitStatus"] = commitStatus // Get assignees. - ctx.Data["Assignees"], err = repo.GetAssignees() + ctx.Data["Assignees"], err = models.GetRepoAssignees(repo) if err != nil { ctx.ServerError("GetAssignees", err) return @@ -412,7 +412,7 @@ func Issues(ctx *context.Context) { } // RetrieveRepoMilestonesAndAssignees find all the milestones and assignees of a repository -func RetrieveRepoMilestonesAndAssignees(ctx *context.Context, repo *models.Repository) { +func RetrieveRepoMilestonesAndAssignees(ctx *context.Context, repo *repo_model.Repository) { var err error ctx.Data["OpenMilestones"], _, err = models.GetMilestones(models.GetMilestonesOption{ RepoID: repo.ID, @@ -431,7 +431,7 @@ func RetrieveRepoMilestonesAndAssignees(ctx *context.Context, repo *models.Repos return } - ctx.Data["Assignees"], err = repo.GetAssignees() + ctx.Data["Assignees"], err = models.GetRepoAssignees(repo) if err != nil { ctx.ServerError("GetAssignees", err) return @@ -440,7 +440,7 @@ func RetrieveRepoMilestonesAndAssignees(ctx *context.Context, repo *models.Repos handleTeamMentions(ctx) } -func retrieveProjects(ctx *context.Context, repo *models.Repository) { +func retrieveProjects(ctx *context.Context, repo *repo_model.Repository) { var err error @@ -479,7 +479,7 @@ type repoReviewerSelection struct { } // RetrieveRepoReviewers find all reviewers of a repository -func RetrieveRepoReviewers(ctx *context.Context, repo *models.Repository, issue *models.Issue, canChooseReviewer bool) { +func RetrieveRepoReviewers(ctx *context.Context, repo *repo_model.Repository, issue *models.Issue, canChooseReviewer bool) { ctx.Data["CanChooseReviewer"] = canChooseReviewer originalAuthorReviews, err := models.GetReviewersFromOriginalAuthorsByIssueID(issue.ID) @@ -513,13 +513,13 @@ func RetrieveRepoReviewers(ctx *context.Context, repo *models.Repository, issue posterID = 0 } - reviewers, err = repo.GetReviewers(ctx.User.ID, posterID) + reviewers, err = models.GetReviewers(repo, ctx.User.ID, posterID) if err != nil { ctx.ServerError("GetReviewers", err) return } - teamReviewers, err = repo.GetReviewerTeams() + teamReviewers, err = models.GetReviewerTeams(repo) if err != nil { ctx.ServerError("GetReviewerTeams", err) return @@ -659,7 +659,7 @@ func RetrieveRepoReviewers(ctx *context.Context, repo *models.Repository, issue } // RetrieveRepoMetas find all the meta information of a repository -func RetrieveRepoMetas(ctx *context.Context, repo *models.Repository, isPull bool) []*models.Label { +func RetrieveRepoMetas(ctx *context.Context, repo *repo_model.Repository, isPull bool) []*models.Label { if !ctx.Repo.CanWriteIssuesOrPulls(isPull) { return nil } @@ -887,11 +887,16 @@ func ValidateRepoMetas(ctx *context.Context, form forms.CreateIssueForm, isPull // Check milestone. milestoneID := form.MilestoneID if milestoneID > 0 { - ctx.Data["Milestone"], err = repo.GetMilestoneByID(milestoneID) + milestone, err := models.GetMilestoneByID(milestoneID) if err != nil { ctx.ServerError("GetMilestoneByID", err) return nil, nil, 0, 0 } + if milestone.RepoID != repo.ID { + ctx.ServerError("GetMilestoneByID", err) + return nil, nil, 0, 0 + } + ctx.Data["Milestone"] = milestone ctx.Data["milestone_id"] = milestoneID } @@ -1019,7 +1024,7 @@ func NewIssuePost(ctx *context.Context) { } // roleDescriptor returns the Role Decriptor for a comment in/with the given repo, poster and issue -func roleDescriptor(repo *models.Repository, poster *user_model.User, issue *models.Issue) (models.RoleDescriptor, error) { +func roleDescriptor(repo *repo_model.Repository, poster *user_model.User, issue *models.Issue) (models.RoleDescriptor, error) { perm, err := models.GetUserRepoPermission(repo, poster) if err != nil { return models.RoleDescriptorNone, err @@ -1085,7 +1090,7 @@ func ViewIssue(ctx *context.Context) { ctx.Redirect(com.Expand(extIssueUnit.ExternalTrackerConfig().ExternalTrackerFormat, metas)) return } - } else if err != nil && !models.IsErrUnitTypeNotExist(err) { + } else if err != nil && !repo_model.IsErrUnitTypeNotExist(err) { ctx.ServerError("GetUnit", err) return } @@ -1498,7 +1503,7 @@ func ViewIssue(ctx *context.Context) { } if perm.CanWrite(unit.TypeCode) { // Check if branch is not protected - if protected, err := pull.HeadRepo.IsProtectedBranch(pull.HeadBranch); err != nil { + if protected, err := models.IsProtectedBranch(pull.HeadRepo.ID, pull.HeadBranch); err != nil { log.Error("IsProtectedBranch: %v", err) } else if !protected { canDelete = true @@ -1535,21 +1540,21 @@ func ViewIssue(ctx *context.Context) { prConfig := prUnit.PullRequestsConfig() // Check correct values and select default - if ms, ok := ctx.Data["MergeStyle"].(models.MergeStyle); !ok || + if ms, ok := ctx.Data["MergeStyle"].(repo_model.MergeStyle); !ok || !prConfig.IsMergeStyleAllowed(ms) { defaultMergeStyle := prConfig.GetDefaultMergeStyle() if prConfig.IsMergeStyleAllowed(defaultMergeStyle) && !ok { ctx.Data["MergeStyle"] = defaultMergeStyle } else if prConfig.AllowMerge { - ctx.Data["MergeStyle"] = models.MergeStyleMerge + ctx.Data["MergeStyle"] = repo_model.MergeStyleMerge } else if prConfig.AllowRebase { - ctx.Data["MergeStyle"] = models.MergeStyleRebase + ctx.Data["MergeStyle"] = repo_model.MergeStyleRebase } else if prConfig.AllowRebaseMerge { - ctx.Data["MergeStyle"] = models.MergeStyleRebaseMerge + ctx.Data["MergeStyle"] = repo_model.MergeStyleRebaseMerge } else if prConfig.AllowSquash { - ctx.Data["MergeStyle"] = models.MergeStyleSquash + ctx.Data["MergeStyle"] = repo_model.MergeStyleSquash } else if prConfig.AllowManualMerge { - ctx.Data["MergeStyle"] = models.MergeStyleManuallyMerged + ctx.Data["MergeStyle"] = repo_model.MergeStyleManuallyMerged } else { ctx.Data["MergeStyle"] = "" } @@ -1897,7 +1902,7 @@ func UpdatePullReviewRequest(ctx *context.Context) { } if reviewID < 0 { // negative reviewIDs represent team requests - if err := issue.Repo.GetOwner(); err != nil { + if err := issue.Repo.GetOwner(db.DefaultContext); err != nil { ctx.ServerError("issue.Repo.GetOwner", err) return } @@ -2467,7 +2472,7 @@ func filterXRefComments(ctx *context.Context, issue *models.Issue) error { if models.CommentTypeIsRef(c.Type) && c.RefRepoID != issue.RepoID && c.RefRepoID != 0 { var err error // Set RefRepo for description in template - c.RefRepo, err = models.GetRepositoryByID(c.RefRepoID) + c.RefRepo, err = repo_model.GetRepositoryByID(c.RefRepoID) if err != nil { return err } diff --git a/routers/web/repo/lfs.go b/routers/web/repo/lfs.go index b1bc06f08b..28d6b12860 100644 --- a/routers/web/repo/lfs.go +++ b/routers/web/repo/lfs.go @@ -47,7 +47,7 @@ func LFSFiles(ctx *context.Context) { if page <= 1 { page = 1 } - total, err := ctx.Repo.Repository.CountLFSMetaObjects() + total, err := models.CountLFSMetaObjects(ctx.Repo.Repository.ID) if err != nil { ctx.ServerError("LFSFiles", err) return @@ -57,7 +57,7 @@ func LFSFiles(ctx *context.Context) { pager := context.NewPagination(int(total), setting.UI.ExplorePagingNum, page, 5) ctx.Data["Title"] = ctx.Tr("repo.settings.lfs") ctx.Data["PageIsSettingsLFS"] = true - lfsMetaObjects, err := ctx.Repo.Repository.GetLFSMetaObjects(pager.Paginater.Current(), setting.UI.ExplorePagingNum) + lfsMetaObjects, err := models.GetLFSMetaObjects(ctx.Repo.Repository.ID, pager.Paginater.Current(), setting.UI.ExplorePagingNum) if err != nil { ctx.ServerError("LFSFiles", err) return @@ -215,8 +215,7 @@ func LFSLockFile(ctx *context.Context) { return } - _, err := models.CreateLFSLock(&models.LFSLock{ - Repo: ctx.Repo.Repository, + _, err := models.CreateLFSLock(ctx.Repo.Repository, &models.LFSLock{ Path: lockPath, OwnerID: ctx.User.ID, }) @@ -238,7 +237,7 @@ func LFSUnlock(ctx *context.Context) { ctx.NotFound("LFSUnlock", nil) return } - _, err := models.DeleteLFSLockByID(ctx.ParamsInt64("lid"), ctx.User, true) + _, err := models.DeleteLFSLockByID(ctx.ParamsInt64("lid"), ctx.Repo.Repository, ctx.User, true) if err != nil { ctx.ServerError("LFSUnlock", err) return @@ -256,7 +255,7 @@ func LFSFileGet(ctx *context.Context) { oid := ctx.Params("oid") ctx.Data["Title"] = oid ctx.Data["PageIsSettingsLFS"] = true - meta, err := ctx.Repo.Repository.GetLFSMetaObjectByOid(oid) + meta, err := models.GetLFSMetaObjectByOid(ctx.Repo.Repository.ID, oid) if err != nil { if err == models.ErrLFSObjectNotExist { ctx.NotFound("LFSFileGet", nil) @@ -343,7 +342,7 @@ func LFSDelete(ctx *context.Context) { return } oid := ctx.Params("oid") - count, err := ctx.Repo.Repository.RemoveLFSMetaObjectByOid(oid) + count, err := models.RemoveLFSMetaObjectByOid(ctx.Repo.Repository.ID, oid) if err != nil { ctx.ServerError("LFSDelete", err) return @@ -444,7 +443,7 @@ func LFSPointerFiles(ctx *context.Context) { Size: pointerBlob.Size, } - if _, err := repo.GetLFSMetaObjectByOid(pointerBlob.Oid); err != nil { + if _, err := models.GetLFSMetaObjectByOid(repo.ID, pointerBlob.Oid); err != nil { if err != models.ErrLFSObjectNotExist { return err } diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index 7593e7fbc8..b08f749cde 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -19,6 +19,7 @@ import ( "code.gitea.io/gitea/models" "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/base" @@ -59,10 +60,10 @@ var ( } ) -func getRepository(ctx *context.Context, repoID int64) *models.Repository { - repo, err := models.GetRepositoryByID(repoID) +func getRepository(ctx *context.Context, repoID int64) *repo_model.Repository { + repo, err := repo_model.GetRepositoryByID(repoID) if err != nil { - if models.IsErrRepoNotExist(err) { + if repo_model.IsErrRepoNotExist(err) { ctx.NotFound("GetRepositoryByID", nil) } else { ctx.ServerError("GetRepositoryByID", err) @@ -89,7 +90,7 @@ func getRepository(ctx *context.Context, repoID int64) *models.Repository { return repo } -func getForkRepository(ctx *context.Context) *models.Repository { +func getForkRepository(ctx *context.Context) *repo_model.Repository { forkRepo := getRepository(ctx, ctx.ParamsInt64(":repoid")) if ctx.Written() { return nil @@ -101,7 +102,7 @@ func getForkRepository(ctx *context.Context) *models.Repository { return nil } - if err := forkRepo.GetOwner(); err != nil { + if err := forkRepo.GetOwner(db.DefaultContext); err != nil { ctx.ServerError("GetOwner", err) return nil } @@ -141,7 +142,7 @@ func getForkRepository(ctx *context.Context) *models.Repository { if !traverseParentRepo.IsFork { break } - traverseParentRepo, err = models.GetRepositoryByID(traverseParentRepo.ForkID) + traverseParentRepo, err = repo_model.GetRepositoryByID(traverseParentRepo.ForkID) if err != nil { ctx.ServerError("GetRepositoryByID", err) return nil @@ -209,7 +210,7 @@ func ForkPost(ctx *context.Context) { if !traverseParentRepo.IsFork { break } - traverseParentRepo, err = models.GetRepositoryByID(traverseParentRepo.ForkID) + traverseParentRepo, err = repo_model.GetRepositoryByID(traverseParentRepo.ForkID) if err != nil { ctx.ServerError("GetRepositoryByID", err) return @@ -702,7 +703,7 @@ func ViewPullFiles(ctx *context.Context) { ctx.Data["RequireHighlightJS"] = true ctx.Data["RequireSimpleMDE"] = true ctx.Data["RequireTribute"] = true - if ctx.Data["Assignees"], err = ctx.Repo.Repository.GetAssignees(); err != nil { + if ctx.Data["Assignees"], err = models.GetRepoAssignees(ctx.Repo.Repository); err != nil { ctx.ServerError("GetAssignees", err) return } @@ -847,7 +848,7 @@ func MergePullRequest(ctx *context.Context) { } // handle manually-merged mark - if models.MergeStyle(form.Do) == models.MergeStyleManuallyMerged { + if repo_model.MergeStyle(form.Do) == repo_model.MergeStyleManuallyMerged { if err = pull_service.MergedManually(pr, ctx.User, ctx.Repo.GitRepo, form.MergeCommitID); err != nil { if models.IsErrInvalidMergeStyle(err) { ctx.Flash.Error(ctx.Tr("repo.pulls.invalid_merge_option")) @@ -902,13 +903,13 @@ func MergePullRequest(ctx *context.Context) { message := strings.TrimSpace(form.MergeTitleField) if len(message) == 0 { - if models.MergeStyle(form.Do) == models.MergeStyleMerge { + if repo_model.MergeStyle(form.Do) == repo_model.MergeStyleMerge { message = pr.GetDefaultMergeMessage() } - if models.MergeStyle(form.Do) == models.MergeStyleRebaseMerge { + if repo_model.MergeStyle(form.Do) == repo_model.MergeStyleRebaseMerge { message = pr.GetDefaultMergeMessage() } - if models.MergeStyle(form.Do) == models.MergeStyleSquash { + if repo_model.MergeStyle(form.Do) == repo_model.MergeStyleSquash { message = pr.GetDefaultSquashMessage() } } @@ -932,7 +933,7 @@ func MergePullRequest(ctx *context.Context) { return } - if err = pull_service.Merge(pr, ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil { + if err = pull_service.Merge(pr, ctx.User, ctx.Repo.GitRepo, repo_model.MergeStyle(form.Do), message); err != nil { if models.IsErrInvalidMergeStyle(err) { ctx.Flash.Error(ctx.Tr("repo.pulls.invalid_merge_option")) ctx.Redirect(issue.Link()) @@ -1227,7 +1228,7 @@ func CleanUpPullRequest(ctx *context.Context) { } else if err = pr.LoadBaseRepo(); err != nil { ctx.ServerError("LoadBaseRepo", err) return - } else if err = pr.HeadRepo.GetOwner(); err != nil { + } else if err = pr.HeadRepo.GetOwner(db.DefaultContext); err != nil { ctx.ServerError("HeadRepo.GetOwner", err) return } diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index 69bd1ed416..b4bab5960b 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -146,8 +146,8 @@ func Create(ctx *context.Context) { ctx.Data["repo_template_name"] = ctx.Tr("repo.template_select") templateID := ctx.FormInt64("template_id") if templateID > 0 { - templateRepo, err := models.GetRepositoryByID(templateID) - if err == nil && templateRepo.CheckUnitUser(ctxUser, unit.TypeCode) { + templateRepo, err := repo_model.GetRepositoryByID(templateID) + if err == nil && models.CheckRepoUnitUser(templateRepo, ctxUser, unit.TypeCode) { ctx.Data["repo_template"] = templateID ctx.Data["repo_template_name"] = templateRepo.Name } @@ -213,7 +213,7 @@ func CreatePost(ctx *context.Context) { return } - var repo *models.Repository + var repo *repo_model.Repository var err error if form.RepoTemplate > 0 { opts := models.GenerateRepoOptions{ @@ -261,7 +261,7 @@ func CreatePost(ctx *context.Context) { DefaultBranch: form.DefaultBranch, AutoInit: form.AutoInit, IsTemplate: form.Template, - TrustModel: models.ToTrustModel(form.TrustModel), + TrustModel: repo_model.ToTrustModel(form.TrustModel), }) if err == nil { log.Trace("Repository created [%d]: %s/%s", repo.ID, ctxUser.Name, repo.Name) diff --git a/routers/web/repo/setting.go b/routers/web/repo/setting.go index 4fc1e91c25..68b53f2e90 100644 --- a/routers/web/repo/setting.go +++ b/routers/web/repo/setting.go @@ -17,6 +17,7 @@ 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" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" @@ -64,6 +65,12 @@ func Settings(ctx *context.Context) { signing, _ := models.SigningKey(ctx.Repo.Repository.RepoPath()) ctx.Data["SigningKeyAvailable"] = len(signing) > 0 ctx.Data["SigningSettings"] = setting.Repository.Signing + pushMirrors, err := repo_model.GetPushMirrorsByRepoID(ctx.Repo.Repository.ID) + if err != nil { + ctx.ServerError("GetPushMirrorsByRepoID", err) + return + } + ctx.Data["PushMirrors"] = pushMirrors ctx.HTML(http.StatusOK, tplSettingsOptions) } @@ -171,7 +178,7 @@ func SettingsPost(ctx *context.Context) { } else { ctx.Repo.Mirror.NextUpdateUnix = 0 } - if err := models.UpdateMirror(ctx.Repo.Mirror); err != nil { + if err := repo_model.UpdateMirror(ctx.Repo.Mirror); err != nil { ctx.Data["Err_Interval"] = true ctx.RenderWithErr(ctx.Tr("repo.mirror_interval_invalid"), tplSettingsOptions, &form) return @@ -217,7 +224,7 @@ func SettingsPost(ctx *context.Context) { ctx.Repo.Mirror.LFS = form.LFS ctx.Repo.Mirror.LFSEndpoint = form.LFSEndpoint - if err := models.UpdateMirror(ctx.Repo.Mirror); err != nil { + if err := repo_model.UpdateMirror(ctx.Repo.Mirror); err != nil { ctx.ServerError("UpdateMirror", err) return } @@ -274,7 +281,7 @@ func SettingsPost(ctx *context.Context) { return } - if err = models.DeletePushMirrorByID(m.ID); err != nil { + if err = repo_model.DeletePushMirrorByID(m.ID); err != nil { ctx.ServerError("DeletePushMirrorByID", err) return } @@ -315,19 +322,19 @@ func SettingsPost(ctx *context.Context) { return } - m := &models.PushMirror{ + m := &repo_model.PushMirror{ RepoID: repo.ID, Repo: repo, RemoteName: fmt.Sprintf("remote_mirror_%s", remoteSuffix), Interval: interval, } - if err := models.InsertPushMirror(m); err != nil { + if err := repo_model.InsertPushMirror(m); err != nil { ctx.ServerError("InsertPushMirror", err) return } if err := mirror_service.AddPushMirrorRemote(m, address); err != nil { - if err := models.DeletePushMirrorByID(m.ID); err != nil { + if err := repo_model.DeletePushMirrorByID(m.ID); err != nil { log.Error("DeletePushMirrorByID %v", err) } ctx.ServerError("AddPushMirrorRemote", err) @@ -339,7 +346,7 @@ func SettingsPost(ctx *context.Context) { case "advanced": var repoChanged bool - var units []models.RepoUnit + var units []repo_model.RepoUnit var deleteUnitTypes []unit_model.Type // This section doesn't require repo_name/RepoName to be set in the form, don't show it @@ -358,19 +365,19 @@ func SettingsPost(ctx *context.Context) { return } - units = append(units, models.RepoUnit{ + units = append(units, repo_model.RepoUnit{ RepoID: repo.ID, Type: unit_model.TypeExternalWiki, - Config: &models.ExternalWikiConfig{ + Config: &repo_model.ExternalWikiConfig{ ExternalWikiURL: form.ExternalWikiURL, }, }) deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeWiki) } else if form.EnableWiki && !form.EnableExternalWiki && !unit_model.TypeWiki.UnitGlobalDisabled() { - units = append(units, models.RepoUnit{ + units = append(units, repo_model.RepoUnit{ RepoID: repo.ID, Type: unit_model.TypeWiki, - Config: new(models.UnitConfig), + Config: new(repo_model.UnitConfig), }) deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeExternalWiki) } else { @@ -393,10 +400,10 @@ func SettingsPost(ctx *context.Context) { ctx.Redirect(repo.Link() + "/settings") return } - units = append(units, models.RepoUnit{ + units = append(units, repo_model.RepoUnit{ RepoID: repo.ID, Type: unit_model.TypeExternalTracker, - Config: &models.ExternalTrackerConfig{ + Config: &repo_model.ExternalTrackerConfig{ ExternalTrackerURL: form.ExternalTrackerURL, ExternalTrackerFormat: form.TrackerURLFormat, ExternalTrackerStyle: form.TrackerIssueStyle, @@ -404,10 +411,10 @@ func SettingsPost(ctx *context.Context) { }) deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeIssues) } else if form.EnableIssues && !form.EnableExternalTracker && !unit_model.TypeIssues.UnitGlobalDisabled() { - units = append(units, models.RepoUnit{ + units = append(units, repo_model.RepoUnit{ RepoID: repo.ID, Type: unit_model.TypeIssues, - Config: &models.IssuesConfig{ + Config: &repo_model.IssuesConfig{ EnableTimetracker: form.EnableTimetracker, AllowOnlyContributorsToTrackTime: form.AllowOnlyContributorsToTrackTime, EnableDependencies: form.EnableIssueDependencies, @@ -424,7 +431,7 @@ func SettingsPost(ctx *context.Context) { } if form.EnableProjects && !unit_model.TypeProjects.UnitGlobalDisabled() { - units = append(units, models.RepoUnit{ + units = append(units, repo_model.RepoUnit{ RepoID: repo.ID, Type: unit_model.TypeProjects, }) @@ -433,10 +440,10 @@ func SettingsPost(ctx *context.Context) { } if form.EnablePulls && !unit_model.TypePullRequests.UnitGlobalDisabled() { - units = append(units, models.RepoUnit{ + units = append(units, repo_model.RepoUnit{ RepoID: repo.ID, Type: unit_model.TypePullRequests, - Config: &models.PullRequestsConfig{ + Config: &repo_model.PullRequestsConfig{ IgnoreWhitespaceConflicts: form.PullsIgnoreWhitespace, AllowMerge: form.PullsAllowMerge, AllowRebase: form.PullsAllowRebase, @@ -445,7 +452,7 @@ func SettingsPost(ctx *context.Context) { AllowManualMerge: form.PullsAllowManualMerge, AutodetectManualMerge: form.EnableAutodetectManualMerge, DefaultDeleteBranchAfterMerge: form.DefaultDeleteBranchAfterMerge, - DefaultMergeStyle: models.MergeStyle(form.PullsDefaultMergeStyle), + DefaultMergeStyle: repo_model.MergeStyle(form.PullsDefaultMergeStyle), }, }) } else if !unit_model.TypePullRequests.UnitGlobalDisabled() { @@ -470,7 +477,7 @@ func SettingsPost(ctx *context.Context) { case "signing": changed := false - trustModel := models.ToTrustModel(form.TrustModel) + trustModel := repo_model.ToTrustModel(form.TrustModel) if trustModel != repo.TrustModel { repo.TrustModel = trustModel changed = true @@ -526,7 +533,7 @@ func SettingsPost(ctx *context.Context) { if _, err := repository.CleanUpMigrateInfo(repo); err != nil { ctx.ServerError("CleanUpMigrateInfo", err) return - } else if err = models.DeleteMirrorByRepoID(ctx.Repo.Repository.ID); err != nil { + } else if err = repo_model.DeleteMirrorByRepoID(ctx.Repo.Repository.ID); err != nil { ctx.ServerError("DeleteMirrorByRepoID", err) return } @@ -539,7 +546,7 @@ func SettingsPost(ctx *context.Context) { ctx.Error(http.StatusNotFound) return } - if err := repo.GetOwner(); err != nil { + if err := repo.GetOwner(db.DefaultContext); err != nil { ctx.ServerError("Convert Fork", err) return } @@ -706,7 +713,7 @@ func SettingsPost(ctx *context.Context) { return } - if err := repo.SetArchiveRepoState(true); err != nil { + if err := models.SetArchiveRepoState(repo, true); err != nil { log.Error("Tried to archive a repo: %s", err) ctx.Flash.Error(ctx.Tr("repo.settings.archive.error")) ctx.Redirect(ctx.Repo.RepoLink + "/settings") @@ -724,7 +731,7 @@ func SettingsPost(ctx *context.Context) { return } - if err := repo.SetArchiveRepoState(false); err != nil { + if err := models.SetArchiveRepoState(repo, false); err != nil { log.Error("Tried to unarchive a repo: %s", err) ctx.Flash.Error(ctx.Tr("repo.settings.unarchive.error")) ctx.Redirect(ctx.Repo.RepoLink + "/settings") @@ -770,14 +777,14 @@ func Collaboration(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("repo.settings") ctx.Data["PageIsSettingsCollaboration"] = true - users, err := ctx.Repo.Repository.GetCollaborators(db.ListOptions{}) + users, err := models.GetCollaborators(ctx.Repo.Repository.ID, db.ListOptions{}) if err != nil { ctx.ServerError("GetCollaborators", err) return } ctx.Data["Collaborators"] = users - teams, err := ctx.Repo.Repository.GetRepoTeams() + teams, err := models.GetRepoTeams(ctx.Repo.Repository) if err != nil { ctx.ServerError("GetRepoTeams", err) return @@ -824,13 +831,13 @@ func CollaborationPost(ctx *context.Context) { return } - if got, err := ctx.Repo.Repository.IsCollaborator(u.ID); err == nil && got { + if got, err := models.IsCollaborator(ctx.Repo.Repository.ID, u.ID); err == nil && got { ctx.Flash.Error(ctx.Tr("repo.settings.add_collaborator_duplicate")) ctx.Redirect(ctx.Repo.RepoLink + "/settings/collaboration") return } - if err = ctx.Repo.Repository.AddCollaborator(u); err != nil { + if err = models.AddCollaborator(ctx.Repo.Repository, u); err != nil { ctx.ServerError("AddCollaborator", err) return } @@ -845,7 +852,8 @@ func CollaborationPost(ctx *context.Context) { // ChangeCollaborationAccessMode response for changing access of a collaboration func ChangeCollaborationAccessMode(ctx *context.Context) { - if err := ctx.Repo.Repository.ChangeCollaborationAccessMode( + if err := models.ChangeCollaborationAccessMode( + ctx.Repo.Repository, ctx.FormInt64("uid"), perm.AccessMode(ctx.FormInt("mode"))); err != nil { log.Error("ChangeCollaborationAccessMode: %v", err) @@ -854,7 +862,7 @@ func ChangeCollaborationAccessMode(ctx *context.Context) { // DeleteCollaboration delete a collaboration for a repository func DeleteCollaboration(ctx *context.Context) { - if err := ctx.Repo.Repository.DeleteCollaboration(ctx.FormInt64("id")); err != nil { + if err := models.DeleteCollaboration(ctx.Repo.Repository, ctx.FormInt64("id")); err != nil { ctx.Flash.Error("DeleteCollaboration: " + err.Error()) } else { ctx.Flash.Success(ctx.Tr("repo.settings.remove_collaborator_success")) @@ -937,7 +945,7 @@ func DeleteTeam(ctx *context.Context) { } // parseOwnerAndRepo get repos by owner -func parseOwnerAndRepo(ctx *context.Context) (*user_model.User, *models.Repository) { +func parseOwnerAndRepo(ctx *context.Context) (*user_model.User, *repo_model.Repository) { owner, err := user_model.GetUserByName(ctx.Params(":username")) if err != nil { if user_model.IsErrUserNotExist(err) { @@ -948,9 +956,9 @@ func parseOwnerAndRepo(ctx *context.Context) (*user_model.User, *models.Reposito return nil, nil } - repo, err := models.GetRepositoryByName(owner.ID, ctx.Params(":reponame")) + repo, err := repo_model.GetRepositoryByName(owner.ID, ctx.Params(":reponame")) if err != nil { - if models.IsErrRepoNotExist(err) { + if repo_model.IsErrRepoNotExist(err) { ctx.NotFound("GetRepositoryByName", err) } else { ctx.ServerError("GetRepositoryByName", err) @@ -1136,7 +1144,7 @@ func UpdateAvatarSetting(ctx *context.Context, form forms.AvatarForm) error { if !(st.IsImage() && !st.IsSvgImage()) { return errors.New(ctx.Tr("settings.uploaded_avatar_not_a_image")) } - if err = ctxRepo.UploadAvatar(data); err != nil { + if err = models.UploadRepoAvatar(ctxRepo, data); err != nil { return fmt.Errorf("UploadAvatar: %v", err) } return nil @@ -1156,23 +1164,24 @@ func SettingsAvatar(ctx *context.Context) { // SettingsDeleteAvatar delete repository avatar func SettingsDeleteAvatar(ctx *context.Context) { - if err := ctx.Repo.Repository.DeleteAvatar(); err != nil { + if err := models.DeleteRepoAvatar(ctx.Repo.Repository); err != nil { ctx.Flash.Error(fmt.Sprintf("DeleteAvatar: %v", err)) } ctx.Redirect(ctx.Repo.RepoLink + "/settings") } -func selectPushMirrorByForm(form *forms.RepoSettingForm, repo *models.Repository) (*models.PushMirror, error) { +func selectPushMirrorByForm(form *forms.RepoSettingForm, repo *repo_model.Repository) (*repo_model.PushMirror, error) { id, err := strconv.ParseInt(form.PushMirrorID, 10, 64) if err != nil { return nil, err } - if err = repo.LoadPushMirrors(); err != nil { + pushMirrors, err := repo_model.GetPushMirrorsByRepoID(repo.ID) + if err != nil { return nil, err } - for _, m := range repo.PushMirrors { + for _, m := range pushMirrors { if m.ID == id { return m, nil } diff --git a/routers/web/repo/setting_protected_branch.go b/routers/web/repo/setting_protected_branch.go index e0580ac6de..1435b820ad 100644 --- a/routers/web/repo/setting_protected_branch.go +++ b/routers/web/repo/setting_protected_branch.go @@ -12,6 +12,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/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" @@ -29,7 +30,7 @@ func ProtectedBranch(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("repo.settings") ctx.Data["PageIsSettingsBranches"] = true - protectedBranches, err := ctx.Repo.Repository.GetProtectedBranches() + protectedBranches, err := models.GetProtectedBranches(ctx.Repo.Repository.ID) if err != nil { ctx.ServerError("GetProtectedBranches", err) return @@ -82,7 +83,7 @@ func ProtectedBranchPost(ctx *context.Context) { return } } - if err := repo.UpdateDefaultBranch(); err != nil { + if err := repo_model.UpdateDefaultBranch(repo); err != nil { ctx.ServerError("SetDefaultBranch", err) return } @@ -123,7 +124,7 @@ func SettingsProtectedBranch(c *context.Context) { } } - users, err := c.Repo.Repository.GetReaders() + users, err := models.GetRepoReaders(c.Repo.Repository) if err != nil { c.ServerError("Repo.Repository.GetReaders", err) return @@ -279,7 +280,7 @@ func SettingsProtectedBranchPost(ctx *context.Context) { ctx.Redirect(fmt.Sprintf("%s/settings/branches/%s", ctx.Repo.RepoLink, util.PathEscapeSegments(branch))) } else { if protectBranch != nil { - if err := ctx.Repo.Repository.DeleteProtectedBranch(protectBranch.ID); err != nil { + if err := models.DeleteProtectedBranch(ctx.Repo.Repository.ID, protectBranch.ID); err != nil { ctx.ServerError("DeleteProtectedBranch", err) return } diff --git a/routers/web/repo/settings_test.go b/routers/web/repo/settings_test.go index 5f997bc36b..fe6e9b52ba 100644 --- a/routers/web/repo/settings_test.go +++ b/routers/web/repo/settings_test.go @@ -11,6 +11,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" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" @@ -112,7 +113,7 @@ func TestCollaborationPost(t *testing.T) { Type: user_model.UserTypeIndividual, } - re := &models.Repository{ + re := &repo_model.Repository{ ID: 2, Owner: u, } @@ -128,7 +129,7 @@ func TestCollaborationPost(t *testing.T) { assert.EqualValues(t, http.StatusFound, ctx.Resp.Status()) - exists, err := re.IsCollaborator(4) + exists, err := models.IsCollaborator(re.ID, 4) assert.NoError(t, err) assert.True(t, exists) } @@ -172,7 +173,7 @@ func TestCollaborationPost_AddCollaboratorTwice(t *testing.T) { Type: user_model.UserTypeIndividual, } - re := &models.Repository{ + re := &repo_model.Repository{ ID: 2, Owner: u, } @@ -188,7 +189,7 @@ func TestCollaborationPost_AddCollaboratorTwice(t *testing.T) { assert.EqualValues(t, http.StatusFound, ctx.Resp.Status()) - exists, err := re.IsCollaborator(4) + exists, err := models.IsCollaborator(re.ID, 4) assert.NoError(t, err) assert.True(t, exists) @@ -238,7 +239,7 @@ func TestAddTeamPost(t *testing.T) { OrgID: 26, } - re := &models.Repository{ + re := &repo_model.Repository{ ID: 43, Owner: org, OwnerID: 26, @@ -278,7 +279,7 @@ func TestAddTeamPost_NotAllowed(t *testing.T) { OrgID: 26, } - re := &models.Repository{ + re := &repo_model.Repository{ ID: 43, Owner: org, OwnerID: 26, @@ -319,7 +320,7 @@ func TestAddTeamPost_AddTeamTwice(t *testing.T) { OrgID: 26, } - re := &models.Repository{ + re := &repo_model.Repository{ ID: 43, Owner: org, OwnerID: 26, @@ -355,7 +356,7 @@ func TestAddTeamPost_NonExistentTeam(t *testing.T) { Type: user_model.UserTypeOrganization, } - re := &models.Repository{ + re := &repo_model.Repository{ ID: 43, Owner: org, OwnerID: 26, @@ -393,7 +394,7 @@ func TestDeleteTeam(t *testing.T) { OrgID: 3, } - re := &models.Repository{ + re := &repo_model.Repository{ ID: 3, Owner: org, OwnerID: 3, diff --git a/routers/web/repo/tag.go b/routers/web/repo/tag.go index 671c388cb1..0114704f82 100644 --- a/routers/web/repo/tag.go +++ b/routers/web/repo/tag.go @@ -135,14 +135,14 @@ func setTagsContext(ctx *context.Context) error { ctx.Data["Title"] = ctx.Tr("repo.settings") ctx.Data["PageIsSettingsTags"] = true - protectedTags, err := ctx.Repo.Repository.GetProtectedTags() + protectedTags, err := models.GetProtectedTags(ctx.Repo.Repository.ID) if err != nil { ctx.ServerError("GetProtectedTags", err) return err } ctx.Data["ProtectedTags"] = protectedTags - users, err := ctx.Repo.Repository.GetReaders() + users, err := models.GetRepoReaders(ctx.Repo.Repository) if err != nil { ctx.ServerError("Repo.Repository.GetReaders", err) return err diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index 33420aab3b..bc8396ab79 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -21,6 +21,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + repo_model "code.gitea.io/gitea/models/repo" unit_model "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" @@ -269,7 +270,7 @@ func renderDirectory(ctx *context.Context, treeLink string) { if isTextFile && setting.LFS.StartServer { pointer, _ := lfs.ReadPointerFromBuffer(buf) if pointer.IsValid() { - meta, err := ctx.Repo.Repository.GetLFSMetaObjectByOid(pointer.Oid) + meta, err := models.GetLFSMetaObjectByOid(ctx.Repo.Repository.ID, pointer.Oid) if err != nil && err != models.ErrLFSObjectNotExist { ctx.ServerError("GetLFSMetaObject", err) return @@ -394,7 +395,7 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st if isTextFile && setting.LFS.StartServer { pointer, _ := lfs.ReadPointerFromBuffer(buf) if pointer.IsValid() { - meta, err := ctx.Repo.Repository.GetLFSMetaObjectByOid(pointer.Oid) + meta, err := models.GetLFSMetaObjectByOid(ctx.Repo.Repository.ID, pointer.Oid) if err != nil && err != models.ErrLFSObjectNotExist { ctx.ServerError("GetLFSMetaObject", err) return @@ -443,7 +444,7 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st ctx.Data["IsTextSource"] = isTextFile || isDisplayingSource // Check LFS Lock - lfsLock, err := ctx.Repo.Repository.GetTreePathLock(ctx.Repo.TreePath) + lfsLock, err := models.GetTreePathLock(ctx.Repo.Repository.ID, ctx.Repo.TreePath) ctx.Data["LFSLock"] = lfsLock if err != nil { ctx.ServerError("GetTreePathLock", err) @@ -641,7 +642,7 @@ func checkHomeCodeViewable(ctx *context.Context) { if ctx.IsSigned { // Set repo notification-status read if unread - if err := ctx.Repo.Repository.ReadBy(ctx.User.ID); err != nil { + if err := models.SetRepoReadBy(ctx.Repo.Repository.ID, ctx.User.ID); err != nil { ctx.ServerError("ReadBy", err) return } @@ -808,7 +809,7 @@ func renderDirectoryFiles(ctx *context.Context, timeout time.Duration) git.Entri } func renderLanguageStats(ctx *context.Context) { - langs, err := ctx.Repo.Repository.GetTopLanguageStats(5) + langs, err := repo_model.GetTopLanguageStats(ctx.Repo.Repository, 5) if err != nil { ctx.ServerError("Repo.GetTopLanguageStats", err) return @@ -926,7 +927,9 @@ func Watchers(ctx *context.Context) { ctx.Data["CardsTitle"] = ctx.Tr("repo.watchers") ctx.Data["PageIsWatchers"] = true - RenderUserCards(ctx, ctx.Repo.Repository.NumWatches, ctx.Repo.Repository.GetWatchers, tplWatchers) + RenderUserCards(ctx, ctx.Repo.Repository.NumWatches, func(opts db.ListOptions) ([]*user_model.User, error) { + return models.GetRepoWatchers(ctx.Repo.Repository.ID, opts) + }, tplWatchers) } // Stars render repository's starred users @@ -951,7 +954,7 @@ func Forks(ctx *context.Context) { pager := context.NewPagination(ctx.Repo.Repository.NumForks, models.ItemsPerPage, page, 5) ctx.Data["Page"] = pager - forks, err := ctx.Repo.Repository.GetForks(db.ListOptions{ + forks, err := models.GetForks(ctx.Repo.Repository, db.ListOptions{ Page: pager.Paginater.Current(), PageSize: models.ItemsPerPage, }) @@ -961,7 +964,7 @@ func Forks(ctx *context.Context) { } for _, fork := range forks { - if err = fork.GetOwner(); err != nil { + if err = fork.GetOwner(db.DefaultContext); err != nil { ctx.ServerError("GetOwner", err) return } diff --git a/routers/web/repo/wiki_test.go b/routers/web/repo/wiki_test.go index 87f2779c1a..1bfda50d19 100644 --- a/routers/web/repo/wiki_test.go +++ b/routers/web/repo/wiki_test.go @@ -9,7 +9,7 @@ import ( "net/http" "testing" - "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" "code.gitea.io/gitea/modules/test" @@ -23,7 +23,7 @@ import ( const content = "Wiki contents for unit tests" const message = "Wiki commit message for unit tests" -func wikiEntry(t *testing.T, repo *models.Repository, wikiName string) *git.TreeEntry { +func wikiEntry(t *testing.T, repo *repo_model.Repository, wikiName string) *git.TreeEntry { wikiRepo, err := git.OpenRepository(repo.WikiPath()) assert.NoError(t, err) defer wikiRepo.Close() @@ -39,7 +39,7 @@ func wikiEntry(t *testing.T, repo *models.Repository, wikiName string) *git.Tree return nil } -func wikiContent(t *testing.T, repo *models.Repository, wikiName string) string { +func wikiContent(t *testing.T, repo *repo_model.Repository, wikiName string) string { entry := wikiEntry(t, repo, wikiName) if !assert.NotNil(t, entry) { return "" @@ -52,11 +52,11 @@ func wikiContent(t *testing.T, repo *models.Repository, wikiName string) string return string(bytes) } -func assertWikiExists(t *testing.T, repo *models.Repository, wikiName string) { +func assertWikiExists(t *testing.T, repo *repo_model.Repository, wikiName string) { assert.NotNil(t, wikiEntry(t, repo, wikiName)) } -func assertWikiNotExists(t *testing.T, repo *models.Repository, wikiName string) { +func assertWikiNotExists(t *testing.T, repo *repo_model.Repository, wikiName string) { assert.Nil(t, wikiEntry(t, repo, wikiName)) } diff --git a/routers/web/user/home.go b/routers/web/user/home.go index ca2705a42f..d3883571ed 100644 --- a/routers/web/user/home.go +++ b/routers/web/user/home.go @@ -16,6 +16,7 @@ import ( "code.gitea.io/gitea/models" "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/base" @@ -96,7 +97,7 @@ func Dashboard(ctx *context.Context) { } var err error - var mirrors []*models.Repository + var mirrors []*repo_model.Repository if ctxUser.IsOrganization() { var env models.AccessibleReposEnvironment if ctx.Org.Team != nil { @@ -114,7 +115,7 @@ func Dashboard(ctx *context.Context) { return } } else { - mirrors, err = models.GetUserMirrorRepositories(ctxUser.ID) + mirrors, err = repo_model.GetUserMirrorRepositories(ctxUser.ID) if err != nil { ctx.ServerError("GetUserMirrorRepositories", err) return @@ -122,7 +123,7 @@ func Dashboard(ctx *context.Context) { } ctx.Data["MaxShowRepoNum"] = setting.UI.User.RepoPagingNum - if err := models.MirrorRepositoryList(mirrors).LoadAttributes(); err != nil { + if err := repo_model.MirrorRepositoryList(mirrors).LoadAttributes(); err != nil { ctx.ServerError("MirrorRepositoryList.LoadAttributes", err) return } @@ -524,7 +525,7 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) { // showReposMap maps repository IDs to their Repository pointers. showReposMap, err := repoIDMap(ctxUser, issueCountByRepo, unitType) if err != nil { - if models.IsErrRepoNotExist(err) { + if repo_model.IsErrRepoNotExist(err) { ctx.NotFound("GetRepositoryByID", err) return } @@ -795,15 +796,15 @@ func issueIDsFromSearch(ctxUser *user_model.User, keyword string, opts *models.I return issueIDsFromSearch, nil } -func repoIDMap(ctxUser *user_model.User, issueCountByRepo map[int64]int64, unitType unit.Type) (map[int64]*models.Repository, error) { - repoByID := make(map[int64]*models.Repository, len(issueCountByRepo)) +func repoIDMap(ctxUser *user_model.User, issueCountByRepo map[int64]int64, unitType unit.Type) (map[int64]*repo_model.Repository, error) { + repoByID := make(map[int64]*repo_model.Repository, len(issueCountByRepo)) for id := range issueCountByRepo { if id <= 0 { continue } if _, ok := repoByID[id]; !ok { - repo, err := models.GetRepositoryByID(id) - if models.IsErrRepoNotExist(err) { + repo, err := repo_model.GetRepositoryByID(id) + if repo_model.IsErrRepoNotExist(err) { return nil, err } else if err != nil { return nil, fmt.Errorf("GetRepositoryByID: [%d]%v", id, err) diff --git a/routers/web/user/profile.go b/routers/web/user/profile.go index 32d2bdde83..77b357c222 100644 --- a/routers/web/user/profile.go +++ b/routers/web/user/profile.go @@ -13,6 +13,7 @@ import ( "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/context" "code.gitea.io/gitea/modules/markup" @@ -196,7 +197,7 @@ func Profile(ctx *context.Context) { topicOnly := ctx.FormBool("topic") var ( - repos []*models.Repository + repos []*repo_model.Repository count int64 total int orderBy db.SearchOrderBy diff --git a/routers/web/user/setting/adopt.go b/routers/web/user/setting/adopt.go index 56ab6ec999..348705b743 100644 --- a/routers/web/user/setting/adopt.go +++ b/routers/web/user/setting/adopt.go @@ -8,6 +8,7 @@ import ( "path/filepath" "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/context" "code.gitea.io/gitea/modules/setting" @@ -31,7 +32,7 @@ func AdoptOrDeleteRepository(ctx *context.Context) { root := user_model.UserPath(ctxUser.LowerName) // check not a repo - has, err := models.IsRepositoryExist(ctxUser, dir) + has, err := repo_model.IsRepositoryExist(ctxUser, dir) if err != nil { ctx.ServerError("IsRepositoryExist", err) return diff --git a/routers/web/user/setting/profile.go b/routers/web/user/setting/profile.go index 0475398a9a..089177874a 100644 --- a/routers/web/user/setting/profile.go +++ b/routers/web/user/setting/profile.go @@ -16,6 +16,7 @@ import ( "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/base" "code.gitea.io/gitea/modules/context" @@ -271,7 +272,7 @@ func Repos(ctx *context.Context) { if adoptOrDelete { repoNames := make([]string, 0, setting.UI.Admin.UserPagingNum) - repos := map[string]*models.Repository{} + repos := map[string]*repo_model.Repository{} // We're going to iterate by pagesize. root := user_model.UserPath(ctxUser.Name) if err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { |