loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Final round of `db.DefaultContext` refactor (#27587)

Last part of #27065

authored by

JakobDev and committed by
GitHub
76a85a4c ae419fa4

+250 -242
+1 -1
cmd/admin_auth.go
··· 105 105 return err 106 106 } 107 107 108 - return auth_service.DeleteSource(source) 108 + return auth_service.DeleteSource(ctx, source) 109 109 }
+2 -2
models/asymkey/gpg_key.go
··· 111 111 } 112 112 113 113 // GPGKeyToEntity retrieve the imported key and the traducted entity 114 - func GPGKeyToEntity(k *GPGKey) (*openpgp.Entity, error) { 115 - impKey, err := GetGPGImportByKeyID(k.KeyID) 114 + func GPGKeyToEntity(ctx context.Context, k *GPGKey) (*openpgp.Entity, error) { 115 + impKey, err := GetGPGImportByKeyID(ctx, k.KeyID) 116 116 if err != nil { 117 117 return nil, err 118 118 }
+7 -3
models/asymkey/gpg_key_import.go
··· 3 3 4 4 package asymkey 5 5 6 - import "code.gitea.io/gitea/models/db" 6 + import ( 7 + "context" 8 + 9 + "code.gitea.io/gitea/models/db" 10 + ) 7 11 8 12 // __________________ ________ ____ __. 9 13 // / _____/\______ \/ _____/ | |/ _|____ ___.__. ··· 31 35 } 32 36 33 37 // GetGPGImportByKeyID returns the import public armored key by given KeyID. 34 - func GetGPGImportByKeyID(keyID string) (*GPGKeyImport, error) { 38 + func GetGPGImportByKeyID(ctx context.Context, keyID string) (*GPGKeyImport, error) { 35 39 key := new(GPGKeyImport) 36 - has, err := db.GetEngine(db.DefaultContext).ID(keyID).Get(key) 40 + has, err := db.GetEngine(ctx).ID(keyID).Get(key) 37 41 if err != nil { 38 42 return nil, err 39 43 } else if !has {
+3 -2
models/asymkey/gpg_key_verify.go
··· 4 4 package asymkey 5 5 6 6 import ( 7 + "context" 7 8 "strconv" 8 9 "time" 9 10 ··· 29 30 // This file provides functions relating verifying gpg keys 30 31 31 32 // VerifyGPGKey marks a GPG key as verified 32 - func VerifyGPGKey(ownerID int64, keyID, token, signature string) (string, error) { 33 - ctx, committer, err := db.TxContext(db.DefaultContext) 33 + func VerifyGPGKey(ctx context.Context, ownerID int64, keyID, token, signature string) (string, error) { 34 + ctx, committer, err := db.TxContext(ctx) 34 35 if err != nil { 35 36 return "", err 36 37 }
+8 -8
models/asymkey/ssh_key_deploy.go
··· 106 106 } 107 107 108 108 // HasDeployKey returns true if public key is a deploy key of given repository. 109 - func HasDeployKey(keyID, repoID int64) bool { 110 - has, _ := db.GetEngine(db.DefaultContext). 109 + func HasDeployKey(ctx context.Context, keyID, repoID int64) bool { 110 + has, _ := db.GetEngine(ctx). 111 111 Where("key_id = ? AND repo_id = ?", keyID, repoID). 112 112 Get(new(DeployKey)) 113 113 return has 114 114 } 115 115 116 116 // AddDeployKey add new deploy key to database and authorized_keys file. 117 - func AddDeployKey(repoID int64, name, content string, readOnly bool) (*DeployKey, error) { 117 + func AddDeployKey(ctx context.Context, repoID int64, name, content string, readOnly bool) (*DeployKey, error) { 118 118 fingerprint, err := CalcFingerprint(content) 119 119 if err != nil { 120 120 return nil, err ··· 125 125 accessMode = perm.AccessModeWrite 126 126 } 127 127 128 - ctx, committer, err := db.TxContext(db.DefaultContext) 128 + ctx, committer, err := db.TxContext(ctx) 129 129 if err != nil { 130 130 return nil, err 131 131 } ··· 197 197 } 198 198 199 199 // UpdateDeployKeyCols updates deploy key information in the specified columns. 200 - func UpdateDeployKeyCols(key *DeployKey, cols ...string) error { 201 - _, err := db.GetEngine(db.DefaultContext).ID(key.ID).Cols(cols...).Update(key) 200 + func UpdateDeployKeyCols(ctx context.Context, key *DeployKey, cols ...string) error { 201 + _, err := db.GetEngine(ctx).ID(key.ID).Cols(cols...).Update(key) 202 202 return err 203 203 } 204 204 ··· 240 240 } 241 241 242 242 // CountDeployKeys returns count deploy keys matching the provided arguments. 243 - func CountDeployKeys(opts *ListDeployKeysOptions) (int64, error) { 244 - return db.GetEngine(db.DefaultContext).Where(opts.toCond()).Count(&DeployKey{}) 243 + func CountDeployKeys(ctx context.Context, opts *ListDeployKeysOptions) (int64, error) { 244 + return db.GetEngine(ctx).Where(opts.toCond()).Count(&DeployKey{}) 245 245 }
+7 -7
models/asymkey/ssh_key_principals.go
··· 25 25 // This file contains functions related to principals 26 26 27 27 // AddPrincipalKey adds new principal to database and authorized_principals file. 28 - func AddPrincipalKey(ownerID int64, content string, authSourceID int64) (*PublicKey, error) { 29 - ctx, committer, err := db.TxContext(db.DefaultContext) 28 + func AddPrincipalKey(ctx context.Context, ownerID int64, content string, authSourceID int64) (*PublicKey, error) { 29 + dbCtx, committer, err := db.TxContext(ctx) 30 30 if err != nil { 31 31 return nil, err 32 32 } 33 33 defer committer.Close() 34 34 35 35 // Principals cannot be duplicated. 36 - has, err := db.GetEngine(ctx). 36 + has, err := db.GetEngine(dbCtx). 37 37 Where("content = ? AND type = ?", content, KeyTypePrincipal). 38 38 Get(new(PublicKey)) 39 39 if err != nil { ··· 50 50 Type: KeyTypePrincipal, 51 51 LoginSourceID: authSourceID, 52 52 } 53 - if err = db.Insert(ctx, key); err != nil { 53 + if err = db.Insert(dbCtx, key); err != nil { 54 54 return nil, fmt.Errorf("addKey: %w", err) 55 55 } 56 56 ··· 60 60 61 61 committer.Close() 62 62 63 - return key, RewriteAllPrincipalKeys(db.DefaultContext) 63 + return key, RewriteAllPrincipalKeys(ctx) 64 64 } 65 65 66 66 // CheckPrincipalKeyString strips spaces and returns an error if the given principal contains newlines ··· 105 105 } 106 106 107 107 // ListPrincipalKeys returns a list of principals belongs to given user. 108 - func ListPrincipalKeys(uid int64, listOptions db.ListOptions) ([]*PublicKey, error) { 109 - sess := db.GetEngine(db.DefaultContext).Where("owner_id = ? AND type = ?", uid, KeyTypePrincipal) 108 + func ListPrincipalKeys(ctx context.Context, uid int64, listOptions db.ListOptions) ([]*PublicKey, error) { 109 + sess := db.GetEngine(ctx).Where("owner_id = ? AND type = ?", uid, KeyTypePrincipal) 110 110 if listOptions.Page != 0 { 111 111 sess = db.SetSessionPagination(sess, &listOptions) 112 112
+3 -2
models/asymkey/ssh_key_verify.go
··· 5 5 6 6 import ( 7 7 "bytes" 8 + "context" 8 9 9 10 "code.gitea.io/gitea/models/db" 10 11 "code.gitea.io/gitea/modules/log" ··· 13 14 ) 14 15 15 16 // VerifySSHKey marks a SSH key as verified 16 - func VerifySSHKey(ownerID int64, fingerprint, token, signature string) (string, error) { 17 - ctx, committer, err := db.TxContext(db.DefaultContext) 17 + func VerifySSHKey(ctx context.Context, ownerID int64, fingerprint, token, signature string) (string, error) { 18 + ctx, committer, err := db.TxContext(ctx) 18 19 if err != nil { 19 20 return "", err 20 21 }
+12 -12
models/auth/oauth2.go
··· 170 170 var base32Lower = base32.NewEncoding(lowerBase32Chars).WithPadding(base32.NoPadding) 171 171 172 172 // GenerateClientSecret will generate the client secret and returns the plaintext and saves the hash at the database 173 - func (app *OAuth2Application) GenerateClientSecret() (string, error) { 173 + func (app *OAuth2Application) GenerateClientSecret(ctx context.Context) (string, error) { 174 174 rBytes, err := util.CryptoRandomBytes(32) 175 175 if err != nil { 176 176 return "", err ··· 184 184 return "", err 185 185 } 186 186 app.ClientSecret = string(hashedSecret) 187 - if _, err := db.GetEngine(db.DefaultContext).ID(app.ID).Cols("client_secret").Update(app); err != nil { 187 + if _, err := db.GetEngine(ctx).ID(app.ID).Cols("client_secret").Update(app); err != nil { 188 188 return "", err 189 189 } 190 190 return clientSecret, nil ··· 284 284 } 285 285 286 286 // UpdateOAuth2Application updates an oauth2 application 287 - func UpdateOAuth2Application(opts UpdateOAuth2ApplicationOptions) (*OAuth2Application, error) { 288 - ctx, committer, err := db.TxContext(db.DefaultContext) 287 + func UpdateOAuth2Application(ctx context.Context, opts UpdateOAuth2ApplicationOptions) (*OAuth2Application, error) { 288 + ctx, committer, err := db.TxContext(ctx) 289 289 if err != nil { 290 290 return nil, err 291 291 } ··· 352 352 } 353 353 354 354 // DeleteOAuth2Application deletes the application with the given id and the grants and auth codes related to it. It checks if the userid was the creator of the app. 355 - func DeleteOAuth2Application(id, userid int64) error { 356 - ctx, committer, err := db.TxContext(db.DefaultContext) 355 + func DeleteOAuth2Application(ctx context.Context, id, userid int64) error { 356 + ctx, committer, err := db.TxContext(ctx) 357 357 if err != nil { 358 358 return err 359 359 } ··· 373 373 } 374 374 375 375 // ListOAuth2Applications returns a list of oauth2 applications belongs to given user. 376 - func ListOAuth2Applications(uid int64, listOptions db.ListOptions) ([]*OAuth2Application, int64, error) { 377 - sess := db.GetEngine(db.DefaultContext). 376 + func ListOAuth2Applications(ctx context.Context, uid int64, listOptions db.ListOptions) ([]*OAuth2Application, int64, error) { 377 + sess := db.GetEngine(ctx). 378 378 Where("uid=?", uid). 379 379 Desc("id") 380 380 ··· 632 632 } 633 633 634 634 // GetActiveOAuth2ProviderSources returns all actived LoginOAuth2 sources 635 - func GetActiveOAuth2ProviderSources() ([]*Source, error) { 635 + func GetActiveOAuth2ProviderSources(ctx context.Context) ([]*Source, error) { 636 636 sources := make([]*Source, 0, 1) 637 - if err := db.GetEngine(db.DefaultContext).Where("is_active = ? and type = ?", true, OAuth2).Find(&sources); err != nil { 637 + if err := db.GetEngine(ctx).Where("is_active = ? and type = ?", true, OAuth2).Find(&sources); err != nil { 638 638 return nil, err 639 639 } 640 640 return sources, nil 641 641 } 642 642 643 643 // GetActiveOAuth2SourceByName returns a OAuth2 AuthSource based on the given name 644 - func GetActiveOAuth2SourceByName(name string) (*Source, error) { 644 + func GetActiveOAuth2SourceByName(ctx context.Context, name string) (*Source, error) { 645 645 authSource := new(Source) 646 - has, err := db.GetEngine(db.DefaultContext).Where("name = ? and type = ? and is_active = ?", name, OAuth2, true).Get(authSource) 646 + has, err := db.GetEngine(ctx).Where("name = ? and type = ? and is_active = ?", name, OAuth2, true).Get(authSource) 647 647 if err != nil { 648 648 return nil, err 649 649 }
+3 -3
models/auth/oauth2_test.go
··· 18 18 func TestOAuth2Application_GenerateClientSecret(t *testing.T) { 19 19 assert.NoError(t, unittest.PrepareTestDatabase()) 20 20 app := unittest.AssertExistsAndLoadBean(t, &auth_model.OAuth2Application{ID: 1}) 21 - secret, err := app.GenerateClientSecret() 21 + secret, err := app.GenerateClientSecret(db.DefaultContext) 22 22 assert.NoError(t, err) 23 23 assert.True(t, len(secret) > 0) 24 24 unittest.AssertExistsAndLoadBean(t, &auth_model.OAuth2Application{ID: 1, ClientSecret: app.ClientSecret}) ··· 28 28 assert.NoError(b, unittest.PrepareTestDatabase()) 29 29 app := unittest.AssertExistsAndLoadBean(b, &auth_model.OAuth2Application{ID: 1}) 30 30 for i := 0; i < b.N; i++ { 31 - _, _ = app.GenerateClientSecret() 31 + _, _ = app.GenerateClientSecret(db.DefaultContext) 32 32 } 33 33 } 34 34 ··· 78 78 func TestOAuth2Application_ValidateClientSecret(t *testing.T) { 79 79 assert.NoError(t, unittest.PrepareTestDatabase()) 80 80 app := unittest.AssertExistsAndLoadBean(t, &auth_model.OAuth2Application{ID: 1}) 81 - secret, err := app.GenerateClientSecret() 81 + secret, err := app.GenerateClientSecret(db.DefaultContext) 82 82 assert.NoError(t, err) 83 83 assert.True(t, app.ValidateClientSecret([]byte(secret))) 84 84 assert.False(t, app.ValidateClientSecret([]byte("fewijfowejgfiowjeoifew")))
+5 -5
models/avatars/avatar.go
··· 94 94 } 95 95 96 96 // GetEmailForHash converts a provided md5sum to the email 97 - func GetEmailForHash(md5Sum string) (string, error) { 97 + func GetEmailForHash(ctx context.Context, md5Sum string) (string, error) { 98 98 return cache.GetString("Avatar:"+md5Sum, func() (string, error) { 99 99 emailHash := EmailHash{ 100 100 Hash: strings.ToLower(strings.TrimSpace(md5Sum)), 101 101 } 102 102 103 - _, err := db.GetEngine(db.DefaultContext).Get(&emailHash) 103 + _, err := db.GetEngine(ctx).Get(&emailHash) 104 104 return emailHash.Email, err 105 105 }) 106 106 } ··· 127 127 128 128 // saveEmailHash returns an avatar link for a provided email, 129 129 // the email and hash are saved into database, which will be used by GetEmailForHash later 130 - func saveEmailHash(email string) string { 130 + func saveEmailHash(ctx context.Context, email string) string { 131 131 lowerEmail := strings.ToLower(strings.TrimSpace(email)) 132 132 emailHash := HashEmail(lowerEmail) 133 133 _, _ = cache.GetString("Avatar:"+emailHash, func() (string, error) { ··· 136 136 Hash: emailHash, 137 137 } 138 138 // OK we're going to open a session just because I think that that might hide away any problems with postgres reporting errors 139 - if err := db.WithTx(db.DefaultContext, func(ctx context.Context) error { 139 + if err := db.WithTx(ctx, func(ctx context.Context) error { 140 140 has, err := db.GetEngine(ctx).Where("email = ? AND hash = ?", emailHash.Email, emailHash.Hash).Get(new(EmailHash)) 141 141 if has || err != nil { 142 142 // Seriously we don't care about any DB problems just return the lowerEmail - we expect the transaction to fail most of the time ··· 196 196 197 197 enableFederatedAvatar := setting.Config().Picture.EnableFederatedAvatar.Value(ctx) 198 198 if enableFederatedAvatar { 199 - emailHash := saveEmailHash(email) 199 + emailHash := saveEmailHash(ctx, email) 200 200 if final { 201 201 // for final link, we can spend more time on slow external query 202 202 var avatarURL *url.URL
+2 -2
models/repo/redirect.go
··· 50 50 } 51 51 52 52 // LookupRedirect look up if a repository has a redirect name 53 - func LookupRedirect(ownerID int64, repoName string) (int64, error) { 53 + func LookupRedirect(ctx context.Context, ownerID int64, repoName string) (int64, error) { 54 54 repoName = strings.ToLower(repoName) 55 55 redirect := &Redirect{OwnerID: ownerID, LowerName: repoName} 56 - if has, err := db.GetEngine(db.DefaultContext).Get(redirect); err != nil { 56 + if has, err := db.GetEngine(ctx).Get(redirect); err != nil { 57 57 return 0, err 58 58 } else if !has { 59 59 return 0, ErrRedirectNotExist{OwnerID: ownerID, RepoName: repoName}
+2 -2
models/repo/redirect_test.go
··· 16 16 func TestLookupRedirect(t *testing.T) { 17 17 assert.NoError(t, unittest.PrepareTestDatabase()) 18 18 19 - repoID, err := repo_model.LookupRedirect(2, "oldrepo1") 19 + repoID, err := repo_model.LookupRedirect(db.DefaultContext, 2, "oldrepo1") 20 20 assert.NoError(t, err) 21 21 assert.EqualValues(t, 1, repoID) 22 22 23 - _, err = repo_model.LookupRedirect(unittest.NonexistentID, "doesnotexist") 23 + _, err = repo_model.LookupRedirect(db.DefaultContext, unittest.NonexistentID, "doesnotexist") 24 24 assert.True(t, repo_model.IsErrRedirectNotExist(err)) 25 25 } 26 26
+16 -16
models/user/external_login_user.go
··· 92 92 } 93 93 94 94 // GetExternalLogin checks if a externalID in loginSourceID scope already exists 95 - func GetExternalLogin(externalLoginUser *ExternalLoginUser) (bool, error) { 96 - return db.GetEngine(db.DefaultContext).Get(externalLoginUser) 95 + func GetExternalLogin(ctx context.Context, externalLoginUser *ExternalLoginUser) (bool, error) { 96 + return db.GetEngine(ctx).Get(externalLoginUser) 97 97 } 98 98 99 99 // ListAccountLinks returns a map with the ExternalLoginUser and its LoginSource 100 - func ListAccountLinks(user *User) ([]*ExternalLoginUser, error) { 100 + func ListAccountLinks(ctx context.Context, user *User) ([]*ExternalLoginUser, error) { 101 101 externalAccounts := make([]*ExternalLoginUser, 0, 5) 102 - err := db.GetEngine(db.DefaultContext).Where("user_id=?", user.ID). 102 + err := db.GetEngine(ctx).Where("user_id=?", user.ID). 103 103 Desc("login_source_id"). 104 104 Find(&externalAccounts) 105 105 if err != nil { ··· 110 110 } 111 111 112 112 // LinkExternalToUser link the external user to the user 113 - func LinkExternalToUser(user *User, externalLoginUser *ExternalLoginUser) error { 114 - has, err := db.GetEngine(db.DefaultContext).Where("external_id=? AND login_source_id=?", externalLoginUser.ExternalID, externalLoginUser.LoginSourceID). 113 + func LinkExternalToUser(ctx context.Context, user *User, externalLoginUser *ExternalLoginUser) error { 114 + has, err := db.GetEngine(ctx).Where("external_id=? AND login_source_id=?", externalLoginUser.ExternalID, externalLoginUser.LoginSourceID). 115 115 NoAutoCondition(). 116 116 Exist(externalLoginUser) 117 117 if err != nil { ··· 120 120 return ErrExternalLoginUserAlreadyExist{externalLoginUser.ExternalID, user.ID, externalLoginUser.LoginSourceID} 121 121 } 122 122 123 - _, err = db.GetEngine(db.DefaultContext).Insert(externalLoginUser) 123 + _, err = db.GetEngine(ctx).Insert(externalLoginUser) 124 124 return err 125 125 } 126 126 127 127 // RemoveAccountLink will remove all external login sources for the given user 128 - func RemoveAccountLink(user *User, loginSourceID int64) (int64, error) { 129 - deleted, err := db.GetEngine(db.DefaultContext).Delete(&ExternalLoginUser{UserID: user.ID, LoginSourceID: loginSourceID}) 128 + func RemoveAccountLink(ctx context.Context, user *User, loginSourceID int64) (int64, error) { 129 + deleted, err := db.GetEngine(ctx).Delete(&ExternalLoginUser{UserID: user.ID, LoginSourceID: loginSourceID}) 130 130 if err != nil { 131 131 return deleted, err 132 132 } ··· 143 143 } 144 144 145 145 // GetUserIDByExternalUserID get user id according to provider and userID 146 - func GetUserIDByExternalUserID(provider, userID string) (int64, error) { 146 + func GetUserIDByExternalUserID(ctx context.Context, provider, userID string) (int64, error) { 147 147 var id int64 148 - _, err := db.GetEngine(db.DefaultContext).Table("external_login_user"). 148 + _, err := db.GetEngine(ctx).Table("external_login_user"). 149 149 Select("user_id"). 150 150 Where("provider=?", provider). 151 151 And("external_id=?", userID). ··· 157 157 } 158 158 159 159 // UpdateExternalUserByExternalID updates an external user's information 160 - func UpdateExternalUserByExternalID(external *ExternalLoginUser) error { 161 - has, err := db.GetEngine(db.DefaultContext).Where("external_id=? AND login_source_id=?", external.ExternalID, external.LoginSourceID). 160 + func UpdateExternalUserByExternalID(ctx context.Context, external *ExternalLoginUser) error { 161 + has, err := db.GetEngine(ctx).Where("external_id=? AND login_source_id=?", external.ExternalID, external.LoginSourceID). 162 162 NoAutoCondition(). 163 163 Exist(external) 164 164 if err != nil { ··· 167 167 return ErrExternalLoginUserNotExist{external.UserID, external.LoginSourceID} 168 168 } 169 169 170 - _, err = db.GetEngine(db.DefaultContext).Where("external_id=? AND login_source_id=?", external.ExternalID, external.LoginSourceID).AllCols().Update(external) 170 + _, err = db.GetEngine(ctx).Where("external_id=? AND login_source_id=?", external.ExternalID, external.LoginSourceID).AllCols().Update(external) 171 171 return err 172 172 } 173 173 ··· 187 187 } 188 188 189 189 // FindExternalUsersByProvider represents external users via provider 190 - func FindExternalUsersByProvider(opts FindExternalUserOptions) ([]ExternalLoginUser, error) { 190 + func FindExternalUsersByProvider(ctx context.Context, opts FindExternalUserOptions) ([]ExternalLoginUser, error) { 191 191 var users []ExternalLoginUser 192 - err := db.GetEngine(db.DefaultContext).Where(opts.toConds()). 192 + err := db.GetEngine(ctx).Where(opts.toConds()). 193 193 Limit(opts.Limit, opts.Start). 194 194 OrderBy("login_source_id ASC, external_id ASC"). 195 195 Find(&users)
+4 -4
models/webhook/hooktask.go
··· 101 101 } 102 102 103 103 // HookTasks returns a list of hook tasks by given conditions. 104 - func HookTasks(hookID int64, page int) ([]*HookTask, error) { 104 + func HookTasks(ctx context.Context, hookID int64, page int) ([]*HookTask, error) { 105 105 tasks := make([]*HookTask, 0, setting.Webhook.PagingNum) 106 - return tasks, db.GetEngine(db.DefaultContext). 106 + return tasks, db.GetEngine(ctx). 107 107 Limit(setting.Webhook.PagingNum, (page-1)*setting.Webhook.PagingNum). 108 108 Where("hook_id=?", hookID). 109 109 Desc("id"). ··· 143 143 } 144 144 145 145 // UpdateHookTask updates information of hook task. 146 - func UpdateHookTask(t *HookTask) error { 147 - _, err := db.GetEngine(db.DefaultContext).ID(t.ID).AllCols().Update(t) 146 + func UpdateHookTask(ctx context.Context, t *HookTask) error { 147 + _, err := db.GetEngine(ctx).ID(t.ID).AllCols().Update(t) 148 148 return err 149 149 } 150 150
+22 -22
models/webhook/webhook.go
··· 155 155 } 156 156 157 157 // History returns history of webhook by given conditions. 158 - func (w *Webhook) History(page int) ([]*HookTask, error) { 159 - return HookTasks(w.ID, page) 158 + func (w *Webhook) History(ctx context.Context, page int) ([]*HookTask, error) { 159 + return HookTasks(ctx, w.ID, page) 160 160 } 161 161 162 162 // UpdateEvent handles conversion from HookEvent to Events. ··· 394 394 395 395 // getWebhook uses argument bean as query condition, 396 396 // ID must be specified and do not assign unnecessary fields. 397 - func getWebhook(bean *Webhook) (*Webhook, error) { 398 - has, err := db.GetEngine(db.DefaultContext).Get(bean) 397 + func getWebhook(ctx context.Context, bean *Webhook) (*Webhook, error) { 398 + has, err := db.GetEngine(ctx).Get(bean) 399 399 if err != nil { 400 400 return nil, err 401 401 } else if !has { ··· 405 405 } 406 406 407 407 // GetWebhookByID returns webhook of repository by given ID. 408 - func GetWebhookByID(id int64) (*Webhook, error) { 409 - return getWebhook(&Webhook{ 408 + func GetWebhookByID(ctx context.Context, id int64) (*Webhook, error) { 409 + return getWebhook(ctx, &Webhook{ 410 410 ID: id, 411 411 }) 412 412 } 413 413 414 414 // GetWebhookByRepoID returns webhook of repository by given ID. 415 - func GetWebhookByRepoID(repoID, id int64) (*Webhook, error) { 416 - return getWebhook(&Webhook{ 415 + func GetWebhookByRepoID(ctx context.Context, repoID, id int64) (*Webhook, error) { 416 + return getWebhook(ctx, &Webhook{ 417 417 ID: id, 418 418 RepoID: repoID, 419 419 }) 420 420 } 421 421 422 422 // GetWebhookByOwnerID returns webhook of a user or organization by given ID. 423 - func GetWebhookByOwnerID(ownerID, id int64) (*Webhook, error) { 424 - return getWebhook(&Webhook{ 423 + func GetWebhookByOwnerID(ctx context.Context, ownerID, id int64) (*Webhook, error) { 424 + return getWebhook(ctx, &Webhook{ 425 425 ID: id, 426 426 OwnerID: ownerID, 427 427 }) ··· 466 466 } 467 467 468 468 // CountWebhooksByOpts count webhooks based on options and ignore pagination 469 - func CountWebhooksByOpts(opts *ListWebhookOptions) (int64, error) { 470 - return db.GetEngine(db.DefaultContext).Where(opts.toCond()).Count(&Webhook{}) 469 + func CountWebhooksByOpts(ctx context.Context, opts *ListWebhookOptions) (int64, error) { 470 + return db.GetEngine(ctx).Where(opts.toCond()).Count(&Webhook{}) 471 471 } 472 472 473 473 // UpdateWebhook updates information of webhook. 474 - func UpdateWebhook(w *Webhook) error { 475 - _, err := db.GetEngine(db.DefaultContext).ID(w.ID).AllCols().Update(w) 474 + func UpdateWebhook(ctx context.Context, w *Webhook) error { 475 + _, err := db.GetEngine(ctx).ID(w.ID).AllCols().Update(w) 476 476 return err 477 477 } 478 478 479 479 // UpdateWebhookLastStatus updates last status of webhook. 480 - func UpdateWebhookLastStatus(w *Webhook) error { 481 - _, err := db.GetEngine(db.DefaultContext).ID(w.ID).Cols("last_status").Update(w) 480 + func UpdateWebhookLastStatus(ctx context.Context, w *Webhook) error { 481 + _, err := db.GetEngine(ctx).ID(w.ID).Cols("last_status").Update(w) 482 482 return err 483 483 } 484 484 485 485 // deleteWebhook uses argument bean as query condition, 486 486 // ID must be specified and do not assign unnecessary fields. 487 - func deleteWebhook(bean *Webhook) (err error) { 488 - ctx, committer, err := db.TxContext(db.DefaultContext) 487 + func deleteWebhook(ctx context.Context, bean *Webhook) (err error) { 488 + ctx, committer, err := db.TxContext(ctx) 489 489 if err != nil { 490 490 return err 491 491 } ··· 503 503 } 504 504 505 505 // DeleteWebhookByRepoID deletes webhook of repository by given ID. 506 - func DeleteWebhookByRepoID(repoID, id int64) error { 507 - return deleteWebhook(&Webhook{ 506 + func DeleteWebhookByRepoID(ctx context.Context, repoID, id int64) error { 507 + return deleteWebhook(ctx, &Webhook{ 508 508 ID: id, 509 509 RepoID: repoID, 510 510 }) 511 511 } 512 512 513 513 // DeleteWebhookByOwnerID deletes webhook of a user or organization by given ID. 514 - func DeleteWebhookByOwnerID(ownerID, id int64) error { 515 - return deleteWebhook(&Webhook{ 514 + func DeleteWebhookByOwnerID(ctx context.Context, ownerID, id int64) error { 515 + return deleteWebhook(ctx, &Webhook{ 516 516 ID: id, 517 517 OwnerID: ownerID, 518 518 })
+14 -14
models/webhook/webhook_test.go
··· 33 33 func TestWebhook_History(t *testing.T) { 34 34 assert.NoError(t, unittest.PrepareTestDatabase()) 35 35 webhook := unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 1}) 36 - tasks, err := webhook.History(0) 36 + tasks, err := webhook.History(db.DefaultContext, 0) 37 37 assert.NoError(t, err) 38 38 if assert.Len(t, tasks, 1) { 39 39 assert.Equal(t, int64(1), tasks[0].ID) 40 40 } 41 41 42 42 webhook = unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 2}) 43 - tasks, err = webhook.History(0) 43 + tasks, err = webhook.History(db.DefaultContext, 0) 44 44 assert.NoError(t, err) 45 45 assert.Len(t, tasks, 0) 46 46 } ··· 101 101 102 102 func TestGetWebhookByRepoID(t *testing.T) { 103 103 assert.NoError(t, unittest.PrepareTestDatabase()) 104 - hook, err := GetWebhookByRepoID(1, 1) 104 + hook, err := GetWebhookByRepoID(db.DefaultContext, 1, 1) 105 105 assert.NoError(t, err) 106 106 assert.Equal(t, int64(1), hook.ID) 107 107 108 - _, err = GetWebhookByRepoID(unittest.NonexistentID, unittest.NonexistentID) 108 + _, err = GetWebhookByRepoID(db.DefaultContext, unittest.NonexistentID, unittest.NonexistentID) 109 109 assert.Error(t, err) 110 110 assert.True(t, IsErrWebhookNotExist(err)) 111 111 } 112 112 113 113 func TestGetWebhookByOwnerID(t *testing.T) { 114 114 assert.NoError(t, unittest.PrepareTestDatabase()) 115 - hook, err := GetWebhookByOwnerID(3, 3) 115 + hook, err := GetWebhookByOwnerID(db.DefaultContext, 3, 3) 116 116 assert.NoError(t, err) 117 117 assert.Equal(t, int64(3), hook.ID) 118 118 119 - _, err = GetWebhookByOwnerID(unittest.NonexistentID, unittest.NonexistentID) 119 + _, err = GetWebhookByOwnerID(db.DefaultContext, unittest.NonexistentID, unittest.NonexistentID) 120 120 assert.Error(t, err) 121 121 assert.True(t, IsErrWebhookNotExist(err)) 122 122 } ··· 167 167 hook.IsActive = true 168 168 hook.ContentType = ContentTypeForm 169 169 unittest.AssertNotExistsBean(t, hook) 170 - assert.NoError(t, UpdateWebhook(hook)) 170 + assert.NoError(t, UpdateWebhook(db.DefaultContext, hook)) 171 171 unittest.AssertExistsAndLoadBean(t, hook) 172 172 } 173 173 174 174 func TestDeleteWebhookByRepoID(t *testing.T) { 175 175 assert.NoError(t, unittest.PrepareTestDatabase()) 176 176 unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 2, RepoID: 1}) 177 - assert.NoError(t, DeleteWebhookByRepoID(1, 2)) 177 + assert.NoError(t, DeleteWebhookByRepoID(db.DefaultContext, 1, 2)) 178 178 unittest.AssertNotExistsBean(t, &Webhook{ID: 2, RepoID: 1}) 179 179 180 - err := DeleteWebhookByRepoID(unittest.NonexistentID, unittest.NonexistentID) 180 + err := DeleteWebhookByRepoID(db.DefaultContext, unittest.NonexistentID, unittest.NonexistentID) 181 181 assert.Error(t, err) 182 182 assert.True(t, IsErrWebhookNotExist(err)) 183 183 } ··· 185 185 func TestDeleteWebhookByOwnerID(t *testing.T) { 186 186 assert.NoError(t, unittest.PrepareTestDatabase()) 187 187 unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 3, OwnerID: 3}) 188 - assert.NoError(t, DeleteWebhookByOwnerID(3, 3)) 188 + assert.NoError(t, DeleteWebhookByOwnerID(db.DefaultContext, 3, 3)) 189 189 unittest.AssertNotExistsBean(t, &Webhook{ID: 3, OwnerID: 3}) 190 190 191 - err := DeleteWebhookByOwnerID(unittest.NonexistentID, unittest.NonexistentID) 191 + err := DeleteWebhookByOwnerID(db.DefaultContext, unittest.NonexistentID, unittest.NonexistentID) 192 192 assert.Error(t, err) 193 193 assert.True(t, IsErrWebhookNotExist(err)) 194 194 } 195 195 196 196 func TestHookTasks(t *testing.T) { 197 197 assert.NoError(t, unittest.PrepareTestDatabase()) 198 - hookTasks, err := HookTasks(1, 1) 198 + hookTasks, err := HookTasks(db.DefaultContext, 1, 1) 199 199 assert.NoError(t, err) 200 200 if assert.Len(t, hookTasks, 1) { 201 201 assert.Equal(t, int64(1), hookTasks[0].ID) 202 202 } 203 203 204 - hookTasks, err = HookTasks(unittest.NonexistentID, 1) 204 + hookTasks, err = HookTasks(db.DefaultContext, unittest.NonexistentID, 1) 205 205 assert.NoError(t, err) 206 206 assert.Len(t, hookTasks, 0) 207 207 } ··· 225 225 hook.PayloadContent = "new payload content" 226 226 hook.IsDelivered = true 227 227 unittest.AssertNotExistsBean(t, hook) 228 - assert.NoError(t, UpdateHookTask(hook)) 228 + assert.NoError(t, UpdateHookTask(db.DefaultContext, hook)) 229 229 unittest.AssertExistsAndLoadBean(t, hook) 230 230 } 231 231
+6 -6
modules/context/repo.go
··· 144 144 } 145 145 146 146 // CanUseTimetracker returns whether or not a user can use the timetracker. 147 - func (r *Repository) CanUseTimetracker(issue *issues_model.Issue, user *user_model.User) bool { 147 + func (r *Repository) CanUseTimetracker(ctx context.Context, issue *issues_model.Issue, user *user_model.User) bool { 148 148 // Checking for following: 149 149 // 1. Is timetracker enabled 150 150 // 2. Is the user a contributor, admin, poster or assignee and do the repository policies require this? 151 - isAssigned, _ := issues_model.IsUserAssignedToIssue(db.DefaultContext, issue, user) 152 - return r.Repository.IsTimetrackerEnabled(db.DefaultContext) && (!r.Repository.AllowOnlyContributorsToTrackTime(db.DefaultContext) || 151 + isAssigned, _ := issues_model.IsUserAssignedToIssue(ctx, issue, user) 152 + return r.Repository.IsTimetrackerEnabled(ctx) && (!r.Repository.AllowOnlyContributorsToTrackTime(ctx) || 153 153 r.Permission.CanWriteIssuesOrPulls(issue.IsPull) || issue.IsPoster(user.ID) || isAssigned) 154 154 } 155 155 156 156 // CanCreateIssueDependencies returns whether or not a user can create dependencies. 157 - func (r *Repository) CanCreateIssueDependencies(user *user_model.User, isPull bool) bool { 158 - return r.Repository.IsDependenciesEnabled(db.DefaultContext) && r.Permission.CanWriteIssuesOrPulls(isPull) 157 + func (r *Repository) CanCreateIssueDependencies(ctx context.Context, user *user_model.User, isPull bool) bool { 158 + return r.Repository.IsDependenciesEnabled(ctx) && r.Permission.CanWriteIssuesOrPulls(isPull) 159 159 } 160 160 161 161 // GetCommitsCount returns cached commit count for current view ··· 498 498 repo, err := repo_model.GetRepositoryByName(ctx, owner.ID, repoName) 499 499 if err != nil { 500 500 if repo_model.IsErrRepoNotExist(err) { 501 - redirectRepoID, err := repo_model.LookupRedirect(owner.ID, repoName) 501 + redirectRepoID, err := repo_model.LookupRedirect(ctx, owner.ID, repoName) 502 502 if err == nil { 503 503 RedirectToRepo(ctx.Base, redirectRepoID) 504 504 } else if repo_model.IsErrRedirectNotExist(err) {
+1 -1
modules/eventsource/manager_run.go
··· 91 91 } 92 92 93 93 for _, userStopwatches := range usersStopwatches { 94 - apiSWs, err := convert.ToStopWatches(userStopwatches.StopWatches) 94 + apiSWs, err := convert.ToStopWatches(ctx, userStopwatches.StopWatches) 95 95 if err != nil { 96 96 if !issues_model.IsErrIssueNotExist(err) { 97 97 log.Error("Unable to APIFormat stopwatches: %v", err)
+1 -1
routers/api/v1/api.go
··· 169 169 repo, err := repo_model.GetRepositoryByName(ctx, owner.ID, repoName) 170 170 if err != nil { 171 171 if repo_model.IsErrRepoNotExist(err) { 172 - redirectRepoID, err := repo_model.LookupRedirect(owner.ID, repoName) 172 + redirectRepoID, err := repo_model.LookupRedirect(ctx, owner.ID, repoName) 173 173 if err == nil { 174 174 context.RedirectToRepo(ctx.Base, redirectRepoID) 175 175 } else if repo_model.IsErrRedirectNotExist(err) {
+2 -2
routers/api/v1/repo/hook.go
··· 58 58 RepoID: ctx.Repo.Repository.ID, 59 59 } 60 60 61 - count, err := webhook.CountWebhooksByOpts(opts) 61 + count, err := webhook.CountWebhooksByOpts(ctx, opts) 62 62 if err != nil { 63 63 ctx.InternalServerError(err) 64 64 return ··· 301 301 // "$ref": "#/responses/empty" 302 302 // "404": 303 303 // "$ref": "#/responses/notFound" 304 - if err := webhook.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil { 304 + if err := webhook.DeleteWebhookByRepoID(ctx, ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil { 305 305 if webhook.IsErrWebhookNotExist(err) { 306 306 ctx.NotFound() 307 307 } else {
+2 -2
routers/api/v1/repo/issue_stopwatch.go
··· 177 177 return nil, errors.New("Unable to write to PRs") 178 178 } 179 179 180 - if !ctx.Repo.CanUseTimetracker(issue, ctx.Doer) { 180 + if !ctx.Repo.CanUseTimetracker(ctx, issue, ctx.Doer) { 181 181 ctx.Status(http.StatusForbidden) 182 182 return nil, errors.New("Cannot use time tracker") 183 183 } ··· 230 230 return 231 231 } 232 232 233 - apiSWs, err := convert.ToStopWatches(sws) 233 + apiSWs, err := convert.ToStopWatches(ctx, sws) 234 234 if err != nil { 235 235 ctx.Error(http.StatusInternalServerError, "APIFormat", err) 236 236 return
+3 -3
routers/api/v1/repo/issue_tracked_time.go
··· 191 191 return 192 192 } 193 193 194 - if !ctx.Repo.CanUseTimetracker(issue, ctx.Doer) { 194 + if !ctx.Repo.CanUseTimetracker(ctx, issue, ctx.Doer) { 195 195 if !ctx.Repo.Repository.IsTimetrackerEnabled(ctx) { 196 196 ctx.Error(http.StatusBadRequest, "", "time tracking disabled") 197 197 return ··· 274 274 return 275 275 } 276 276 277 - if !ctx.Repo.CanUseTimetracker(issue, ctx.Doer) { 277 + if !ctx.Repo.CanUseTimetracker(ctx, issue, ctx.Doer) { 278 278 if !ctx.Repo.Repository.IsTimetrackerEnabled(ctx) { 279 279 ctx.JSON(http.StatusBadRequest, struct{ Message string }{Message: "time tracking disabled"}) 280 280 return ··· 347 347 return 348 348 } 349 349 350 - if !ctx.Repo.CanUseTimetracker(issue, ctx.Doer) { 350 + if !ctx.Repo.CanUseTimetracker(ctx, issue, ctx.Doer) { 351 351 if !ctx.Repo.Repository.IsTimetrackerEnabled(ctx) { 352 352 ctx.JSON(http.StatusBadRequest, struct{ Message string }{Message: "time tracking disabled"}) 353 353 return
+2 -2
routers/api/v1/repo/key.go
··· 96 96 return 97 97 } 98 98 99 - count, err := asymkey_model.CountDeployKeys(opts) 99 + count, err := asymkey_model.CountDeployKeys(ctx, opts) 100 100 if err != nil { 101 101 ctx.InternalServerError(err) 102 102 return ··· 238 238 return 239 239 } 240 240 241 - key, err := asymkey_model.AddDeployKey(ctx.Repo.Repository.ID, form.Title, content, form.ReadOnly) 241 + key, err := asymkey_model.AddDeployKey(ctx, ctx.Repo.Repository.ID, form.Title, content, form.ReadOnly) 242 242 if err != nil { 243 243 HandleAddKeyError(ctx, err) 244 244 return
+1 -1
routers/api/v1/repo/pull.go
··· 811 811 812 812 // handle manually-merged mark 813 813 if manuallyMerged { 814 - if err := pull_service.MergedManually(pr, ctx.Doer, ctx.Repo.GitRepo, form.MergeCommitID); err != nil { 814 + if err := pull_service.MergedManually(ctx, pr, ctx.Doer, ctx.Repo.GitRepo, form.MergeCommitID); err != nil { 815 815 if models.IsErrInvalidMergeStyle(err) { 816 816 ctx.Error(http.StatusMethodNotAllowed, "Invalid merge style", fmt.Errorf("%s is not allowed an allowed merge style for this repository", repo_model.MergeStyle(form.Do))) 817 817 return
+5 -5
routers/api/v1/user/app.go
··· 236 236 ctx.Error(http.StatusBadRequest, "", "error creating oauth2 application") 237 237 return 238 238 } 239 - secret, err := app.GenerateClientSecret() 239 + secret, err := app.GenerateClientSecret(ctx) 240 240 if err != nil { 241 241 ctx.Error(http.StatusBadRequest, "", "error creating application secret") 242 242 return ··· 266 266 // "200": 267 267 // "$ref": "#/responses/OAuth2ApplicationList" 268 268 269 - apps, total, err := auth_model.ListOAuth2Applications(ctx.Doer.ID, utils.GetListOptions(ctx)) 269 + apps, total, err := auth_model.ListOAuth2Applications(ctx, ctx.Doer.ID, utils.GetListOptions(ctx)) 270 270 if err != nil { 271 271 ctx.Error(http.StatusInternalServerError, "ListOAuth2Applications", err) 272 272 return ··· 302 302 // "404": 303 303 // "$ref": "#/responses/notFound" 304 304 appID := ctx.ParamsInt64(":id") 305 - if err := auth_model.DeleteOAuth2Application(appID, ctx.Doer.ID); err != nil { 305 + if err := auth_model.DeleteOAuth2Application(ctx, appID, ctx.Doer.ID); err != nil { 306 306 if auth_model.IsErrOAuthApplicationNotFound(err) { 307 307 ctx.NotFound() 308 308 } else { ··· 377 377 378 378 data := web.GetForm(ctx).(*api.CreateOAuth2ApplicationOptions) 379 379 380 - app, err := auth_model.UpdateOAuth2Application(auth_model.UpdateOAuth2ApplicationOptions{ 380 + app, err := auth_model.UpdateOAuth2Application(ctx, auth_model.UpdateOAuth2ApplicationOptions{ 381 381 Name: data.Name, 382 382 UserID: ctx.Doer.ID, 383 383 ID: appID, ··· 392 392 } 393 393 return 394 394 } 395 - app.ClientSecret, err = app.GenerateClientSecret() 395 + app.ClientSecret, err = app.GenerateClientSecret(ctx) 396 396 if err != nil { 397 397 ctx.Error(http.StatusBadRequest, "", "error updating application secret") 398 398 return
+2 -2
routers/api/v1/user/gpg_key.go
··· 185 185 return 186 186 } 187 187 188 - _, err := asymkey_model.VerifyGPGKey(ctx.Doer.ID, form.KeyID, token, form.Signature) 188 + _, err := asymkey_model.VerifyGPGKey(ctx, ctx.Doer.ID, form.KeyID, token, form.Signature) 189 189 if err != nil && asymkey_model.IsErrGPGInvalidTokenSignature(err) { 190 - _, err = asymkey_model.VerifyGPGKey(ctx.Doer.ID, form.KeyID, lastToken, form.Signature) 190 + _, err = asymkey_model.VerifyGPGKey(ctx, ctx.Doer.ID, form.KeyID, lastToken, form.Signature) 191 191 } 192 192 193 193 if err != nil {
+5 -5
routers/api/v1/utils/hook.go
··· 26 26 OwnerID: owner.ID, 27 27 } 28 28 29 - count, err := webhook.CountWebhooksByOpts(opts) 29 + count, err := webhook.CountWebhooksByOpts(ctx, opts) 30 30 if err != nil { 31 31 ctx.InternalServerError(err) 32 32 return ··· 53 53 54 54 // GetOwnerHook gets an user or organization webhook. Errors are written to ctx. 55 55 func GetOwnerHook(ctx *context.APIContext, ownerID, hookID int64) (*webhook.Webhook, error) { 56 - w, err := webhook.GetWebhookByOwnerID(ownerID, hookID) 56 + w, err := webhook.GetWebhookByOwnerID(ctx, ownerID, hookID) 57 57 if err != nil { 58 58 if webhook.IsErrWebhookNotExist(err) { 59 59 ctx.NotFound() ··· 68 68 // GetRepoHook get a repo's webhook. If there is an error, write to `ctx` 69 69 // accordingly and return the error 70 70 func GetRepoHook(ctx *context.APIContext, repoID, hookID int64) (*webhook.Webhook, error) { 71 - w, err := webhook.GetWebhookByRepoID(repoID, hookID) 71 + w, err := webhook.GetWebhookByRepoID(ctx, repoID, hookID) 72 72 if err != nil { 73 73 if webhook.IsErrWebhookNotExist(err) { 74 74 ctx.NotFound() ··· 392 392 w.IsActive = *form.Active 393 393 } 394 394 395 - if err := webhook.UpdateWebhook(w); err != nil { 395 + if err := webhook.UpdateWebhook(ctx, w); err != nil { 396 396 ctx.Error(http.StatusInternalServerError, "UpdateWebhook", err) 397 397 return false 398 398 } ··· 401 401 402 402 // DeleteOwnerHook deletes the hook owned by the owner. 403 403 func DeleteOwnerHook(ctx *context.APIContext, owner *user_model.User, hookID int64) { 404 - if err := webhook.DeleteWebhookByOwnerID(owner.ID, hookID); err != nil { 404 + if err := webhook.DeleteWebhookByOwnerID(ctx, owner.ID, hookID); err != nil { 405 405 if webhook.IsErrWebhookNotExist(err) { 406 406 ctx.NotFound() 407 407 } else {
+1 -1
routers/init.go
··· 136 136 mustInitCtx(ctx, common.InitDBEngine) 137 137 log.Info("ORM engine initialization successful!") 138 138 mustInit(system.Init) 139 - mustInit(oauth2.Init) 139 + mustInitCtx(ctx, oauth2.Init) 140 140 141 141 mustInitCtx(ctx, models.Init) 142 142 mustInitCtx(ctx, authmodel.Init)
+1 -1
routers/private/key.go
··· 35 35 return 36 36 } 37 37 deployKey.UpdatedUnix = timeutil.TimeStampNow() 38 - if err = asymkey_model.UpdateDeployKeyCols(deployKey, "updated_unix"); err != nil { 38 + if err = asymkey_model.UpdateDeployKeyCols(ctx, deployKey, "updated_unix"); err != nil { 39 39 ctx.JSON(http.StatusInternalServerError, private.Response{ 40 40 Err: err.Error(), 41 41 })
+1 -1
routers/web/admin/auths.go
··· 448 448 return 449 449 } 450 450 451 - if err = auth_service.DeleteSource(source); err != nil { 451 + if err = auth_service.DeleteSource(ctx, source); err != nil { 452 452 if auth.IsErrSourceInUse(err) { 453 453 ctx.Flash.Error(ctx.Tr("admin.auths.still_in_used")) 454 454 } else {
+5 -5
routers/web/auth/auth.go
··· 160 160 return 161 161 } 162 162 163 - orderedOAuth2Names, oauth2Providers, err := oauth2.GetActiveOAuth2Providers() 163 + orderedOAuth2Names, oauth2Providers, err := oauth2.GetActiveOAuth2Providers(ctx) 164 164 if err != nil { 165 165 ctx.ServerError("UserSignIn", err) 166 166 return ··· 184 184 func SignInPost(ctx *context.Context) { 185 185 ctx.Data["Title"] = ctx.Tr("sign_in") 186 186 187 - orderedOAuth2Names, oauth2Providers, err := oauth2.GetActiveOAuth2Providers() 187 + orderedOAuth2Names, oauth2Providers, err := oauth2.GetActiveOAuth2Providers(ctx) 188 188 if err != nil { 189 189 ctx.ServerError("UserSignIn", err) 190 190 return ··· 408 408 409 409 ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/sign_up" 410 410 411 - orderedOAuth2Names, oauth2Providers, err := oauth2.GetActiveOAuth2Providers() 411 + orderedOAuth2Names, oauth2Providers, err := oauth2.GetActiveOAuth2Providers(ctx) 412 412 if err != nil { 413 413 ctx.ServerError("UserSignUp", err) 414 414 return ··· 438 438 439 439 ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/sign_up" 440 440 441 - orderedOAuth2Names, oauth2Providers, err := oauth2.GetActiveOAuth2Providers() 441 + orderedOAuth2Names, oauth2Providers, err := oauth2.GetActiveOAuth2Providers(ctx) 442 442 if err != nil { 443 443 ctx.ServerError("UserSignUp", err) 444 444 return ··· 604 604 605 605 // update external user information 606 606 if gothUser != nil { 607 - if err := externalaccount.UpdateExternalUser(u, *gothUser); err != nil { 607 + if err := externalaccount.UpdateExternalUser(ctx, u, *gothUser); err != nil { 608 608 if !errors.Is(err, util.ErrNotExist) { 609 609 log.Error("UpdateExternalUser failed: %v", err) 610 610 }
+1 -1
routers/web/auth/linkaccount.go
··· 271 271 } 272 272 } 273 273 274 - authSource, err := auth.GetActiveOAuth2SourceByName(gothUser.Provider) 274 + authSource, err := auth.GetActiveOAuth2SourceByName(ctx, gothUser.Provider) 275 275 if err != nil { 276 276 ctx.ServerError("CreateUser", err) 277 277 return
+5 -5
routers/web/auth/oauth.go
··· 847 847 func SignInOAuth(ctx *context.Context) { 848 848 provider := ctx.Params(":provider") 849 849 850 - authSource, err := auth.GetActiveOAuth2SourceByName(provider) 850 + authSource, err := auth.GetActiveOAuth2SourceByName(ctx, provider) 851 851 if err != nil { 852 852 ctx.ServerError("SignIn", err) 853 853 return ··· 868 868 869 869 if err = authSource.Cfg.(*oauth2.Source).Callout(ctx.Req, ctx.Resp); err != nil { 870 870 if strings.Contains(err.Error(), "no provider for ") { 871 - if err = oauth2.ResetOAuth2(); err != nil { 871 + if err = oauth2.ResetOAuth2(ctx); err != nil { 872 872 ctx.ServerError("SignIn", err) 873 873 return 874 874 } ··· 898 898 } 899 899 900 900 // first look if the provider is still active 901 - authSource, err := auth.GetActiveOAuth2SourceByName(provider) 901 + authSource, err := auth.GetActiveOAuth2SourceByName(ctx, provider) 902 902 if err != nil { 903 903 ctx.ServerError("SignIn", err) 904 904 return ··· 1151 1151 } 1152 1152 1153 1153 // update external user information 1154 - if err := externalaccount.UpdateExternalUser(u, gothUser); err != nil { 1154 + if err := externalaccount.UpdateExternalUser(ctx, u, gothUser); err != nil { 1155 1155 if !errors.Is(err, util.ErrNotExist) { 1156 1156 log.Error("UpdateExternalUser failed: %v", err) 1157 1157 } ··· 1274 1274 ExternalID: gothUser.UserID, 1275 1275 LoginSourceID: authSource.ID, 1276 1276 } 1277 - hasUser, err = user_model.GetExternalLogin(externalLoginUser) 1277 + hasUser, err = user_model.GetExternalLogin(request.Context(), externalLoginUser) 1278 1278 if err != nil { 1279 1279 return nil, goth.User{}, err 1280 1280 }
+1 -1
routers/web/org/setting.go
··· 233 233 234 234 // DeleteWebhook response for delete webhook 235 235 func DeleteWebhook(ctx *context.Context) { 236 - if err := webhook.DeleteWebhookByOwnerID(ctx.Org.Organization.ID, ctx.FormInt64("id")); err != nil { 236 + if err := webhook.DeleteWebhookByOwnerID(ctx, ctx.Org.Organization.ID, ctx.FormInt64("id")); err != nil { 237 237 ctx.Flash.Error("DeleteWebhookByOwnerID: " + err.Error()) 238 238 } else { 239 239 ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
+1 -1
routers/web/repo/http.go
··· 108 108 repo, err := repo_model.GetRepositoryByName(ctx, owner.ID, reponame) 109 109 if err != nil { 110 110 if repo_model.IsErrRepoNotExist(err) { 111 - if redirectRepoID, err := repo_model.LookupRedirect(owner.ID, reponame); err == nil { 111 + if redirectRepoID, err := repo_model.LookupRedirect(ctx, owner.ID, reponame); err == nil { 112 112 context.RedirectToRepo(ctx.Base, redirectRepoID) 113 113 return nil 114 114 }
+3 -3
routers/web/repo/issue.go
··· 830 830 } 831 831 832 832 // Contains true if the user can create issue dependencies 833 - ctx.Data["CanCreateIssueDependencies"] = ctx.Repo.CanCreateIssueDependencies(ctx.Doer, isPull) 833 + ctx.Data["CanCreateIssueDependencies"] = ctx.Repo.CanCreateIssueDependencies(ctx, ctx.Doer, isPull) 834 834 835 835 return labels 836 836 } ··· 1548 1548 ctx.Data["OtherStopwatchURL"] = swIssue.Link() 1549 1549 } 1550 1550 } 1551 - ctx.Data["CanUseTimetracker"] = ctx.Repo.CanUseTimetracker(issue, ctx.Doer) 1551 + ctx.Data["CanUseTimetracker"] = ctx.Repo.CanUseTimetracker(ctx, issue, ctx.Doer) 1552 1552 } else { 1553 1553 ctx.Data["CanUseTimetracker"] = false 1554 1554 } ··· 1559 1559 } 1560 1560 1561 1561 // Check if the user can use the dependencies 1562 - ctx.Data["CanCreateIssueDependencies"] = ctx.Repo.CanCreateIssueDependencies(ctx.Doer, issue.IsPull) 1562 + ctx.Data["CanCreateIssueDependencies"] = ctx.Repo.CanCreateIssueDependencies(ctx, ctx.Doer, issue.IsPull) 1563 1563 1564 1564 // check if dependencies can be created across repositories 1565 1565 ctx.Data["AllowCrossRepositoryDependencies"] = setting.Service.AllowCrossRepositoryDependencies
+2 -2
routers/web/repo/issue_dependency.go
··· 22 22 } 23 23 24 24 // Check if the Repo is allowed to have dependencies 25 - if !ctx.Repo.CanCreateIssueDependencies(ctx.Doer, issue.IsPull) { 25 + if !ctx.Repo.CanCreateIssueDependencies(ctx, ctx.Doer, issue.IsPull) { 26 26 ctx.Error(http.StatusForbidden, "CanCreateIssueDependencies") 27 27 return 28 28 } ··· 97 97 } 98 98 99 99 // Check if the Repo is allowed to have dependencies 100 - if !ctx.Repo.CanCreateIssueDependencies(ctx.Doer, issue.IsPull) { 100 + if !ctx.Repo.CanCreateIssueDependencies(ctx, ctx.Doer, issue.IsPull) { 101 101 ctx.Error(http.StatusForbidden, "CanCreateIssueDependencies") 102 102 return 103 103 }
+2 -2
routers/web/repo/issue_stopwatch.go
··· 26 26 showSuccessMessage = true 27 27 } 28 28 29 - if !c.Repo.CanUseTimetracker(issue, c.Doer) { 29 + if !c.Repo.CanUseTimetracker(c, issue, c.Doer) { 30 30 c.NotFound("CanUseTimetracker", nil) 31 31 return 32 32 } ··· 50 50 if c.Written() { 51 51 return 52 52 } 53 - if !c.Repo.CanUseTimetracker(issue, c.Doer) { 53 + if !c.Repo.CanUseTimetracker(c, issue, c.Doer) { 54 54 c.NotFound("CanUseTimetracker", nil) 55 55 return 56 56 }
+2 -2
routers/web/repo/issue_timetrack.go
··· 22 22 if c.Written() { 23 23 return 24 24 } 25 - if !c.Repo.CanUseTimetracker(issue, c.Doer) { 25 + if !c.Repo.CanUseTimetracker(c, issue, c.Doer) { 26 26 c.NotFound("CanUseTimetracker", nil) 27 27 return 28 28 } ··· 56 56 if c.Written() { 57 57 return 58 58 } 59 - if !c.Repo.CanUseTimetracker(issue, c.Doer) { 59 + if !c.Repo.CanUseTimetracker(c, issue, c.Doer) { 60 60 c.NotFound("CanUseTimetracker", nil) 61 61 return 62 62 }
+1 -1
routers/web/repo/pull.go
··· 1170 1170 1171 1171 // handle manually-merged mark 1172 1172 if manuallyMerged { 1173 - if err := pull_service.MergedManually(pr, ctx.Doer, ctx.Repo.GitRepo, form.MergeCommitID); err != nil { 1173 + if err := pull_service.MergedManually(ctx, pr, ctx.Doer, ctx.Repo.GitRepo, form.MergeCommitID); err != nil { 1174 1174 switch { 1175 1175 1176 1176 case models.IsErrInvalidMergeStyle(err):
+1 -1
routers/web/repo/setting/deploy_key.go
··· 70 70 return 71 71 } 72 72 73 - key, err := asymkey_model.AddDeployKey(ctx.Repo.Repository.ID, form.Title, content, !form.IsWritable) 73 + key, err := asymkey_model.AddDeployKey(ctx, ctx.Repo.Repository.ID, form.Title, content, !form.IsWritable) 74 74 if err != nil { 75 75 ctx.Data["HasError"] = true 76 76 switch {
+6 -6
routers/web/repo/setting/webhook.go
··· 300 300 if err := w.UpdateEvent(); err != nil { 301 301 ctx.ServerError("UpdateEvent", err) 302 302 return 303 - } else if err := webhook.UpdateWebhook(w); err != nil { 303 + } else if err := webhook.UpdateWebhook(ctx, w); err != nil { 304 304 ctx.ServerError("UpdateWebhook", err) 305 305 return 306 306 } ··· 589 589 590 590 var w *webhook.Webhook 591 591 if orCtx.RepoID > 0 { 592 - w, err = webhook.GetWebhookByRepoID(orCtx.RepoID, ctx.ParamsInt64(":id")) 592 + w, err = webhook.GetWebhookByRepoID(ctx, orCtx.RepoID, ctx.ParamsInt64(":id")) 593 593 } else if orCtx.OwnerID > 0 { 594 - w, err = webhook.GetWebhookByOwnerID(orCtx.OwnerID, ctx.ParamsInt64(":id")) 594 + w, err = webhook.GetWebhookByOwnerID(ctx, orCtx.OwnerID, ctx.ParamsInt64(":id")) 595 595 } else if orCtx.IsAdmin { 596 596 w, err = webhook.GetSystemOrDefaultWebhook(ctx, ctx.ParamsInt64(":id")) 597 597 } ··· 618 618 ctx.Data["PackagistHook"] = webhook_service.GetPackagistHook(w) 619 619 } 620 620 621 - ctx.Data["History"], err = w.History(1) 621 + ctx.Data["History"], err = w.History(ctx, 1) 622 622 if err != nil { 623 623 ctx.ServerError("History", err) 624 624 } ··· 643 643 // TestWebhook test if web hook is work fine 644 644 func TestWebhook(ctx *context.Context) { 645 645 hookID := ctx.ParamsInt64(":id") 646 - w, err := webhook.GetWebhookByRepoID(ctx.Repo.Repository.ID, hookID) 646 + w, err := webhook.GetWebhookByRepoID(ctx, ctx.Repo.Repository.ID, hookID) 647 647 if err != nil { 648 648 ctx.Flash.Error("GetWebhookByRepoID: " + err.Error()) 649 649 ctx.Status(http.StatusInternalServerError) ··· 724 724 725 725 // DeleteWebhook delete a webhook 726 726 func DeleteWebhook(ctx *context.Context) { 727 - if err := webhook.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.FormInt64("id")); err != nil { 727 + if err := webhook.DeleteWebhookByRepoID(ctx, ctx.Repo.Repository.ID, ctx.FormInt64("id")); err != nil { 728 728 ctx.Flash.Error("DeleteWebhookByRepoID: " + err.Error()) 729 729 } else { 730 730 ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
+1 -1
routers/web/user/avatar.go
··· 47 47 // AvatarByEmailHash redirects the browser to the email avatar link 48 48 func AvatarByEmailHash(ctx *context.Context) { 49 49 hash := ctx.Params(":hash") 50 - email, err := avatars.GetEmailForHash(hash) 50 + email, err := avatars.GetEmailForHash(ctx, hash) 51 51 if err != nil { 52 52 ctx.ServerError("invalid avatar hash: "+hash, err) 53 53 return
+1 -1
routers/web/user/home.go
··· 784 784 entities := make([]*openpgp.Entity, 0) 785 785 failedEntitiesID := make([]string, 0) 786 786 for _, k := range keys { 787 - e, err := asymkey_model.GPGKeyToEntity(k) 787 + e, err := asymkey_model.GPGKeyToEntity(ctx, k) 788 788 if err != nil { 789 789 if asymkey_model.IsErrGPGKeyImportNotExist(err) { 790 790 failedEntitiesID = append(failedEntitiesID, k.KeyID)
+6 -6
routers/web/user/setting/keys.go
··· 61 61 ctx.Redirect(setting.AppSubURL + "/user/settings/keys") 62 62 return 63 63 } 64 - if _, err = asymkey_model.AddPrincipalKey(ctx.Doer.ID, content, 0); err != nil { 64 + if _, err = asymkey_model.AddPrincipalKey(ctx, ctx.Doer.ID, content, 0); err != nil { 65 65 ctx.Data["HasPrincipalError"] = true 66 66 switch { 67 67 case asymkey_model.IsErrKeyAlreadyExist(err), asymkey_model.IsErrKeyNameAlreadyUsed(err): ··· 131 131 token := asymkey_model.VerificationToken(ctx.Doer, 1) 132 132 lastToken := asymkey_model.VerificationToken(ctx.Doer, 0) 133 133 134 - keyID, err := asymkey_model.VerifyGPGKey(ctx.Doer.ID, form.KeyID, token, form.Signature) 134 + keyID, err := asymkey_model.VerifyGPGKey(ctx, ctx.Doer.ID, form.KeyID, token, form.Signature) 135 135 if err != nil && asymkey_model.IsErrGPGInvalidTokenSignature(err) { 136 - keyID, err = asymkey_model.VerifyGPGKey(ctx.Doer.ID, form.KeyID, lastToken, form.Signature) 136 + keyID, err = asymkey_model.VerifyGPGKey(ctx, ctx.Doer.ID, form.KeyID, lastToken, form.Signature) 137 137 } 138 138 if err != nil { 139 139 ctx.Data["HasGPGVerifyError"] = true ··· 195 195 token := asymkey_model.VerificationToken(ctx.Doer, 1) 196 196 lastToken := asymkey_model.VerificationToken(ctx.Doer, 0) 197 197 198 - fingerprint, err := asymkey_model.VerifySSHKey(ctx.Doer.ID, form.Fingerprint, token, form.Signature) 198 + fingerprint, err := asymkey_model.VerifySSHKey(ctx, ctx.Doer.ID, form.Fingerprint, token, form.Signature) 199 199 if err != nil && asymkey_model.IsErrSSHInvalidTokenSignature(err) { 200 - fingerprint, err = asymkey_model.VerifySSHKey(ctx.Doer.ID, form.Fingerprint, lastToken, form.Signature) 200 + fingerprint, err = asymkey_model.VerifySSHKey(ctx, ctx.Doer.ID, form.Fingerprint, lastToken, form.Signature) 201 201 } 202 202 if err != nil { 203 203 ctx.Data["HasSSHVerifyError"] = true ··· 285 285 // generate a new aes cipher using the csrfToken 286 286 ctx.Data["TokenToSign"] = tokenToSign 287 287 288 - principals, err := asymkey_model.ListPrincipalKeys(ctx.Doer.ID, db.ListOptions{}) 288 + principals, err := asymkey_model.ListPrincipalKeys(ctx, ctx.Doer.ID, db.ListOptions{}) 289 289 if err != nil { 290 290 ctx.ServerError("ListPrincipalKeys", err) 291 291 return
+4 -4
routers/web/user/setting/oauth2_common.go
··· 62 62 // render the edit page with secret 63 63 ctx.Flash.Success(ctx.Tr("settings.create_oauth2_application_success"), true) 64 64 ctx.Data["App"] = app 65 - ctx.Data["ClientSecret"], err = app.GenerateClientSecret() 65 + ctx.Data["ClientSecret"], err = app.GenerateClientSecret(ctx) 66 66 if err != nil { 67 67 ctx.ServerError("GenerateClientSecret", err) 68 68 return ··· 101 101 102 102 // TODO validate redirect URI 103 103 var err error 104 - if ctx.Data["App"], err = auth.UpdateOAuth2Application(auth.UpdateOAuth2ApplicationOptions{ 104 + if ctx.Data["App"], err = auth.UpdateOAuth2Application(ctx, auth.UpdateOAuth2ApplicationOptions{ 105 105 ID: ctx.ParamsInt64("id"), 106 106 Name: form.Name, 107 107 RedirectURIs: util.SplitTrimSpace(form.RedirectURIs, "\n"), ··· 131 131 return 132 132 } 133 133 ctx.Data["App"] = app 134 - ctx.Data["ClientSecret"], err = app.GenerateClientSecret() 134 + ctx.Data["ClientSecret"], err = app.GenerateClientSecret(ctx) 135 135 if err != nil { 136 136 ctx.ServerError("GenerateClientSecret", err) 137 137 return ··· 142 142 143 143 // DeleteApp deletes the given oauth2 application 144 144 func (oa *OAuth2CommonHandlers) DeleteApp(ctx *context.Context) { 145 - if err := auth.DeleteOAuth2Application(ctx.ParamsInt64("id"), oa.OwnerID); err != nil { 145 + if err := auth.DeleteOAuth2Application(ctx, ctx.ParamsInt64("id"), oa.OwnerID); err != nil { 146 146 ctx.ServerError("DeleteOAuth2Application", err) 147 147 return 148 148 }
+3 -3
routers/web/user/setting/security/security.go
··· 41 41 if id <= 0 { 42 42 ctx.Flash.Error("Account link id is not given") 43 43 } else { 44 - if _, err := user_model.RemoveAccountLink(ctx.Doer, id); err != nil { 44 + if _, err := user_model.RemoveAccountLink(ctx, ctx.Doer, id); err != nil { 45 45 ctx.Flash.Error("RemoveAccountLink: " + err.Error()) 46 46 } else { 47 47 ctx.Flash.Success(ctx.Tr("settings.remove_account_link_success")) ··· 73 73 } 74 74 ctx.Data["Tokens"] = tokens 75 75 76 - accountLinks, err := user_model.ListAccountLinks(ctx.Doer) 76 + accountLinks, err := user_model.ListAccountLinks(ctx, ctx.Doer) 77 77 if err != nil { 78 78 ctx.ServerError("ListAccountLinks", err) 79 79 return ··· 105 105 } 106 106 ctx.Data["AccountLinks"] = sources 107 107 108 - orderedOAuth2Names, oauth2Providers, err := oauth2.GetActiveOAuth2Providers() 108 + orderedOAuth2Names, oauth2Providers, err := oauth2.GetActiveOAuth2Providers(ctx) 109 109 if err != nil { 110 110 ctx.ServerError("GetActiveOAuth2Providers", err) 111 111 return
+1 -1
routers/web/user/setting/webhooks.go
··· 36 36 37 37 // DeleteWebhook response for delete webhook 38 38 func DeleteWebhook(ctx *context.Context) { 39 - if err := webhook.DeleteWebhookByOwnerID(ctx.Doer.ID, ctx.FormInt64("id")); err != nil { 39 + if err := webhook.DeleteWebhookByOwnerID(ctx, ctx.Doer.ID, ctx.FormInt64("id")); err != nil { 40 40 ctx.Flash.Error("DeleteWebhookByOwnerID: " + err.Error()) 41 41 } else { 42 42 ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
+1 -1
routers/web/user/stop_watch.go
··· 29 29 return 30 30 } 31 31 32 - apiSWs, err := convert.ToStopWatches(sws) 32 + apiSWs, err := convert.ToStopWatches(ctx, sws) 33 33 if err != nil { 34 34 ctx.Error(http.StatusInternalServerError, err.Error()) 35 35 return
+7 -8
services/actions/notifier.go
··· 6 6 import ( 7 7 "context" 8 8 9 - "code.gitea.io/gitea/models/db" 10 9 issues_model "code.gitea.io/gitea/models/issues" 11 10 packages_model "code.gitea.io/gitea/models/packages" 12 11 perm_model "code.gitea.io/gitea/models/perm" ··· 68 67 // Merge pull request calls issue.changeStatus so we need to handle separately. 69 68 apiPullRequest := &api.PullRequestPayload{ 70 69 Index: issue.Index, 71 - PullRequest: convert.ToAPIPullRequest(db.DefaultContext, issue.PullRequest, nil), 70 + PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil), 72 71 Repository: convert.ToRepo(ctx, issue.Repo, permission), 73 72 Sender: convert.ToUser(ctx, doer, nil), 74 73 CommitID: commitID, ··· 296 295 WithPayload(&api.PullRequestPayload{ 297 296 Action: api.HookIssueReviewed, 298 297 Index: review.Issue.Index, 299 - PullRequest: convert.ToAPIPullRequest(db.DefaultContext, pr, nil), 298 + PullRequest: convert.ToAPIPullRequest(ctx, pr, nil), 300 299 Repository: convert.ToRepo(ctx, review.Issue.Repo, permission), 301 300 Sender: convert.ToUser(ctx, review.Reviewer, nil), 302 301 Review: &api.ReviewPayload{ ··· 320 319 return 321 320 } 322 321 323 - if err := pr.Issue.LoadRepo(db.DefaultContext); err != nil { 322 + if err := pr.Issue.LoadRepo(ctx); err != nil { 324 323 log.Error("pr.Issue.LoadRepo: %v", err) 325 324 return 326 325 } ··· 334 333 // Merge pull request calls issue.changeStatus so we need to handle separately. 335 334 apiPullRequest := &api.PullRequestPayload{ 336 335 Index: pr.Issue.Index, 337 - PullRequest: convert.ToAPIPullRequest(db.DefaultContext, pr, nil), 336 + PullRequest: convert.ToAPIPullRequest(ctx, pr, nil), 338 337 Repository: convert.ToRepo(ctx, pr.Issue.Repo, permission), 339 338 Sender: convert.ToUser(ctx, doer, nil), 340 339 Action: api.HookIssueClosed, ··· 413 412 ctx = withMethod(ctx, "SyncPushCommits") 414 413 415 414 apiPusher := convert.ToUser(ctx, pusher, nil) 416 - apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(db.DefaultContext, repo.RepoPath(), repo.HTMLURL()) 415 + apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(ctx, repo.RepoPath(), repo.HTMLURL()) 417 416 if err != nil { 418 417 log.Error("commits.ToAPIPayloadCommits failed: %v", err) 419 418 return ··· 484 483 return 485 484 } 486 485 487 - if err := pr.Issue.LoadRepo(db.DefaultContext); err != nil { 486 + if err := pr.Issue.LoadRepo(ctx); err != nil { 488 487 log.Error("pr.Issue.LoadRepo: %v", err) 489 488 return 490 489 } ··· 509 508 return 510 509 } 511 510 512 - if err := pr.Issue.LoadRepo(db.DefaultContext); err != nil { 511 + if err := pr.Issue.LoadRepo(ctx); err != nil { 513 512 log.Error("pr.Issue.LoadRepo: %v", err) 514 513 return 515 514 }
+6 -4
services/auth/source.go
··· 4 4 package auth 5 5 6 6 import ( 7 + "context" 8 + 7 9 "code.gitea.io/gitea/models/auth" 8 10 "code.gitea.io/gitea/models/db" 9 11 user_model "code.gitea.io/gitea/models/user" 10 12 ) 11 13 12 14 // DeleteSource deletes a AuthSource record in DB. 13 - func DeleteSource(source *auth.Source) error { 14 - count, err := db.GetEngine(db.DefaultContext).Count(&user_model.User{LoginSource: source.ID}) 15 + func DeleteSource(ctx context.Context, source *auth.Source) error { 16 + count, err := db.GetEngine(ctx).Count(&user_model.User{LoginSource: source.ID}) 15 17 if err != nil { 16 18 return err 17 19 } else if count > 0 { ··· 20 22 } 21 23 } 22 24 23 - count, err = db.GetEngine(db.DefaultContext).Count(&user_model.ExternalLoginUser{LoginSourceID: source.ID}) 25 + count, err = db.GetEngine(ctx).Count(&user_model.ExternalLoginUser{LoginSourceID: source.ID}) 24 26 if err != nil { 25 27 return err 26 28 } else if count > 0 { ··· 35 37 } 36 38 } 37 39 38 - _, err = db.GetEngine(db.DefaultContext).ID(source.ID).Delete(new(auth.Source)) 40 + _, err = db.GetEngine(ctx).ID(source.ID).Delete(new(auth.Source)) 39 41 return err 40 42 }
+7 -6
services/auth/source/oauth2/init.go
··· 4 4 package oauth2 5 5 6 6 import ( 7 + "context" 7 8 "encoding/gob" 8 9 "net/http" 9 10 "sync" ··· 26 27 const ProviderHeaderKey = "gitea-oauth2-provider" 27 28 28 29 // Init initializes the oauth source 29 - func Init() error { 30 + func Init(ctx context.Context) error { 30 31 if err := InitSigningKey(); err != nil { 31 32 return err 32 33 } ··· 51 52 // Unlock our mutex 52 53 gothRWMutex.Unlock() 53 54 54 - return initOAuth2Sources() 55 + return initOAuth2Sources(ctx) 55 56 } 56 57 57 58 // ResetOAuth2 clears existing OAuth2 providers and loads them from DB 58 - func ResetOAuth2() error { 59 + func ResetOAuth2(ctx context.Context) error { 59 60 ClearProviders() 60 - return initOAuth2Sources() 61 + return initOAuth2Sources(ctx) 61 62 } 62 63 63 64 // initOAuth2Sources is used to load and register all active OAuth2 providers 64 - func initOAuth2Sources() error { 65 - authSources, _ := auth.GetActiveOAuth2ProviderSources() 65 + func initOAuth2Sources(ctx context.Context) error { 66 + authSources, _ := auth.GetActiveOAuth2ProviderSources(ctx) 66 67 for _, source := range authSources { 67 68 oauth2Source, ok := source.Cfg.(*Source) 68 69 if !ok {
+3 -2
services/auth/source/oauth2/providers.go
··· 4 4 package oauth2 5 5 6 6 import ( 7 + "context" 7 8 "errors" 8 9 "fmt" 9 10 "html" ··· 97 98 // GetActiveOAuth2Providers returns the map of configured active OAuth2 providers 98 99 // key is used as technical name (like in the callbackURL) 99 100 // values to display 100 - func GetActiveOAuth2Providers() ([]string, map[string]Provider, error) { 101 + func GetActiveOAuth2Providers(ctx context.Context) ([]string, map[string]Provider, error) { 101 102 // Maybe also separate used and unused providers so we can force the registration of only 1 active provider for each type 102 103 103 - authSources, err := auth.GetActiveOAuth2ProviderSources() 104 + authSources, err := auth.GetActiveOAuth2ProviderSources(ctx) 104 105 if err != nil { 105 106 return nil, nil, err 106 107 }
+3 -4
services/convert/issue.go
··· 9 9 "net/url" 10 10 "strings" 11 11 12 - "code.gitea.io/gitea/models/db" 13 12 issues_model "code.gitea.io/gitea/models/issues" 14 13 repo_model "code.gitea.io/gitea/models/repo" 15 14 user_model "code.gitea.io/gitea/models/user" ··· 150 149 } 151 150 152 151 // ToStopWatches convert Stopwatch list to api.StopWatches 153 - func ToStopWatches(sws []*issues_model.Stopwatch) (api.StopWatches, error) { 152 + func ToStopWatches(ctx context.Context, sws []*issues_model.Stopwatch) (api.StopWatches, error) { 154 153 result := api.StopWatches(make([]api.StopWatch, 0, len(sws))) 155 154 156 155 issueCache := make(map[int64]*issues_model.Issue) ··· 165 164 for _, sw := range sws { 166 165 issue, ok = issueCache[sw.IssueID] 167 166 if !ok { 168 - issue, err = issues_model.GetIssueByID(db.DefaultContext, sw.IssueID) 167 + issue, err = issues_model.GetIssueByID(ctx, sw.IssueID) 169 168 if err != nil { 170 169 return nil, err 171 170 } 172 171 } 173 172 repo, ok = repoCache[issue.RepoID] 174 173 if !ok { 175 - repo, err = repo_model.GetRepositoryByID(db.DefaultContext, issue.RepoID) 174 + repo, err = repo_model.GetRepositoryByID(ctx, issue.RepoID) 176 175 if err != nil { 177 176 return nil, err 178 177 }
+7 -7
services/externalaccount/user.go
··· 16 16 "github.com/markbates/goth" 17 17 ) 18 18 19 - func toExternalLoginUser(user *user_model.User, gothUser goth.User) (*user_model.ExternalLoginUser, error) { 20 - authSource, err := auth.GetActiveOAuth2SourceByName(gothUser.Provider) 19 + func toExternalLoginUser(ctx context.Context, user *user_model.User, gothUser goth.User) (*user_model.ExternalLoginUser, error) { 20 + authSource, err := auth.GetActiveOAuth2SourceByName(ctx, gothUser.Provider) 21 21 if err != nil { 22 22 return nil, err 23 23 } ··· 44 44 45 45 // LinkAccountToUser link the gothUser to the user 46 46 func LinkAccountToUser(ctx context.Context, user *user_model.User, gothUser goth.User) error { 47 - externalLoginUser, err := toExternalLoginUser(user, gothUser) 47 + externalLoginUser, err := toExternalLoginUser(ctx, user, gothUser) 48 48 if err != nil { 49 49 return err 50 50 } 51 51 52 - if err := user_model.LinkExternalToUser(user, externalLoginUser); err != nil { 52 + if err := user_model.LinkExternalToUser(ctx, user, externalLoginUser); err != nil { 53 53 return err 54 54 } 55 55 ··· 71 71 } 72 72 73 73 // UpdateExternalUser updates external user's information 74 - func UpdateExternalUser(user *user_model.User, gothUser goth.User) error { 75 - externalLoginUser, err := toExternalLoginUser(user, gothUser) 74 + func UpdateExternalUser(ctx context.Context, user *user_model.User, gothUser goth.User) error { 75 + externalLoginUser, err := toExternalLoginUser(ctx, user, gothUser) 76 76 if err != nil { 77 77 return err 78 78 } 79 79 80 - return user_model.UpdateExternalUserByExternalID(externalLoginUser) 80 + return user_model.UpdateExternalUserByExternalID(ctx, externalLoginUser) 81 81 } 82 82 83 83 // UpdateMigrationsByType updates all migrated repositories' posterid from gitServiceType to replace originalAuthorID to posterID
+1 -1
services/migrations/gitea_uploader.go
··· 989 989 func (g *GiteaLocalUploader) remapExternalUser(source user_model.ExternalUserMigrated, target user_model.ExternalUserRemappable) (userid int64, err error) { 990 990 userid, ok := g.userMap[source.GetExternalID()] 991 991 if !ok { 992 - userid, err = user_model.GetUserIDByExternalUserID(g.gitServiceType.Name(), fmt.Sprintf("%d", source.GetExternalID())) 992 + userid, err = user_model.GetUserIDByExternalUserID(g.ctx, g.gitServiceType.Name(), fmt.Sprintf("%d", source.GetExternalID())) 993 993 if err != nil { 994 994 log.Error("GetUserIDByExternalUserID: %v", err) 995 995 return 0, err
+1 -1
services/migrations/gitea_uploader_test.go
··· 210 210 LoginSourceID: 0, 211 211 Provider: structs.GiteaService.Name(), 212 212 } 213 - err = user_model.LinkExternalToUser(linkedUser, externalLoginUser) 213 + err = user_model.LinkExternalToUser(db.DefaultContext, linkedUser, externalLoginUser) 214 214 assert.NoError(t, err) 215 215 216 216 //
+1 -1
services/migrations/update.go
··· 45 45 default: 46 46 } 47 47 48 - users, err := user_model.FindExternalUsersByProvider(user_model.FindExternalUserOptions{ 48 + users, err := user_model.FindExternalUsersByProvider(ctx, user_model.FindExternalUserOptions{ 49 49 Provider: provider, 50 50 Start: start, 51 51 Limit: batchSize,
+4 -4
services/pull/lfs.go
··· 40 40 // 6. Take the output of cat-file --batch and check if each file in turn 41 41 // to see if they're pointers to files in the LFS store associated with 42 42 // the head repo and add them to the base repo if so 43 - go createLFSMetaObjectsFromCatFileBatch(catFileBatchReader, &wg, pr) 43 + go createLFSMetaObjectsFromCatFileBatch(db.DefaultContext, catFileBatchReader, &wg, pr) 44 44 45 45 // 5. Take the shas of the blobs and batch read them 46 46 go pipeline.CatFileBatch(ctx, shasToBatchReader, catFileBatchWriter, &wg, tmpBasePath) ··· 68 68 return nil 69 69 } 70 70 71 - func createLFSMetaObjectsFromCatFileBatch(catFileBatchReader *io.PipeReader, wg *sync.WaitGroup, pr *issues_model.PullRequest) { 71 + func createLFSMetaObjectsFromCatFileBatch(ctx context.Context, catFileBatchReader *io.PipeReader, wg *sync.WaitGroup, pr *issues_model.PullRequest) { 72 72 defer wg.Done() 73 73 defer catFileBatchReader.Close() 74 74 ··· 116 116 } 117 117 118 118 // Then we need to check that this pointer is in the db 119 - if _, err := git_model.GetLFSMetaObjectByOid(db.DefaultContext, pr.HeadRepoID, pointer.Oid); err != nil { 119 + if _, err := git_model.GetLFSMetaObjectByOid(ctx, pr.HeadRepoID, pointer.Oid); err != nil { 120 120 if err == git_model.ErrLFSObjectNotExist { 121 121 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) 122 122 continue ··· 129 129 // Therefore it should be associated with the base repo 130 130 meta := &git_model.LFSMetaObject{Pointer: pointer} 131 131 meta.RepositoryID = pr.BaseRepoID 132 - if _, err := git_model.NewLFSMetaObject(db.DefaultContext, meta); err != nil { 132 + if _, err := git_model.NewLFSMetaObject(ctx, meta); err != nil { 133 133 _ = catFileBatchReader.CloseWithError(err) 134 134 break 135 135 }
+2 -2
services/pull/merge.go
··· 464 464 } 465 465 466 466 // MergedManually mark pr as merged manually 467 - func MergedManually(pr *issues_model.PullRequest, doer *user_model.User, baseGitRepo *git.Repository, commitID string) error { 467 + func MergedManually(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.User, baseGitRepo *git.Repository, commitID string) error { 468 468 pullWorkingPool.CheckIn(fmt.Sprint(pr.ID)) 469 469 defer pullWorkingPool.CheckOut(fmt.Sprint(pr.ID)) 470 470 471 - if err := db.WithTx(db.DefaultContext, func(ctx context.Context) error { 471 + if err := db.WithTx(ctx, func(ctx context.Context) error { 472 472 if err := pr.LoadBaseRepo(ctx); err != nil { 473 473 return err 474 474 }
+3 -3
services/pull/pull.go
··· 811 811 gitRepos[issue.RepoID] = gitRepo 812 812 } 813 813 814 - statuses, lastStatus, err := getAllCommitStatus(gitRepo, issue.PullRequest) 814 + statuses, lastStatus, err := getAllCommitStatus(ctx, gitRepo, issue.PullRequest) 815 815 if err != nil { 816 816 log.Error("getAllCommitStatus: cant get commit statuses of pull [%d]: %v", issue.PullRequest.ID, err) 817 817 continue ··· 823 823 } 824 824 825 825 // getAllCommitStatus get pr's commit statuses. 826 - func getAllCommitStatus(gitRepo *git.Repository, pr *issues_model.PullRequest) (statuses []*git_model.CommitStatus, lastStatus *git_model.CommitStatus, err error) { 826 + func getAllCommitStatus(ctx context.Context, gitRepo *git.Repository, pr *issues_model.PullRequest) (statuses []*git_model.CommitStatus, lastStatus *git_model.CommitStatus, err error) { 827 827 sha, shaErr := gitRepo.GetRefCommitID(pr.GetGitRefName()) 828 828 if shaErr != nil { 829 829 return nil, nil, shaErr 830 830 } 831 831 832 - statuses, _, err = git_model.GetLatestCommitStatus(db.DefaultContext, pr.BaseRepo.ID, sha, db.ListOptions{ListAll: true}) 832 + statuses, _, err = git_model.GetLatestCommitStatus(ctx, pr.BaseRepo.ID, sha, db.ListOptions{ListAll: true}) 833 833 lastStatus = git_model.CalcCommitStatus(statuses) 834 834 return statuses, lastStatus, err 835 835 }
+3 -3
services/webhook/deliver.go
··· 34 34 35 35 // Deliver deliver hook task 36 36 func Deliver(ctx context.Context, t *webhook_model.HookTask) error { 37 - w, err := webhook_model.GetWebhookByID(t.HookID) 37 + w, err := webhook_model.GetWebhookByID(ctx, t.HookID) 38 38 if err != nil { 39 39 return err 40 40 } ··· 185 185 log.Trace("Hook delivery failed: %s", t.UUID) 186 186 } 187 187 188 - if err := webhook_model.UpdateHookTask(t); err != nil { 188 + if err := webhook_model.UpdateHookTask(ctx, t); err != nil { 189 189 log.Error("UpdateHookTask [%d]: %v", t.ID, err) 190 190 } 191 191 ··· 195 195 } else { 196 196 w.LastStatus = webhook_module.HookStatusFail 197 197 } 198 - if err = webhook_model.UpdateWebhookLastStatus(w); err != nil { 198 + if err = webhook_model.UpdateWebhookLastStatus(ctx, w); err != nil { 199 199 log.Error("UpdateWebhookLastStatus: %v", err) 200 200 return 201 201 }
+3 -3
tests/integration/api_private_serv_test.go
··· 26 26 assert.Equal(t, int64(1), key.ID) 27 27 assert.Equal(t, "user2@localhost", key.Name) 28 28 29 - deployKey, err := asymkey_model.AddDeployKey(1, "test-deploy", "sk-ecdsa-sha2-nistp256@openssh.com AAAAInNrLWVjZHNhLXNoYTItbmlzdHAyNTZAb3BlbnNzaC5jb20AAAAIbmlzdHAyNTYAAABBBGXEEzWmm1dxb+57RoK5KVCL0w2eNv9cqJX2AGGVlkFsVDhOXHzsadS3LTK4VlEbbrDMJdoti9yM8vclA8IeRacAAAAEc3NoOg== nocomment", false) 29 + deployKey, err := asymkey_model.AddDeployKey(ctx, 1, "test-deploy", "sk-ecdsa-sha2-nistp256@openssh.com AAAAInNrLWVjZHNhLXNoYTItbmlzdHAyNTZAb3BlbnNzaC5jb20AAAAIbmlzdHAyNTYAAABBBGXEEzWmm1dxb+57RoK5KVCL0w2eNv9cqJX2AGGVlkFsVDhOXHzsadS3LTK4VlEbbrDMJdoti9yM8vclA8IeRacAAAAEc3NoOg== nocomment", false) 30 30 assert.NoError(t, err) 31 31 32 32 key, user, err = private.ServNoCommand(ctx, deployKey.KeyID) ··· 84 84 assert.Empty(t, results) 85 85 86 86 // Add reading deploy key 87 - deployKey, err := asymkey_model.AddDeployKey(19, "test-deploy", "sk-ecdsa-sha2-nistp256@openssh.com AAAAInNrLWVjZHNhLXNoYTItbmlzdHAyNTZAb3BlbnNzaC5jb20AAAAIbmlzdHAyNTYAAABBBGXEEzWmm1dxb+57RoK5KVCL0w2eNv9cqJX2AGGVlkFsVDhOXHzsadS3LTK4VlEbbrDMJdoti9yM8vclA8IeRacAAAAEc3NoOg== nocomment", true) 87 + deployKey, err := asymkey_model.AddDeployKey(ctx, 19, "test-deploy", "sk-ecdsa-sha2-nistp256@openssh.com AAAAInNrLWVjZHNhLXNoYTItbmlzdHAyNTZAb3BlbnNzaC5jb20AAAAIbmlzdHAyNTYAAABBBGXEEzWmm1dxb+57RoK5KVCL0w2eNv9cqJX2AGGVlkFsVDhOXHzsadS3LTK4VlEbbrDMJdoti9yM8vclA8IeRacAAAAEc3NoOg== nocomment", true) 88 88 assert.NoError(t, err) 89 89 90 90 // Can pull from repo we're a deploy key for ··· 116 116 assert.Empty(t, results) 117 117 118 118 // Add writing deploy key 119 - deployKey, err = asymkey_model.AddDeployKey(20, "test-deploy", "sk-ecdsa-sha2-nistp256@openssh.com AAAAInNrLWVjZHNhLXNoYTItbmlzdHAyNTZAb3BlbnNzaC5jb20AAAAIbmlzdHAyNTYAAABBBGXEEzWmm1dxb+57RoK5KVCL0w2eNv9cqJX2AGGVlkFsVDhOXHzsadS3LTK4VlEbbrDMJdoti9yM8vclA8IeRacAAAAEc3NoOg== nocomment", false) 119 + deployKey, err = asymkey_model.AddDeployKey(ctx, 20, "test-deploy", "sk-ecdsa-sha2-nistp256@openssh.com AAAAInNrLWVjZHNhLXNoYTItbmlzdHAyNTZAb3BlbnNzaC5jb20AAAAIbmlzdHAyNTYAAABBBGXEEzWmm1dxb+57RoK5KVCL0w2eNv9cqJX2AGGVlkFsVDhOXHzsadS3LTK4VlEbbrDMJdoti9yM8vclA8IeRacAAAAEc3NoOg== nocomment", false) 120 120 assert.NoError(t, err) 121 121 122 122 // Cannot push to a private repo with reading key
+8 -8
tests/integration/pull_merge_test.go
··· 68 68 69 69 func TestPullMerge(t *testing.T) { 70 70 onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { 71 - hookTasks, err := webhook.HookTasks(1, 1) // Retrieve previous hook number 71 + hookTasks, err := webhook.HookTasks(db.DefaultContext, 1, 1) // Retrieve previous hook number 72 72 assert.NoError(t, err) 73 73 hookTasksLenBefore := len(hookTasks) 74 74 ··· 82 82 assert.EqualValues(t, "pulls", elem[3]) 83 83 testPullMerge(t, session, elem[1], elem[2], elem[4], repo_model.MergeStyleMerge) 84 84 85 - hookTasks, err = webhook.HookTasks(1, 1) 85 + hookTasks, err = webhook.HookTasks(db.DefaultContext, 1, 1) 86 86 assert.NoError(t, err) 87 87 assert.Len(t, hookTasks, hookTasksLenBefore+1) 88 88 }) ··· 90 90 91 91 func TestPullRebase(t *testing.T) { 92 92 onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { 93 - hookTasks, err := webhook.HookTasks(1, 1) // Retrieve previous hook number 93 + hookTasks, err := webhook.HookTasks(db.DefaultContext, 1, 1) // Retrieve previous hook number 94 94 assert.NoError(t, err) 95 95 hookTasksLenBefore := len(hookTasks) 96 96 ··· 104 104 assert.EqualValues(t, "pulls", elem[3]) 105 105 testPullMerge(t, session, elem[1], elem[2], elem[4], repo_model.MergeStyleRebase) 106 106 107 - hookTasks, err = webhook.HookTasks(1, 1) 107 + hookTasks, err = webhook.HookTasks(db.DefaultContext, 1, 1) 108 108 assert.NoError(t, err) 109 109 assert.Len(t, hookTasks, hookTasksLenBefore+1) 110 110 }) ··· 112 112 113 113 func TestPullRebaseMerge(t *testing.T) { 114 114 onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { 115 - hookTasks, err := webhook.HookTasks(1, 1) // Retrieve previous hook number 115 + hookTasks, err := webhook.HookTasks(db.DefaultContext, 1, 1) // Retrieve previous hook number 116 116 assert.NoError(t, err) 117 117 hookTasksLenBefore := len(hookTasks) 118 118 ··· 126 126 assert.EqualValues(t, "pulls", elem[3]) 127 127 testPullMerge(t, session, elem[1], elem[2], elem[4], repo_model.MergeStyleRebaseMerge) 128 128 129 - hookTasks, err = webhook.HookTasks(1, 1) 129 + hookTasks, err = webhook.HookTasks(db.DefaultContext, 1, 1) 130 130 assert.NoError(t, err) 131 131 assert.Len(t, hookTasks, hookTasksLenBefore+1) 132 132 }) ··· 134 134 135 135 func TestPullSquash(t *testing.T) { 136 136 onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { 137 - hookTasks, err := webhook.HookTasks(1, 1) // Retrieve previous hook number 137 + hookTasks, err := webhook.HookTasks(db.DefaultContext, 1, 1) // Retrieve previous hook number 138 138 assert.NoError(t, err) 139 139 hookTasksLenBefore := len(hookTasks) 140 140 ··· 149 149 assert.EqualValues(t, "pulls", elem[3]) 150 150 testPullMerge(t, session, elem[1], elem[2], elem[4], repo_model.MergeStyleSquash) 151 151 152 - hookTasks, err = webhook.HookTasks(1, 1) 152 + hookTasks, err = webhook.HookTasks(db.DefaultContext, 1, 1) 153 153 assert.NoError(t, err) 154 154 assert.Len(t, hookTasks, hookTasksLenBefore+1) 155 155 })