MCPcopy Create free account
hub / github.com/gogs/gogs / GetUserRepositories

Method GetUserRepositories

internal/database/org.go:481–524  ·  view source on GitHub ↗

GetUserRepositories returns a range of repositories in organization which the user has access to, and total number of records based on given condition.

(userID int64, page, pageSize int)

Source from the content-addressed store, hash-verified

479// GetUserRepositories returns a range of repositories in organization which the user has access to,
480// and total number of records based on given condition.
481func (org *User) GetUserRepositories(userID int64, page, pageSize int) ([]*Repository, int64, error) {
482 teamIDs, err := org.GetUserTeamIDs(userID)
483 if err != nil {
484 return nil, 0, errors.Newf("GetUserTeamIDs: %v", err)
485 }
486 if len(teamIDs) == 0 {
487 // user has no team but "IN ()" is invalid SQL
488 teamIDs = []int64{-1} // there is no team with id=-1
489 }
490
491 var teamRepoIDs []int64
492 if err = x.Table("team_repo").In("team_id", teamIDs).Distinct("repo_id").Find(&teamRepoIDs); err != nil {
493 return nil, 0, errors.Newf("get team repository IDs: %v", err)
494 }
495 if len(teamRepoIDs) == 0 {
496 // team has no repo but "IN ()" is invalid SQL
497 teamRepoIDs = []int64{-1} // there is no repo with id=-1
498 }
499
500 if page <= 0 {
501 page = 1
502 }
503 repos := make([]*Repository, 0, pageSize)
504 if err = x.Where("owner_id = ?", org.ID).
505 And(builder.Or(
506 builder.And(builder.Expr("is_private = ?", false), builder.Expr("is_unlisted = ?", false)),
507 builder.In("id", teamRepoIDs))).
508 Desc("updated_unix").
509 Limit(pageSize, (page-1)*pageSize).
510 Find(&repos); err != nil {
511 return nil, 0, errors.Newf("get user repositories: %v", err)
512 }
513
514 repoCount, err := x.Where("owner_id = ?", org.ID).
515 And(builder.Or(
516 builder.Expr("is_private = ?", false),
517 builder.In("id", teamRepoIDs))).
518 Count(new(Repository))
519 if err != nil {
520 return nil, 0, errors.Newf("count user repositories: %v", err)
521 }
522
523 return repos, repoCount, nil
524}
525
526// GetUserMirrorRepositories returns mirror repositories of the organization which the user has access to.
527func (org *User) GetUserMirrorRepositories(userID int64) ([]*Repository, error) {

Callers 5

RemoveOrgUserFunction · 0.80
DashboardFunction · 0.80
IssuesFunction · 0.80
showOrgProfileFunction · 0.80
listUserRepositoriesFunction · 0.80

Implementers 1

mailerUserinternal/database/issue_mail.go

Calls 6

GetUserTeamIDsMethod · 0.95
InMethod · 0.80
TableMethod · 0.80
WhereMethod · 0.80
FindMethod · 0.65
CountMethod · 0.45

Tested by

no test coverage detected