Replace more db.DefaultContext (#27628)

Target #27065
This commit is contained in:
Lunny Xiao 2023-10-15 23:46:06 +08:00 committed by GitHub
parent 7480aacdad
commit cddf245c12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
33 changed files with 99 additions and 85 deletions

View File

@ -5,6 +5,7 @@
package main
import (
"context"
"fmt"
"os"
"path/filepath"
@ -18,7 +19,7 @@ import (
var (
generators = []struct {
gen func() (string, error)
gen func(ctx context.Context) (string, error)
name string
}{
{
@ -41,16 +42,17 @@ func main() {
fmt.Printf("PrepareTestDatabase: %+v\n", err)
os.Exit(1)
}
ctx := context.Background()
if len(os.Args) == 0 {
for _, r := range os.Args {
if err := generate(r); err != nil {
if err := generate(ctx, r); err != nil {
fmt.Printf("generate '%s': %+v\n", r, err)
os.Exit(1)
}
}
} else {
for _, g := range generators {
if err := generate(g.name); err != nil {
if err := generate(ctx, g.name); err != nil {
fmt.Printf("generate '%s': %+v\n", g.name, err)
os.Exit(1)
}
@ -58,10 +60,10 @@ func main() {
}
}
func generate(name string) error {
func generate(ctx context.Context, name string) error {
for _, g := range generators {
if g.name == name {
data, err := g.gen()
data, err := g.gen(ctx)
if err != nil {
return err
}

View File

@ -115,7 +115,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
// Note: db.GetEngine(ctx).Iterate does not get latest data after insert/delete, so we have to call this function
// outside any session scope independently.
func RewriteAllPublicKeys(ctx context.Context) error {
// Don't rewrite key if internal server

View File

@ -40,7 +40,7 @@ import (
const authorizedPrincipalsFile = "authorized_principals"
// RewriteAllPrincipalKeys removes any authorized principal 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
// Note: db.GetEngine(ctx).Iterate does not get latest data after insert/delete, so we have to call this function
// outside any session scope independently.
func RewriteAllPrincipalKeys(ctx context.Context) error {
// Don't rewrite key if internal server

View File

@ -4,6 +4,7 @@
package models
import (
"context"
"fmt"
"strings"
@ -14,15 +15,15 @@ import (
// GetYamlFixturesAccess returns a string containing the contents
// for the access table, as recalculated using repo.RecalculateAccesses()
func GetYamlFixturesAccess() (string, error) {
func GetYamlFixturesAccess(ctx context.Context) (string, error) {
repos := make([]*repo_model.Repository, 0, 50)
if err := db.GetEngine(db.DefaultContext).Find(&repos); err != nil {
if err := db.GetEngine(ctx).Find(&repos); err != nil {
return "", err
}
for _, repo := range repos {
repo.MustOwner(db.DefaultContext)
if err := access_model.RecalculateAccesses(db.DefaultContext, repo); err != nil {
repo.MustOwner(ctx)
if err := access_model.RecalculateAccesses(ctx, repo); err != nil {
return "", err
}
}
@ -30,7 +31,7 @@ func GetYamlFixturesAccess() (string, error) {
var b strings.Builder
accesses := make([]*access_model.Access, 0, 200)
if err := db.GetEngine(db.DefaultContext).OrderBy("user_id, repo_id").Find(&accesses); err != nil {
if err := db.GetEngine(ctx).OrderBy("user_id, repo_id").Find(&accesses); err != nil {
return "", err
}

View File

@ -4,10 +4,12 @@
package models
import (
"context"
"os"
"path/filepath"
"testing"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/util"
@ -17,8 +19,8 @@ import (
func TestFixtureGeneration(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
test := func(gen func() (string, error), name string) {
expected, err := gen()
test := func(ctx context.Context, gen func(ctx context.Context) (string, error), name string) {
expected, err := gen(ctx)
if !assert.NoError(t, err) {
return
}
@ -31,5 +33,5 @@ func TestFixtureGeneration(t *testing.T) {
assert.EqualValues(t, expected, data, "Differences detected for %s", p)
}
test(GetYamlFixturesAccess, "access")
test(db.DefaultContext, GetYamlFixturesAccess, "access")
}

View File

@ -97,8 +97,8 @@ func removeOrgUser(ctx context.Context, orgID, userID int64) error {
}
// RemoveOrgUser removes user from given organization.
func RemoveOrgUser(orgID, userID int64) error {
ctx, committer, err := db.TxContext(db.DefaultContext)
func RemoveOrgUser(ctx context.Context, orgID, userID int64) error {
ctx, committer, err := db.TxContext(ctx)
if err != nil {
return err
}

View File

@ -6,6 +6,7 @@ package models
import (
"testing"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/organization"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
@ -20,7 +21,7 @@ func TestUser_RemoveMember(t *testing.T) {
// remove a user that is a member
unittest.AssertExistsAndLoadBean(t, &organization.OrgUser{UID: 4, OrgID: 3})
prevNumMembers := org.NumMembers
assert.NoError(t, RemoveOrgUser(org.ID, 4))
assert.NoError(t, RemoveOrgUser(db.DefaultContext, org.ID, 4))
unittest.AssertNotExistsBean(t, &organization.OrgUser{UID: 4, OrgID: 3})
org = unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
assert.Equal(t, prevNumMembers-1, org.NumMembers)
@ -28,7 +29,7 @@ func TestUser_RemoveMember(t *testing.T) {
// remove a user that is not a member
unittest.AssertNotExistsBean(t, &organization.OrgUser{UID: 5, OrgID: 3})
prevNumMembers = org.NumMembers
assert.NoError(t, RemoveOrgUser(org.ID, 5))
assert.NoError(t, RemoveOrgUser(db.DefaultContext, org.ID, 5))
unittest.AssertNotExistsBean(t, &organization.OrgUser{UID: 5, OrgID: 3})
org = unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
assert.Equal(t, prevNumMembers, org.NumMembers)
@ -44,7 +45,7 @@ func TestRemoveOrgUser(t *testing.T) {
if unittest.BeanExists(t, &organization.OrgUser{OrgID: orgID, UID: userID}) {
expectedNumMembers--
}
assert.NoError(t, RemoveOrgUser(orgID, userID))
assert.NoError(t, RemoveOrgUser(db.DefaultContext, orgID, userID))
unittest.AssertNotExistsBean(t, &organization.OrgUser{OrgID: orgID, UID: userID})
org = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: orgID})
assert.EqualValues(t, expectedNumMembers, org.NumMembers)
@ -52,7 +53,7 @@ func TestRemoveOrgUser(t *testing.T) {
testSuccess(3, 4)
testSuccess(3, 4)
err := RemoveOrgUser(7, 5)
err := RemoveOrgUser(db.DefaultContext, 7, 5)
assert.Error(t, err)
assert.True(t, organization.IsErrLastOrgOwner(err))
unittest.AssertExistsAndLoadBean(t, &organization.OrgUser{OrgID: 7, UID: 5})

View File

@ -716,7 +716,7 @@ func FindUserCodeAccessibleOwnerRepoIDs(ctx context.Context, ownerID int64, user
}
// GetUserRepositories returns a list of repositories of given user.
func GetUserRepositories(opts *SearchRepoOptions) (RepositoryList, int64, error) {
func GetUserRepositories(ctx context.Context, opts *SearchRepoOptions) (RepositoryList, int64, error) {
if len(opts.OrderBy) == 0 {
opts.OrderBy = "updated_unix DESC"
}
@ -734,7 +734,7 @@ func GetUserRepositories(opts *SearchRepoOptions) (RepositoryList, int64, error)
cond = cond.And(builder.In("lower_name", opts.LowerNames))
}
sess := db.GetEngine(db.DefaultContext)
sess := db.GetEngine(ctx)
count, err := sess.Where(cond).Count(new(Repository))
if err != nil {

View File

@ -107,12 +107,12 @@ func watchRepoMode(ctx context.Context, watch Watch, mode WatchMode) (err error)
}
// WatchRepoMode watch repository in specific mode.
func WatchRepoMode(userID, repoID int64, mode WatchMode) (err error) {
func WatchRepoMode(ctx context.Context, userID, repoID int64, mode WatchMode) (err error) {
var watch Watch
if watch, err = GetWatch(db.DefaultContext, userID, repoID); err != nil {
if watch, err = GetWatch(ctx, userID, repoID); err != nil {
return err
}
return watchRepoMode(db.DefaultContext, watch, mode)
return watchRepoMode(ctx, watch, mode)
}
// WatchRepo watch or unwatch repository.

View File

@ -122,18 +122,18 @@ func TestWatchRepoMode(t *testing.T) {
unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1}, 0)
assert.NoError(t, repo_model.WatchRepoMode(12, 1, repo_model.WatchModeAuto))
assert.NoError(t, repo_model.WatchRepoMode(db.DefaultContext, 12, 1, repo_model.WatchModeAuto))
unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1}, 1)
unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1, Mode: repo_model.WatchModeAuto}, 1)
assert.NoError(t, repo_model.WatchRepoMode(12, 1, repo_model.WatchModeNormal))
assert.NoError(t, repo_model.WatchRepoMode(db.DefaultContext, 12, 1, repo_model.WatchModeNormal))
unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1}, 1)
unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1, Mode: repo_model.WatchModeNormal}, 1)
assert.NoError(t, repo_model.WatchRepoMode(12, 1, repo_model.WatchModeDont))
assert.NoError(t, repo_model.WatchRepoMode(db.DefaultContext, 12, 1, repo_model.WatchModeDont))
unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1}, 1)
unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1, Mode: repo_model.WatchModeDont}, 1)
assert.NoError(t, repo_model.WatchRepoMode(12, 1, repo_model.WatchModeNone))
assert.NoError(t, repo_model.WatchRepoMode(db.DefaultContext, 12, 1, repo_model.WatchModeNone))
unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1}, 0)
}

View File

@ -23,8 +23,8 @@ func init() {
}
// SaveAppStateContent saves the app state item to database
func SaveAppStateContent(key, content string) error {
return db.WithTx(db.DefaultContext, func(ctx context.Context) error {
func SaveAppStateContent(ctx context.Context, key, content string) error {
return db.WithTx(ctx, func(ctx context.Context) error {
eng := db.GetEngine(ctx)
// try to update existing row
res, err := eng.Exec("UPDATE app_state SET revision=revision+1, content=? WHERE id=?", content, key)
@ -43,8 +43,8 @@ func SaveAppStateContent(key, content string) error {
}
// GetAppStateContent gets an app state from database
func GetAppStateContent(key string) (content string, err error) {
e := db.GetEngine(db.DefaultContext)
func GetAppStateContent(ctx context.Context, key string) (content string, err error) {
e := db.GetEngine(ctx)
appState := &AppState{ID: key}
has, err := e.Get(appState)
if err != nil {

View File

@ -3,10 +3,12 @@
package system
import "context"
// StateStore is the interface to get/set app state items
type StateStore interface {
Get(item StateItem) error
Set(item StateItem) error
Get(ctx context.Context, item StateItem) error
Set(ctx context.Context, item StateItem) error
}
// StateItem provides the name for a state item. the name will be used to generate filenames, etc

View File

@ -6,6 +6,7 @@ package system
import (
"testing"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"github.com/stretchr/testify/assert"
@ -40,25 +41,25 @@ func TestAppStateDB(t *testing.T) {
as := &DBStore{}
item1 := new(testItem1)
assert.NoError(t, as.Get(item1))
assert.NoError(t, as.Get(db.DefaultContext, item1))
assert.Equal(t, "", item1.Val1)
assert.EqualValues(t, 0, item1.Val2)
item1 = new(testItem1)
item1.Val1 = "a"
item1.Val2 = 2
assert.NoError(t, as.Set(item1))
assert.NoError(t, as.Set(db.DefaultContext, item1))
item2 := new(testItem2)
item2.K = "V"
assert.NoError(t, as.Set(item2))
assert.NoError(t, as.Set(db.DefaultContext, item2))
item1 = new(testItem1)
assert.NoError(t, as.Get(item1))
assert.NoError(t, as.Get(db.DefaultContext, item1))
assert.Equal(t, "a", item1.Val1)
assert.EqualValues(t, 2, item1.Val2)
item2 = new(testItem2)
assert.NoError(t, as.Get(item2))
assert.NoError(t, as.Get(db.DefaultContext, item2))
assert.Equal(t, "V", item2.K)
}

View File

@ -4,6 +4,8 @@
package system
import (
"context"
"code.gitea.io/gitea/models/system"
"code.gitea.io/gitea/modules/json"
@ -14,8 +16,8 @@ import (
type DBStore struct{}
// Get reads the state item
func (f *DBStore) Get(item StateItem) error {
content, err := system.GetAppStateContent(item.Name())
func (f *DBStore) Get(ctx context.Context, item StateItem) error {
content, err := system.GetAppStateContent(ctx, item.Name())
if err != nil {
return err
}
@ -26,10 +28,10 @@ func (f *DBStore) Get(item StateItem) error {
}
// Set saves the state item
func (f *DBStore) Set(item StateItem) error {
func (f *DBStore) Set(ctx context.Context, item StateItem) error {
b, err := json.Marshal(item)
if err != nil {
return err
}
return system.SaveAppStateContent(item.Name(), util.BytesToReadOnlyString(b))
return system.SaveAppStateContent(ctx, item.Name(), util.BytesToReadOnlyString(b))
}

View File

@ -4,6 +4,7 @@
package updatechecker
import (
"context"
"io"
"net/http"
@ -58,31 +59,31 @@ func GiteaUpdateChecker(httpEndpoint string) error {
return err
}
return UpdateRemoteVersion(respData.Latest.Version)
return UpdateRemoteVersion(req.Context(), respData.Latest.Version)
}
// UpdateRemoteVersion updates the latest available version of Gitea
func UpdateRemoteVersion(version string) (err error) {
return system.AppState.Set(&CheckerState{LatestVersion: version})
func UpdateRemoteVersion(ctx context.Context, version string) (err error) {
return system.AppState.Set(ctx, &CheckerState{LatestVersion: version})
}
// GetRemoteVersion returns the current remote version (or currently installed version if fail to fetch from DB)
func GetRemoteVersion() string {
func GetRemoteVersion(ctx context.Context) string {
item := new(CheckerState)
if err := system.AppState.Get(item); err != nil {
if err := system.AppState.Get(ctx, item); err != nil {
return ""
}
return item.LatestVersion
}
// GetNeedUpdate returns true whether a newer version of Gitea is available
func GetNeedUpdate() bool {
func GetNeedUpdate(ctx context.Context) bool {
curVer, err := version.NewVersion(setting.AppVer)
if err != nil {
// return false to fail silently
return false
}
remoteVerStr := GetRemoteVersion()
remoteVerStr := GetRemoteVersion(ctx)
if remoteVerStr == "" {
// no remote version is known
return false

View File

@ -318,7 +318,7 @@ func DeleteMember(ctx *context.APIContext) {
if ctx.Written() {
return
}
if err := models.RemoveOrgUser(ctx.Org.Organization.ID, member.ID); err != nil {
if err := models.RemoveOrgUser(ctx, ctx.Org.Organization.ID, member.ID); err != nil {
ctx.Error(http.StatusInternalServerError, "RemoveOrgUser", err)
}
ctx.Status(http.StatusNoContent)

View File

@ -385,7 +385,7 @@ func Delete(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
if err := org.DeleteOrganization(ctx.Org.Organization); err != nil {
if err := org.DeleteOrganization(ctx, ctx.Org.Organization); err != nil {
ctx.Error(http.StatusInternalServerError, "DeleteOrganization", err)
return
}

View File

@ -21,7 +21,7 @@ import (
func listUserRepos(ctx *context.APIContext, u *user_model.User, private bool) {
opts := utils.GetListOptions(ctx)
repos, count, err := repo_model.GetUserRepositories(&repo_model.SearchRepoOptions{
repos, count, err := repo_model.GetUserRepositories(ctx, &repo_model.SearchRepoOptions{
Actor: u,
Private: private,
ListOptions: opts,

View File

@ -72,7 +72,7 @@ func mustInitCtx(ctx context.Context, fn func(ctx context.Context) error) {
func syncAppConfForGit(ctx context.Context) error {
runtimeState := new(system.RuntimeState)
if err := system.AppState.Get(runtimeState); err != nil {
if err := system.AppState.Get(ctx, runtimeState); err != nil {
return err
}
@ -95,7 +95,7 @@ func syncAppConfForGit(ctx context.Context) error {
log.Info("re-write ssh public keys ...")
mustInitCtx(ctx, asymkey_model.RewriteAllPublicKeys)
return system.AppState.Set(runtimeState)
return system.AppState.Set(ctx, runtimeState)
}
return nil
}

View File

@ -127,8 +127,8 @@ func prepareDeprecatedWarningsAlert(ctx *context.Context) {
func Dashboard(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("admin.dashboard")
ctx.Data["PageIsAdminDashboard"] = true
ctx.Data["NeedUpdate"] = updatechecker.GetNeedUpdate()
ctx.Data["RemoteVersion"] = updatechecker.GetRemoteVersion()
ctx.Data["NeedUpdate"] = updatechecker.GetNeedUpdate(ctx)
ctx.Data["RemoteVersion"] = updatechecker.GetRemoteVersion(ctx)
// FIXME: update periodically
updateSystemStatus()
ctx.Data["SysStatus"] = sysStatus

View File

@ -104,14 +104,14 @@ func MembersAction(ctx *context.Context) {
ctx.Error(http.StatusNotFound)
return
}
err = models.RemoveOrgUser(org.ID, uid)
err = models.RemoveOrgUser(ctx, org.ID, uid)
if organization.IsErrLastOrgOwner(err) {
ctx.Flash.Error(ctx.Tr("form.last_org_owner"))
ctx.JSONRedirect(ctx.Org.OrgLink + "/members")
return
}
case "leave":
err = models.RemoveOrgUser(org.ID, ctx.Doer.ID)
err = models.RemoveOrgUser(ctx, org.ID, ctx.Doer.ID)
if err == nil {
ctx.Flash.Success(ctx.Tr("form.organization_leave_success", org.DisplayName()))
ctx.JSON(http.StatusOK, map[string]any{

View File

@ -124,7 +124,7 @@ func SettingsPost(ctx *context.Context) {
// update forks visibility
if visibilityChanged {
repos, _, err := repo_model.GetUserRepositories(&repo_model.SearchRepoOptions{
repos, _, err := repo_model.GetUserRepositories(ctx, &repo_model.SearchRepoOptions{
Actor: org.AsUser(), Private: true, ListOptions: db.ListOptions{Page: 1, PageSize: org.NumRepos},
})
if err != nil {
@ -180,7 +180,7 @@ func SettingsDelete(ctx *context.Context) {
return
}
if err := org_service.DeleteOrganization(ctx.Org.Organization); err != nil {
if err := org_service.DeleteOrganization(ctx, ctx.Org.Organization); err != nil {
if models.IsErrUserOwnRepos(err) {
ctx.Flash.Error(ctx.Tr("form.org_still_own_repo"))
ctx.Redirect(ctx.Org.OrgLink + "/settings/delete")

View File

@ -7,6 +7,7 @@ import (
"net/http"
"testing"
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/contexttest"
@ -25,7 +26,7 @@ func TestArchivedIssues(t *testing.T) {
ctx.Req.Form.Set("state", "open")
// Assume: User 30 has access to two Repos with Issues, one of the Repos being archived.
repos, _, _ := repo_model.GetUserRepositories(&repo_model.SearchRepoOptions{Actor: ctx.Doer})
repos, _, _ := repo_model.GetUserRepositories(db.DefaultContext, &repo_model.SearchRepoOptions{Actor: ctx.Doer})
assert.Len(t, repos, 3)
IsArchived := make(map[int64]bool)
NumIssues := make(map[int64]int)

View File

@ -383,7 +383,7 @@ func PackageSettings(ctx *context.Context) {
ctx.Data["IsPackagesPage"] = true
ctx.Data["PackageDescriptor"] = pd
repos, _, _ := repo_model.GetUserRepositories(&repo_model.SearchRepoOptions{
repos, _, _ := repo_model.GetUserRepositories(ctx, &repo_model.SearchRepoOptions{
Actor: pd.Owner,
Private: true,
})

View File

@ -287,7 +287,7 @@ func Repos(ctx *context.Context) {
return
}
userRepos, _, err := repo_model.GetUserRepositories(&repo_model.SearchRepoOptions{
userRepos, _, err := repo_model.GetUserRepositories(ctx, &repo_model.SearchRepoOptions{
Actor: ctxUser,
Private: true,
ListOptions: db.ListOptions{
@ -312,7 +312,7 @@ func Repos(ctx *context.Context) {
ctx.Data["Dirs"] = repoNames
ctx.Data["ReposMap"] = repos
} else {
repos, count64, err := repo_model.GetUserRepositories(&repo_model.SearchRepoOptions{Actor: ctxUser, Private: true, ListOptions: opts})
repos, count64, err := repo_model.GetUserRepositories(ctx, &repo_model.SearchRepoOptions{Actor: ctxUser, Private: true, ListOptions: opts})
if err != nil {
ctx.ServerError("GetUserRepositories", err)
return

View File

@ -4,9 +4,9 @@
package auth
import (
"context"
"net/http"
"code.gitea.io/gitea/models/db"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/log"
)
@ -29,7 +29,7 @@ func (s *Session) Name() string {
// object for that uid.
// Returns nil if there is no user uid stored in the session.
func (s *Session) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) (*user_model.User, error) {
user := SessionUser(sess)
user := SessionUser(req.Context(), sess)
if user != nil {
return user, nil
}
@ -37,7 +37,7 @@ func (s *Session) Verify(req *http.Request, w http.ResponseWriter, store DataSto
}
// SessionUser returns the user object corresponding to the "uid" session variable.
func SessionUser(sess SessionStore) *user_model.User {
func SessionUser(ctx context.Context, sess SessionStore) *user_model.User {
if sess == nil {
return nil
}
@ -55,7 +55,7 @@ func SessionUser(sess SessionStore) *user_model.User {
}
// Get user object
user, err := user_model.GetUserByID(db.DefaultContext, id)
user, err := user_model.GetUserByID(ctx, id)
if err != nil {
if !user_model.IsErrUserNotExist(err) {
log.Error("GetUserById: %v", err)

View File

@ -19,8 +19,8 @@ import (
)
// DeleteOrganization completely and permanently deletes everything of organization.
func DeleteOrganization(org *org_model.Organization) error {
ctx, commiter, err := db.TxContext(db.DefaultContext)
func DeleteOrganization(ctx context.Context, org *org_model.Organization) error {
ctx, commiter, err := db.TxContext(ctx)
if err != nil {
return err
}

View File

@ -7,6 +7,7 @@ import (
"testing"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/organization"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
@ -21,17 +22,17 @@ func TestMain(m *testing.M) {
func TestDeleteOrganization(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 6})
assert.NoError(t, DeleteOrganization(org))
assert.NoError(t, DeleteOrganization(db.DefaultContext, org))
unittest.AssertNotExistsBean(t, &organization.Organization{ID: 6})
unittest.AssertNotExistsBean(t, &organization.OrgUser{OrgID: 6})
unittest.AssertNotExistsBean(t, &organization.Team{OrgID: 6})
org = unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
err := DeleteOrganization(org)
err := DeleteOrganization(db.DefaultContext, org)
assert.Error(t, err)
assert.True(t, models.IsErrUserOwnRepos(err))
user := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 5})
assert.Error(t, DeleteOrganization(user))
assert.Error(t, DeleteOrganization(db.DefaultContext, user))
unittest.CheckConsistencyFor(t, &user_model.User{}, &organization.Team{})
}

View File

@ -259,7 +259,7 @@ func checkUnadoptedRepositories(ctx context.Context, userName string, repoNamesT
}
return err
}
repos, _, err := repo_model.GetUserRepositories(&repo_model.SearchRepoOptions{
repos, _, err := repo_model.GetUserRepositories(ctx, &repo_model.SearchRepoOptions{
Actor: ctxUser,
Private: true,
ListOptions: db.ListOptions{

View File

@ -48,10 +48,11 @@ func runMigrateTask(ctx context.Context, t *admin_model.Task) (err error) {
log.Critical("PANIC during runMigrateTask[%d] by DoerID[%d] to RepoID[%d] for OwnerID[%d]: %v\nStacktrace: %v", t.ID, t.DoerID, t.RepoID, t.OwnerID, e, log.Stack(2))
}
// fixme: Because ctx is canceled here, so the db.DefaultContext is needed.
ctx := db.DefaultContext
if err == nil {
err = admin_model.FinishMigrateTask(db.DefaultContext, t)
err = admin_model.FinishMigrateTask(ctx, t)
if err == nil {
notify_service.MigrateRepository(db.DefaultContext, t.Doer, t.Owner, t.Repo)
notify_service.MigrateRepository(ctx, t.Doer, t.Owner, t.Repo)
return
}
@ -63,8 +64,7 @@ func runMigrateTask(ctx context.Context, t *admin_model.Task) (err error) {
t.EndTime = timeutil.TimeStampNow()
t.Status = structs.TaskStatusFailed
t.Message = err.Error()
// fixme: Because ctx is canceled here, so the db.DefaultContext is needed.
if err := t.UpdateCols(db.DefaultContext, "status", "message", "end_time"); err != nil {
if err := t.UpdateCols(ctx, "status", "message", "end_time"); err != nil {
log.Error("Task UpdateCols failed: %v", err)
}

View File

@ -159,7 +159,7 @@ func DeleteUser(ctx context.Context, u *user_model.User, purge bool) error {
// An alternative option here would be write a DeleteAllRepositoriesForUserID function which would delete all of the repos
// but such a function would likely get out of date
for {
repos, _, err := repo_model.GetUserRepositories(&repo_model.SearchRepoOptions{
repos, _, err := repo_model.GetUserRepositories(ctx, &repo_model.SearchRepoOptions{
ListOptions: db.ListOptions{
PageSize: repo_model.RepositoryListDefaultPageSize,
Page: 1,
@ -204,7 +204,7 @@ func DeleteUser(ctx context.Context, u *user_model.User, purge bool) error {
break
}
for _, org := range orgs {
if err := models.RemoveOrgUser(org.ID, u.ID); err != nil {
if err := models.RemoveOrgUser(ctx, org.ID, u.ID); err != nil {
if organization.IsErrLastOrgOwner(err) {
err = organization.DeleteOrganization(ctx, org)
}

View File

@ -41,7 +41,7 @@ func TestDeleteUser(t *testing.T) {
orgUsers := make([]*organization.OrgUser, 0, 10)
assert.NoError(t, db.GetEngine(db.DefaultContext).Find(&orgUsers, &organization.OrgUser{UID: userID}))
for _, orgUser := range orgUsers {
if err := models.RemoveOrgUser(orgUser.OrgID, orgUser.UID); err != nil {
if err := models.RemoveOrgUser(db.DefaultContext, orgUser.OrgID, orgUser.UID); err != nil {
assert.True(t, organization.IsErrLastOrgOwner(err))
return
}

View File

@ -430,7 +430,7 @@ func TestLDAPGroupTeamSyncAddMember(t *testing.T) {
assert.True(t, isMember, "Membership should be added to the right team")
err = models.RemoveTeamMember(db.DefaultContext, team, user.ID)
assert.NoError(t, err)
err = models.RemoveOrgUser(usersOrgs[0].ID, user.ID)
err = models.RemoveOrgUser(db.DefaultContext, usersOrgs[0].ID, user.ID)
assert.NoError(t, err)
} else {
// assert members of LDAP group "cn=admin_staff" keep initial team membership since mapped team does not exist