aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
author6543 <6543@obermui.de>2022-03-10 19:12:10 +0100
committerGitHub <noreply@github.com>2022-03-10 19:12:10 +0100
commitba470a85dd657c586aa07bd5bb4010b93aa8dfa9 (patch)
tree536171945dae016b096224e10a0dc9c2160bec68 /models
parentcc98737ca81d9552f20c277e6ad0031927f9b757 (diff)
downloadgitea-ba470a85dd657c586aa07bd5bb4010b93aa8dfa9.tar.gz
gitea-ba470a85dd657c586aa07bd5bb4010b93aa8dfa9.zip
use xorm builder for models.getReviewers() (#19033)
* xorm builder * dedup code
Diffstat (limited to 'models')
-rw-r--r--models/repo.go62
1 files changed, 28 insertions, 34 deletions
diff --git a/models/repo.go b/models/repo.go
index 53199bcca3..d20e5f81d3 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -218,50 +218,44 @@ func getReviewers(ctx context.Context, repo *repo_model.Repository, doerID, post
return nil, err
}
- var users []*user_model.User
- e := db.GetEngine(ctx)
+ cond := builder.And(builder.Neq{"`user`.id": posterID})
if repo.IsPrivate || repo.Owner.Visibility == api.VisibleTypePrivate {
// This a private repository:
// Anyone who can read the repository is a requestable reviewer
- if err := e.
- SQL("SELECT * FROM `user` WHERE id in ("+
- "SELECT user_id FROM `access` WHERE repo_id = ? AND mode >= ? AND user_id != ?"+ // private org repos
- ") ORDER BY name",
- repo.ID, perm.AccessModeRead,
- posterID).
- Find(&users); err != nil {
- return nil, err
- }
+
+ cond = cond.And(builder.In("`user`.id",
+ builder.Select("user_id").From("access").Where(
+ builder.Eq{"repo_id": repo.ID}.
+ And(builder.Gte{"mode": perm.AccessModeRead}),
+ ),
+ ))
if repo.Owner.Type == user_model.UserTypeIndividual && repo.Owner.ID != posterID {
// as private *user* repos don't generate an entry in the `access` table,
// the owner of a private repo needs to be explicitly added.
- users = append(users, repo.Owner)
+ cond = cond.Or(builder.Eq{"`user`.id": repo.Owner.ID})
}
- return users, nil
- }
-
- // This is a "public" repository:
- // Any user that has read access, is a watcher or organization member can be requested to review
- if err := e.
- SQL("SELECT * FROM `user` WHERE id IN ( "+
- "SELECT user_id FROM `access` WHERE repo_id = ? AND mode >= ? "+
- "UNION "+
- "SELECT user_id FROM `watch` WHERE repo_id = ? AND mode IN (?, ?) "+
- "UNION "+
- "SELECT uid AS user_id FROM `org_user` WHERE org_id = ? "+
- ") AND id != ? ORDER BY name",
- repo.ID, perm.AccessModeRead,
- repo.ID, repo_model.WatchModeNormal, repo_model.WatchModeAuto,
- repo.OwnerID,
- posterID).
- Find(&users); err != nil {
- return nil, err
- }
-
- return users, nil
+ } else {
+ // This is a "public" repository:
+ // Any user that has read access, is a watcher or organization member can be requested to review
+ cond = cond.And(builder.And(builder.In("`user`.id",
+ builder.Select("user_id").From("access").
+ Where(builder.Eq{"repo_id": repo.ID}.
+ And(builder.Gte{"mode": perm.AccessModeRead})),
+ ).Or(builder.In("`user`.id",
+ builder.Select("user_id").From("watch").
+ Where(builder.Eq{"repo_id": repo.ID}.
+ And(builder.In("mode", repo_model.WatchModeNormal, repo_model.WatchModeAuto))),
+ ).Or(builder.In("`user`.id",
+ builder.Select("uid").From("org_user").
+ Where(builder.Eq{"org_id": repo.OwnerID}),
+ )))))
+ }
+
+ users := make([]*user_model.User, 0, 8)
+ return users, db.GetEngine(ctx).Where(cond).OrderBy("name").Find(&users)
}
// GetReviewers get all users can be requested to review: