1
0
Fork 0
forked from forgejo/forgejo

refactor some functions to support ctx as first parameter (#21878)

Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Co-authored-by: Lauris BH <lauris@nix.lv>
This commit is contained in:
Lunny Xiao 2022-12-03 10:48:26 +08:00 committed by GitHub
parent 8698458f48
commit 0a7d3ff786
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
145 changed files with 360 additions and 369 deletions

View file

@ -44,7 +44,7 @@ func GetCollaborators(ctx context.Context, repoID int64, listOptions db.ListOpti
collaborators := make([]*Collaborator, 0, len(collaborations))
for _, c := range collaborations {
user, err := user_model.GetUserByIDCtx(ctx, c.UserID)
user, err := user_model.GetUserByID(ctx, c.UserID)
if err != nil {
if user_model.IsErrUserNotExist(err) {
log.Warn("Inconsistent DB: User: %d is listed as collaborator of %-v but does not exist", c.UserID, repoID)

View file

@ -17,14 +17,14 @@ func TestGetUserFork(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
// User13 has repo 11 forked from repo10
repo, err := repo_model.GetRepositoryByID(10)
repo, err := repo_model.GetRepositoryByID(db.DefaultContext, 10)
assert.NoError(t, err)
assert.NotNil(t, repo)
repo, err = repo_model.GetUserFork(db.DefaultContext, repo.ID, 13)
assert.NoError(t, err)
assert.NotNil(t, repo)
repo, err = repo_model.GetRepositoryByID(9)
repo, err = repo_model.GetRepositoryByID(db.DefaultContext, 9)
assert.NoError(t, err)
assert.NotNil(t, repo)
repo, err = repo_model.GetUserFork(db.DefaultContext, repo.ID, 13)

View file

@ -26,7 +26,7 @@ func (repo *Repository) CanEnableTimetracker() bool {
}
// IsTimetrackerEnabled returns whether or not the timetracker is enabled. It returns the default value from config if an error occurs.
func (repo *Repository) IsTimetrackerEnabled() bool {
func (repo *Repository) IsTimetrackerEnabled() bool { // Notice: It will be used in template so don't remove directly
return repo.IsTimetrackerEnabledCtx(db.DefaultContext)
}

View file

@ -52,7 +52,7 @@ func (m *Mirror) GetRepository() *Repository {
return m.Repo
}
var err error
m.Repo, err = GetRepositoryByIDCtx(db.DefaultContext, m.RepoID)
m.Repo, err = GetRepositoryByID(db.DefaultContext, m.RepoID)
if err != nil {
log.Error("getRepositoryByID[%d]: %v", m.ID, err)
}

View file

@ -61,7 +61,7 @@ func (m *PushMirror) GetRepository() *Repository {
return m.Repo
}
var err error
m.Repo, err = GetRepositoryByIDCtx(db.DefaultContext, m.RepoID)
m.Repo, err = GetRepositoryByID(db.DefaultContext, m.RepoID)
if err != nil {
log.Error("getRepositoryByID[%d]: %v", m.ID, err)
}

View file

@ -93,13 +93,13 @@ func init() {
func (r *Release) LoadAttributes(ctx context.Context) error {
var err error
if r.Repo == nil {
r.Repo, err = GetRepositoryByIDCtx(ctx, r.RepoID)
r.Repo, err = GetRepositoryByID(ctx, r.RepoID)
if err != nil {
return err
}
}
if r.Publisher == nil {
r.Publisher, err = user_model.GetUserByIDCtx(ctx, r.PublisherID)
r.Publisher, err = user_model.GetUserByID(ctx, r.PublisherID)
if err != nil {
if user_model.IsErrUserNotExist(err) {
r.Publisher = user_model.NewGhostUser()

View file

@ -315,7 +315,7 @@ func (repo *Repository) LoadUnits(ctx context.Context) (err error) {
}
// UnitEnabled if this repository has the given unit enabled
func (repo *Repository) UnitEnabled(tp unit.Type) (result bool) {
func (repo *Repository) UnitEnabled(tp unit.Type) (result bool) { // Notice: Don't remove this function directly, because it has been used in go template.
return repo.UnitEnabledCtx(db.DefaultContext, tp)
}
@ -390,7 +390,7 @@ func (repo *Repository) GetOwner(ctx context.Context) (err error) {
return nil
}
repo.Owner, err = user_model.GetUserByIDCtx(ctx, repo.OwnerID)
repo.Owner, err = user_model.GetUserByID(ctx, repo.OwnerID)
return err
}
@ -467,16 +467,12 @@ func (repo *Repository) ComposeDocumentMetas() map[string]string {
// GetBaseRepo populates repo.BaseRepo for a fork repository and
// returns an error on failure (NOTE: no error is returned for
// non-fork repositories, and BaseRepo will be left untouched)
func (repo *Repository) GetBaseRepo() (err error) {
return repo.getBaseRepo(db.DefaultContext)
}
func (repo *Repository) getBaseRepo(ctx context.Context) (err error) {
func (repo *Repository) GetBaseRepo(ctx context.Context) (err error) {
if !repo.IsFork {
return nil
}
repo.BaseRepo, err = GetRepositoryByIDCtx(ctx, repo.ForkID)
repo.BaseRepo, err = GetRepositoryByID(ctx, repo.ForkID)
return err
}
@ -611,11 +607,6 @@ func (repo *Repository) GetTrustModel() TrustModelType {
return trustModel
}
// GetRepositoryByOwnerAndName returns the repository by given ownername and reponame.
func GetRepositoryByOwnerAndName(ownerName, repoName string) (*Repository, error) {
return GetRepositoryByOwnerAndNameCtx(db.DefaultContext, ownerName, repoName)
}
// __________ .__ __
// \______ \ ____ ______ ____ _____|__|/ |_ ___________ ___.__.
// | _// __ \\____ \ / _ \/ ___/ \ __\/ _ \_ __ < | |
@ -647,8 +638,8 @@ func (err ErrRepoNotExist) Unwrap() error {
return util.ErrNotExist
}
// GetRepositoryByOwnerAndNameCtx returns the repository by given owner name and repo name
func GetRepositoryByOwnerAndNameCtx(ctx context.Context, ownerName, repoName string) (*Repository, error) {
// GetRepositoryByOwnerAndName returns the repository by given owner name and repo name
func GetRepositoryByOwnerAndName(ctx context.Context, ownerName, repoName string) (*Repository, error) {
var repo Repository
has, err := db.GetEngine(ctx).Table("repository").Select("repository.*").
Join("INNER", "`user`", "`user`.id = repository.owner_id").
@ -678,8 +669,8 @@ func GetRepositoryByName(ownerID int64, name string) (*Repository, error) {
return repo, err
}
// GetRepositoryByIDCtx returns the repository by given id if exists.
func GetRepositoryByIDCtx(ctx context.Context, id int64) (*Repository, error) {
// GetRepositoryByID returns the repository by given id if exists.
func GetRepositoryByID(ctx context.Context, id int64) (*Repository, error) {
repo := new(Repository)
has, err := db.GetEngine(ctx).ID(id).Get(repo)
if err != nil {
@ -690,11 +681,6 @@ func GetRepositoryByIDCtx(ctx context.Context, id int64) (*Repository, error) {
return repo, nil
}
// GetRepositoryByID returns the repository by given id if exists.
func GetRepositoryByID(id int64) (*Repository, error) {
return GetRepositoryByIDCtx(db.DefaultContext, id)
}
// GetRepositoriesMapByIDs returns the repositories by given id slice.
func GetRepositoriesMapByIDs(ids []int64) (map[int64]*Repository, error) {
repos := make(map[int64]*Repository, len(ids))
@ -722,7 +708,7 @@ func GetTemplateRepo(ctx context.Context, repo *Repository) (*Repository, error)
return nil, nil
}
return GetRepositoryByIDCtx(ctx, repo.TemplateID)
return GetRepositoryByID(ctx, repo.TemplateID)
}
// TemplateRepo returns the repository, which is template of this repository

View file

@ -115,7 +115,7 @@ func TestMetas(t *testing.T) {
externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markup.IssueNameStyleRegexp
testSuccess(markup.IssueNameStyleRegexp)
repo, err := repo_model.GetRepositoryByID(3)
repo, err := repo_model.GetRepositoryByID(db.DefaultContext, 3)
assert.NoError(t, err)
metas = repo.ComposeMetas()