Repository: fedora-infra/bodhi

Issue:
There are several validators for username that are essentially copied code
There are several validators that make sure that various kwargs reference existing username. For example, check out [```validate_username()```](https://github.com/fedora-infra/bodhi/blob/3.0.0/bodhi/server/validators.py#L619-L632), [```validate_update_owner()```](https://github.com/fedora-infra/bodhi/blob/3.0.0/bodhi/server/validators.py#L648-L661), and [```validate_ignore_user()```](https://github.com/fedora-infra/bodhi/blob/3.0.0/bodhi/server/validators.py#L664-L677). These validators are nearly identical to the point where it'd be easy to write a common method they all use, or a validator generator function.

There may be others, so check for that too.


Discussion:
hey @bowlofeggs, do you like to share your thoughts on this? https://github.com/fedora-infra/bodhi/pull/1982

Resolving patch:
diff --git a/bodhi/server/validators.py b/bodhi/server/validators.py
--- a/bodhi/server/validators.py
+++ b/bodhi/server/validators.py
@@ -756,28 +756,6 @@ def validate_bugs(request, **kwargs):
                                "Invalid bug ID specified: {}".format(bugs))
 
 
-def validate_username(request, **kwargs):
-    """
-    Make sure the referenced user exists.
-
-    Args:
-        request (pyramid.util.Request): The current request.
-        kwargs (dict): The kwargs of the related service definition. Unused.
-    """
-    username = request.validated.get("user")
-    if username is None:
-        return
-
-    db = request.db
-    user = db.query(User).filter_by(name=username).first()
-
-    if user:
-        request.validated["user"] = user
-    else:
-        request.errors.add("querystring", "user",
-                           "Invalid user specified: {}".format(username))
-
-
 def validate_update(request, **kwargs):
     """
     Make sure the requested update exists.
@@ -797,15 +775,16 @@ def validate_update(request, **kwargs):
         request.errors.status = HTTPNotFound.code
 
 
-def validate_update_owner(request, **kwargs):
+def ensure_user_exists(param, request):
     """
-    Make sure the referenced update owner is an existing user.
+    Ensure the user referenced by param exists and if it does replace it with the User object.
 
     Args:
+        param (string): Request parameter that references a username to be validated.
         request (pyramid.util.Request): The current request.
         kwargs (dict): The kwargs of the related service definition. Unused.
     """
-    username = request.validated.get("update_owner")
+    username = request.validated.get(param)
     if username is None:
         return
 
@@ -813,32 +792,43 @@ def validate_update_owner(request, **kwargs):
     user = db.query(User).filter_by(name=username).first()
 
     if user:
-        request.validated["update_owner"] = user
+        request.validated[param] = user
     else:
-        request.errors.add("querystring", "update_owner",
+        request.errors.add("querystring", param,
                            "Invalid user specified: {}".format(username))
 
 
-def validate_ignore_user(request, **kwargs):
+def validate_username(request, **kwargs):
     """
-    Make sure the ignore_user parameter references an existing user.
+    Make sure the referenced user exists.
 
     Args:
         request (pyramid.util.Request): The current request.
         kwargs (dict): The kwargs of the related service definition. Unused.
     """
-    username = request.validated.get("ignore_user")
-    if username is None:
-        return
+    return ensure_user_exists("user", request)
 
-    db = request.db
-    user = db.query(User).filter_by(name=username).first()
 
-    if user:
-        request.validated["ignore_user"] = user
-    else:
-        request.errors.add("querystring", "ignore_user",
-                           "Invalid user specified: {}".format(username))
+def validate_update_owner(request, **kwargs):
+    """
+    Make sure the referenced update owner is an existing user.
+
+    Args:
+        request (pyramid.util.Request): The current request.
+        kwargs (dict): The kwargs of the related service definition. Unused.
+    """
+    return ensure_user_exists("update_owner", request)
+
+
+def validate_ignore_user(request, **kwargs):
+    """
+    Make sure the ignore_user parameter references an existing user.
+
+    Args:
+        request (pyramid.util.Request): The current request.
+        kwargs (dict): The kwargs of the related service definition. Unused.
+    """
+    return ensure_user_exists("ignore_user", request)
 
 
 def validate_update_id(request, **kwargs):


Test patch: