-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Reimplement GetUserOrgsList to make it simple and clear #32486
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9ae61bb
Reimplement GetUserOrgsList to make it simple and clear
lunny 0cf938c
Fix bug
lunny 1ed9934
Add comment about the repository counting
lunny 1a152ad
Fix bug
lunny 0688517
Update models/organization/org_list.go
lunny 877efb6
Fix order
lunny 4a93199
Merge branch 'main' into lunny/reimplement_GetUserOrgsLit
lunny edf61d6
Merge branch 'lunny/reimplement_GetUserOrgsLit' of github.com:lunny/g…
lunny da72d00
Merge branch 'main' into lunny/reimplement_GetUserOrgsLit
GiteaBot 6f24c42
Merge branch 'main' into lunny/reimplement_GetUserOrgsLit
GiteaBot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package organization | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/models/db" | ||
"code.gitea.io/gitea/models/perm" | ||
user_model "code.gitea.io/gitea/models/user" | ||
"code.gitea.io/gitea/modules/structs" | ||
|
||
"xorm.io/builder" | ||
) | ||
|
||
// SearchOrganizationsOptions options to filter organizations | ||
type SearchOrganizationsOptions struct { | ||
db.ListOptions | ||
All bool | ||
} | ||
|
||
// FindOrgOptions finds orgs options | ||
type FindOrgOptions struct { | ||
db.ListOptions | ||
UserID int64 | ||
IncludePrivate bool | ||
} | ||
|
||
func queryUserOrgIDs(userID int64, includePrivate bool) *builder.Builder { | ||
cond := builder.Eq{"uid": userID} | ||
if !includePrivate { | ||
cond["is_public"] = true | ||
} | ||
return builder.Select("org_id").From("org_user").Where(cond) | ||
} | ||
|
||
func (opts FindOrgOptions) ToConds() builder.Cond { | ||
var cond builder.Cond = builder.Eq{"`user`.`type`": user_model.UserTypeOrganization} | ||
if opts.UserID > 0 { | ||
cond = cond.And(builder.In("`user`.`id`", queryUserOrgIDs(opts.UserID, opts.IncludePrivate))) | ||
} | ||
if !opts.IncludePrivate { | ||
cond = cond.And(builder.Eq{"`user`.visibility": structs.VisibleTypePublic}) | ||
} | ||
return cond | ||
} | ||
|
||
func (opts FindOrgOptions) ToOrders() string { | ||
return "`user`.name ASC" | ||
} | ||
|
||
// GetOrgsCanCreateRepoByUserID returns a list of organizations where given user ID | ||
// are allowed to create repos. | ||
func GetOrgsCanCreateRepoByUserID(ctx context.Context, userID int64) ([]*Organization, error) { | ||
orgs := make([]*Organization, 0, 10) | ||
|
||
return orgs, db.GetEngine(ctx).Where(builder.In("id", builder.Select("`user`.id").From("`user`"). | ||
Join("INNER", "`team_user`", "`team_user`.org_id = `user`.id"). | ||
Join("INNER", "`team`", "`team`.id = `team_user`.team_id"). | ||
Where(builder.Eq{"`team_user`.uid": userID}). | ||
And(builder.Eq{"`team`.authorize": perm.AccessModeOwner}.Or(builder.Eq{"`team`.can_create_org_repo": true})))). | ||
Asc("`user`.name"). | ||
Find(&orgs) | ||
} | ||
|
||
// MinimalOrg represents a simple organization with only the needed columns | ||
type MinimalOrg = Organization | ||
|
||
// GetUserOrgsList returns all organizations the given user has access to | ||
func GetUserOrgsList(ctx context.Context, user *user_model.User) ([]*MinimalOrg, error) { | ||
schema, err := db.TableInfo(new(user_model.User)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
outputCols := []string{ | ||
"id", | ||
"name", | ||
"full_name", | ||
"visibility", | ||
"avatar", | ||
"avatar_email", | ||
"use_custom_avatar", | ||
} | ||
|
||
selectColumns := &strings.Builder{} | ||
for i, col := range outputCols { | ||
fmt.Fprintf(selectColumns, "`%s`.%s", schema.Name, col) | ||
if i < len(outputCols)-1 { | ||
selectColumns.WriteString(", ") | ||
} | ||
} | ||
columnsStr := selectColumns.String() | ||
|
||
var orgs []*MinimalOrg | ||
if err := db.GetEngine(ctx).Select(columnsStr). | ||
Table("user"). | ||
Where(builder.In("`user`.`id`", queryUserOrgIDs(user.ID, true))). | ||
Find(&orgs); err != nil { | ||
return nil, err | ||
} | ||
|
||
type orgCount struct { | ||
OrgID int64 | ||
RepoCount int | ||
} | ||
var orgCounts []orgCount | ||
if err := db.GetEngine(ctx). | ||
Select("owner_id AS org_id, COUNT(DISTINCT(repository.id)) as repo_count"). | ||
Table("repository"). | ||
Join("INNER", "org_user", "owner_id = org_user.org_id"). | ||
Where("org_user.uid = ?", user.ID). | ||
And(builder.Or( | ||
builder.Eq{"repository.is_private": false}, | ||
builder.In("repository.id", builder.Select("repo_id").From("team_repo"). | ||
InnerJoin("team_user", "team_user.team_id = team_repo.team_id"). | ||
Where(builder.Eq{"team_user.uid": user.ID})), | ||
builder.In("repository.id", builder.Select("repo_id").From("collaboration"). | ||
Where(builder.Eq{"user_id": user.ID})), | ||
)). | ||
GroupBy("owner_id").Find(&orgCounts); err != nil { | ||
return nil, err | ||
} | ||
|
||
orgCountMap := make(map[int64]int, len(orgCounts)) | ||
for _, orgCount := range orgCounts { | ||
orgCountMap[orgCount.OrgID] = orgCount.RepoCount | ||
} | ||
|
||
for _, org := range orgs { | ||
org.NumRepos = orgCountMap[org.ID] | ||
} | ||
|
||
return orgs, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package organization_test | ||
|
||
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" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCountOrganizations(t *testing.T) { | ||
assert.NoError(t, unittest.PrepareTestDatabase()) | ||
expected, err := db.GetEngine(db.DefaultContext).Where("type=?", user_model.UserTypeOrganization).Count(&organization.Organization{}) | ||
assert.NoError(t, err) | ||
cnt, err := db.Count[organization.Organization](db.DefaultContext, organization.FindOrgOptions{IncludePrivate: true}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expected, cnt) | ||
} | ||
|
||
func TestFindOrgs(t *testing.T) { | ||
assert.NoError(t, unittest.PrepareTestDatabase()) | ||
|
||
orgs, err := db.Find[organization.Organization](db.DefaultContext, organization.FindOrgOptions{ | ||
UserID: 4, | ||
IncludePrivate: true, | ||
}) | ||
assert.NoError(t, err) | ||
if assert.Len(t, orgs, 1) { | ||
assert.EqualValues(t, 3, orgs[0].ID) | ||
} | ||
|
||
orgs, err = db.Find[organization.Organization](db.DefaultContext, organization.FindOrgOptions{ | ||
UserID: 4, | ||
IncludePrivate: false, | ||
}) | ||
assert.NoError(t, err) | ||
assert.Len(t, orgs, 0) | ||
|
||
total, err := db.Count[organization.Organization](db.DefaultContext, organization.FindOrgOptions{ | ||
UserID: 4, | ||
IncludePrivate: true, | ||
}) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, 1, total) | ||
} | ||
|
||
func TestGetUserOrgsList(t *testing.T) { | ||
assert.NoError(t, unittest.PrepareTestDatabase()) | ||
orgs, err := organization.GetUserOrgsList(db.DefaultContext, &user_model.User{ID: 4}) | ||
assert.NoError(t, err) | ||
if assert.Len(t, orgs, 1) { | ||
assert.EqualValues(t, 3, orgs[0].ID) | ||
// repo_id: 3 is in the team, 32 is public, 5 is private with no team | ||
assert.EqualValues(t, 2, orgs[0].NumRepos) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.