models/issue: improve quality and performance of NewIssue function

This commit is contained in:
Unknwon
2016-08-15 18:40:32 -07:00
parent 4a46613916
commit 4042d1f0c3
13 changed files with 146 additions and 173 deletions

View File

@ -316,33 +316,43 @@ func (repo *Repository) DeleteWiki() {
}
}
// GetAssignees returns all users that have write access of repository.
func (repo *Repository) GetAssignees() (_ []*User, err error) {
if err = repo.GetOwner(); err != nil {
func (repo *Repository) getAssignees(e Engine) (_ []*User, err error) {
if err = repo.getOwner(e); err != nil {
return nil, err
}
accesses := make([]*Access, 0, 10)
if err = x.Where("repo_id=? AND mode>=?", repo.ID, ACCESS_MODE_WRITE).Find(&accesses); err != nil {
if err = e.Where("repo_id = ? AND mode >= ?", repo.ID, ACCESS_MODE_WRITE).Find(&accesses); err != nil {
return nil, err
}
if len(accesses) == 0 {
return []*User{}, nil
}
users := make([]*User, 0, len(accesses)+1) // Just waste 1 unit does not matter.
userIDs := make([]int64, len(accesses))
for i := 0; i < len(accesses); i++ {
userIDs[i] = accesses[i].UserID
}
// Leave a seat for owner itself to append later, but if owner is an organization
// and just waste 1 unit is cheaper than re-allocate memory once.
users := make([]*User, 0, len(userIDs)+1)
if err = e.In("id", userIDs).Find(&users); err != nil {
return nil, err
}
if !repo.Owner.IsOrganization() {
users = append(users, repo.Owner)
}
var u *User
for i := range accesses {
u, err = GetUserByID(accesses[i].UserID)
if err != nil {
return nil, err
}
users = append(users, u)
}
return users, nil
}
// GetAssignees returns all users that have write access and can be assigned to issues
// of the repository,
func (repo *Repository) GetAssignees() (_ []*User, err error) {
return repo.getAssignees(x)
}
// GetAssigneeByID returns the user that has write access of repository by given ID.
func (repo *Repository) GetAssigneeByID(userID int64) (*User, error) {
return GetAssigneeByID(repo, userID)
@ -420,6 +430,8 @@ func (repo *Repository) AllowsPulls() bool {
return repo.CanEnablePulls() && repo.EnablePulls
}
// FIXME: should have a mutex to prevent producing same index for two issues that are created
// closely enough.
func (repo *Repository) NextIssueIndex() int64 {
return int64(repo.NumIssues+repo.NumPulls) + 1
}