aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2024-11-17 19:06:25 -0800
committerGitHub <noreply@github.com>2024-11-18 03:06:25 +0000
commit4f879a00df029e09b40f64bf8de0572704766115 (patch)
treebb6d9b0b70fdc1d4e92e298355f244f52e831930 /models
parentf122aaf9ff627515922a68782339725e2d7c079a (diff)
downloadgitea-4f879a00df029e09b40f64bf8de0572704766115.tar.gz
gitea-4f879a00df029e09b40f64bf8de0572704766115.zip
Refactor find forks and fix possible bugs that weak permissions check (#32528)
- Move models/GetForks to services/FindForks - Add doer as a parameter of FindForks to check permissions - Slight performance optimization for get forks API with batch loading of repository units - Add tests for forking repository to organizations --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Diffstat (limited to 'models')
-rw-r--r--models/repo/fork.go15
-rw-r--r--models/repo/repo_list.go26
2 files changed, 18 insertions, 23 deletions
diff --git a/models/repo/fork.go b/models/repo/fork.go
index 07cd31c269..1c75e86458 100644
--- a/models/repo/fork.go
+++ b/models/repo/fork.go
@@ -54,21 +54,6 @@ func GetUserFork(ctx context.Context, repoID, userID int64) (*Repository, error)
return &forkedRepo, nil
}
-// GetForks returns all the forks of the repository
-func GetForks(ctx context.Context, repo *Repository, listOptions db.ListOptions) ([]*Repository, error) {
- sess := db.GetEngine(ctx)
-
- var forks []*Repository
- if listOptions.Page == 0 {
- forks = make([]*Repository, 0, repo.NumForks)
- } else {
- forks = make([]*Repository, 0, listOptions.PageSize)
- sess = db.SetSessionPagination(sess, &listOptions)
- }
-
- return forks, sess.Find(&forks, &Repository{ForkID: repo.ID})
-}
-
// IncrementRepoForkNum increment repository fork number
func IncrementRepoForkNum(ctx context.Context, repoID int64) error {
_, err := db.GetEngine(ctx).Exec("UPDATE `repository` SET num_forks=num_forks+1 WHERE id=?", repoID)
diff --git a/models/repo/repo_list.go b/models/repo/repo_list.go
index 1bffadbf0a..9bed2e9197 100644
--- a/models/repo/repo_list.go
+++ b/models/repo/repo_list.go
@@ -98,8 +98,7 @@ func (repos RepositoryList) IDs() []int64 {
return repoIDs
}
-// LoadAttributes loads the attributes for the given RepositoryList
-func (repos RepositoryList) LoadAttributes(ctx context.Context) error {
+func (repos RepositoryList) LoadOwners(ctx context.Context) error {
if len(repos) == 0 {
return nil
}
@@ -107,10 +106,6 @@ func (repos RepositoryList) LoadAttributes(ctx context.Context) error {
userIDs := container.FilterSlice(repos, func(repo *Repository) (int64, bool) {
return repo.OwnerID, true
})
- repoIDs := make([]int64, len(repos))
- for i := range repos {
- repoIDs[i] = repos[i].ID
- }
// Load owners.
users := make(map[int64]*user_model.User, len(userIDs))
@@ -123,12 +118,19 @@ func (repos RepositoryList) LoadAttributes(ctx context.Context) error {
for i := range repos {
repos[i].Owner = users[repos[i].OwnerID]
}
+ return nil
+}
+
+func (repos RepositoryList) LoadLanguageStats(ctx context.Context) error {
+ if len(repos) == 0 {
+ return nil
+ }
// Load primary language.
stats := make(LanguageStatList, 0, len(repos))
if err := db.GetEngine(ctx).
Where("`is_primary` = ? AND `language` != ?", true, "other").
- In("`repo_id`", repoIDs).
+ In("`repo_id`", repos.IDs()).
Find(&stats); err != nil {
return fmt.Errorf("find primary languages: %w", err)
}
@@ -141,10 +143,18 @@ func (repos RepositoryList) LoadAttributes(ctx context.Context) error {
}
}
}
-
return nil
}
+// LoadAttributes loads the attributes for the given RepositoryList
+func (repos RepositoryList) LoadAttributes(ctx context.Context) error {
+ if err := repos.LoadOwners(ctx); err != nil {
+ return err
+ }
+
+ return repos.LoadLanguageStats(ctx)
+}
+
// SearchRepoOptions holds the search options
type SearchRepoOptions struct {
db.ListOptions