use xorm builder for models.getReviewers() (#19033)

* xorm builder

* dedup code
tokarchuk/v1.17
6543 3 years ago committed by GitHub
parent cc98737ca8
commit ba470a85dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 62
      models/repo.go

@ -218,50 +218,44 @@ func getReviewers(ctx context.Context, repo *repo_model.Repository, doerID, post
return nil, err return nil, err
} }
var users []*user_model.User cond := builder.And(builder.Neq{"`user`.id": posterID})
e := db.GetEngine(ctx)
if repo.IsPrivate || repo.Owner.Visibility == api.VisibleTypePrivate { if repo.IsPrivate || repo.Owner.Visibility == api.VisibleTypePrivate {
// This a private repository: // This a private repository:
// Anyone who can read the repository is a requestable reviewer // Anyone who can read the repository is a requestable reviewer
if err := e.
SQL("SELECT * FROM `user` WHERE id in ("+ cond = cond.And(builder.In("`user`.id",
"SELECT user_id FROM `access` WHERE repo_id = ? AND mode >= ? AND user_id != ?"+ // private org repos builder.Select("user_id").From("access").Where(
") ORDER BY name", builder.Eq{"repo_id": repo.ID}.
repo.ID, perm.AccessModeRead, And(builder.Gte{"mode": perm.AccessModeRead}),
posterID). ),
Find(&users); err != nil { ))
return nil, err
}
if repo.Owner.Type == user_model.UserTypeIndividual && repo.Owner.ID != posterID { if repo.Owner.Type == user_model.UserTypeIndividual && repo.Owner.ID != posterID {
// as private *user* repos don't generate an entry in the `access` table, // as private *user* repos don't generate an entry in the `access` table,
// the owner of a private repo needs to be explicitly added. // 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 } else {
} // This is a "public" repository:
// Any user that has read access, is a watcher or organization member can be requested to review
// This is a "public" repository: cond = cond.And(builder.And(builder.In("`user`.id",
// Any user that has read access, is a watcher or organization member can be requested to review builder.Select("user_id").From("access").
if err := e. Where(builder.Eq{"repo_id": repo.ID}.
SQL("SELECT * FROM `user` WHERE id IN ( "+ And(builder.Gte{"mode": perm.AccessModeRead})),
"SELECT user_id FROM `access` WHERE repo_id = ? AND mode >= ? "+ ).Or(builder.In("`user`.id",
"UNION "+ builder.Select("user_id").From("watch").
"SELECT user_id FROM `watch` WHERE repo_id = ? AND mode IN (?, ?) "+ Where(builder.Eq{"repo_id": repo.ID}.
"UNION "+ And(builder.In("mode", repo_model.WatchModeNormal, repo_model.WatchModeAuto))),
"SELECT uid AS user_id FROM `org_user` WHERE org_id = ? "+ ).Or(builder.In("`user`.id",
") AND id != ? ORDER BY name", builder.Select("uid").From("org_user").
repo.ID, perm.AccessModeRead, Where(builder.Eq{"org_id": repo.OwnerID}),
repo.ID, repo_model.WatchModeNormal, repo_model.WatchModeAuto, )))))
repo.OwnerID, }
posterID).
Find(&users); err != nil { users := make([]*user_model.User, 0, 8)
return nil, err return users, db.GetEngine(ctx).Where(cond).OrderBy("name").Find(&users)
}
return users, nil
} }
// GetReviewers get all users can be requested to review: // GetReviewers get all users can be requested to review:

Loading…
Cancel
Save