Skip to content

Commit

Permalink
Another round of db.DefaultContext refactor (go-gitea#27103)
Browse files Browse the repository at this point in the history
Part of go-gitea#27065

---------

Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
  • Loading branch information
2 people authored and GiteaBot committed Sep 25, 2023
1 parent 597b04f commit 6f083d6
Show file tree
Hide file tree
Showing 109 changed files with 353 additions and 306 deletions.
4 changes: 2 additions & 2 deletions cmd/admin.go
Expand Up @@ -389,7 +389,7 @@ func runRepoSyncReleases(_ *cli.Context) error {
}
log.Trace(" currentNumReleases is %d, running SyncReleasesWithTags", oldnum)

if err = repo_module.SyncReleasesWithTags(repo, gitRepo); err != nil {
if err = repo_module.SyncReleasesWithTags(ctx, repo, gitRepo); err != nil {
log.Warn(" SyncReleasesWithTags: %v", err)
gitRepo.Close()
continue
Expand Down Expand Up @@ -438,7 +438,7 @@ func runRegenerateKeys(_ *cli.Context) error {
if err := initDB(ctx); err != nil {
return err
}
return asymkey_model.RewriteAllPublicKeys()
return asymkey_model.RewriteAllPublicKeys(ctx)
}

func parseOAuth2Config(c *cli.Context) *oauth2.Source {
Expand Down
3 changes: 2 additions & 1 deletion cmd/migrate_storage_test.go
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"testing"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/packages"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
Expand All @@ -30,7 +31,7 @@ func TestMigratePackages(t *testing.T) {
assert.NoError(t, err)
defer buf.Close()

v, f, err := packages_service.CreatePackageAndAddFile(&packages_service.PackageCreationInfo{
v, f, err := packages_service.CreatePackageAndAddFile(db.DefaultContext, &packages_service.PackageCreationInfo{
PackageInfo: packages_service.PackageInfo{
Owner: creator,
PackageType: packages.TypeGeneric,
Expand Down
14 changes: 8 additions & 6 deletions models/activities/user_heatmap.go
Expand Up @@ -4,6 +4,8 @@
package activities

import (
"context"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/organization"
user_model "code.gitea.io/gitea/models/user"
Expand All @@ -18,16 +20,16 @@ type UserHeatmapData struct {
}

// GetUserHeatmapDataByUser returns an array of UserHeatmapData
func GetUserHeatmapDataByUser(user, doer *user_model.User) ([]*UserHeatmapData, error) {
return getUserHeatmapData(user, nil, doer)
func GetUserHeatmapDataByUser(ctx context.Context, user, doer *user_model.User) ([]*UserHeatmapData, error) {
return getUserHeatmapData(ctx, user, nil, doer)
}

// GetUserHeatmapDataByUserTeam returns an array of UserHeatmapData
func GetUserHeatmapDataByUserTeam(user *user_model.User, team *organization.Team, doer *user_model.User) ([]*UserHeatmapData, error) {
return getUserHeatmapData(user, team, doer)
func GetUserHeatmapDataByUserTeam(ctx context.Context, user *user_model.User, team *organization.Team, doer *user_model.User) ([]*UserHeatmapData, error) {
return getUserHeatmapData(ctx, user, team, doer)
}

func getUserHeatmapData(user *user_model.User, team *organization.Team, doer *user_model.User) ([]*UserHeatmapData, error) {
func getUserHeatmapData(ctx context.Context, user *user_model.User, team *organization.Team, doer *user_model.User) ([]*UserHeatmapData, error) {
hdata := make([]*UserHeatmapData, 0)

if !ActivityReadable(user, doer) {
Expand Down Expand Up @@ -60,7 +62,7 @@ func getUserHeatmapData(user *user_model.User, team *organization.Team, doer *us
return nil, err
}

return hdata, db.GetEngine(db.DefaultContext).
return hdata, db.GetEngine(ctx).
Select(groupBy+" AS timestamp, count(user_id) as contributions").
Table("action").
Where(cond).
Expand Down
2 changes: 1 addition & 1 deletion models/activities/user_heatmap_test.go
Expand Up @@ -83,7 +83,7 @@ func TestGetUserHeatmapDataByUser(t *testing.T) {
assert.NoError(t, err)

// Get the heatmap and compare
heatmap, err := activities_model.GetUserHeatmapDataByUser(user, doer)
heatmap, err := activities_model.GetUserHeatmapDataByUser(db.DefaultContext, user, doer)
var contributions int
for _, hm := range heatmap {
contributions += int(hm.Contributions)
Expand Down
18 changes: 9 additions & 9 deletions models/asymkey/gpg_key.go
Expand Up @@ -88,14 +88,14 @@ func ListGPGKeys(ctx context.Context, uid int64, listOptions db.ListOptions) ([]
}

// CountUserGPGKeys return number of gpg keys a user own
func CountUserGPGKeys(userID int64) (int64, error) {
return db.GetEngine(db.DefaultContext).Where("owner_id=? AND primary_key_id=''", userID).Count(&GPGKey{})
func CountUserGPGKeys(ctx context.Context, userID int64) (int64, error) {
return db.GetEngine(ctx).Where("owner_id=? AND primary_key_id=''", userID).Count(&GPGKey{})
}

// GetGPGKeyByID returns public key by given ID.
func GetGPGKeyByID(keyID int64) (*GPGKey, error) {
func GetGPGKeyByID(ctx context.Context, keyID int64) (*GPGKey, error) {
key := new(GPGKey)
has, err := db.GetEngine(db.DefaultContext).ID(keyID).Get(key)
has, err := db.GetEngine(ctx).ID(keyID).Get(key)
if err != nil {
return nil, err
} else if !has {
Expand All @@ -105,9 +105,9 @@ func GetGPGKeyByID(keyID int64) (*GPGKey, error) {
}

// GetGPGKeysByKeyID returns public key by given ID.
func GetGPGKeysByKeyID(keyID string) ([]*GPGKey, error) {
func GetGPGKeysByKeyID(ctx context.Context, keyID string) ([]*GPGKey, error) {
keys := make([]*GPGKey, 0, 1)
return keys, db.GetEngine(db.DefaultContext).Where("key_id=?", keyID).Find(&keys)
return keys, db.GetEngine(ctx).Where("key_id=?", keyID).Find(&keys)
}

// GPGKeyToEntity retrieve the imported key and the traducted entity
Expand Down Expand Up @@ -224,8 +224,8 @@ func deleteGPGKey(ctx context.Context, keyID string) (int64, error) {
}

// DeleteGPGKey deletes GPG key information in database.
func DeleteGPGKey(doer *user_model.User, id int64) (err error) {
key, err := GetGPGKeyByID(id)
func DeleteGPGKey(ctx context.Context, doer *user_model.User, id int64) (err error) {
key, err := GetGPGKeyByID(ctx, id)
if err != nil {
if IsErrGPGKeyNotExist(err) {
return nil
Expand All @@ -238,7 +238,7 @@ func DeleteGPGKey(doer *user_model.User, id int64) (err error) {
return ErrGPGKeyAccessDenied{doer.ID, key.ID}
}

ctx, committer, err := db.TxContext(db.DefaultContext)
ctx, committer, err := db.TxContext(ctx)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions models/asymkey/gpg_key_add.go
Expand Up @@ -66,13 +66,13 @@ func addGPGSubKey(ctx context.Context, key *GPGKey) (err error) {
}

// AddGPGKey adds new public key to database.
func AddGPGKey(ownerID int64, content, token, signature string) ([]*GPGKey, error) {
func AddGPGKey(ctx context.Context, ownerID int64, content, token, signature string) ([]*GPGKey, error) {
ekeys, err := checkArmoredGPGKeyString(content)
if err != nil {
return nil, err
}

ctx, committer, err := db.TxContext(db.DefaultContext)
ctx, committer, err := db.TxContext(ctx)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions models/asymkey/gpg_key_commit_verification.go
Expand Up @@ -392,7 +392,7 @@ func hashAndVerifyForKeyID(ctx context.Context, sig *packet.Signature, payload s
if keyID == "" {
return nil
}
keys, err := GetGPGKeysByKeyID(keyID)
keys, err := GetGPGKeysByKeyID(ctx, keyID)
if err != nil {
log.Error("GetGPGKeysByKeyID: %v", err)
return &CommitVerification{
Expand All @@ -407,7 +407,7 @@ func hashAndVerifyForKeyID(ctx context.Context, sig *packet.Signature, payload s
for _, key := range keys {
var primaryKeys []*GPGKey
if key.PrimaryKeyID != "" {
primaryKeys, err = GetGPGKeysByKeyID(key.PrimaryKeyID)
primaryKeys, err = GetGPGKeysByKeyID(ctx, key.PrimaryKeyID)
if err != nil {
log.Error("GetGPGKeysByKeyID: %v", err)
return &CommitVerification{
Expand Down
3 changes: 2 additions & 1 deletion models/asymkey/gpg_key_test.go
Expand Up @@ -7,6 +7,7 @@ import (
"testing"
"time"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/timeutil"
Expand Down Expand Up @@ -228,7 +229,7 @@ Q0KHb+QcycSgbDx0ZAvdIacuKvBBcbxrsmFUI4LR+oIup0G9gUc0roPvr014jYQL
=zHo9
-----END PGP PUBLIC KEY BLOCK-----`

keys, err := AddGPGKey(1, testEmailWithUpperCaseLetters, "", "")
keys, err := AddGPGKey(db.DefaultContext, 1, testEmailWithUpperCaseLetters, "", "")
assert.NoError(t, err)
if assert.NotEmpty(t, keys) {
key := keys[0]
Expand Down
4 changes: 2 additions & 2 deletions models/asymkey/ssh_key_authorized_keys.go
Expand Up @@ -117,7 +117,7 @@ func appendAuthorizedKeysToFile(keys ...*PublicKey) error {
// RewriteAllPublicKeys removes any authorized key and rewrite all keys from database again.
// Note: db.GetEngine(db.DefaultContext).Iterate does not get latest data after insert/delete, so we have to call this function
// outside any session scope independently.
func RewriteAllPublicKeys() error {
func RewriteAllPublicKeys(ctx context.Context) error {
// Don't rewrite key if internal server
if setting.SSH.StartBuiltinServer || !setting.SSH.CreateAuthorizedKeysFile {
return nil
Expand Down Expand Up @@ -165,7 +165,7 @@ func RewriteAllPublicKeys() error {
}
}

if err := RegeneratePublicKeys(db.DefaultContext, t); err != nil {
if err := RegeneratePublicKeys(ctx, t); err != nil {
return err
}

Expand Down
10 changes: 7 additions & 3 deletions models/issues/issue_index.go
Expand Up @@ -3,12 +3,16 @@

package issues

import "code.gitea.io/gitea/models/db"
import (
"context"

"code.gitea.io/gitea/models/db"
)

// RecalculateIssueIndexForRepo create issue_index for repo if not exist and
// update it based on highest index of existing issues assigned to a repo
func RecalculateIssueIndexForRepo(repoID int64) error {
ctx, committer, err := db.TxContext(db.DefaultContext)
func RecalculateIssueIndexForRepo(ctx context.Context, repoID int64) error {
ctx, committer, err := db.TxContext(ctx)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions models/issues/issue_stats.go
Expand Up @@ -80,9 +80,9 @@ func CountIssues(ctx context.Context, opts *IssuesOptions) (int64, error) {
}

// GetIssueStats returns issue statistic information by given conditions.
func GetIssueStats(opts *IssuesOptions) (*IssueStats, error) {
func GetIssueStats(ctx context.Context, opts *IssuesOptions) (*IssueStats, error) {
if len(opts.IssueIDs) <= MaxQueryParameters {
return getIssueStatsChunk(opts, opts.IssueIDs)
return getIssueStatsChunk(ctx, opts, opts.IssueIDs)
}

// If too long a list of IDs is provided, we get the statistics in
Expand All @@ -95,7 +95,7 @@ func GetIssueStats(opts *IssuesOptions) (*IssueStats, error) {
if chunk > len(opts.IssueIDs) {
chunk = len(opts.IssueIDs)
}
stats, err := getIssueStatsChunk(opts, opts.IssueIDs[i:chunk])
stats, err := getIssueStatsChunk(ctx, opts, opts.IssueIDs[i:chunk])
if err != nil {
return nil, err
}
Expand All @@ -112,10 +112,10 @@ func GetIssueStats(opts *IssuesOptions) (*IssueStats, error) {
return accum, nil
}

func getIssueStatsChunk(opts *IssuesOptions, issueIDs []int64) (*IssueStats, error) {
func getIssueStatsChunk(ctx context.Context, opts *IssuesOptions, issueIDs []int64) (*IssueStats, error) {
stats := &IssueStats{}

sess := db.GetEngine(db.DefaultContext).
sess := db.GetEngine(ctx).
Join("INNER", "repository", "`issue`.repo_id = `repository`.id")

var err error
Expand Down
2 changes: 1 addition & 1 deletion models/issues/issue_test.go
Expand Up @@ -369,7 +369,7 @@ func TestCorrectIssueStats(t *testing.T) {

// Now we will call the GetIssueStats with these IDs and if working,
// get the correct stats back.
issueStats, err := issues_model.GetIssueStats(&issues_model.IssuesOptions{
issueStats, err := issues_model.GetIssueStats(db.DefaultContext, &issues_model.IssuesOptions{
RepoIDs: []int64{1},
IssueIDs: ids,
})
Expand Down
2 changes: 1 addition & 1 deletion models/issues/pull.go
Expand Up @@ -311,7 +311,7 @@ func (pr *PullRequest) LoadRequestedReviewers(ctx context.Context) error {
return nil
}

reviews, err := GetReviewsByIssueID(pr.Issue.ID)
reviews, err := GetReviewsByIssueID(ctx, pr.Issue.ID)
if err != nil {
return err
}
Expand Down
32 changes: 16 additions & 16 deletions models/issues/reaction.go
Expand Up @@ -71,11 +71,11 @@ type Reaction struct {
}

// LoadUser load user of reaction
func (r *Reaction) LoadUser() (*user_model.User, error) {
func (r *Reaction) LoadUser(ctx context.Context) (*user_model.User, error) {
if r.User != nil {
return r.User, nil
}
user, err := user_model.GetUserByID(db.DefaultContext, r.UserID)
user, err := user_model.GetUserByID(ctx, r.UserID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -141,16 +141,16 @@ func (opts *FindReactionsOptions) toConds() builder.Cond {
}

// FindCommentReactions returns a ReactionList of all reactions from an comment
func FindCommentReactions(issueID, commentID int64) (ReactionList, int64, error) {
return FindReactions(db.DefaultContext, FindReactionsOptions{
func FindCommentReactions(ctx context.Context, issueID, commentID int64) (ReactionList, int64, error) {
return FindReactions(ctx, FindReactionsOptions{
IssueID: issueID,
CommentID: commentID,
})
}

// FindIssueReactions returns a ReactionList of all reactions from an issue
func FindIssueReactions(issueID int64, listOptions db.ListOptions) (ReactionList, int64, error) {
return FindReactions(db.DefaultContext, FindReactionsOptions{
func FindIssueReactions(ctx context.Context, issueID int64, listOptions db.ListOptions) (ReactionList, int64, error) {
return FindReactions(ctx, FindReactionsOptions{
ListOptions: listOptions,
IssueID: issueID,
CommentID: -1,
Expand Down Expand Up @@ -218,12 +218,12 @@ type ReactionOptions struct {
}

// CreateReaction creates reaction for issue or comment.
func CreateReaction(opts *ReactionOptions) (*Reaction, error) {
func CreateReaction(ctx context.Context, opts *ReactionOptions) (*Reaction, error) {
if !setting.UI.ReactionsLookup.Contains(opts.Type) {
return nil, ErrForbiddenIssueReaction{opts.Type}
}

ctx, committer, err := db.TxContext(db.DefaultContext)
ctx, committer, err := db.TxContext(ctx)
if err != nil {
return nil, err
}
Expand All @@ -241,17 +241,17 @@ func CreateReaction(opts *ReactionOptions) (*Reaction, error) {
}

// CreateIssueReaction creates a reaction on issue.
func CreateIssueReaction(doerID, issueID int64, content string) (*Reaction, error) {
return CreateReaction(&ReactionOptions{
func CreateIssueReaction(ctx context.Context, doerID, issueID int64, content string) (*Reaction, error) {
return CreateReaction(ctx, &ReactionOptions{
Type: content,
DoerID: doerID,
IssueID: issueID,
})
}

// CreateCommentReaction creates a reaction on comment.
func CreateCommentReaction(doerID, issueID, commentID int64, content string) (*Reaction, error) {
return CreateReaction(&ReactionOptions{
func CreateCommentReaction(ctx context.Context, doerID, issueID, commentID int64, content string) (*Reaction, error) {
return CreateReaction(ctx, &ReactionOptions{
Type: content,
DoerID: doerID,
IssueID: issueID,
Expand Down Expand Up @@ -279,8 +279,8 @@ func DeleteReaction(ctx context.Context, opts *ReactionOptions) error {
}

// DeleteIssueReaction deletes a reaction on issue.
func DeleteIssueReaction(doerID, issueID int64, content string) error {
return DeleteReaction(db.DefaultContext, &ReactionOptions{
func DeleteIssueReaction(ctx context.Context, doerID, issueID int64, content string) error {
return DeleteReaction(ctx, &ReactionOptions{
Type: content,
DoerID: doerID,
IssueID: issueID,
Expand All @@ -289,8 +289,8 @@ func DeleteIssueReaction(doerID, issueID int64, content string) error {
}

// DeleteCommentReaction deletes a reaction on comment.
func DeleteCommentReaction(doerID, issueID, commentID int64, content string) error {
return DeleteReaction(db.DefaultContext, &ReactionOptions{
func DeleteCommentReaction(ctx context.Context, doerID, issueID, commentID int64, content string) error {
return DeleteReaction(ctx, &ReactionOptions{
Type: content,
DoerID: doerID,
IssueID: issueID,
Expand Down

0 comments on commit 6f083d6

Please sign in to comment.