aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2017-02-17 08:58:19 +0800
committerGitHub <noreply@github.com>2017-02-17 08:58:19 +0800
commit847527fd6dd8e53eea918e6e41da6ebc64be1388 (patch)
treed240617fa9787331e540326bdde4574bfbbeac7c /models
parent669dad71f830693a83a50e3de52a86e3940497b6 (diff)
downloadgitea-847527fd6dd8e53eea918e6e41da6ebc64be1388.tar.gz
gitea-847527fd6dd8e53eea918e6e41da6ebc64be1388.zip
Fix all the bugs in issues and pulls on dashboard (#943)
* fix all the bugs in issues and pulls on dashboard * small fix and refactor * add method getRepoIDs for IssueList
Diffstat (limited to 'models')
-rw-r--r--models/issue.go59
-rw-r--r--models/user.go28
2 files changed, 87 insertions, 0 deletions
diff --git a/models/issue.go b/models/issue.go
index 3ae34009bf..b160004fd5 100644
--- a/models/issue.go
+++ b/models/issue.go
@@ -1399,3 +1399,62 @@ func updateIssue(e Engine, issue *Issue) error {
func UpdateIssue(issue *Issue) error {
return updateIssue(x, issue)
}
+
+// IssueList defines a list of issues
+type IssueList []*Issue
+
+func (issues IssueList) getRepoIDs() []int64 {
+ repoIDs := make([]int64, 0, len(issues))
+ for _, issue := range issues {
+ var has bool
+ for _, repoID := range repoIDs {
+ if repoID == issue.RepoID {
+ has = true
+ break
+ }
+ }
+ if !has {
+ repoIDs = append(repoIDs, issue.RepoID)
+ }
+ }
+ return repoIDs
+}
+
+func (issues IssueList) loadRepositories(e Engine) ([]*Repository, error) {
+ if len(issues) == 0 {
+ return nil, nil
+ }
+
+ repoIDs := issues.getRepoIDs()
+ rows, err := e.
+ Where("id > 0").
+ In("id", repoIDs).
+ Rows(new(Repository))
+ if err != nil {
+ return nil, fmt.Errorf("find repository: %v", err)
+ }
+ defer rows.Close()
+
+ repositories := make([]*Repository, 0, len(repoIDs))
+ repoMaps := make(map[int64]*Repository, len(repoIDs))
+ for rows.Next() {
+ var repo Repository
+ err = rows.Scan(&repo)
+ if err != nil {
+ return nil, fmt.Errorf("find repository: %v", err)
+ }
+
+ repositories = append(repositories, &repo)
+ repoMaps[repo.ID] = &repo
+ }
+
+ for _, issue := range issues {
+ issue.Repo = repoMaps[issue.RepoID]
+ }
+ return repositories, nil
+}
+
+// LoadRepositories loads issues' all repositories
+func (issues IssueList) LoadRepositories() ([]*Repository, error) {
+ return issues.loadRepositories(x)
+}
diff --git a/models/user.go b/models/user.go
index 5f0816cd48..c9ddbe7c8b 100644
--- a/models/user.go
+++ b/models/user.go
@@ -500,6 +500,34 @@ func (u *User) GetRepositories(page, pageSize int) (err error) {
return err
}
+// GetRepositoryIDs returns repositories IDs where user owned
+func (u *User) GetRepositoryIDs() ([]int64, error) {
+ var ids []int64
+ return ids, x.Table("repository").Cols("id").Where("owner_id = ?", u.ID).Find(&ids)
+}
+
+// GetOrgRepositoryIDs returns repositories IDs where user's team owned
+func (u *User) GetOrgRepositoryIDs() ([]int64, error) {
+ var ids []int64
+ return ids, x.Table("repository").
+ Cols("repository.id").
+ Join("INNER", "team_user", "repository.owner_id = team_user.org_id AND team_user.uid = ?", u.ID).
+ GroupBy("repository.id").Find(&ids)
+}
+
+// GetAccessRepoIDs returns all repsitories IDs where user's or user is a team member orgnizations
+func (u *User) GetAccessRepoIDs() ([]int64, error) {
+ ids, err := u.GetRepositoryIDs()
+ if err != nil {
+ return nil, err
+ }
+ ids2, err := u.GetOrgRepositoryIDs()
+ if err != nil {
+ return nil, err
+ }
+ return append(ids, ids2...), nil
+}
+
// GetMirrorRepositories returns mirror repositories that user owns, including private repositories.
func (u *User) GetMirrorRepositories() ([]*Repository, error) {
return GetUserMirrorRepositories(u.ID)