Browse Source

repo: load base repository for loadAttributes

pull/4126/merge
Unknwon 8 years ago
parent
commit
3efc0d8681
No known key found for this signature in database
GPG Key ID: 7A02C406FAC875A2
  1. 39
      models/repo.go
  2. 15
      routers/api/v1/repo/repo.go

39
models/repo.go

@ -1021,7 +1021,7 @@ func createRepository(e *xorm.Session, doer, owner *User, repo *Repository) (err
return fmt.Errorf("newRepoAction: %v", err) return fmt.Errorf("newRepoAction: %v", err)
} }
return nil return repo.loadAttributes(e)
} }
// CreateRepository creates a repository for given user or organization. // CreateRepository creates a repository for given user or organization.
@ -1993,12 +1993,12 @@ func (repos RepositoryList) loadAttributes(e Engine) error {
} }
// Load owners // Load owners
set := make(map[int64]*User) userSet := make(map[int64]*User)
for i := range repos { for i := range repos {
set[repos[i].OwnerID] = nil userSet[repos[i].OwnerID] = nil
} }
userIDs := make([]int64, 0, len(set)) userIDs := make([]int64, 0, len(userSet))
for userID := range set { for userID := range userSet {
userIDs = append(userIDs, userID) userIDs = append(userIDs, userID)
} }
users := make([]*User, 0, len(userIDs)) users := make([]*User, 0, len(userIDs))
@ -2006,11 +2006,36 @@ func (repos RepositoryList) loadAttributes(e Engine) error {
return fmt.Errorf("find users: %v", err) return fmt.Errorf("find users: %v", err)
} }
for i := range users { for i := range users {
set[users[i].ID] = users[i] userSet[users[i].ID] = users[i]
}
for i := range repos {
repos[i].Owner = userSet[repos[i].OwnerID]
}
// Load base repositories
repoSet := make(map[int64]*Repository)
for i := range repos {
if repos[i].IsFork {
repoSet[repos[i].ForkID] = nil
}
}
baseIDs := make([]int64, 0, len(repoSet))
for baseID := range repoSet {
baseIDs = append(baseIDs, baseID)
}
baseRepos := make([]*Repository, 0, len(baseIDs))
if err := e.Where("id > 0").In("id", baseIDs).Find(&baseRepos); err != nil {
return fmt.Errorf("find base repositories: %v", err)
}
for i := range baseRepos {
repoSet[baseRepos[i].ID] = baseRepos[i]
} }
for i := range repos { for i := range repos {
repos[i].Owner = set[repos[i].OwnerID] if repos[i].IsFork {
repos[i].BaseRepo = repoSet[repos[i].ForkID]
}
} }
return nil return nil
} }

15
routers/api/v1/repo/repo.go

@ -56,15 +56,16 @@ func Search(c *context.APIContext) {
return return
} }
if err = models.RepositoryList(repos).LoadAttributes(); err != nil {
c.JSON(500, map[string]interface{}{
"ok": false,
"error": err.Error(),
})
return
}
results := make([]*api.Repository, len(repos)) results := make([]*api.Repository, len(repos))
for i := range repos { for i := range repos {
if err = repos[i].GetOwner(); err != nil {
c.JSON(500, map[string]interface{}{
"ok": false,
"error": err.Error(),
})
return
}
results[i] = repos[i].APIFormat(nil) results[i] = repos[i].APIFormat(nil)
} }

Loading…
Cancel
Save