From c123eb41af8e392746e041b42f5c6ee6afd72901 Mon Sep 17 00:00:00 2001
From: Lenka Segura <lenka@sepu.cz>
Date: Jan 03 2019 08:19:54 +0000
Subject: Fix pull-request filtering by status in API calls


---

diff --git a/dev-data.py b/dev-data.py
index f2bff1d..6d771fa 100644
--- a/dev-data.py
+++ b/dev-data.py
@@ -355,11 +355,83 @@ def insert_data(session, username, user_email):
         repo_to=repo,
         branch_to='master',
         title='Fixing code for unittest',
-        user=username
+        user=username,
+        status="Open"
     )
     session.commit()
 
-    ######################################
+    repo = pagure.lib.query.get_authorized_project(session, 'test')
+    forked_repo = pagure.lib.query.get_authorized_project(session, 'test')
+    req = pagure.lib.query.new_pull_request(
+        session=session,
+        repo_from=forked_repo,
+        branch_from='master',
+        repo_to=repo,
+        branch_to='master',
+        title='add very nice README',
+        user=username,
+        status="Open"
+    )
+    session.commit()
+
+    repo = pagure.lib.query.get_authorized_project(session, 'test')
+    forked_repo = pagure.lib.query.get_authorized_project(session, 'test')
+    req = pagure.lib.query.new_pull_request(
+        session=session,
+        repo_from=forked_repo,
+        branch_from='master',
+        repo_to=repo,
+        branch_to='master',
+        title='Add README',
+        user=username,
+        status="Closed"
+    )
+    session.commit()
+
+    repo = pagure.lib.query.get_authorized_project(session, 'test')
+    forked_repo = pagure.lib.query.get_authorized_project(session, 'test')
+    req = pagure.lib.query.new_pull_request(
+        session=session,
+        repo_from=forked_repo,
+        branch_from='master',
+        repo_to=repo,
+        branch_to='master',
+        title='Fix some containers',
+        user=username,
+        status="Merged"
+    )
+    session.commit()
+
+    repo = pagure.lib.query.get_authorized_project(session, 'test')
+    forked_repo = pagure.lib.query.get_authorized_project(session, 'test')
+    req = pagure.lib.query.new_pull_request(
+        session=session,
+        repo_from=forked_repo,
+        branch_from='master',
+        repo_to=repo,
+        branch_to='master',
+        title='Fix pull request statuses',
+        user=username,
+        status="Closed"
+    )
+    session.commit()
+
+    repo = pagure.lib.query.get_authorized_project(session, 'test')
+    forked_repo = pagure.lib.query.get_authorized_project(session, 'test')
+    req = pagure.lib.query.new_pull_request(
+        session=session,
+        repo_from=forked_repo,
+        branch_from='master',
+        repo_to=repo,
+        branch_to='master',
+        title='Fixing UI of issue',
+        user=username,
+        status="Merged"
+    )
+    session.commit()
+
+
+#####################################
     # tokens
     tests.create_tokens(session, user_id=pingou.id, project_id=project1.id)
 
@@ -426,12 +498,40 @@ def insert_data(session, username, user_email):
         percent=80,
         comment="Jenkins build passes",
         url=str(pr.id),
-        status="Open"
+        status="success"
+    )
+    session.add(item)
+    session.commit()
+
+    pr = pagure.lib.query.get_pull_request_of_user(session, "foo")[1]
+    item = pagure.lib.model.PullRequestFlag(
+        uid="oink oink uid",
+        pull_request_uid=pr.uid,
+        user_id=pingou.id,
+        username=pingou.user,
+        percent=80,
+        comment="Jenkins does not pass",
+        url=str(pr.id),
+        status="failure"
     )
     session.add(item)
     session.commit()
 
     ######################################
+    # pull_request_assignee
+    pr = pagure.lib.query.search_pull_requests(session, requestid='1006')
+    pr.assignee_id = pingou.id
+    session.commit()
+    
+    pr = pagure.lib.query.search_pull_requests(session, requestid='1007')
+    pr.assignee_id = you.id
+    session.commit()
+
+    pr = pagure.lib.query.search_pull_requests(session, requestid='1004')
+    pr.assignee_id = foo.id
+    session.commit()
+
+    ######################################
     # tags_issues
     repo = pagure.lib.query.get_authorized_project(session, 'test')
     issues = pagure.lib.query.search_issues(session, repo)
@@ -595,7 +695,7 @@ if __name__ == "__main__":
         empty_dev_db(meta, eng)
 
     if args.populate or args.all:
-        session = create_session(_config['DB_URL'])
+        session = pagure.lib.query.create_session(_config['DB_URL'])
         invalid_option = ['pingou', 'bar@pingou.com', 'foo', 'foo@bar.com']
         print("")
         user_name = six.moves.input(
diff --git a/pagure/api/fork.py b/pagure/api/fork.py
index c2b7e71..0f563d9 100644
--- a/pagure/api/fork.py
+++ b/pagure/api/fork.py
@@ -164,7 +164,7 @@ def api_pull_request_views(repo, username=None, namespace=None):
 
     status_text = ("%s" % status).lower()
     requests = []
-    if status_text in ["0", "false", "closed"]:
+    if status_text in ["0", "false"]:
         requests = pagure.lib.query.search_pull_requests(
             flask.g.session,
             project_id=repo.id,
diff --git a/pagure/lib/query.py b/pagure/lib/query.py
index 2b5c855..3c3f3d1 100644
--- a/pagure/lib/query.py
+++ b/pagure/lib/query.py
@@ -45,7 +45,7 @@ import sqlalchemy
 import sqlalchemy.schema
 
 from six.moves.urllib_parse import urlparse, urlencode, parse_qsl
-from sqlalchemy import func
+from sqlalchemy import func, create_engine
 from sqlalchemy import asc, desc
 from sqlalchemy.orm import aliased
 from sqlalchemy.orm import sessionmaker
@@ -162,14 +162,14 @@ def create_session(db_url=None, debug=False, pool_recycle=3600):
         if db_url is None:
             raise ValueError("First call to create_session needs db_url")
         if db_url.startswith("postgres"):  # pragma: no cover
-            engine = sqlalchemy.create_engine(
+            engine = create_engine(
                 db_url,
                 echo=debug,
                 pool_recycle=pool_recycle,
                 client_encoding="utf8",
             )
         else:  # pragma: no cover
-            engine = sqlalchemy.create_engine(
+            engine = create_engine(
                 db_url, echo=debug, pool_recycle=pool_recycle
             )
 
diff --git a/pagure/ui/filters.py b/pagure/ui/filters.py
index 51c6c22..7a061f1 100644
--- a/pagure/ui/filters.py
+++ b/pagure/ui/filters.py
@@ -762,7 +762,7 @@ def table_get_link_order(column, order_key, order):
 def flag_to_label(flag):
     """ For a given flag return the bootstrap label to use
     """
-    return pagure_config["FLAG_STATUSES_LABELS"][flag.status.lower()]
+    return pagure_config["FLAG_STATUSES_LABELS"][flag.status]
 
 
 @UI_NS.app_template_filter("join_prefix")

