Skip to content

db: migrate org.go to organizations.go with GORM #7538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 29 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixup
  • Loading branch information
unknwon committed Dec 17, 2023
commit 371c56e3063848dab049b636085062b68ba6a280
2 changes: 1 addition & 1 deletion internal/context/go_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func ServeGoGet() macaron.Handler {

owner, err := db.Users.GetByUsername(c.Req.Context(), ownerName)
if err == nil {
repo, err := db.Repos.GetByName(c.Req.Context(), owner.ID, repoName)
repo, err := db.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/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func HandleOrgAssignment(c *Context, args ...bool) {
c.Org.IsTeamMember = true
c.Org.IsTeamAdmin = true
} else {
c.Org.IsMember, _ = db.Orgs.HasMember(c.Req.Context(), org.ID, c.User.ID)
c.Org.IsMember, _ = db.Organizations.HasMember(c.Req.Context(), org.ID, c.User.ID)
}
} else {
// Fake data.
Expand Down
2 changes: 1 addition & 1 deletion internal/context/repo.go
Original file line number Diff line number Diff line change
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 && db.Repos.HasForkedBy(c.Req.Context(), c.Repo.Repository.ID, c.User.ID)) {
if c.Repo.IsWriter() || (c.IsLogged && db.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/db/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (db *actions) ListByUser(ctx context.Context, userID, actorID, afterID int6

// notifyWatchers creates rows in action table for watchers who are able to see the action.
func (db *actions) notifyWatchers(ctx context.Context, act *Action) error {
watches, err := NewReposStore(db.DB).ListWatches(ctx, act.RepoID)
watches, err := NewRepositoriesStore(db.DB).ListWatches(ctx, act.RepoID)
if err != nil {
return errors.Wrap(err, "list watches")
}
Expand Down Expand Up @@ -489,7 +489,7 @@ type CommitRepoOptions struct {
}

func (db *actions) CommitRepo(ctx context.Context, opts CommitRepoOptions) error {
err := NewReposStore(db.DB).Touch(ctx, opts.Repo.ID)
err := NewRepositoriesStore(db.DB).Touch(ctx, opts.Repo.ID)
if err != nil {
return errors.Wrap(err, "touch repository")
}
Expand Down Expand Up @@ -633,7 +633,7 @@ type PushTagOptions struct {
}

func (db *actions) PushTag(ctx context.Context, opts PushTagOptions) error {
err := NewReposStore(db.DB).Touch(ctx, opts.Repo.ID)
err := NewRepositoriesStore(db.DB).Touch(ctx, opts.Repo.ID)
if err != nil {
return errors.Wrap(err, "touch repository")
}
Expand Down
18 changes: 9 additions & 9 deletions internal/db/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestActions(t *testing.T) {
func actionsCommitRepo(t *testing.T, ctx context.Context, db *actions) {
alice, err := NewUsersStore(db.DB).Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
require.NoError(t, err)
repo, err := NewReposStore(db.DB).Create(ctx,
repo, err := NewRepositoriesStore(db.DB).Create(ctx,
alice.ID,
CreateRepoOptions{
Name: "example",
Expand Down Expand Up @@ -440,7 +440,7 @@ func actionsListByUser(t *testing.T, ctx context.Context, db *actions) {
func actionsMergePullRequest(t *testing.T, ctx context.Context, db *actions) {
alice, err := NewUsersStore(db.DB).Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
require.NoError(t, err)
repo, err := NewReposStore(db.DB).Create(ctx,
repo, err := NewRepositoriesStore(db.DB).Create(ctx,
alice.ID,
CreateRepoOptions{
Name: "example",
Expand Down Expand Up @@ -485,7 +485,7 @@ func actionsMergePullRequest(t *testing.T, ctx context.Context, db *actions) {
func actionsMirrorSyncCreate(t *testing.T, ctx context.Context, db *actions) {
alice, err := NewUsersStore(db.DB).Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
require.NoError(t, err)
repo, err := NewReposStore(db.DB).Create(ctx,
repo, err := NewRepositoriesStore(db.DB).Create(ctx,
alice.ID,
CreateRepoOptions{
Name: "example",
Expand Down Expand Up @@ -526,7 +526,7 @@ func actionsMirrorSyncCreate(t *testing.T, ctx context.Context, db *actions) {
func actionsMirrorSyncDelete(t *testing.T, ctx context.Context, db *actions) {
alice, err := NewUsersStore(db.DB).Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
require.NoError(t, err)
repo, err := NewReposStore(db.DB).Create(ctx,
repo, err := NewRepositoriesStore(db.DB).Create(ctx,
alice.ID,
CreateRepoOptions{
Name: "example",
Expand Down Expand Up @@ -567,7 +567,7 @@ func actionsMirrorSyncDelete(t *testing.T, ctx context.Context, db *actions) {
func actionsMirrorSyncPush(t *testing.T, ctx context.Context, db *actions) {
alice, err := NewUsersStore(db.DB).Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
require.NoError(t, err)
repo, err := NewReposStore(db.DB).Create(ctx,
repo, err := NewRepositoriesStore(db.DB).Create(ctx,
alice.ID,
CreateRepoOptions{
Name: "example",
Expand Down Expand Up @@ -632,7 +632,7 @@ func actionsMirrorSyncPush(t *testing.T, ctx context.Context, db *actions) {
func actionsNewRepo(t *testing.T, ctx context.Context, db *actions) {
alice, err := NewUsersStore(db.DB).Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
require.NoError(t, err)
repo, err := NewReposStore(db.DB).Create(ctx,
repo, err := NewRepositoriesStore(db.DB).Create(ctx,
alice.ID,
CreateRepoOptions{
Name: "example",
Expand Down Expand Up @@ -711,7 +711,7 @@ func actionsPushTag(t *testing.T, ctx context.Context, db *actions) {

alice, err := NewUsersStore(db.DB).Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
require.NoError(t, err)
repo, err := NewReposStore(db.DB).Create(ctx,
repo, err := NewRepositoriesStore(db.DB).Create(ctx,
alice.ID,
CreateRepoOptions{
Name: "example",
Expand Down Expand Up @@ -803,7 +803,7 @@ func actionsPushTag(t *testing.T, ctx context.Context, db *actions) {
func actionsRenameRepo(t *testing.T, ctx context.Context, db *actions) {
alice, err := NewUsersStore(db.DB).Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
require.NoError(t, err)
repo, err := NewReposStore(db.DB).Create(ctx,
repo, err := NewRepositoriesStore(db.DB).Create(ctx,
alice.ID,
CreateRepoOptions{
Name: "example",
Expand Down Expand Up @@ -842,7 +842,7 @@ func actionsTransferRepo(t *testing.T, ctx context.Context, db *actions) {
require.NoError(t, err)
bob, err := NewUsersStore(db.DB).Create(ctx, "bob", "bob@example.com", CreateUserOptions{})
require.NoError(t, err)
repo, err := NewReposStore(db.DB).Create(ctx,
repo, err := NewRepositoriesStore(db.DB).Create(ctx,
alice.ID,
CreateRepoOptions{
Name: "example",
Expand Down
4 changes: 2 additions & 2 deletions internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ func Init(w logger.Writer) (*gorm.DB, error) {
LoginSources = &loginSources{DB: db, files: sourceFiles}
LFS = &lfs{DB: db}
Notices = NewNoticesStore(db)
Orgs = NewOrgsStore(db)
Organizations = NewOrganizationsStore(db)
Perms = NewPermsStore(db)
Repos = NewReposStore(db)
Repositories = NewRepositoriesStore(db)
TwoFactors = &twoFactors{DB: db}
Users = NewUsersStore(db)

Expand Down
8 changes: 4 additions & 4 deletions internal/db/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ func SetMockPermsStore(t *testing.T, mock PermsStore) {
})
}

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

Expand Down
2 changes: 1 addition & 1 deletion internal/db/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ type Statistic struct {

func GetStatistic(ctx context.Context) (stats Statistic) {
stats.Counter.User = Users.Count(ctx)
stats.Counter.Org = Orgs.Count(ctx)
stats.Counter.Org = Organizations.Count(ctx)
stats.Counter.PublicKey, _ = x.Count(new(PublicKey))
stats.Counter.Repo = CountRepositories(true)
stats.Counter.Watch, _ = x.Count(new(Watch))
Expand Down
86 changes: 0 additions & 86 deletions internal/db/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,97 +7,11 @@ package db
import (
"context"
"fmt"
"os"
"strings"

"xorm.io/builder"
"xorm.io/xorm"

"gogs.io/gogs/internal/errutil"
"gogs.io/gogs/internal/repoutil"
"gogs.io/gogs/internal/userutil"
)

// CreateOrganization creates record of a new organization.
func CreateOrganization(org, owner *User) (err error) {
if err = isUsernameAllowed(org.Name); err != nil {
return err
}

if Users.IsUsernameUsed(context.TODO(), org.Name, 0) {
return ErrUserAlreadyExist{
args: errutil.Args{
"name": org.Name,
},
}
}

org.LowerName = strings.ToLower(org.Name)
if org.Rands, err = userutil.RandomSalt(); err != nil {
return err
}
if org.Salt, err = userutil.RandomSalt(); err != nil {
return err
}
org.UseCustomAvatar = true
org.MaxRepoCreation = -1
org.NumTeams = 1
org.NumMembers = 1

sess := x.NewSession()
defer sess.Close()
if err = sess.Begin(); err != nil {
return err
}

if _, err = sess.Insert(org); err != nil {
return fmt.Errorf("insert organization: %v", err)
}
_ = userutil.GenerateRandomAvatar(org.ID, org.Name, org.Email)

// Add initial creator to organization and owner team.
if _, err = sess.Insert(&OrgUser{
UserID: owner.ID,
OrgID: org.ID,
IsOwner: true,
NumTeams: 1,
}); err != nil {
return fmt.Errorf("insert org-user relation: %v", err)
}

// Create default owner team.
t := &Team{
OrgID: org.ID,
LowerName: strings.ToLower(TeamNameOwners),
Name: TeamNameOwners,
Authorize: AccessModeOwner,
NumMembers: 1,
}
if _, err = sess.Insert(t); err != nil {
return fmt.Errorf("insert owner team: %v", err)
}

if _, err = sess.Insert(&TeamUser{
UID: owner.ID,
OrgID: org.ID,
TeamID: t.ID,
}); err != nil {
return fmt.Errorf("insert team-user relation: %v", err)
}

if err = os.MkdirAll(repoutil.UserPath(org.Name), os.ModePerm); err != nil {
return fmt.Errorf("create directory: %v", err)
}

return sess.Commit()
}

// Organizations returns number of organizations in given page.
func Organizations(page, pageSize int) ([]*User, error) {
orgs := make([]*User, 0, pageSize)
return orgs, x.Limit(pageSize, (page-1)*pageSize).Where("type=1").Asc("id").Find(&orgs)
}

// deleteBeans deletes all given beans, beans should contain delete conditions.
func deleteBeans(e Engine, beans ...any) (err error) {
for i := range beans {
Expand Down
2 changes: 1 addition & 1 deletion internal/db/org_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ func AddTeamMember(orgID, teamID, userID int64) error {
return nil
}

if err := Orgs.AddMember(context.TODO(), orgID, userID); err != nil {
if err := Organizations.AddMember(context.TODO(), orgID, userID); err != nil {
return err
}

Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy