mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
Move repository model into models/repo (#17933)
* Some refactors related repository model * Move more methods out of repository * Move repository into models/repo * Fix test * Fix test * some improvements * Remove unnecessary function
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user