Skip to content

Commit

Permalink
all: unwrap database.RepositoriesStore interface (#7706)
Browse files Browse the repository at this point in the history
  • Loading branch information
unknwon committed Mar 27, 2024
1 parent e1e75ed commit 4d05804
Show file tree
Hide file tree
Showing 21 changed files with 289 additions and 1,463 deletions.
2 changes: 1 addition & 1 deletion internal/context/go_get.go
Expand Up @@ -29,7 +29,7 @@ func ServeGoGet() macaron.Handler {

owner, err := database.Users.GetByUsername(c.Req.Context(), ownerName)
if err == nil {
repo, err := database.Repos.GetByName(c.Req.Context(), owner.ID, repoName)
repo, err := database.Handle.Repositories().GetByName(c.Req.Context(), owner.ID, repoName)
if err == nil && repo.DefaultBranch != "" {
branchName = repo.DefaultBranch
}
Expand Down
2 changes: 1 addition & 1 deletion internal/context/repo.go
Expand Up @@ -403,7 +403,7 @@ func RepoRef() macaron.Handler {
c.Data["IsViewCommit"] = c.Repo.IsViewCommit

// People who have push access or have forked repository can propose a new pull request.
if c.Repo.IsWriter() || (c.IsLogged && database.Repos.HasForkedBy(c.Req.Context(), c.Repo.Repository.ID, c.User.ID)) {
if c.Repo.IsWriter() || (c.IsLogged && database.Handle.Repositories().HasForkedBy(c.Req.Context(), c.Repo.Repository.ID, c.User.ID)) {
// Pull request is allowed if this is a fork repository
// and base repository accepts pull requests.
if c.Repo.Repository.BaseRepo != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/database/actions.go
Expand Up @@ -124,7 +124,7 @@ func (s *ActionsStore) ListByUser(ctx context.Context, userID, actorID, afterID

// notifyWatchers creates rows in action table for watchers who are able to see the action.
func (s *ActionsStore) notifyWatchers(ctx context.Context, act *Action) error {
watches, err := NewReposStore(s.db).ListWatches(ctx, act.RepoID)
watches, err := newReposStore(s.db).ListWatches(ctx, act.RepoID)
if err != nil {
return errors.Wrap(err, "list watches")
}
Expand Down Expand Up @@ -465,7 +465,7 @@ type CommitRepoOptions struct {
// regular push also creates a new branch, then another action with type
// ActionCreateBranch is created.
func (s *ActionsStore) CommitRepo(ctx context.Context, opts CommitRepoOptions) error {
err := NewReposStore(s.db).Touch(ctx, opts.Repo.ID)
err := newReposStore(s.db).Touch(ctx, opts.Repo.ID)
if err != nil {
return errors.Wrap(err, "touch repository")
}
Expand Down Expand Up @@ -612,7 +612,7 @@ type PushTagOptions struct {
// the type ActionDeleteTag is created if the push deletes a tag. Otherwise, an
// action with the type ActionPushTag is created for a regular push.
func (s *ActionsStore) PushTag(ctx context.Context, opts PushTagOptions) error {
err := NewReposStore(s.db).Touch(ctx, opts.Repo.ID)
err := newReposStore(s.db).Touch(ctx, opts.Repo.ID)
if err != nil {
return errors.Wrap(err, "touch repository")
}
Expand Down
18 changes: 9 additions & 9 deletions internal/database/actions_test.go
Expand Up @@ -135,7 +135,7 @@ func TestActions(t *testing.T) {
func actionsCommitRepo(t *testing.T, ctx context.Context, s *ActionsStore) {
alice, err := NewUsersStore(s.db).Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
require.NoError(t, err)
repo, err := NewReposStore(s.db).Create(ctx,
repo, err := newReposStore(s.db).Create(ctx,
alice.ID,
CreateRepoOptions{
Name: "example",
Expand Down Expand Up @@ -438,7 +438,7 @@ func actionsListByUser(t *testing.T, ctx context.Context, s *ActionsStore) {
func actionsMergePullRequest(t *testing.T, ctx context.Context, s *ActionsStore) {
alice, err := NewUsersStore(s.db).Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
require.NoError(t, err)
repo, err := NewReposStore(s.db).Create(ctx,
repo, err := newReposStore(s.db).Create(ctx,
alice.ID,
CreateRepoOptions{
Name: "example",
Expand Down Expand Up @@ -483,7 +483,7 @@ func actionsMergePullRequest(t *testing.T, ctx context.Context, s *ActionsStore)
func actionsMirrorSyncCreate(t *testing.T, ctx context.Context, s *ActionsStore) {
alice, err := NewUsersStore(s.db).Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
require.NoError(t, err)
repo, err := NewReposStore(s.db).Create(ctx,
repo, err := newReposStore(s.db).Create(ctx,
alice.ID,
CreateRepoOptions{
Name: "example",
Expand Down Expand Up @@ -524,7 +524,7 @@ func actionsMirrorSyncCreate(t *testing.T, ctx context.Context, s *ActionsStore)
func actionsMirrorSyncDelete(t *testing.T, ctx context.Context, s *ActionsStore) {
alice, err := NewUsersStore(s.db).Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
require.NoError(t, err)
repo, err := NewReposStore(s.db).Create(ctx,
repo, err := newReposStore(s.db).Create(ctx,
alice.ID,
CreateRepoOptions{
Name: "example",
Expand Down Expand Up @@ -565,7 +565,7 @@ func actionsMirrorSyncDelete(t *testing.T, ctx context.Context, s *ActionsStore)
func actionsMirrorSyncPush(t *testing.T, ctx context.Context, s *ActionsStore) {
alice, err := NewUsersStore(s.db).Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
require.NoError(t, err)
repo, err := NewReposStore(s.db).Create(ctx,
repo, err := newReposStore(s.db).Create(ctx,
alice.ID,
CreateRepoOptions{
Name: "example",
Expand Down Expand Up @@ -630,7 +630,7 @@ func actionsMirrorSyncPush(t *testing.T, ctx context.Context, s *ActionsStore) {
func actionsNewRepo(t *testing.T, ctx context.Context, s *ActionsStore) {
alice, err := NewUsersStore(s.db).Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
require.NoError(t, err)
repo, err := NewReposStore(s.db).Create(ctx,
repo, err := newReposStore(s.db).Create(ctx,
alice.ID,
CreateRepoOptions{
Name: "example",
Expand Down Expand Up @@ -709,7 +709,7 @@ func actionsPushTag(t *testing.T, ctx context.Context, s *ActionsStore) {

alice, err := NewUsersStore(s.db).Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
require.NoError(t, err)
repo, err := NewReposStore(s.db).Create(ctx,
repo, err := newReposStore(s.db).Create(ctx,
alice.ID,
CreateRepoOptions{
Name: "example",
Expand Down Expand Up @@ -801,7 +801,7 @@ func actionsPushTag(t *testing.T, ctx context.Context, s *ActionsStore) {
func actionsRenameRepo(t *testing.T, ctx context.Context, s *ActionsStore) {
alice, err := NewUsersStore(s.db).Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
require.NoError(t, err)
repo, err := NewReposStore(s.db).Create(ctx,
repo, err := newReposStore(s.db).Create(ctx,
alice.ID,
CreateRepoOptions{
Name: "example",
Expand Down Expand Up @@ -840,7 +840,7 @@ func actionsTransferRepo(t *testing.T, ctx context.Context, s *ActionsStore) {
require.NoError(t, err)
bob, err := NewUsersStore(s.db).Create(ctx, "bob", "bob@example.com", CreateUserOptions{})
require.NoError(t, err)
repo, err := NewReposStore(s.db).Create(ctx,
repo, err := newReposStore(s.db).Create(ctx,
alice.ID,
CreateRepoOptions{
Name: "example",
Expand Down
5 changes: 4 additions & 1 deletion internal/database/database.go
Expand Up @@ -123,7 +123,6 @@ func NewConnection(w logger.Writer) (*gorm.DB, error) {
}

// Initialize stores, sorted in alphabetical order.
Repos = NewReposStore(db)
TwoFactors = &twoFactorsStore{DB: db}
Users = NewUsersStore(db)

Expand Down Expand Up @@ -183,3 +182,7 @@ func (db *DB) Permissions() *PermissionsStore {
func (db *DB) PublicKey() *PublicKeysStore {
return newPublicKeysStore(db.db)
}

func (db *DB) Repositories() *RepositoriesStore {
return newReposStore(db.db)
}
8 changes: 0 additions & 8 deletions internal/database/mocks.go
Expand Up @@ -8,14 +8,6 @@ import (
"testing"
)

func SetMockReposStore(t *testing.T, mock ReposStore) {
before := Repos
Repos = mock
t.Cleanup(func() {
Repos = before
})
}

func SetMockTwoFactorsStore(t *testing.T, mock TwoFactorsStore) {
before := TwoFactors
TwoFactors = mock
Expand Down

0 comments on commit 4d05804

Please sign in to comment.