diff options
author | JakobDev <jakobdev@gmx.de> | 2023-10-14 10:37:24 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-14 08:37:24 +0000 |
commit | 76a85a4ce90fead1eb2b743a42b41617b4592889 (patch) | |
tree | ef172bbbc48c24e0d95cd5c689426bad16205a69 /services | |
parent | ae419fa49403537725c806a5f3f1e5b274f52eb7 (diff) | |
download | gitea-76a85a4ce90fead1eb2b743a42b41617b4592889.tar.gz gitea-76a85a4ce90fead1eb2b743a42b41617b4592889.zip |
Final round of `db.DefaultContext` refactor (#27587)
Last part of #27065
Diffstat (limited to 'services')
-rw-r--r-- | services/actions/notifier.go | 15 | ||||
-rw-r--r-- | services/auth/source.go | 10 | ||||
-rw-r--r-- | services/auth/source/oauth2/init.go | 13 | ||||
-rw-r--r-- | services/auth/source/oauth2/providers.go | 5 | ||||
-rw-r--r-- | services/convert/issue.go | 7 | ||||
-rw-r--r-- | services/externalaccount/user.go | 14 | ||||
-rw-r--r-- | services/migrations/gitea_uploader.go | 2 | ||||
-rw-r--r-- | services/migrations/gitea_uploader_test.go | 2 | ||||
-rw-r--r-- | services/migrations/update.go | 2 | ||||
-rw-r--r-- | services/pull/lfs.go | 8 | ||||
-rw-r--r-- | services/pull/merge.go | 4 | ||||
-rw-r--r-- | services/pull/pull.go | 6 | ||||
-rw-r--r-- | services/webhook/deliver.go | 6 |
13 files changed, 48 insertions, 46 deletions
diff --git a/services/actions/notifier.go b/services/actions/notifier.go index 6dc44143b6..5a71d1cd79 100644 --- a/services/actions/notifier.go +++ b/services/actions/notifier.go @@ -6,7 +6,6 @@ package actions import ( "context" - "code.gitea.io/gitea/models/db" issues_model "code.gitea.io/gitea/models/issues" packages_model "code.gitea.io/gitea/models/packages" perm_model "code.gitea.io/gitea/models/perm" @@ -68,7 +67,7 @@ func (n *actionsNotifier) IssueChangeStatus(ctx context.Context, doer *user_mode // Merge pull request calls issue.changeStatus so we need to handle separately. apiPullRequest := &api.PullRequestPayload{ Index: issue.Index, - PullRequest: convert.ToAPIPullRequest(db.DefaultContext, issue.PullRequest, nil), + PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil), Repository: convert.ToRepo(ctx, issue.Repo, permission), Sender: convert.ToUser(ctx, doer, nil), CommitID: commitID, @@ -296,7 +295,7 @@ func (n *actionsNotifier) PullRequestReview(ctx context.Context, pr *issues_mode WithPayload(&api.PullRequestPayload{ Action: api.HookIssueReviewed, Index: review.Issue.Index, - PullRequest: convert.ToAPIPullRequest(db.DefaultContext, pr, nil), + PullRequest: convert.ToAPIPullRequest(ctx, pr, nil), Repository: convert.ToRepo(ctx, review.Issue.Repo, permission), Sender: convert.ToUser(ctx, review.Reviewer, nil), Review: &api.ReviewPayload{ @@ -320,7 +319,7 @@ func (*actionsNotifier) MergePullRequest(ctx context.Context, doer *user_model.U return } - if err := pr.Issue.LoadRepo(db.DefaultContext); err != nil { + if err := pr.Issue.LoadRepo(ctx); err != nil { log.Error("pr.Issue.LoadRepo: %v", err) return } @@ -334,7 +333,7 @@ func (*actionsNotifier) MergePullRequest(ctx context.Context, doer *user_model.U // Merge pull request calls issue.changeStatus so we need to handle separately. apiPullRequest := &api.PullRequestPayload{ Index: pr.Issue.Index, - PullRequest: convert.ToAPIPullRequest(db.DefaultContext, pr, nil), + PullRequest: convert.ToAPIPullRequest(ctx, pr, nil), Repository: convert.ToRepo(ctx, pr.Issue.Repo, permission), Sender: convert.ToUser(ctx, doer, nil), Action: api.HookIssueClosed, @@ -413,7 +412,7 @@ func (n *actionsNotifier) SyncPushCommits(ctx context.Context, pusher *user_mode ctx = withMethod(ctx, "SyncPushCommits") apiPusher := convert.ToUser(ctx, pusher, nil) - apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(db.DefaultContext, repo.RepoPath(), repo.HTMLURL()) + apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(ctx, repo.RepoPath(), repo.HTMLURL()) if err != nil { log.Error("commits.ToAPIPayloadCommits failed: %v", err) return @@ -484,7 +483,7 @@ func (n *actionsNotifier) PullRequestSynchronized(ctx context.Context, doer *use return } - if err := pr.Issue.LoadRepo(db.DefaultContext); err != nil { + if err := pr.Issue.LoadRepo(ctx); err != nil { log.Error("pr.Issue.LoadRepo: %v", err) return } @@ -509,7 +508,7 @@ func (n *actionsNotifier) PullRequestChangeTargetBranch(ctx context.Context, doe return } - if err := pr.Issue.LoadRepo(db.DefaultContext); err != nil { + if err := pr.Issue.LoadRepo(ctx); err != nil { log.Error("pr.Issue.LoadRepo: %v", err) return } diff --git a/services/auth/source.go b/services/auth/source.go index aae3a78102..69b71a6dea 100644 --- a/services/auth/source.go +++ b/services/auth/source.go @@ -4,14 +4,16 @@ package auth import ( + "context" + "code.gitea.io/gitea/models/auth" "code.gitea.io/gitea/models/db" user_model "code.gitea.io/gitea/models/user" ) // DeleteSource deletes a AuthSource record in DB. -func DeleteSource(source *auth.Source) error { - count, err := db.GetEngine(db.DefaultContext).Count(&user_model.User{LoginSource: source.ID}) +func DeleteSource(ctx context.Context, source *auth.Source) error { + count, err := db.GetEngine(ctx).Count(&user_model.User{LoginSource: source.ID}) if err != nil { return err } else if count > 0 { @@ -20,7 +22,7 @@ func DeleteSource(source *auth.Source) error { } } - count, err = db.GetEngine(db.DefaultContext).Count(&user_model.ExternalLoginUser{LoginSourceID: source.ID}) + count, err = db.GetEngine(ctx).Count(&user_model.ExternalLoginUser{LoginSourceID: source.ID}) if err != nil { return err } else if count > 0 { @@ -35,6 +37,6 @@ func DeleteSource(source *auth.Source) error { } } - _, err = db.GetEngine(db.DefaultContext).ID(source.ID).Delete(new(auth.Source)) + _, err = db.GetEngine(ctx).ID(source.ID).Delete(new(auth.Source)) return err } diff --git a/services/auth/source/oauth2/init.go b/services/auth/source/oauth2/init.go index 32fe545c90..cfaddaa35d 100644 --- a/services/auth/source/oauth2/init.go +++ b/services/auth/source/oauth2/init.go @@ -4,6 +4,7 @@ package oauth2 import ( + "context" "encoding/gob" "net/http" "sync" @@ -26,7 +27,7 @@ const UsersStoreKey = "gitea-oauth2-sessions" const ProviderHeaderKey = "gitea-oauth2-provider" // Init initializes the oauth source -func Init() error { +func Init(ctx context.Context) error { if err := InitSigningKey(); err != nil { return err } @@ -51,18 +52,18 @@ func Init() error { // Unlock our mutex gothRWMutex.Unlock() - return initOAuth2Sources() + return initOAuth2Sources(ctx) } // ResetOAuth2 clears existing OAuth2 providers and loads them from DB -func ResetOAuth2() error { +func ResetOAuth2(ctx context.Context) error { ClearProviders() - return initOAuth2Sources() + return initOAuth2Sources(ctx) } // initOAuth2Sources is used to load and register all active OAuth2 providers -func initOAuth2Sources() error { - authSources, _ := auth.GetActiveOAuth2ProviderSources() +func initOAuth2Sources(ctx context.Context) error { + authSources, _ := auth.GetActiveOAuth2ProviderSources(ctx) for _, source := range authSources { oauth2Source, ok := source.Cfg.(*Source) if !ok { diff --git a/services/auth/source/oauth2/providers.go b/services/auth/source/oauth2/providers.go index e3a0cb0335..cd158614a2 100644 --- a/services/auth/source/oauth2/providers.go +++ b/services/auth/source/oauth2/providers.go @@ -4,6 +4,7 @@ package oauth2 import ( + "context" "errors" "fmt" "html" @@ -97,10 +98,10 @@ func GetOAuth2Providers() []Provider { // GetActiveOAuth2Providers returns the map of configured active OAuth2 providers // key is used as technical name (like in the callbackURL) // values to display -func GetActiveOAuth2Providers() ([]string, map[string]Provider, error) { +func GetActiveOAuth2Providers(ctx context.Context) ([]string, map[string]Provider, error) { // Maybe also separate used and unused providers so we can force the registration of only 1 active provider for each type - authSources, err := auth.GetActiveOAuth2ProviderSources() + authSources, err := auth.GetActiveOAuth2ProviderSources(ctx) if err != nil { return nil, nil, err } diff --git a/services/convert/issue.go b/services/convert/issue.go index 708eac36cf..39d785e108 100644 --- a/services/convert/issue.go +++ b/services/convert/issue.go @@ -9,7 +9,6 @@ import ( "net/url" "strings" - "code.gitea.io/gitea/models/db" issues_model "code.gitea.io/gitea/models/issues" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" @@ -150,7 +149,7 @@ func ToTrackedTime(ctx context.Context, t *issues_model.TrackedTime) (apiT *api. } // ToStopWatches convert Stopwatch list to api.StopWatches -func ToStopWatches(sws []*issues_model.Stopwatch) (api.StopWatches, error) { +func ToStopWatches(ctx context.Context, sws []*issues_model.Stopwatch) (api.StopWatches, error) { result := api.StopWatches(make([]api.StopWatch, 0, len(sws))) issueCache := make(map[int64]*issues_model.Issue) @@ -165,14 +164,14 @@ func ToStopWatches(sws []*issues_model.Stopwatch) (api.StopWatches, error) { for _, sw := range sws { issue, ok = issueCache[sw.IssueID] if !ok { - issue, err = issues_model.GetIssueByID(db.DefaultContext, sw.IssueID) + issue, err = issues_model.GetIssueByID(ctx, sw.IssueID) if err != nil { return nil, err } } repo, ok = repoCache[issue.RepoID] if !ok { - repo, err = repo_model.GetRepositoryByID(db.DefaultContext, issue.RepoID) + repo, err = repo_model.GetRepositoryByID(ctx, issue.RepoID) if err != nil { return nil, err } diff --git a/services/externalaccount/user.go b/services/externalaccount/user.go index a80ba6fee7..e2de41da18 100644 --- a/services/externalaccount/user.go +++ b/services/externalaccount/user.go @@ -16,8 +16,8 @@ import ( "github.com/markbates/goth" ) -func toExternalLoginUser(user *user_model.User, gothUser goth.User) (*user_model.ExternalLoginUser, error) { - authSource, err := auth.GetActiveOAuth2SourceByName(gothUser.Provider) +func toExternalLoginUser(ctx context.Context, user *user_model.User, gothUser goth.User) (*user_model.ExternalLoginUser, error) { + authSource, err := auth.GetActiveOAuth2SourceByName(ctx, gothUser.Provider) if err != nil { return nil, err } @@ -44,12 +44,12 @@ func toExternalLoginUser(user *user_model.User, gothUser goth.User) (*user_model // LinkAccountToUser link the gothUser to the user func LinkAccountToUser(ctx context.Context, user *user_model.User, gothUser goth.User) error { - externalLoginUser, err := toExternalLoginUser(user, gothUser) + externalLoginUser, err := toExternalLoginUser(ctx, user, gothUser) if err != nil { return err } - if err := user_model.LinkExternalToUser(user, externalLoginUser); err != nil { + if err := user_model.LinkExternalToUser(ctx, user, externalLoginUser); err != nil { return err } @@ -71,13 +71,13 @@ func LinkAccountToUser(ctx context.Context, user *user_model.User, gothUser goth } // UpdateExternalUser updates external user's information -func UpdateExternalUser(user *user_model.User, gothUser goth.User) error { - externalLoginUser, err := toExternalLoginUser(user, gothUser) +func UpdateExternalUser(ctx context.Context, user *user_model.User, gothUser goth.User) error { + externalLoginUser, err := toExternalLoginUser(ctx, user, gothUser) if err != nil { return err } - return user_model.UpdateExternalUserByExternalID(externalLoginUser) + return user_model.UpdateExternalUserByExternalID(ctx, externalLoginUser) } // UpdateMigrationsByType updates all migrated repositories' posterid from gitServiceType to replace originalAuthorID to posterID diff --git a/services/migrations/gitea_uploader.go b/services/migrations/gitea_uploader.go index 36b5e15298..ddc2cbd4ec 100644 --- a/services/migrations/gitea_uploader.go +++ b/services/migrations/gitea_uploader.go @@ -989,7 +989,7 @@ func (g *GiteaLocalUploader) remapLocalUser(source user_model.ExternalUserMigrat func (g *GiteaLocalUploader) remapExternalUser(source user_model.ExternalUserMigrated, target user_model.ExternalUserRemappable) (userid int64, err error) { userid, ok := g.userMap[source.GetExternalID()] if !ok { - userid, err = user_model.GetUserIDByExternalUserID(g.gitServiceType.Name(), fmt.Sprintf("%d", source.GetExternalID())) + userid, err = user_model.GetUserIDByExternalUserID(g.ctx, g.gitServiceType.Name(), fmt.Sprintf("%d", source.GetExternalID())) if err != nil { log.Error("GetUserIDByExternalUserID: %v", err) return 0, err diff --git a/services/migrations/gitea_uploader_test.go b/services/migrations/gitea_uploader_test.go index 847e76d9b1..84db83bc67 100644 --- a/services/migrations/gitea_uploader_test.go +++ b/services/migrations/gitea_uploader_test.go @@ -210,7 +210,7 @@ func TestGiteaUploadRemapExternalUser(t *testing.T) { LoginSourceID: 0, Provider: structs.GiteaService.Name(), } - err = user_model.LinkExternalToUser(linkedUser, externalLoginUser) + err = user_model.LinkExternalToUser(db.DefaultContext, linkedUser, externalLoginUser) assert.NoError(t, err) // diff --git a/services/migrations/update.go b/services/migrations/update.go index f14c07cc80..d466832363 100644 --- a/services/migrations/update.go +++ b/services/migrations/update.go @@ -45,7 +45,7 @@ func updateMigrationPosterIDByGitService(ctx context.Context, tp structs.GitServ default: } - users, err := user_model.FindExternalUsersByProvider(user_model.FindExternalUserOptions{ + users, err := user_model.FindExternalUsersByProvider(ctx, user_model.FindExternalUserOptions{ Provider: provider, Start: start, Limit: batchSize, diff --git a/services/pull/lfs.go b/services/pull/lfs.go index 3943372468..2ca82b5633 100644 --- a/services/pull/lfs.go +++ b/services/pull/lfs.go @@ -40,7 +40,7 @@ func LFSPush(ctx context.Context, tmpBasePath, mergeHeadSHA, mergeBaseSHA string // 6. Take the output of cat-file --batch and check if each file in turn // to see if they're pointers to files in the LFS store associated with // the head repo and add them to the base repo if so - go createLFSMetaObjectsFromCatFileBatch(catFileBatchReader, &wg, pr) + go createLFSMetaObjectsFromCatFileBatch(db.DefaultContext, catFileBatchReader, &wg, pr) // 5. Take the shas of the blobs and batch read them go pipeline.CatFileBatch(ctx, shasToBatchReader, catFileBatchWriter, &wg, tmpBasePath) @@ -68,7 +68,7 @@ func LFSPush(ctx context.Context, tmpBasePath, mergeHeadSHA, mergeBaseSHA string return nil } -func createLFSMetaObjectsFromCatFileBatch(catFileBatchReader *io.PipeReader, wg *sync.WaitGroup, pr *issues_model.PullRequest) { +func createLFSMetaObjectsFromCatFileBatch(ctx context.Context, catFileBatchReader *io.PipeReader, wg *sync.WaitGroup, pr *issues_model.PullRequest) { defer wg.Done() defer catFileBatchReader.Close() @@ -116,7 +116,7 @@ func createLFSMetaObjectsFromCatFileBatch(catFileBatchReader *io.PipeReader, wg } // Then we need to check that this pointer is in the db - if _, err := git_model.GetLFSMetaObjectByOid(db.DefaultContext, pr.HeadRepoID, pointer.Oid); err != nil { + if _, err := git_model.GetLFSMetaObjectByOid(ctx, pr.HeadRepoID, pointer.Oid); err != nil { if err == git_model.ErrLFSObjectNotExist { log.Warn("During merge of: %d in %-v, there is a pointer to LFS Oid: %s which although present in the LFS store is not associated with the head repo %-v", pr.Index, pr.BaseRepo, pointer.Oid, pr.HeadRepo) continue @@ -129,7 +129,7 @@ func createLFSMetaObjectsFromCatFileBatch(catFileBatchReader *io.PipeReader, wg // Therefore it should be associated with the base repo meta := &git_model.LFSMetaObject{Pointer: pointer} meta.RepositoryID = pr.BaseRepoID - if _, err := git_model.NewLFSMetaObject(db.DefaultContext, meta); err != nil { + if _, err := git_model.NewLFSMetaObject(ctx, meta); err != nil { _ = catFileBatchReader.CloseWithError(err) break } diff --git a/services/pull/merge.go b/services/pull/merge.go index 185060cbac..e6f02e6e02 100644 --- a/services/pull/merge.go +++ b/services/pull/merge.go @@ -464,11 +464,11 @@ func CheckPullBranchProtections(ctx context.Context, pr *issues_model.PullReques } // MergedManually mark pr as merged manually -func MergedManually(pr *issues_model.PullRequest, doer *user_model.User, baseGitRepo *git.Repository, commitID string) error { +func MergedManually(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.User, baseGitRepo *git.Repository, commitID string) error { pullWorkingPool.CheckIn(fmt.Sprint(pr.ID)) defer pullWorkingPool.CheckOut(fmt.Sprint(pr.ID)) - if err := db.WithTx(db.DefaultContext, func(ctx context.Context) error { + if err := db.WithTx(ctx, func(ctx context.Context) error { if err := pr.LoadBaseRepo(ctx); err != nil { return err } diff --git a/services/pull/pull.go b/services/pull/pull.go index e4096f8ac4..2f5143903a 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -811,7 +811,7 @@ func GetIssuesAllCommitStatus(ctx context.Context, issues issues_model.IssueList gitRepos[issue.RepoID] = gitRepo } - statuses, lastStatus, err := getAllCommitStatus(gitRepo, issue.PullRequest) + statuses, lastStatus, err := getAllCommitStatus(ctx, gitRepo, issue.PullRequest) if err != nil { log.Error("getAllCommitStatus: cant get commit statuses of pull [%d]: %v", issue.PullRequest.ID, err) continue @@ -823,13 +823,13 @@ func GetIssuesAllCommitStatus(ctx context.Context, issues issues_model.IssueList } // getAllCommitStatus get pr's commit statuses. -func getAllCommitStatus(gitRepo *git.Repository, pr *issues_model.PullRequest) (statuses []*git_model.CommitStatus, lastStatus *git_model.CommitStatus, err error) { +func getAllCommitStatus(ctx context.Context, gitRepo *git.Repository, pr *issues_model.PullRequest) (statuses []*git_model.CommitStatus, lastStatus *git_model.CommitStatus, err error) { sha, shaErr := gitRepo.GetRefCommitID(pr.GetGitRefName()) if shaErr != nil { return nil, nil, shaErr } - statuses, _, err = git_model.GetLatestCommitStatus(db.DefaultContext, pr.BaseRepo.ID, sha, db.ListOptions{ListAll: true}) + statuses, _, err = git_model.GetLatestCommitStatus(ctx, pr.BaseRepo.ID, sha, db.ListOptions{ListAll: true}) lastStatus = git_model.CalcCommitStatus(statuses) return statuses, lastStatus, err } diff --git a/services/webhook/deliver.go b/services/webhook/deliver.go index fd7a3d7fba..19c34772ca 100644 --- a/services/webhook/deliver.go +++ b/services/webhook/deliver.go @@ -34,7 +34,7 @@ import ( // Deliver deliver hook task func Deliver(ctx context.Context, t *webhook_model.HookTask) error { - w, err := webhook_model.GetWebhookByID(t.HookID) + w, err := webhook_model.GetWebhookByID(ctx, t.HookID) if err != nil { return err } @@ -185,7 +185,7 @@ func Deliver(ctx context.Context, t *webhook_model.HookTask) error { log.Trace("Hook delivery failed: %s", t.UUID) } - if err := webhook_model.UpdateHookTask(t); err != nil { + if err := webhook_model.UpdateHookTask(ctx, t); err != nil { log.Error("UpdateHookTask [%d]: %v", t.ID, err) } @@ -195,7 +195,7 @@ func Deliver(ctx context.Context, t *webhook_model.HookTask) error { } else { w.LastStatus = webhook_module.HookStatusFail } - if err = webhook_model.UpdateWebhookLastStatus(w); err != nil { + if err = webhook_model.UpdateWebhookLastStatus(ctx, w); err != nil { log.Error("UpdateWebhookLastStatus: %v", err) return } |