From 86f2c4b05d8cb57e07a14c915b676542df00c81b Mon Sep 17 00:00:00 2001
From: Giulia Naponiello <gnaponie@redhat.com>
Date: Oct 01 2018 13:59:40 +0000
Subject: Adjust greenwave to support new PELC type


There is a new PELC subject type that is "component-version",
and we need Greenwave to accept this new type.

---

diff --git a/greenwave/api_v1.py b/greenwave/api_v1.py
index 1a869e6..b2096b8 100644
--- a/greenwave/api_v1.py
+++ b/greenwave/api_v1.py
@@ -44,6 +44,8 @@ def subject_list_to_type_identifier(subject):
         return ('koji_build', subject[0]['item'])
     if len(subject) == 1 and 'original_spec_nvr' in subject[0]:
         return ('koji_build', subject[0]['original_spec_nvr'])
+    if len(subject) == 1 and subject[0].get('type') == 'component-version' and 'item' in subject[0]:
+        return ('component-version', subject[0]['item'])
     raise BadRequest('Unrecognised subject type: %r' % subject)
 
 
@@ -62,6 +64,8 @@ def subject_type_identifier_to_list(subject_type, subject_identifier):
         return [{'type': 'koji_build', 'item': subject_identifier}]
     elif subject_type == 'compose':
         return [{'productmd.compose.id': subject_identifier}]
+    elif subject_type == 'component-version':
+        return [{'type': 'component-version', 'item': subject_identifier}]
     else:
         raise BadRequest('Unrecognised subject type: %s' % subject_type)
 
@@ -274,7 +278,6 @@ def make_decision():
     :statuscode 200: A decision was made.
     :statuscode 400: Invalid data was given.
     """  # noqa: E501
-
     if request.get_json():
         if ('product_version' not in request.get_json() or
                 not request.get_json()['product_version']):
diff --git a/greenwave/policies.py b/greenwave/policies.py
index f3782a1..7a28b81 100644
--- a/greenwave/policies.py
+++ b/greenwave/policies.py
@@ -51,6 +51,8 @@ def subject_type_identifier_to_item(subject_type, subject_identifier):
         return {'type': 'koji_build', 'item': subject_identifier}
     elif subject_type == 'compose':
         return {'productmd.compose.id': subject_identifier}
+    elif subject_type == 'component-version':
+        return {'type': 'component-version', 'item': subject_identifier}
     else:
         raise RuntimeError('Unrecognised subject type: %s' % subject_type)
 
@@ -488,7 +490,7 @@ class Policy(SafeYAMLObject):
         'decision_context': SafeYAMLString(),
         # TODO: Handle brew-build value better.
         'subject_type': SafeYAMLChoice(
-            'koji_build', 'bodhi_update', 'compose', 'brew-build'),
+            'koji_build', 'bodhi_update', 'compose', 'brew-build', 'component-version'),
         'rules': SafeYAMLList(Rule),
         'blacklist': SafeYAMLList(str, optional=True),
         'relevance_key': SafeYAMLString(optional=True),
diff --git a/greenwave/resources.py b/greenwave/resources.py
index 6dc7a42..bd818f5 100644
--- a/greenwave/resources.py
+++ b/greenwave/resources.py
@@ -125,6 +125,10 @@ class ResultsRetriever(object):
             params['type'] = 'compose'
             params['item'] = subject_identifier
             results.extend(self._make_request(params=params))
+        elif subject_type == 'component-version':
+            params['type'] = subject_type
+            params['item'] = subject_identifier
+            results = self._make_request(params=params)
         else:
             raise RuntimeError('Unhandled subject type %r' % subject_type)
 
diff --git a/greenwave/tests/test_policies.py b/greenwave/tests/test_policies.py
index 22dfbf0..4d6979b 100644
--- a/greenwave/tests/test_policies.py
+++ b/greenwave/tests/test_policies.py
@@ -768,3 +768,27 @@ def test_policies_to_json():
         'relevance_key': None,
         'relevance_value': None,
     }
+
+
+def test_policy_with_subject_type_component_version(tmpdir):
+    nv = '389-ds-base-1.4.0.10'
+    p = tmpdir.join('fedora.yaml')
+    p.write("""
+--- !Policy
+id: "test-new-subject-type"
+product_versions:
+- fedora-29
+decision_context: decision_context_test_component_version
+subject_type: component-version
+blacklist: []
+rules:
+  - !PassingTestCaseRule {test_case_name: test_for_new_type}
+        """)
+    policies = load_policies(tmpdir.strpath)
+    policy = policies[0]
+    results = DummyResultsRetriever(nv, 'test_for_new_type', 'PASSED',
+                                    'component-version')
+    waivers = []
+    decision = policy.check(nv, results, waivers)
+    assert len(decision) == 1
+    assert isinstance(decision[0], RuleSatisfied)

