diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-12-10 16:14:24 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-10 16:14:24 +0800 |
commit | 3ca5dc7e32b372d14ff80d96f14b8f6a805862f1 (patch) | |
tree | 50d193ed0dacf2888d57b193a9b0d36065aff205 /routers/web/repo | |
parent | 0a9fcf63a49799ad3b0f146c54879161bac61e10 (diff) | |
download | gitea-3ca5dc7e32b372d14ff80d96f14b8f6a805862f1.tar.gz gitea-3ca5dc7e32b372d14ff80d96f14b8f6a805862f1.zip |
Move keys to models/asymkey (#17917)
* Move keys to models/keys
* Rename models/keys -> models/asymkey
* change the missed package name
* Fix package alias
* Fix test
* Fix docs
* Fix test
* Fix test
* merge
Diffstat (limited to 'routers/web/repo')
-rw-r--r-- | routers/web/repo/commit.go | 7 | ||||
-rw-r--r-- | routers/web/repo/issue.go | 7 | ||||
-rw-r--r-- | routers/web/repo/setting.go | 29 | ||||
-rw-r--r-- | routers/web/repo/settings_test.go | 5 | ||||
-rw-r--r-- | routers/web/repo/view.go | 7 |
5 files changed, 32 insertions, 23 deletions
diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index 5bc9f9eae9..0a51e43cf7 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -11,6 +11,7 @@ import ( "strings" "code.gitea.io/gitea/models" + asymkey_model "code.gitea.io/gitea/models/asymkey" "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" @@ -344,13 +345,15 @@ func Diff(ctx *context.Context) { ctx.Data["CommitStatus"] = models.CalcCommitStatus(statuses) ctx.Data["CommitStatuses"] = statuses - verification := models.ParseCommitWithSignature(commit) + verification := asymkey_model.ParseCommitWithSignature(commit) ctx.Data["Verification"] = verification ctx.Data["Author"] = user_model.ValidateCommitWithEmail(commit) ctx.Data["Parents"] = parents ctx.Data["DiffNotAvailable"] = diff.NumFiles == 0 - if err := models.CalculateTrustStatus(verification, ctx.Repo.Repository, nil); err != nil { + if err := asymkey_model.CalculateTrustStatus(verification, ctx.Repo.Repository.GetTrustModel(), func(user *user_model.User) (bool, error) { + return models.IsUserRepoAdmin(ctx.Repo.Repository, user) + }, nil); err != nil { ctx.ServerError("CalculateTrustStatus", err) return } diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index a5eecd0c33..f03a8dfbc2 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -34,6 +34,7 @@ import ( "code.gitea.io/gitea/modules/upload" "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/web" + asymkey_service "code.gitea.io/gitea/services/asymkey" comment_service "code.gitea.io/gitea/services/comments" "code.gitea.io/gitea/services/forms" issue_service "code.gitea.io/gitea/services/issue" @@ -1583,12 +1584,12 @@ func ViewIssue(ctx *context.Context) { } ctx.Data["WillSign"] = false if ctx.User != nil { - sign, key, _, err := pull.SignMerge(ctx.User, pull.BaseRepo.RepoPath(), pull.BaseBranch, pull.GetGitRefName()) + sign, key, _, err := asymkey_service.SignMerge(pull, ctx.User, pull.BaseRepo.RepoPath(), pull.BaseBranch, pull.GetGitRefName()) ctx.Data["WillSign"] = sign ctx.Data["SigningKey"] = key if err != nil { - if models.IsErrWontSign(err) { - ctx.Data["WontSignReason"] = err.(*models.ErrWontSign).Reason + if asymkey_service.IsErrWontSign(err) { + ctx.Data["WontSignReason"] = err.(*asymkey_service.ErrWontSign).Reason } else { ctx.Data["WontSignReason"] = "error" log.Error("Error whilst checking if could sign pr %d in repo %s. Error: %v", pull.ID, pull.BaseRepo.FullName(), err) diff --git a/routers/web/repo/setting.go b/routers/web/repo/setting.go index 68b53f2e90..bb118a9035 100644 --- a/routers/web/repo/setting.go +++ b/routers/web/repo/setting.go @@ -15,6 +15,7 @@ import ( "time" "code.gitea.io/gitea/models" + asymkey_model "code.gitea.io/gitea/models/asymkey" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/perm" repo_model "code.gitea.io/gitea/models/repo" @@ -34,6 +35,7 @@ import ( "code.gitea.io/gitea/modules/validation" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/routers/utils" + asymkey_service "code.gitea.io/gitea/services/asymkey" "code.gitea.io/gitea/services/forms" "code.gitea.io/gitea/services/mailer" "code.gitea.io/gitea/services/migrations" @@ -62,7 +64,7 @@ func Settings(ctx *context.Context) { ctx.Data["DisableNewPushMirrors"] = setting.Mirror.DisableNewPush ctx.Data["DefaultMirrorInterval"] = setting.Mirror.DefaultInterval - signing, _ := models.SigningKey(ctx.Repo.Repository.RepoPath()) + signing, _ := asymkey_service.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) @@ -476,7 +478,6 @@ func SettingsPost(ctx *context.Context) { case "signing": changed := false - trustModel := repo_model.ToTrustModel(form.TrustModel) if trustModel != repo.TrustModel { repo.TrustModel = trustModel @@ -673,7 +674,7 @@ func SettingsPost(ctx *context.Context) { ctx.Repo.GitRepo.Close() } - if err := repo_service.DeleteRepository(ctx.User, ctx.Repo.Repository); err != nil { + if err := repo_service.DeleteRepository(ctx.User, ctx.Repo.Repository, true); err != nil { ctx.ServerError("DeleteRepository", err) return } @@ -1029,7 +1030,7 @@ func DeployKeys(ctx *context.Context) { ctx.Data["PageIsSettingsKeys"] = true ctx.Data["DisableSSH"] = setting.SSH.Disabled - keys, err := models.ListDeployKeys(&models.ListDeployKeysOptions{RepoID: ctx.Repo.Repository.ID}) + keys, err := asymkey_model.ListDeployKeys(db.DefaultContext, &asymkey_model.ListDeployKeysOptions{RepoID: ctx.Repo.Repository.ID}) if err != nil { ctx.ServerError("ListDeployKeys", err) return @@ -1045,7 +1046,7 @@ func DeployKeysPost(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys") ctx.Data["PageIsSettingsKeys"] = true - keys, err := models.ListDeployKeys(&models.ListDeployKeysOptions{RepoID: ctx.Repo.Repository.ID}) + keys, err := asymkey_model.ListDeployKeys(db.DefaultContext, &asymkey_model.ListDeployKeysOptions{RepoID: ctx.Repo.Repository.ID}) if err != nil { ctx.ServerError("ListDeployKeys", err) return @@ -1057,11 +1058,11 @@ func DeployKeysPost(ctx *context.Context) { return } - content, err := models.CheckPublicKeyString(form.Content) + content, err := asymkey_model.CheckPublicKeyString(form.Content) if err != nil { - if models.IsErrSSHDisabled(err) { + if db.IsErrSSHDisabled(err) { ctx.Flash.Info(ctx.Tr("settings.ssh_disabled")) - } else if models.IsErrKeyUnableVerify(err) { + } else if asymkey_model.IsErrKeyUnableVerify(err) { ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key")) } else { ctx.Data["HasError"] = true @@ -1072,20 +1073,20 @@ func DeployKeysPost(ctx *context.Context) { return } - key, err := models.AddDeployKey(ctx.Repo.Repository.ID, form.Title, content, !form.IsWritable) + key, err := asymkey_model.AddDeployKey(ctx.Repo.Repository.ID, form.Title, content, !form.IsWritable) if err != nil { ctx.Data["HasError"] = true switch { - case models.IsErrDeployKeyAlreadyExist(err): + case asymkey_model.IsErrDeployKeyAlreadyExist(err): ctx.Data["Err_Content"] = true ctx.RenderWithErr(ctx.Tr("repo.settings.key_been_used"), tplDeployKeys, &form) - case models.IsErrKeyAlreadyExist(err): + case asymkey_model.IsErrKeyAlreadyExist(err): ctx.Data["Err_Content"] = true ctx.RenderWithErr(ctx.Tr("settings.ssh_key_been_used"), tplDeployKeys, &form) - case models.IsErrKeyNameAlreadyUsed(err): + case asymkey_model.IsErrKeyNameAlreadyUsed(err): ctx.Data["Err_Title"] = true ctx.RenderWithErr(ctx.Tr("repo.settings.key_name_used"), tplDeployKeys, &form) - case models.IsErrDeployKeyNameAlreadyUsed(err): + case asymkey_model.IsErrDeployKeyNameAlreadyUsed(err): ctx.Data["Err_Title"] = true ctx.RenderWithErr(ctx.Tr("repo.settings.key_name_used"), tplDeployKeys, &form) default: @@ -1101,7 +1102,7 @@ func DeployKeysPost(ctx *context.Context) { // DeleteDeployKey response for deleting a deploy key func DeleteDeployKey(ctx *context.Context) { - if err := models.DeleteDeployKey(ctx.User, ctx.FormInt64("id")); err != nil { + if err := asymkey_service.DeleteDeployKey(ctx.User, ctx.FormInt64("id")); err != nil { ctx.Flash.Error("DeleteDeployKey: " + err.Error()) } else { ctx.Flash.Success(ctx.Tr("repo.settings.deploy_key_deletion_success")) diff --git a/routers/web/repo/settings_test.go b/routers/web/repo/settings_test.go index fe6e9b52ba..5b62e1ed16 100644 --- a/routers/web/repo/settings_test.go +++ b/routers/web/repo/settings_test.go @@ -10,6 +10,7 @@ import ( "testing" "code.gitea.io/gitea/models" + asymkey_model "code.gitea.io/gitea/models/asymkey" "code.gitea.io/gitea/models/perm" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" @@ -61,7 +62,7 @@ func TestAddReadOnlyDeployKey(t *testing.T) { DeployKeysPost(ctx) assert.EqualValues(t, http.StatusFound, ctx.Resp.Status()) - unittest.AssertExistsAndLoadBean(t, &models.DeployKey{ + unittest.AssertExistsAndLoadBean(t, &asymkey_model.DeployKey{ Name: addKeyForm.Title, Content: addKeyForm.Content, Mode: perm.AccessModeRead, @@ -91,7 +92,7 @@ func TestAddReadWriteOnlyDeployKey(t *testing.T) { DeployKeysPost(ctx) assert.EqualValues(t, http.StatusFound, ctx.Resp.Status()) - unittest.AssertExistsAndLoadBean(t, &models.DeployKey{ + unittest.AssertExistsAndLoadBean(t, &asymkey_model.DeployKey{ Name: addKeyForm.Title, Content: addKeyForm.Content, Mode: perm.AccessModeWrite, diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index bc8396ab79..9e0f670cf9 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -20,6 +20,7 @@ import ( "time" "code.gitea.io/gitea/models" + asymkey_model "code.gitea.io/gitea/models/asymkey" "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" unit_model "code.gitea.io/gitea/models/unit" @@ -777,9 +778,11 @@ func renderDirectoryFiles(ctx *context.Context, timeout time.Duration) git.Entri ctx.Data["LatestCommit"] = latestCommit if latestCommit != nil { - verification := models.ParseCommitWithSignature(latestCommit) + verification := asymkey_model.ParseCommitWithSignature(latestCommit) - if err := models.CalculateTrustStatus(verification, ctx.Repo.Repository, nil); err != nil { + if err := asymkey_model.CalculateTrustStatus(verification, ctx.Repo.Repository.GetTrustModel(), func(user *user_model.User) (bool, error) { + return models.IsUserRepoAdmin(ctx.Repo.Repository, user) + }, nil); err != nil { ctx.ServerError("CalculateTrustStatus", err) return nil } |