From c579426a6dd2e49e5c88fe71975a4f264710fb2e Mon Sep 17 00:00:00 2001
From: Lukas Holecek <lholecek@redhat.com>
Date: Jun 11 2018 08:10:40 +0000
Subject: Fix showing all results and waivers with verbose flag


---

diff --git a/functional-tests/test_api_v1.py b/functional-tests/test_api_v1.py
index 45a023c..d9c561d 100644
--- a/functional-tests/test_api_v1.py
+++ b/functional-tests/test_api_v1.py
@@ -254,6 +254,68 @@ def test_make_a_decision_with_verbose_flag(requests_session, greenwave_server, t
     assert res_data['satisfied_requirements'] == expected_satisfied_requirements
 
 
+def test_make_a_decision_with_verbose_flag_and_multiple_nvrs_with_results(
+        requests_session, greenwave_server, testdatabuilder):
+    build_nvrs = [testdatabuilder.unique_nvr(), testdatabuilder.unique_nvr()]
+
+    results = []
+    for nvr in reversed(build_nvrs):
+        for testcase_name in TASKTRON_RELEASE_CRITICAL_TASKS:
+            results.append(testdatabuilder.create_result(
+                item=nvr, testcase_name=testcase_name, outcome='PASSED'))
+
+    update = testdatabuilder.create_bodhi_update(build_nvrs=build_nvrs)
+    data = {
+        'decision_context': 'bodhi_update_push_stable',
+        'product_version': 'fedora-26',
+        'subject_type': 'bodhi_update',
+        'subject_identifier': update['updateid'],
+        'verbose': True,
+    }
+
+    r = requests_session.post(greenwave_server + 'api/v1.0/decision',
+                              headers={'Content-Type': 'application/json'},
+                              data=json.dumps(data))
+    assert r.status_code == 200
+    res_data = r.json()
+
+    assert len(res_data['results']) == len(TASKTRON_RELEASE_CRITICAL_TASKS) * len(build_nvrs)
+    assert res_data['results'] == list(reversed(results))
+
+
+def test_make_a_decision_with_verbose_flag_and_multiple_nvrs_with_waivers(
+        requests_session, greenwave_server, testdatabuilder):
+    build_nvrs = [testdatabuilder.unique_nvr(), testdatabuilder.unique_nvr()]
+
+    waivers = []
+    for nvr in reversed(build_nvrs):
+        for testcase_name in TASKTRON_RELEASE_CRITICAL_TASKS:
+            waiver = testdatabuilder.create_waiver(
+                nvr=nvr,
+                product_version='fedora-26',
+                testcase_name=testcase_name,
+                comment='This is fine')
+            waivers.append(waiver)
+
+    update = testdatabuilder.create_bodhi_update(build_nvrs=build_nvrs)
+    data = {
+        'decision_context': 'bodhi_update_push_stable',
+        'product_version': 'fedora-26',
+        'subject_type': 'bodhi_update',
+        'subject_identifier': update['updateid'],
+        'verbose': True,
+    }
+
+    r = requests_session.post(greenwave_server + 'api/v1.0/decision',
+                              headers={'Content-Type': 'application/json'},
+                              data=json.dumps(data))
+    assert r.status_code == 200
+    res_data = r.json()
+
+    assert len(res_data['waivers']) == len(TASKTRON_RELEASE_CRITICAL_TASKS) * len(build_nvrs)
+    assert res_data['waivers'] == list(reversed(waivers))
+
+
 def test_make_a_decision_on_failed_result_with_waiver(
         requests_session, greenwave_server, testdatabuilder):
     nvr = testdatabuilder.unique_nvr()
diff --git a/greenwave/api_v1.py b/greenwave/api_v1.py
index b0f95da..f5b1ad4 100644
--- a/greenwave/api_v1.py
+++ b/greenwave/api_v1.py
@@ -335,12 +335,16 @@ def make_decision():
     if build_policies:
         build_nvrs = retrieve_builds_in_update(subject_identifier)
         for nvr in build_nvrs:
-            results = retrieve_results('koji_build', nvr)
-            results = [r for r in results if r['id'] not in ignore_results]
-            waivers = retrieve_waivers(product_version, 'koji_build', nvr)
-            waivers = [w for w in waivers if w['id'] not in ignore_waivers]
+            nvr_results = retrieve_results('koji_build', nvr)
+            nvr_results = [r for r in nvr_results if r['id'] not in ignore_results]
+            results.extend(nvr_results)
+
+            nvr_waivers = retrieve_waivers(product_version, 'koji_build', nvr)
+            nvr_waivers = [w for w in nvr_waivers if w['id'] not in ignore_waivers]
+            waivers.extend(nvr_waivers)
+
             for policy in build_policies:
-                answers.extend(policy.check(nvr, results, waivers))
+                answers.extend(policy.check(nvr, nvr_results, nvr_waivers))
 
     res = {
         'policies_satisfied': all(answer.is_satisfied for answer in answers),

