From 1923eb5da4d622031cb2c14aa5f2adfb0cf5ba57 Mon Sep 17 00:00:00 2001
From: Valerij Maljulin <vmaljuli@redhat.com>
Date: Nov 06 2019 14:22:13 +0000
Subject: Use extra->source instead of just source.

This fixes #470.

Signed-off-by: Valerij Maljulin <vmaljuli@redhat.com>

---

diff --git a/greenwave/resources.py b/greenwave/resources.py
index 97d2315..8b2c098 100644
--- a/greenwave/resources.py
+++ b/greenwave/resources.py
@@ -143,7 +143,15 @@ def retrieve_scm_from_koji_build(nvr, build, koji_url):
     if not build:
         raise NotFound('Failed to find Koji build for "{}" at "{}"'.format(nvr, koji_url))
 
-    source = build.get('source')
+    source = None
+    try:
+        source = build['extra']['source']['original_url']
+    except (TypeError, KeyError, AttributeError):
+        pass
+    finally:
+        if not source:
+            source = build.get('source')
+
     if not source:
         raise NoSourceException(
             'Failed to retrieve SCM URL from Koji build "{}" at "{}" '
diff --git a/greenwave/tests/test_policies.py b/greenwave/tests/test_policies.py
index 7fb5e39..45b0ea6 100644
--- a/greenwave/tests/test_policies.py
+++ b/greenwave/tests/test_policies.py
@@ -1053,7 +1053,7 @@ def test_on_demand_policy_match(two_rules):
     with app.app_context():
         with mock.patch('xmlrpc.client.ServerProxy') as koji_server:
             koji_server_instance = mock.MagicMock()
-            koji_server_instance.getBuild.return_value = {'source': None}
+            koji_server_instance.getBuild.return_value = {'extra': {'source': None}}
             koji_server.return_value = koji_server_instance
             policy = OnDemandPolicy.create_from_json(serverside_json)
 
diff --git a/greenwave/tests/test_retrieve_gating_yaml.py b/greenwave/tests/test_retrieve_gating_yaml.py
index e3ff07b..6212ac1 100644
--- a/greenwave/tests/test_retrieve_gating_yaml.py
+++ b/greenwave/tests/test_retrieve_gating_yaml.py
@@ -20,7 +20,27 @@ def test_retrieve_scm_from_rpm_build():
     nvr = 'nethack-3.6.1-3.fc29'
     build = {
         'nvr': nvr,
-        'source': 'git+https://src.fedoraproject.org/rpms/nethack.git#0c1a84e0e8a152897003bd7e27b3f407ff6ba040' # noqa
+        'extra': {
+            'source': {
+                'original_url': 'git+https://src.fedoraproject.org/rpms/nethack.git#'
+                                '0c1a84e0e8a152897003bd7e27b3f407ff6ba040'
+            }
+        },
+        # also check, that there's no fallback to source
+        'source': 'git+https://src.fedoraproject.org/rpms/nethack.git#master'
+    }
+    namespace, pkg_name, rev = retrieve_scm_from_koji_build(nvr, build, KOJI_URL)
+    assert namespace == 'rpms'
+    assert rev == '0c1a84e0e8a152897003bd7e27b3f407ff6ba040'
+    assert pkg_name == 'nethack'
+
+
+def test_retrieve_scm_from_rpm_build_fallback_to_source():
+    nvr = 'nethack-3.6.1-3.fc29'
+    build = {
+        'nvr': nvr,
+        'source': 'git+https://src.fedoraproject.org/rpms/nethack.git#'
+                  '0c1a84e0e8a152897003bd7e27b3f407ff6ba040'
     }
     namespace, pkg_name, rev = retrieve_scm_from_koji_build(nvr, build, KOJI_URL)
     assert namespace == 'rpms'
@@ -32,7 +52,9 @@ def test_retrieve_scm_from_container_build():
     nvr = 'golang-github-openshift-prometheus-alert-buffer-container-v3.10.0-0.34.0.0'
     build = {
         'nvr': nvr,
-        'source': 'git://pkgs.devel.redhat.com/containers/golang-github-openshift-prometheus-alert-buffer#46af2f8efbfb0a4e7e7d5676f4efb997f72d4b8c' # noqa
+        'source': 'git://pkgs.devel.redhat.com/containers/'
+                  'golang-github-openshift-prometheus-alert-buffer#'
+                  '46af2f8efbfb0a4e7e7d5676f4efb997f72d4b8c'
     }
     namespace, pkg_name, rev = retrieve_scm_from_koji_build(nvr, build, KOJI_URL)
     assert namespace == 'containers'
@@ -62,7 +84,11 @@ def test_retrieve_scm_from_build_without_namespace():
     nvr = 'foo-1.2.3-1.fc29'
     build = {
         'nvr': nvr,
-        'source': 'git+https://src.fedoraproject.org/foo.git#deadbeef',
+        'extra': {
+            'source': {
+                'original_url': 'git+https://src.fedoraproject.org/foo.git#deadbeef'
+            }
+        }
     }
     namespace, pkg_name, rev = retrieve_scm_from_koji_build(nvr, build, KOJI_URL)
     assert namespace == ''
@@ -74,7 +100,11 @@ def test_retrieve_scm_from_build_with_missing_rev():
     nvr = 'foo-1.2.3-1.fc29'
     build = {
         'nvr': nvr,
-        'source': 'git+https://src.fedoraproject.org/rpms/foo.git',
+        'extra': {
+            'source': {
+                'original_url': 'git+https://src.fedoraproject.org/rpms/foo.git'
+            }
+        }
     }
     expected_error = 'missing URL fragment with SCM revision information'
     with pytest.raises(BadGateway, match=expected_error):
@@ -103,8 +133,6 @@ def test_retrieve_yaml_remote_rule_connection_error():
     app = greenwave.app_factory.create_app()
     with app.app_context():
         with mock.patch('requests.Session.request') as mocked_request:
-            # Return 404, because we are only interested in the URL in the request
-            # and whether it is correct even with empty namespace.
             response = mock.MagicMock()
             response.status_code = 200
             mocked_request.side_effect = [

