|
|
@ -1065,71 +1065,74 @@ func GetRepositoryCount(user *User) (int64, error) { |
|
|
|
return x.Count(&Repository{OwnerId: user.Id}) |
|
|
|
return x.Count(&Repository{OwnerId: user.Id}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// GetCollaboratorNames returns a list of user name of repository's collaborators.
|
|
|
|
// GetCollaborators returns the collaborators for a repository
|
|
|
|
func GetCollaboratorNames(repoName string) ([]string, error) { |
|
|
|
func (r *Repository) GetCollaborators() ([]*User, error) { |
|
|
|
accesses := make([]*Access, 0, 10) |
|
|
|
collaborations := make([]*Collaboration, 0) |
|
|
|
if err := x.Find(&accesses, &Access{RepoName: strings.ToLower(repoName)}); err != nil { |
|
|
|
if err := x.Find(&collaborations, &Collaboration{RepoID: r.Id}); err != nil { |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
names := make([]string, len(accesses)) |
|
|
|
users := make([]*User, len(collaborations)) |
|
|
|
for i := range accesses { |
|
|
|
for i, c := range collaborations { |
|
|
|
names[i] = accesses[i].UserName |
|
|
|
user, err := GetUserById(c.UserID) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return nil, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
users[i] = user |
|
|
|
} |
|
|
|
} |
|
|
|
return names, nil |
|
|
|
return users, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// CollaborativeRepository represents a repository with collaborative information.
|
|
|
|
// Add collaborator and accompanying access
|
|
|
|
type CollaborativeRepository struct { |
|
|
|
func (r *Repository) AddCollaborator(u *User) error { |
|
|
|
*Repository |
|
|
|
collaboration := &Collaboration{RepoID: r.Id, UserID: u.Id} |
|
|
|
CanPush bool |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GetCollaborativeRepos returns a list of repositories that user is collaborator.
|
|
|
|
has, err := x.Get(collaboration) |
|
|
|
func GetCollaborativeRepos(uname string) ([]*CollaborativeRepository, error) { |
|
|
|
if err != nil { |
|
|
|
uname = strings.ToLower(uname) |
|
|
|
return err |
|
|
|
accesses := make([]*Access, 0, 10) |
|
|
|
} |
|
|
|
if err := x.Find(&accesses, &Access{UserName: uname}); err != nil { |
|
|
|
if has { |
|
|
|
return nil, err |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
repos := make([]*CollaborativeRepository, 0, 10) |
|
|
|
if _, err = x.InsertOne(collaboration); err != nil { |
|
|
|
for _, access := range accesses { |
|
|
|
return err |
|
|
|
infos := strings.Split(access.RepoName, "/") |
|
|
|
} |
|
|
|
if infos[0] == uname { |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
u, err := GetUserByName(infos[0]) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return nil, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
repo, err := GetRepositoryByName(u.Id, infos[1]) |
|
|
|
if err = r.GetOwner(); err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return nil, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
repo.Owner = u |
|
|
|
|
|
|
|
repos = append(repos, &CollaborativeRepository{repo, access.Mode == WRITABLE}) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return repos, nil |
|
|
|
|
|
|
|
|
|
|
|
return AddAccess(&Access{UserName: u.LowerName, RepoName: path.Join(r.Owner.LowerName, r.LowerName), Mode: WRITABLE}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// GetCollaborators returns a list of users of repository's collaborators.
|
|
|
|
// Delete collaborator and accompanying access
|
|
|
|
func GetCollaborators(repoName string) (us []*User, err error) { |
|
|
|
func (r *Repository) DeleteCollaborator(u *User) error { |
|
|
|
accesses := make([]*Access, 0, 10) |
|
|
|
collaboration := &Collaboration{RepoID: r.Id, UserID: u.Id} |
|
|
|
if err = x.Find(&accesses, &Access{RepoName: strings.ToLower(repoName)}); err != nil { |
|
|
|
|
|
|
|
return nil, err |
|
|
|
if has, err := x.Delete(collaboration); err != nil || has == 0 { |
|
|
|
|
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
us = make([]*User, len(accesses)) |
|
|
|
if err := r.GetOwner(); err != nil { |
|
|
|
for i := range accesses { |
|
|
|
return err |
|
|
|
us[i], err = GetUserByName(accesses[i].UserName) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
needDelete := true |
|
|
|
|
|
|
|
if r.Owner.IsOrganization() { |
|
|
|
|
|
|
|
auth, err := GetHighestAuthorize(r.Owner.Id, u.Id, r.Id, 0) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if auth > 0 { |
|
|
|
|
|
|
|
needDelete = false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if needDelete { |
|
|
|
|
|
|
|
return DeleteAccess(&Access{UserName: u.LowerName, RepoName: path.Join(r.Owner.LowerName, r.LowerName), Mode: WRITABLE}) |
|
|
|
} |
|
|
|
} |
|
|
|
return us, nil |
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type SearchOption struct { |
|
|
|
type SearchOption struct { |
|
|
@ -1547,3 +1550,11 @@ func ForkRepository(u *User, oldRepo *Repository, name, desc string) (*Repositor |
|
|
|
|
|
|
|
|
|
|
|
return repo, nil |
|
|
|
return repo, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// A Collaboration is a relation between an individual and a repository
|
|
|
|
|
|
|
|
type Collaboration struct { |
|
|
|
|
|
|
|
ID int64 `xorm:"pk autoincr"` |
|
|
|
|
|
|
|
RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"` |
|
|
|
|
|
|
|
UserID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"` |
|
|
|
|
|
|
|
Created time.Time `xorm:"CREATED"` |
|
|
|
|
|
|
|
} |
|
|
|