diff options
author | 6543 <6543@obermui.de> | 2021-08-12 14:43:08 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-12 14:43:08 +0200 |
commit | 2289580bb7ef8dfa4124c2b3bfb89897dbb35f46 (patch) | |
tree | e68aae604bb3d738b44156504a1415adaf042c68 /models/ssh_key_deploy.go | |
parent | ca13e1d56c561f72cf8ad251fe61b1898abfec51 (diff) | |
download | gitea-2289580bb7ef8dfa4124c2b3bfb89897dbb35f46.tar.gz gitea-2289580bb7ef8dfa4124c2b3bfb89897dbb35f46.zip |
[API] generalize list header (#16551)
* Add info about list endpoints to CONTRIBUTING.md
* Let all list endpoints return X-Total-Count header
* Add TODOs for GetCombinedCommitStatusByRef
* Fix models/issue_stopwatch.go
* Rrefactor models.ListDeployKeys
* Introduce helper func and use them for SetLinkHeader related func
Diffstat (limited to 'models/ssh_key_deploy.go')
-rw-r--r-- | models/ssh_key_deploy.go | 56 |
1 files changed, 34 insertions, 22 deletions
diff --git a/models/ssh_key_deploy.go b/models/ssh_key_deploy.go index 3189bcf456..e7d486b9f5 100644 --- a/models/ssh_key_deploy.go +++ b/models/ssh_key_deploy.go @@ -264,17 +264,40 @@ func deleteDeployKey(sess Engine, doer *User, id int64) error { return nil } -// ListDeployKeys returns all deploy keys by given repository ID. -func ListDeployKeys(repoID int64, listOptions ListOptions) ([]*DeployKey, error) { - return listDeployKeys(x, repoID, listOptions) +// ListDeployKeysOptions are options for ListDeployKeys +type ListDeployKeysOptions struct { + ListOptions + RepoID int64 + KeyID int64 + Fingerprint string +} + +func (opt ListDeployKeysOptions) toCond() builder.Cond { + cond := builder.NewCond() + if opt.RepoID != 0 { + cond = cond.And(builder.Eq{"repo_id": opt.RepoID}) + } + if opt.KeyID != 0 { + cond = cond.And(builder.Eq{"key_id": opt.KeyID}) + } + if opt.Fingerprint != "" { + cond = cond.And(builder.Eq{"fingerprint": opt.Fingerprint}) + } + return cond } -func listDeployKeys(e Engine, repoID int64, listOptions ListOptions) ([]*DeployKey, error) { - sess := e.Where("repo_id = ?", repoID) - if listOptions.Page != 0 { - sess = listOptions.setSessionPagination(sess) +// ListDeployKeys returns a list of deploy keys matching the provided arguments. +func ListDeployKeys(opts *ListDeployKeysOptions) ([]*DeployKey, error) { + return listDeployKeys(x, opts) +} - keys := make([]*DeployKey, 0, listOptions.PageSize) +func listDeployKeys(e Engine, opts *ListDeployKeysOptions) ([]*DeployKey, error) { + sess := e.Where(opts.toCond()) + + if opts.Page != 0 { + sess = opts.setSessionPagination(sess) + + keys := make([]*DeployKey, 0, opts.PageSize) return keys, sess.Find(&keys) } @@ -282,18 +305,7 @@ func listDeployKeys(e Engine, repoID int64, listOptions ListOptions) ([]*DeployK return keys, sess.Find(&keys) } -// SearchDeployKeys returns a list of deploy keys matching the provided arguments. -func SearchDeployKeys(repoID, keyID int64, fingerprint string) ([]*DeployKey, error) { - keys := make([]*DeployKey, 0, 5) - cond := builder.NewCond() - if repoID != 0 { - cond = cond.And(builder.Eq{"repo_id": repoID}) - } - if keyID != 0 { - cond = cond.And(builder.Eq{"key_id": keyID}) - } - if fingerprint != "" { - cond = cond.And(builder.Eq{"fingerprint": fingerprint}) - } - return keys, x.Where(cond).Find(&keys) +// CountDeployKeys returns count deploy keys matching the provided arguments. +func CountDeployKeys(opts *ListDeployKeysOptions) (int64, error) { + return x.Where(opts.toCond()).Count(&DeployKey{}) } |