mirror of
https://github.com/gogs/gogs.git
synced 2025-05-31 11:42:13 +00:00
GetUserMirrorRepositories
This commit is contained in:
parent
2971cabae1
commit
9797508a0a
@ -1,43 +0,0 @@
|
|||||||
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a MIT-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package db
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"xorm.io/builder"
|
|
||||||
)
|
|
||||||
|
|
||||||
// GetUserMirrorRepositories returns mirror repositories of the organization which the user has access to.
|
|
||||||
func (u *User) GetUserMirrorRepositories(userID int64) ([]*Repository, error) {
|
|
||||||
teamIDs, err := u.GetUserTeamIDs(userID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("GetUserTeamIDs: %v", err)
|
|
||||||
}
|
|
||||||
if len(teamIDs) == 0 {
|
|
||||||
teamIDs = []int64{-1}
|
|
||||||
}
|
|
||||||
|
|
||||||
var teamRepoIDs []int64
|
|
||||||
err = x.Table("team_repo").In("team_id", teamIDs).Distinct("repo_id").Find(&teamRepoIDs)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("get team repository ids: %v", err)
|
|
||||||
}
|
|
||||||
if len(teamRepoIDs) == 0 {
|
|
||||||
// team has no repo but "IN ()" is invalid SQL
|
|
||||||
teamRepoIDs = []int64{-1} // there is no repo with id=-1
|
|
||||||
}
|
|
||||||
|
|
||||||
repos := make([]*Repository, 0, 10)
|
|
||||||
if err = x.Where("owner_id = ?", u.ID).
|
|
||||||
And("is_private = ?", false).
|
|
||||||
Or(builder.In("id", teamRepoIDs)).
|
|
||||||
And("is_mirror = ?", true). // Don't move up because it's an independent condition
|
|
||||||
Desc("updated_unix").
|
|
||||||
Find(&repos); err != nil {
|
|
||||||
return nil, fmt.Errorf("get user repositories: %v", err)
|
|
||||||
}
|
|
||||||
return repos, nil
|
|
||||||
}
|
|
@ -689,20 +689,6 @@ func (u *User) getUserTeams(e Engine, userID int64, cols ...string) ([]*Team, er
|
|||||||
Cols(cols...).Find(&teams)
|
Cols(cols...).Find(&teams)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUserTeamIDs returns of all team IDs of the organization that user is member of.
|
|
||||||
func (u *User) GetUserTeamIDs(userID int64) ([]int64, error) {
|
|
||||||
teams, err := u.getUserTeams(x, userID, "team.id")
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("getUserTeams [%d]: %v", userID, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
teamIDs := make([]int64, len(teams))
|
|
||||||
for i := range teams {
|
|
||||||
teamIDs[i] = teams[i].ID
|
|
||||||
}
|
|
||||||
return teamIDs, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetTeams returns all teams that belong to organization,
|
// GetTeams returns all teams that belong to organization,
|
||||||
// and that the user has joined.
|
// and that the user has joined.
|
||||||
func (u *User) GetUserTeams(userID int64) ([]*Team, error) {
|
func (u *User) GetUserTeams(userID int64) ([]*Team, error) {
|
||||||
|
@ -65,9 +65,11 @@ type OrganizationsStore interface {
|
|||||||
|
|
||||||
// AccessibleRepositoriesByUser returns a range of repositories in the
|
// AccessibleRepositoriesByUser returns a range of repositories in the
|
||||||
// organization that the user has access to and the total number of it. Results
|
// organization that the user has access to and the total number of it. Results
|
||||||
// are paginated by given page and page size, and sorted by the given order
|
// are paginated by given page and page size, and OrderByUpdatedDesc is used.
|
||||||
// (e.g. "updated_unix DESC").
|
|
||||||
AccessibleRepositoriesByUser(ctx context.Context, orgID, userID int64, page, pageSize int, opts AccessibleRepositoriesByUserOptions) ([]*Repository, int64, error)
|
AccessibleRepositoriesByUser(ctx context.Context, orgID, userID int64, page, pageSize int, opts AccessibleRepositoriesByUserOptions) ([]*Repository, int64, error)
|
||||||
|
// MirrorRepositoriesByUser returns a list of mirror repositories of the
|
||||||
|
// organization which the user has access to.
|
||||||
|
MirrorRepositoriesByUser(ctx context.Context, orgID, userID int64) ([]*Repository, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
var Organizations OrganizationsStore
|
var Organizations OrganizationsStore
|
||||||
@ -299,6 +301,40 @@ func (db *organizations) AccessibleRepositoriesByUser(ctx context.Context, orgID
|
|||||||
return repos, count, nil
|
return repos, count, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *organizations) MirrorRepositoriesByUser(ctx context.Context, orgID, userID int64) ([]*Repository, error) {
|
||||||
|
/*
|
||||||
|
Equivalent SQL for PostgreSQL:
|
||||||
|
|
||||||
|
SELECT * FROM "repository"
|
||||||
|
JOIN team_repo ON repository.id = team_repo.repo_id
|
||||||
|
WHERE
|
||||||
|
owner_id = @orgID
|
||||||
|
AND repository.is_mirror = TRUE
|
||||||
|
AND (
|
||||||
|
team_repo.team_id IN (
|
||||||
|
SELECT team_id FROM "team_user"
|
||||||
|
WHERE team_user.org_id = @orgID AND uid = @userID)
|
||||||
|
)
|
||||||
|
OR repository.is_private = FALSE
|
||||||
|
)
|
||||||
|
ORDER BY updated_unix DESC
|
||||||
|
*/
|
||||||
|
var repos []*Repository
|
||||||
|
return repos, db.WithContext(ctx).
|
||||||
|
Joins("JOIN team_repo ON repository.id = team_repo.repo_id").
|
||||||
|
Where("owner_id = ? AND repository.is_mirror = ? AND (?)", orgID, true, db.
|
||||||
|
Where("team_repo.team_id IN (?)", db.
|
||||||
|
Select("team_id").
|
||||||
|
Table("team_user").
|
||||||
|
Where("team_user.org_id = ? AND uid = ?", orgID, userID),
|
||||||
|
).
|
||||||
|
Or("repository.is_private = ?", false),
|
||||||
|
).
|
||||||
|
Order("updated_unix DESC").
|
||||||
|
Find(&repos).
|
||||||
|
Error
|
||||||
|
}
|
||||||
|
|
||||||
func (db *organizations) getOrgUser(ctx context.Context, orgID, userID int64) (*OrgUser, error) {
|
func (db *organizations) getOrgUser(ctx context.Context, orgID, userID int64) (*OrgUser, error) {
|
||||||
var ou OrgUser
|
var ou OrgUser
|
||||||
return &ou, db.WithContext(ctx).Where("org_id = ? AND uid = ?", orgID, userID).First(&ou).Error
|
return &ou, db.WithContext(ctx).Where("org_id = ? AND uid = ?", orgID, userID).First(&ou).Error
|
||||||
|
@ -153,7 +153,7 @@ func Dashboard(c *context.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
mirrors, err = ctxUser.GetUserMirrorRepositories(c.User.ID)
|
mirrors, err = db.Organizations.MirrorRepositoriesByUser(c.Req.Context(), ctxUser.ID, c.User.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Error(err, "get user mirror repositories")
|
c.Error(err, "get user mirror repositories")
|
||||||
return
|
return
|
||||||
|
Loading…
x
Reference in New Issue
Block a user