diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-09-19 19:49:59 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-19 19:49:59 +0800 |
commit | a4bfef265d9e512830350635a0489c2cdcd6508f (patch) | |
tree | 1e3c2ec94276dfcb2f8ba73a2ac075ba39c4a34a /models/gpg_key_add.go | |
parent | 462306e263db5a809dbe2cdf62e99307aeff28de (diff) | |
download | gitea-a4bfef265d9e512830350635a0489c2cdcd6508f.tar.gz gitea-a4bfef265d9e512830350635a0489c2cdcd6508f.zip |
Move db related basic functions to models/db (#17075)
* Move db related basic functions to models/db
* Fix lint
* Fix lint
* Fix test
* Fix lint
* Fix lint
* revert unnecessary change
* Fix test
* Fix wrong replace string
* Use *Context
* Correct committer spelling and fix wrong replaced words
Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'models/gpg_key_add.go')
-rw-r--r-- | models/gpg_key_add.go | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/models/gpg_key_add.go b/models/gpg_key_add.go index 1e589e7fee..91a30120a6 100644 --- a/models/gpg_key_add.go +++ b/models/gpg_key_add.go @@ -7,6 +7,7 @@ package models import ( "strings" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/log" "github.com/keybase/go-crypto/openpgp" @@ -28,7 +29,7 @@ import ( // This file contains functions relating to adding GPG Keys // addGPGKey add key, import and subkeys to database -func addGPGKey(e Engine, key *GPGKey, content string) (err error) { +func addGPGKey(e db.Engine, key *GPGKey, content string) (err error) { // Add GPGKeyImport if _, err = e.Insert(GPGKeyImport{ KeyID: key.KeyID, @@ -50,7 +51,7 @@ func addGPGKey(e Engine, key *GPGKey, content string) (err error) { } // addGPGSubKey add subkeys to database -func addGPGSubKey(e Engine, key *GPGKey) (err error) { +func addGPGSubKey(e db.Engine, key *GPGKey) (err error) { // Save GPG primary key. if _, err = e.Insert(key); err != nil { return err @@ -71,11 +72,12 @@ func AddGPGKey(ownerID int64, content, token, signature string) ([]*GPGKey, erro return nil, err } - sess := x.NewSession() - defer sess.Close() - if err = sess.Begin(); err != nil { + ctx, committer, err := db.TxContext() + if err != nil { return nil, err } + defer committer.Close() + keys := make([]*GPGKey, 0, len(ekeys)) verified := false @@ -101,7 +103,7 @@ func AddGPGKey(ownerID int64, content, token, signature string) ([]*GPGKey, erro for _, ekey := range ekeys { // Key ID cannot be duplicated. - has, err := sess.Where("key_id=?", ekey.PrimaryKey.KeyIdString()). + has, err := ctx.Engine().Where("key_id=?", ekey.PrimaryKey.KeyIdString()). Get(new(GPGKey)) if err != nil { return nil, err @@ -116,10 +118,10 @@ func AddGPGKey(ownerID int64, content, token, signature string) ([]*GPGKey, erro return nil, err } - if err = addGPGKey(sess, key, content); err != nil { + if err = addGPGKey(ctx.Engine(), key, content); err != nil { return nil, err } keys = append(keys, key) } - return keys, sess.Commit() + return keys, committer.Commit() } |