summaryrefslogtreecommitdiffstats
path: root/services
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2023-11-24 11:49:41 +0800
committerGitHub <noreply@github.com>2023-11-24 03:49:41 +0000
commitdf1e7d0067bb39913eb681ccc920649884fb1938 (patch)
tree2419feab5c28658adb7f71878df646bdc9bdc50e /services
parentd24a8223ce1e47a0c9b103aae07f67c3112ca048 (diff)
downloadgitea-df1e7d0067bb39913eb681ccc920649884fb1938.tar.gz
gitea-df1e7d0067bb39913eb681ccc920649884fb1938.zip
Use db.Find instead of writing methods for every object (#28084)
For those simple objects, it's unnecessary to write the find and count methods again and again.
Diffstat (limited to 'services')
-rw-r--r--services/actions/clear_tasks.go4
-rw-r--r--services/actions/job_emitter.go2
-rw-r--r--services/actions/notifier_helper.go7
-rw-r--r--services/asymkey/ssh_key_test.go5
-rw-r--r--services/auth/httpsign.go5
-rw-r--r--services/auth/signin.go2
-rw-r--r--services/auth/source/oauth2/init.go3
-rw-r--r--services/auth/source/oauth2/providers.go3
-rw-r--r--services/auth/sspi.go3
-rw-r--r--services/auth/sync.go2
-rw-r--r--services/migrations/update.go13
-rw-r--r--services/pull/review.go6
-rw-r--r--services/repository/delete.go6
-rw-r--r--services/repository/hooks.go2
-rw-r--r--services/secrets/secrets.go6
-rw-r--r--services/user/user.go2
-rw-r--r--services/webhook/webhook.go5
17 files changed, 44 insertions, 32 deletions
diff --git a/services/actions/clear_tasks.go b/services/actions/clear_tasks.go
index 7c7043c42f..67373782d5 100644
--- a/services/actions/clear_tasks.go
+++ b/services/actions/clear_tasks.go
@@ -33,7 +33,7 @@ func StopEndlessTasks(ctx context.Context) error {
}
func stopTasks(ctx context.Context, opts actions_model.FindTaskOptions) error {
- tasks, err := actions_model.FindTasks(ctx, opts)
+ tasks, err := db.Find[actions_model.ActionTask](ctx, opts)
if err != nil {
return fmt.Errorf("find tasks: %w", err)
}
@@ -74,7 +74,7 @@ func stopTasks(ctx context.Context, opts actions_model.FindTaskOptions) error {
// CancelAbandonedJobs cancels the jobs which have waiting status, but haven't been picked by a runner for a long time
func CancelAbandonedJobs(ctx context.Context) error {
- jobs, _, err := actions_model.FindRunJobs(ctx, actions_model.FindRunJobOptions{
+ jobs, err := db.Find[actions_model.ActionRunJob](ctx, actions_model.FindRunJobOptions{
Statuses: []actions_model.Status{actions_model.StatusWaiting, actions_model.StatusBlocked},
UpdatedBefore: timeutil.TimeStamp(time.Now().Add(-setting.Actions.AbandonedJobTimeout).Unix()),
})
diff --git a/services/actions/job_emitter.go b/services/actions/job_emitter.go
index f7ec615364..fe39312386 100644
--- a/services/actions/job_emitter.go
+++ b/services/actions/job_emitter.go
@@ -44,7 +44,7 @@ func jobEmitterQueueHandler(items ...*jobUpdate) []*jobUpdate {
}
func checkJobsOfRun(ctx context.Context, runID int64) error {
- jobs, _, err := actions_model.FindRunJobs(ctx, actions_model.FindRunJobOptions{RunID: runID})
+ jobs, err := db.Find[actions_model.ActionRunJob](ctx, actions_model.FindRunJobOptions{RunID: runID})
if err != nil {
return err
}
diff --git a/services/actions/notifier_helper.go b/services/actions/notifier_helper.go
index 1c08cec007..175b8a4118 100644
--- a/services/actions/notifier_helper.go
+++ b/services/actions/notifier_helper.go
@@ -11,6 +11,7 @@ import (
"strings"
actions_model "code.gitea.io/gitea/models/actions"
+ "code.gitea.io/gitea/models/db"
issues_model "code.gitea.io/gitea/models/issues"
packages_model "code.gitea.io/gitea/models/packages"
access_model "code.gitea.io/gitea/models/perm/access"
@@ -298,7 +299,7 @@ func handleWorkflows(
continue
}
- alljobs, _, err := actions_model.FindRunJobs(ctx, actions_model.FindRunJobOptions{RunID: run.ID})
+ alljobs, err := db.Find[actions_model.ActionRunJob](ctx, actions_model.FindRunJobOptions{RunID: run.ID})
if err != nil {
log.Error("FindRunJobs: %v", err)
continue
@@ -377,7 +378,7 @@ func ifNeedApproval(ctx context.Context, run *actions_model.ActionRun, repo *rep
}
// don't need approval if the user has been approved before
- if count, err := actions_model.CountRuns(ctx, actions_model.FindRunOptions{
+ if count, err := db.Count[actions_model.ActionRun](ctx, actions_model.FindRunOptions{
RepoID: repo.ID,
TriggerUserID: user.ID,
Approved: true,
@@ -408,7 +409,7 @@ func handleSchedules(
return nil
}
- if count, err := actions_model.CountSchedules(ctx, actions_model.FindScheduleOptions{RepoID: input.Repo.ID}); err != nil {
+ if count, err := db.Count[actions_model.ActionSchedule](ctx, actions_model.FindScheduleOptions{RepoID: input.Repo.ID}); err != nil {
log.Error("CountSchedules: %v", err)
return err
} else if count > 0 {
diff --git a/services/asymkey/ssh_key_test.go b/services/asymkey/ssh_key_test.go
index 3a39a9a1db..fbd5d13ab2 100644
--- a/services/asymkey/ssh_key_test.go
+++ b/services/asymkey/ssh_key_test.go
@@ -67,7 +67,10 @@ ssh-dss AAAAB3NzaC1kc3MAAACBAOChCC7lf6Uo9n7BmZ6M8St19PZf4Tn59NriyboW2x/DZuYAz3ib
for i, kase := range testCases {
s.ID = int64(i) + 20
asymkey_model.AddPublicKeysBySource(db.DefaultContext, user, s, []string{kase.keyString})
- keys, err := asymkey_model.ListPublicKeysBySource(db.DefaultContext, user.ID, s.ID)
+ keys, err := db.Find[asymkey_model.PublicKey](db.DefaultContext, asymkey_model.FindPublicKeyOptions{
+ OwnerID: user.ID,
+ LoginSourceID: s.ID,
+ })
assert.NoError(t, err)
if err != nil {
continue
diff --git a/services/auth/httpsign.go b/services/auth/httpsign.go
index d5c8ea33aa..b604349f80 100644
--- a/services/auth/httpsign.go
+++ b/services/auth/httpsign.go
@@ -12,6 +12,7 @@ import (
"strings"
asymkey_model "code.gitea.io/gitea/models/asymkey"
+ "code.gitea.io/gitea/models/db"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
@@ -92,7 +93,9 @@ func VerifyPubKey(r *http.Request) (*asymkey_model.PublicKey, error) {
keyID := verifier.KeyId()
- publicKeys, err := asymkey_model.SearchPublicKey(r.Context(), 0, keyID)
+ publicKeys, err := db.Find[asymkey_model.PublicKey](r.Context(), asymkey_model.FindPublicKeyOptions{
+ Fingerprint: keyID,
+ })
if err != nil {
return nil, err
}
diff --git a/services/auth/signin.go b/services/auth/signin.go
index 2e53453681..fafe3ef3c6 100644
--- a/services/auth/signin.go
+++ b/services/auth/signin.go
@@ -86,7 +86,7 @@ func UserSignIn(ctx context.Context, username, password string) (*user_model.Use
}
}
- sources, err := auth.FindSources(ctx, auth.FindSourcesOptions{
+ sources, err := db.Find[auth.Source](ctx, auth.FindSourcesOptions{
IsActive: util.OptionalBoolTrue,
})
if err != nil {
diff --git a/services/auth/source/oauth2/init.go b/services/auth/source/oauth2/init.go
index 0ebbdaebd4..3ad6e307f1 100644
--- a/services/auth/source/oauth2/init.go
+++ b/services/auth/source/oauth2/init.go
@@ -10,6 +10,7 @@ import (
"sync"
"code.gitea.io/gitea/models/auth"
+ "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
@@ -64,7 +65,7 @@ func ResetOAuth2(ctx context.Context) error {
// initOAuth2Sources is used to load and register all active OAuth2 providers
func initOAuth2Sources(ctx context.Context) error {
- authSources, err := auth.FindSources(ctx, auth.FindSourcesOptions{
+ authSources, err := db.Find[auth.Source](ctx, auth.FindSourcesOptions{
IsActive: util.OptionalBoolTrue,
LoginType: auth.OAuth2,
})
diff --git a/services/auth/source/oauth2/providers.go b/services/auth/source/oauth2/providers.go
index 3b45b252f7..f4edb507f2 100644
--- a/services/auth/source/oauth2/providers.go
+++ b/services/auth/source/oauth2/providers.go
@@ -13,6 +13,7 @@ import (
"sort"
"code.gitea.io/gitea/models/auth"
+ "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
@@ -107,7 +108,7 @@ func CreateProviderFromSource(source *auth.Source) (Provider, error) {
// GetOAuth2Providers returns the list of configured OAuth2 providers
func GetOAuth2Providers(ctx context.Context, isActive util.OptionalBool) ([]Provider, error) {
- authSources, err := auth.FindSources(ctx, auth.FindSourcesOptions{
+ authSources, err := db.Find[auth.Source](ctx, auth.FindSourcesOptions{
IsActive: isActive,
LoginType: auth.OAuth2,
})
diff --git a/services/auth/sspi.go b/services/auth/sspi.go
index bc8ec948f2..57ba0462c5 100644
--- a/services/auth/sspi.go
+++ b/services/auth/sspi.go
@@ -12,6 +12,7 @@ import (
"code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/avatars"
+ "code.gitea.io/gitea/models/db"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/base"
gitea_context "code.gitea.io/gitea/modules/context"
@@ -130,7 +131,7 @@ func (s *SSPI) Verify(req *http.Request, w http.ResponseWriter, store DataStore,
// getConfig retrieves the SSPI configuration from login sources
func (s *SSPI) getConfig(ctx context.Context) (*sspi.Source, error) {
- sources, err := auth.FindSources(ctx, auth.FindSourcesOptions{
+ sources, err := db.Find[auth.Source](ctx, auth.FindSourcesOptions{
IsActive: util.OptionalBoolTrue,
LoginType: auth.SSPI,
})
diff --git a/services/auth/sync.go b/services/auth/sync.go
index 11a59d41ae..7562ac812b 100644
--- a/services/auth/sync.go
+++ b/services/auth/sync.go
@@ -15,7 +15,7 @@ import (
func SyncExternalUsers(ctx context.Context, updateExisting bool) error {
log.Trace("Doing: SyncExternalUsers")
- ls, err := auth.FindSources(ctx, auth.FindSourcesOptions{})
+ ls, err := db.Find[auth.Source](ctx, auth.FindSourcesOptions{})
if err != nil {
log.Error("SyncExternalUsers: %v", err)
return err
diff --git a/services/migrations/update.go b/services/migrations/update.go
index d466832363..4a49206f82 100644
--- a/services/migrations/update.go
+++ b/services/migrations/update.go
@@ -36,8 +36,7 @@ func updateMigrationPosterIDByGitService(ctx context.Context, tp structs.GitServ
}
const batchSize = 100
- var start int
- for {
+ for page := 0; ; page++ {
select {
case <-ctx.Done():
log.Warn("UpdateMigrationPosterIDByGitService(%s) cancelled", tp.Name())
@@ -45,10 +44,13 @@ func updateMigrationPosterIDByGitService(ctx context.Context, tp structs.GitServ
default:
}
- users, err := user_model.FindExternalUsersByProvider(ctx, user_model.FindExternalUserOptions{
+ users, err := db.Find[user_model.ExternalLoginUser](ctx, user_model.FindExternalUserOptions{
+ ListOptions: db.ListOptions{
+ PageSize: batchSize,
+ Page: page,
+ },
Provider: provider,
- Start: start,
- Limit: batchSize,
+ OrderBy: "login_source_id ASC, external_id ASC",
})
if err != nil {
return err
@@ -70,7 +72,6 @@ func updateMigrationPosterIDByGitService(ctx context.Context, tp structs.GitServ
if len(users) < batchSize {
break
}
- start += len(users)
}
return nil
}
diff --git a/services/pull/review.go b/services/pull/review.go
index 3f5644b0cd..e48f380154 100644
--- a/services/pull/review.go
+++ b/services/pull/review.go
@@ -49,16 +49,16 @@ func InvalidateCodeComments(ctx context.Context, prs issues_model.PullRequestLis
return nil
}
issueIDs := prs.GetIssueIDs()
- var codeComments []*issues_model.Comment
- if err := db.Find(ctx, &issues_model.FindCommentsOptions{
+ codeComments, err := db.Find[issues_model.Comment](ctx, issues_model.FindCommentsOptions{
ListOptions: db.ListOptions{
ListAll: true,
},
Type: issues_model.CommentTypeCode,
Invalidated: util.OptionalBoolFalse,
IssueIDs: issueIDs,
- }, &codeComments); err != nil {
+ })
+ if err != nil {
return fmt.Errorf("find code comments: %v", err)
}
for _, comment := range codeComments {
diff --git a/services/repository/delete.go b/services/repository/delete.go
index 861dfa2dcd..08d6800ee7 100644
--- a/services/repository/delete.go
+++ b/services/repository/delete.go
@@ -54,13 +54,13 @@ func DeleteRepositoryDirectly(ctx context.Context, doer *user_model.User, repoID
}
// Query the action tasks of this repo, they will be needed after they have been deleted to remove the logs
- tasks, err := actions_model.FindTasks(ctx, actions_model.FindTaskOptions{RepoID: repoID})
+ tasks, err := db.Find[actions_model.ActionTask](ctx, actions_model.FindTaskOptions{RepoID: repoID})
if err != nil {
return fmt.Errorf("find actions tasks of repo %v: %w", repoID, err)
}
// Query the artifacts of this repo, they will be needed after they have been deleted to remove artifacts files in ObjectStorage
- artifacts, err := actions_model.ListArtifactsByRepoID(ctx, repoID)
+ artifacts, err := db.Find[actions_model.ActionArtifact](ctx, actions_model.FindArtifactsOptions{RepoID: repoID})
if err != nil {
return fmt.Errorf("list actions artifacts of repo %v: %w", repoID, err)
}
@@ -75,7 +75,7 @@ func DeleteRepositoryDirectly(ctx context.Context, doer *user_model.User, repoID
}
// Delete Deploy Keys
- deployKeys, err := asymkey_model.ListDeployKeys(ctx, &asymkey_model.ListDeployKeysOptions{RepoID: repoID})
+ deployKeys, err := db.Find[asymkey_model.DeployKey](ctx, asymkey_model.ListDeployKeysOptions{RepoID: repoID})
if err != nil {
return fmt.Errorf("listDeployKeys: %w", err)
}
diff --git a/services/repository/hooks.go b/services/repository/hooks.go
index 8506fa3413..7b82f36b43 100644
--- a/services/repository/hooks.go
+++ b/services/repository/hooks.go
@@ -85,7 +85,7 @@ func GenerateGitHooks(ctx context.Context, templateRepo, generateRepo *repo_mode
// GenerateWebhooks generates webhooks from a template repository
func GenerateWebhooks(ctx context.Context, templateRepo, generateRepo *repo_model.Repository) error {
- templateWebhooks, err := webhook.ListWebhooksByOpts(ctx, &webhook.ListWebhookOptions{RepoID: templateRepo.ID})
+ templateWebhooks, err := db.Find[webhook.Webhook](ctx, webhook.ListWebhookOptions{RepoID: templateRepo.ID})
if err != nil {
return err
}
diff --git a/services/secrets/secrets.go b/services/secrets/secrets.go
index 1c4772d6bf..97e15ba6c7 100644
--- a/services/secrets/secrets.go
+++ b/services/secrets/secrets.go
@@ -15,7 +15,7 @@ func CreateOrUpdateSecret(ctx context.Context, ownerID, repoID int64, name, data
return nil, false, err
}
- s, err := secret_model.FindSecrets(ctx, secret_model.FindSecretsOptions{
+ s, err := db.Find[secret_model.Secret](ctx, secret_model.FindSecretsOptions{
OwnerID: ownerID,
RepoID: repoID,
Name: name,
@@ -40,7 +40,7 @@ func CreateOrUpdateSecret(ctx context.Context, ownerID, repoID int64, name, data
}
func DeleteSecretByID(ctx context.Context, ownerID, repoID, secretID int64) error {
- s, err := secret_model.FindSecrets(ctx, secret_model.FindSecretsOptions{
+ s, err := db.Find[secret_model.Secret](ctx, secret_model.FindSecretsOptions{
OwnerID: ownerID,
RepoID: repoID,
SecretID: secretID,
@@ -60,7 +60,7 @@ func DeleteSecretByName(ctx context.Context, ownerID, repoID int64, name string)
return err
}
- s, err := secret_model.FindSecrets(ctx, secret_model.FindSecretsOptions{
+ s, err := db.Find[secret_model.Secret](ctx, secret_model.FindSecretsOptions{
OwnerID: ownerID,
RepoID: repoID,
Name: name,
diff --git a/services/user/user.go b/services/user/user.go
index 4a4908fe8e..932e359c9f 100644
--- a/services/user/user.go
+++ b/services/user/user.go
@@ -172,7 +172,7 @@ func DeleteUser(ctx context.Context, u *user_model.User, purge bool) error {
// An alternative option here would be write a function which would delete all organizations but it seems
// but such a function would likely get out of date
for {
- orgs, err := organization.FindOrgs(ctx, organization.FindOrgOptions{
+ orgs, err := db.Find[organization.Organization](ctx, organization.FindOrgOptions{
ListOptions: db.ListOptions{
PageSize: repo_model.RepositoryListDefaultPageSize,
Page: 1,
diff --git a/services/webhook/webhook.go b/services/webhook/webhook.go
index 9d5dab85f7..ac18da3525 100644
--- a/services/webhook/webhook.go
+++ b/services/webhook/webhook.go
@@ -9,6 +9,7 @@ import (
"fmt"
"strings"
+ "code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
webhook_model "code.gitea.io/gitea/models/webhook"
@@ -222,7 +223,7 @@ func PrepareWebhooks(ctx context.Context, source EventSource, event webhook_modu
var ws []*webhook_model.Webhook
if source.Repository != nil {
- repoHooks, err := webhook_model.ListWebhooksByOpts(ctx, &webhook_model.ListWebhookOptions{
+ repoHooks, err := db.Find[webhook_model.Webhook](ctx, webhook_model.ListWebhookOptions{
RepoID: source.Repository.ID,
IsActive: util.OptionalBoolTrue,
})
@@ -236,7 +237,7 @@ func PrepareWebhooks(ctx context.Context, source EventSource, event webhook_modu
// append additional webhooks of a user or organization
if owner != nil {
- ownerHooks, err := webhook_model.ListWebhooksByOpts(ctx, &webhook_model.ListWebhookOptions{
+ ownerHooks, err := db.Find[webhook_model.Webhook](ctx, webhook_model.ListWebhookOptions{
OwnerID: owner.ID,
IsActive: util.OptionalBoolTrue,
})