···9191}
92929393// AddPublicKey adds new public key to database and authorized_keys file.
9494-func AddPublicKey(ownerID int64, name, content string, authSourceID int64) (*PublicKey, error) {
9494+func AddPublicKey(ctx context.Context, ownerID int64, name, content string, authSourceID int64) (*PublicKey, error) {
9595 log.Trace(content)
96969797 fingerprint, err := CalcFingerprint(content)
···9999 return nil, err
100100 }
101101102102- ctx, committer, err := db.TxContext(db.DefaultContext)
102102+ ctx, committer, err := db.TxContext(ctx)
103103 if err != nil {
104104 return nil, err
105105 }
···136136}
137137138138// GetPublicKeyByID returns public key by given ID.
139139-func GetPublicKeyByID(keyID int64) (*PublicKey, error) {
139139+func GetPublicKeyByID(ctx context.Context, keyID int64) (*PublicKey, error) {
140140 key := new(PublicKey)
141141- has, err := db.GetEngine(db.DefaultContext).
141141+ has, err := db.GetEngine(ctx).
142142 ID(keyID).
143143 Get(key)
144144 if err != nil {
···180180}
181181182182// SearchPublicKey returns a list of public keys matching the provided arguments.
183183-func SearchPublicKey(uid int64, fingerprint string) ([]*PublicKey, error) {
183183+func SearchPublicKey(ctx context.Context, uid int64, fingerprint string) ([]*PublicKey, error) {
184184 keys := make([]*PublicKey, 0, 5)
185185 cond := builder.NewCond()
186186 if uid != 0 {
···189189 if fingerprint != "" {
190190 cond = cond.And(builder.Eq{"fingerprint": fingerprint})
191191 }
192192- return keys, db.GetEngine(db.DefaultContext).Where(cond).Find(&keys)
192192+ return keys, db.GetEngine(ctx).Where(cond).Find(&keys)
193193}
194194195195// ListPublicKeys returns a list of public keys belongs to given user.
196196-func ListPublicKeys(uid int64, listOptions db.ListOptions) ([]*PublicKey, error) {
197197- sess := db.GetEngine(db.DefaultContext).Where("owner_id = ? AND type != ?", uid, KeyTypePrincipal)
196196+func ListPublicKeys(ctx context.Context, uid int64, listOptions db.ListOptions) ([]*PublicKey, error) {
197197+ sess := db.GetEngine(ctx).Where("owner_id = ? AND type != ?", uid, KeyTypePrincipal)
198198 if listOptions.Page != 0 {
199199 sess = db.SetSessionPagination(sess, &listOptions)
200200···207207}
208208209209// CountPublicKeys count public keys a user has
210210-func CountPublicKeys(userID int64) (int64, error) {
211211- sess := db.GetEngine(db.DefaultContext).Where("owner_id = ? AND type != ?", userID, KeyTypePrincipal)
210210+func CountPublicKeys(ctx context.Context, userID int64) (int64, error) {
211211+ sess := db.GetEngine(ctx).Where("owner_id = ? AND type != ?", userID, KeyTypePrincipal)
212212 return sess.Count(&PublicKey{})
213213}
214214215215// ListPublicKeysBySource returns a list of synchronized public keys for a given user and login source.
216216-func ListPublicKeysBySource(uid, authSourceID int64) ([]*PublicKey, error) {
216216+func ListPublicKeysBySource(ctx context.Context, uid, authSourceID int64) ([]*PublicKey, error) {
217217 keys := make([]*PublicKey, 0, 5)
218218- return keys, db.GetEngine(db.DefaultContext).
218218+ return keys, db.GetEngine(ctx).
219219 Where("owner_id = ? AND login_source_id = ?", uid, authSourceID).
220220 Find(&keys)
221221}
222222223223// UpdatePublicKeyUpdated updates public key use time.
224224-func UpdatePublicKeyUpdated(id int64) error {
224224+func UpdatePublicKeyUpdated(ctx context.Context, id int64) error {
225225 // Check if key exists before update as affected rows count is unreliable
226226 // and will return 0 affected rows if two updates are made at the same time
227227- if cnt, err := db.GetEngine(db.DefaultContext).ID(id).Count(&PublicKey{}); err != nil {
227227+ if cnt, err := db.GetEngine(ctx).ID(id).Count(&PublicKey{}); err != nil {
228228 return err
229229 } else if cnt != 1 {
230230 return ErrKeyNotExist{id}
231231 }
232232233233- _, err := db.GetEngine(db.DefaultContext).ID(id).Cols("updated_unix").Update(&PublicKey{
233233+ _, err := db.GetEngine(ctx).ID(id).Cols("updated_unix").Update(&PublicKey{
234234 UpdatedUnix: timeutil.TimeStampNow(),
235235 })
236236 if err != nil {
···250250}
251251252252// PublicKeysAreExternallyManaged returns whether the provided KeyID represents an externally managed Key
253253-func PublicKeysAreExternallyManaged(keys []*PublicKey) ([]bool, error) {
253253+func PublicKeysAreExternallyManaged(ctx context.Context, keys []*PublicKey) ([]bool, error) {
254254 sources := make([]*auth.Source, 0, 5)
255255 externals := make([]bool, len(keys))
256256keyloop:
···272272273273 if source == nil {
274274 var err error
275275- source, err = auth.GetSourceByID(key.LoginSourceID)
275275+ source, err = auth.GetSourceByID(ctx, key.LoginSourceID)
276276 if err != nil {
277277 if auth.IsErrSourceNotExist(err) {
278278 externals[i] = false
···295295}
296296297297// PublicKeyIsExternallyManaged returns whether the provided KeyID represents an externally managed Key
298298-func PublicKeyIsExternallyManaged(id int64) (bool, error) {
299299- key, err := GetPublicKeyByID(id)
298298+func PublicKeyIsExternallyManaged(ctx context.Context, id int64) (bool, error) {
299299+ key, err := GetPublicKeyByID(ctx, id)
300300 if err != nil {
301301 return false, err
302302 }
303303 if key.LoginSourceID == 0 {
304304 return false, nil
305305 }
306306- source, err := auth.GetSourceByID(key.LoginSourceID)
306306+ source, err := auth.GetSourceByID(ctx, key.LoginSourceID)
307307 if err != nil {
308308 if auth.IsErrSourceNotExist(err) {
309309 return false, nil
···318318}
319319320320// deleteKeysMarkedForDeletion returns true if ssh keys needs update
321321-func deleteKeysMarkedForDeletion(keys []string) (bool, error) {
321321+func deleteKeysMarkedForDeletion(ctx context.Context, keys []string) (bool, error) {
322322 // Start session
323323- ctx, committer, err := db.TxContext(db.DefaultContext)
323323+ ctx, committer, err := db.TxContext(ctx)
324324 if err != nil {
325325 return false, err
326326 }
···349349}
350350351351// AddPublicKeysBySource add a users public keys. Returns true if there are changes.
352352-func AddPublicKeysBySource(usr *user_model.User, s *auth.Source, sshPublicKeys []string) bool {
352352+func AddPublicKeysBySource(ctx context.Context, usr *user_model.User, s *auth.Source, sshPublicKeys []string) bool {
353353 var sshKeysNeedUpdate bool
354354 for _, sshKey := range sshPublicKeys {
355355 var err error
···368368 marshalled = marshalled[:len(marshalled)-1]
369369 sshKeyName := fmt.Sprintf("%s-%s", s.Name, ssh.FingerprintSHA256(out))
370370371371- if _, err := AddPublicKey(usr.ID, sshKeyName, marshalled, s.ID); err != nil {
371371+ if _, err := AddPublicKey(ctx, usr.ID, sshKeyName, marshalled, s.ID); err != nil {
372372 if IsErrKeyAlreadyExist(err) {
373373 log.Trace("AddPublicKeysBySource[%s]: Public SSH Key %s already exists for user", sshKeyName, usr.Name)
374374 } else {
···387387}
388388389389// SynchronizePublicKeys updates a users public keys. Returns true if there are changes.
390390-func SynchronizePublicKeys(usr *user_model.User, s *auth.Source, sshPublicKeys []string) bool {
390390+func SynchronizePublicKeys(ctx context.Context, usr *user_model.User, s *auth.Source, sshPublicKeys []string) bool {
391391 var sshKeysNeedUpdate bool
392392393393 log.Trace("synchronizePublicKeys[%s]: Handling Public SSH Key synchronization for user %s", s.Name, usr.Name)
394394395395 // Get Public Keys from DB with current LDAP source
396396 var giteaKeys []string
397397- keys, err := ListPublicKeysBySource(usr.ID, s.ID)
397397+ keys, err := ListPublicKeysBySource(ctx, usr.ID, s.ID)
398398 if err != nil {
399399 log.Error("synchronizePublicKeys[%s]: Error listing Public SSH Keys for user %s: %v", s.Name, usr.Name, err)
400400 }
···429429 newKeys = append(newKeys, key)
430430 }
431431 }
432432- if AddPublicKeysBySource(usr, s, newKeys) {
432432+ if AddPublicKeysBySource(ctx, usr, s, newKeys) {
433433 sshKeysNeedUpdate = true
434434 }
435435···443443 }
444444445445 // Delete keys from DB that no longer exist in the source
446446- needUpd, err := deleteKeysMarkedForDeletion(giteaKeysToDelete)
446446+ needUpd, err := deleteKeysMarkedForDeletion(ctx, giteaKeysToDelete)
447447 if err != nil {
448448 log.Error("synchronizePublicKeys[%s]: Error deleting Public Keys marked for deletion for user %s: %v", s.Name, usr.Name, err)
449449 }
+1-1
models/asymkey/ssh_key_commit_verification.go
···2121func ParseCommitWithSSHSignature(ctx context.Context, c *git.Commit, committer *user_model.User) *CommitVerification {
2222 // Now try to associate the signature with the committer, if present
2323 if committer.ID != 0 {
2424- keys, err := ListPublicKeys(committer.ID, db.ListOptions{})
2424+ keys, err := ListPublicKeys(ctx, committer.ID, db.ListOptions{})
2525 if err != nil { // Skipping failed to get ssh keys of user
2626 log.Error("ListPublicKeys: %v", err)
2727 return &CommitVerification{
···152152}
153153154154func linkAccount(ctx *context.Context, u *user_model.User, gothUser goth.User, remember bool) {
155155- updateAvatarIfNeed(gothUser.AvatarURL, u)
155155+ updateAvatarIfNeed(ctx, gothUser.AvatarURL, u)
156156157157 // If this user is enrolled in 2FA, we can't sign the user in just yet.
158158 // Instead, redirect them to the 2FA authentication page.
···8282 // map the provider display name with the AuthSource
8383 sources := make(map[*auth_model.Source]string)
8484 for _, externalAccount := range accountLinks {
8585- if authSource, err := auth_model.GetSourceByID(externalAccount.LoginSourceID); err == nil {
8585+ if authSource, err := auth_model.GetSourceByID(ctx, externalAccount.LoginSourceID); err == nil {
8686 var providerDisplayName string
87878888 type DisplayNamed interface {
+2-1
routers/web/web.go
···99 "strings"
10101111 auth_model "code.gitea.io/gitea/models/auth"
1212+ "code.gitea.io/gitea/models/db"
1213 "code.gitea.io/gitea/models/perm"
1314 "code.gitea.io/gitea/models/unit"
1415 "code.gitea.io/gitea/modules/context"
···9495 group.Add(&auth_service.ReverseProxy{})
9596 }
96979797- if setting.IsWindows && auth_model.IsSSPIEnabled() {
9898+ if setting.IsWindows && auth_model.IsSSPIEnabled(db.DefaultContext) {
9899 group.Add(&auth_service.SSPI{}) // it MUST be the last, see the comment of SSPI
99100 }
100101
···9191 return nil
9292 }
93939494- if pr.IsWorkInProgress() {
9494+ if pr.IsWorkInProgress(ctx) {
9595 return ErrIsWorkInProgress
9696 }
9797···360360 if err := TestPatch(pr); err != nil {
361361 log.Error("testPatch[%-v]: %v", pr, err)
362362 pr.Status = issues_model.PullRequestStatusError
363363- if err := pr.UpdateCols("status"); err != nil {
363363+ if err := pr.UpdateCols(ctx, "status"); err != nil {
364364 log.Error("update pr [%-v] status to PullRequestStatusError failed: %v", pr, err)
365365 }
366366 return
+1-1
services/pull/pull.go
···125125 return err
126126 }
127127128128- if !pr.IsWorkInProgress() {
128128+ if !pr.IsWorkInProgress(ctx) {
129129 if err := issues_model.PullRequestCodeOwnersReview(ctx, issue, pr); err != nil {
130130 return err
131131 }
+6-6
services/repository/archiver/archiver.go
···172172 }
173173}
174174175175-func doArchive(r *ArchiveRequest) (*repo_model.RepoArchiver, error) {
176176- txCtx, committer, err := db.TxContext(db.DefaultContext)
175175+func doArchive(ctx context.Context, r *ArchiveRequest) (*repo_model.RepoArchiver, error) {
176176+ txCtx, committer, err := db.TxContext(ctx)
177177 if err != nil {
178178 return nil, err
179179 }
···291291// anything. In all cases, the caller should be examining the *ArchiveRequest
292292// being returned for completion, as it may be different than the one they passed
293293// in.
294294-func ArchiveRepository(request *ArchiveRequest) (*repo_model.RepoArchiver, error) {
295295- return doArchive(request)
294294+func ArchiveRepository(ctx context.Context, request *ArchiveRequest) (*repo_model.RepoArchiver, error) {
295295+ return doArchive(ctx, request)
296296}
297297298298var archiverQueue *queue.WorkerPoolQueue[*ArchiveRequest]
299299300300// Init initializes archiver
301301-func Init() error {
301301+func Init(ctx context.Context) error {
302302 handler := func(items ...*ArchiveRequest) []*ArchiveRequest {
303303 for _, archiveReq := range items {
304304 log.Trace("ArchiverData Process: %#v", archiveReq)
305305- if _, err := doArchive(archiveReq); err != nil {
305305+ if _, err := doArchive(ctx, archiveReq); err != nil {
306306 log.Error("Archive %v failed: %v", archiveReq, err)
307307 }
308308 }
+7-6
services/repository/archiver/archiver_test.go
···88 "testing"
99 "time"
10101111+ "code.gitea.io/gitea/models/db"
1112 "code.gitea.io/gitea/models/unittest"
1213 "code.gitea.io/gitea/modules/contexttest"
1314···7980 inFlight[1] = tgzReq
8081 inFlight[2] = secondReq
81828282- ArchiveRepository(zipReq)
8383- ArchiveRepository(tgzReq)
8484- ArchiveRepository(secondReq)
8383+ ArchiveRepository(db.DefaultContext, zipReq)
8484+ ArchiveRepository(db.DefaultContext, tgzReq)
8585+ ArchiveRepository(db.DefaultContext, secondReq)
85868687 // Make sure sending an unprocessed request through doesn't affect the queue
8788 // count.
8888- ArchiveRepository(zipReq)
8989+ ArchiveRepository(db.DefaultContext, zipReq)
89909091 // Sleep two seconds to make sure the queue doesn't change.
9192 time.Sleep(2 * time.Second)
···100101 // We still have the other three stalled at completion, waiting to remove
101102 // from archiveInProgress. Try to submit this new one before its
102103 // predecessor has cleared out of the queue.
103103- ArchiveRepository(zipReq2)
104104+ ArchiveRepository(db.DefaultContext, zipReq2)
104105105106 // Now we'll submit a request and TimedWaitForCompletion twice, before and
106107 // after we release it. We should trigger both the timeout and non-timeout
···108109 timedReq, err := NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, secondCommit+".tar.gz")
109110 assert.NoError(t, err)
110111 assert.NotNil(t, timedReq)
111111- ArchiveRepository(timedReq)
112112+ ArchiveRepository(db.DefaultContext, timedReq)
112113113114 zipReq2, err = NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, firstCommit+".zip")
114115 assert.NoError(t, err)