summaryrefslogtreecommitdiffstats
path: root/models/asymkey/ssh_key_deploy.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2022-05-20 22:08:52 +0800
committerGitHub <noreply@github.com>2022-05-20 22:08:52 +0800
commitfd7d83ace60258acf7139c4c787aa8af75b7ba8c (patch)
tree50038348ec10485f72344f3ac80324e04abc1283 /models/asymkey/ssh_key_deploy.go
parentd81e31ad7826a81fc7139f329f250594610a274b (diff)
downloadgitea-fd7d83ace60258acf7139c4c787aa8af75b7ba8c.tar.gz
gitea-fd7d83ace60258acf7139c4c787aa8af75b7ba8c.zip
Move almost all functions' parameter db.Engine to context.Context (#19748)
* Move almost all functions' parameter db.Engine to context.Context * remove some unnecessary wrap functions
Diffstat (limited to 'models/asymkey/ssh_key_deploy.go')
-rw-r--r--models/asymkey/ssh_key_deploy.go29
1 files changed, 11 insertions, 18 deletions
diff --git a/models/asymkey/ssh_key_deploy.go b/models/asymkey/ssh_key_deploy.go
index fe2ade43ae..9a97d37f93 100644
--- a/models/asymkey/ssh_key_deploy.go
+++ b/models/asymkey/ssh_key_deploy.go
@@ -67,9 +67,9 @@ func init() {
db.RegisterModel(new(DeployKey))
}
-func checkDeployKey(e db.Engine, keyID, repoID int64, name string) error {
+func checkDeployKey(ctx context.Context, keyID, repoID int64, name string) error {
// Note: We want error detail, not just true or false here.
- has, err := e.
+ has, err := db.GetEngine(ctx).
Where("key_id = ? AND repo_id = ?", keyID, repoID).
Get(new(DeployKey))
if err != nil {
@@ -78,7 +78,7 @@ func checkDeployKey(e db.Engine, keyID, repoID int64, name string) error {
return ErrDeployKeyAlreadyExist{keyID, repoID}
}
- has, err = e.
+ has, err = db.GetEngine(ctx).
Where("repo_id = ? AND name = ?", repoID, name).
Get(new(DeployKey))
if err != nil {
@@ -91,8 +91,8 @@ func checkDeployKey(e db.Engine, keyID, repoID int64, name string) error {
}
// addDeployKey adds new key-repo relation.
-func addDeployKey(e db.Engine, keyID, repoID int64, name, fingerprint string, mode perm.AccessMode) (*DeployKey, error) {
- if err := checkDeployKey(e, keyID, repoID, name); err != nil {
+func addDeployKey(ctx context.Context, keyID, repoID int64, name, fingerprint string, mode perm.AccessMode) (*DeployKey, error) {
+ if err := checkDeployKey(ctx, keyID, repoID, name); err != nil {
return nil, err
}
@@ -103,8 +103,7 @@ func addDeployKey(e db.Engine, keyID, repoID int64, name, fingerprint string, mo
Fingerprint: fingerprint,
Mode: mode,
}
- _, err := e.Insert(key)
- return key, err
+ return key, db.Insert(ctx, key)
}
// HasDeployKey returns true if public key is a deploy key of given repository.
@@ -133,12 +132,10 @@ func AddDeployKey(repoID int64, name, content string, readOnly bool) (*DeployKey
}
defer committer.Close()
- sess := db.GetEngine(ctx)
-
pkey := &PublicKey{
Fingerprint: fingerprint,
}
- has, err := sess.Get(pkey)
+ has, err := db.GetByBean(ctx, pkey)
if err != nil {
return nil, err
}
@@ -153,12 +150,12 @@ func AddDeployKey(repoID int64, name, content string, readOnly bool) (*DeployKey
pkey.Type = KeyTypeDeploy
pkey.Content = content
pkey.Name = name
- if err = addKey(sess, pkey); err != nil {
+ if err = addKey(ctx, pkey); err != nil {
return nil, fmt.Errorf("addKey: %v", err)
}
}
- key, err := addDeployKey(sess, pkey.ID, repoID, name, pkey.Fingerprint, accessMode)
+ key, err := addDeployKey(ctx, pkey.ID, repoID, name, pkey.Fingerprint, accessMode)
if err != nil {
return nil, err
}
@@ -179,16 +176,12 @@ func GetDeployKeyByID(ctx context.Context, id int64) (*DeployKey, error) {
}
// GetDeployKeyByRepo returns deploy key by given public key ID and repository ID.
-func GetDeployKeyByRepo(keyID, repoID int64) (*DeployKey, error) {
- return getDeployKeyByRepo(db.GetEngine(db.DefaultContext), keyID, repoID)
-}
-
-func getDeployKeyByRepo(e db.Engine, keyID, repoID int64) (*DeployKey, error) {
+func GetDeployKeyByRepo(ctx context.Context, keyID, repoID int64) (*DeployKey, error) {
key := &DeployKey{
KeyID: keyID,
RepoID: repoID,
}
- has, err := e.Get(key)
+ has, err := db.GetByBean(ctx, key)
if err != nil {
return nil, err
} else if !has {