#565 Side-tags repo URL new parameters
Merged 4 years ago by vmaljulin. Opened 4 years ago by vmaljulin.
vmaljulin/greenwave side_tags_subj_id  into  master

@@ -0,0 +1,3 @@ 

+ --- !SubjectType

+ id: brew-build-group

+ supports_remote_rule: true

file modified
+2 -2
@@ -30,8 +30,8 @@ 

          'https://src.fedoraproject.org/{pkg_namespace}{pkg_name}/raw/{rev}/f/gating.yaml'

      REMOTE_RULE_POLICIES = {

          'brew-build-group': (

-             'https://git.example.com/devops/greenwave-policies/side-tags/raw/master/{pkg_namespace}'

-             '{pkg_name}.yaml'

+             'https://git.example.com/devops/greenwave-policies/side-tags/raw/master/'

+             '{subject_id}.yaml'

          ),

          '*': 'https://src.fedoraproject.org/{pkg_namespace}{pkg_name}/raw/{rev}/f/gating.yaml'

      }

file modified
+31 -15
@@ -433,23 +433,11 @@ 

          'required': SafeYAMLBool(optional=True, default=False),

      }

  

-     def _get_sub_policies(self, policy, subject):

+     @staticmethod

+     def _get_sub_policies(policy, subject):

          if not subject.supports_remote_rule:

              return []

  

-         try:

-             pkg_namespace, pkg_name, rev = greenwave.resources.retrieve_scm_from_koji(

-                 subject.identifier

-             )

-         except greenwave.resources.NoSourceException as e:

-             log.error(e)

-             return None

- 

-         # if the element is actually a container and not a pkg there will be a "-container"

-         # string at the end of the "pkg_name" and it will not match with the one in the

-         # remote rule file URL

-         if pkg_namespace == 'containers':

-             pkg_name = re.sub('-container$', '', pkg_name)

          rr_policies_conf = current_app.config.get('REMOTE_RULE_POLICIES', {})

          cur_subject_url = rr_policies_conf.get(

              policy.subject_type, current_app.config.get(
@@ -459,8 +447,36 @@ 

          if not cur_subject_url:

              raise RuntimeError(f'Cannot use a remote rule for {subject} subject '

                                 f'as it has not been configured')

+ 

+         response = None

+         url_params = {}

+         if '{pkg_name}' in cur_subject_url or '{pkg_namespace}' in cur_subject_url or \

+                 '{rev}' in cur_subject_url:

+             try:

+                 pkg_namespace, pkg_name, rev = greenwave.resources.retrieve_scm_from_koji(

+                     subject.identifier

+                 )

+             except greenwave.resources.NoSourceException as e:

+                 log.error(e)

+                 return None

+ 

+             # if the element is actually a container and not a pkg there will be a "-container"

+             # string at the end of the "pkg_name" and it will not match with the one in the

+             # remote rule file URL

+             if pkg_namespace == 'containers':

+                 pkg_name = re.sub('-container$', '', pkg_name)

+             if pkg_namespace:

+                 pkg_namespace += '/'

+             url_params.update(rev=rev, pkg_name=pkg_name, pkg_namespace=pkg_namespace)

+ 

+         if '{subject_id}' in cur_subject_url:

+             subj_id = subject.identifier

+             if subj_id.startswith('sha256:'):

+                 subj_id = subj_id[7:]

+             url_params.update(subject_id=subj_id)

+ 

          response = greenwave.resources.retrieve_yaml_remote_rule(

-             rev, pkg_name, pkg_namespace, cur_subject_url

+             cur_subject_url.format(**url_params)

          )

  

          if response is None:

file modified
+1 -7
@@ -164,14 +164,8 @@ 

  

  

  @cached

- def retrieve_yaml_remote_rule(rev, pkg_name, pkg_namespace, url_template):

+ def retrieve_yaml_remote_rule(url):

      """ Retrieve a remote rule file content from the git web UI. """

-     data = {

-         "pkg_namespace": pkg_namespace + ('/' if pkg_namespace else ''),

-         "pkg_name": pkg_name,

-         "rev": rev

-     }

-     url = url_template.format(**data)

      response = requests_session.request('HEAD', url)

      if response.status_code == 404:

          return None

@@ -188,6 +188,7 @@ 

      assert len(data['subject_types'])

      assert [x['id'] for x in data['subject_types']] == [

          'bodhi_update',

+         'brew-build-group',

          'compose',

          'koji_build',

          'redhat-container-image',

@@ -339,16 +339,13 @@ 

                  assert len(decision) == 1

                  assert isinstance(decision[0], TestResultFailed)

                  f.assert_called_with(

-                     'c3c47a08a66451cb9686c49f040776ed35a0d1bb',

-                     'nethack',

-                     namespace,

-                     'https://src.fedoraproject.org/{pkg_namespace}'

-                     '{pkg_name}/raw/{rev}/f/gating.yaml'

+                     'https://src.fedoraproject.org/{0}'.format(

+                         '' if not namespace else namespace + '/'

+                     ) + 'nethack/raw/c3c47a08a66451cb9686c49f040776ed35a0d1bb/f/gating.yaml'

                  )

  

  

- @pytest.mark.parametrize('namespace', ["rpms", ""])

- def test_remote_rule_policy_old_config(tmpdir, namespace):

+ def test_remote_rule_policy_old_config(tmpdir):

      """ Testing the RemoteRule with the koji interaction.

      In this case we are just mocking koji """

  
@@ -385,14 +382,14 @@ 

  

          config = TestingConfig()

          config.DIST_GIT_BASE_URL = 'http://localhost.localdomain/'

-         config.DIST_GIT_URL_TEMPLATE = '{DIST_GIT_BASE_URL}{other_params}/blablabla/gating.yaml'

+         config.DIST_GIT_URL_TEMPLATE = '{DIST_GIT_BASE_URL}{pkg_name}/{rev}/gating.yaml'

  

          app = create_app(config)

  

          with app.app_context():

              with mock.patch('greenwave.resources.retrieve_scm_from_koji') as scm:

                  scm.return_value = (

-                     namespace, 'nethack', 'c3c47a08a66451cb9686c49f040776ed35a0d1bb'

+                     'rpms', 'nethack', 'c3c47a08a66451cb9686c49f040776ed35a0d1bb'

                  )

                  with mock.patch('greenwave.resources.retrieve_yaml_remote_rule') as f:

                      f.return_value = remote_fragment
@@ -406,21 +403,22 @@ 

                      assert isinstance(decision[0], RuleSatisfied)

  

                      f.assert_called_once_with(

-                         'c3c47a08a66451cb9686c49f040776ed35a0d1bb',

-                         'nethack',

-                         namespace,

-                         'http://localhost.localdomain/{other_params}/blablabla/gating.yaml'

+                         'http://localhost.localdomain/nethack/'

+                         'c3c47a08a66451cb9686c49f040776ed35a0d1bb/gating.yaml'

                      )

      finally:

          Config.REMOTE_RULE_POLICIES = config_remote_rules_backup

  

  

- @pytest.mark.parametrize('namespace', ["rpms", ""])

- def test_remote_rule_policy_brew_build_group(tmpdir, namespace):

+ def test_remote_rule_policy_brew_build_group(tmpdir):

      """ Testing the RemoteRule with the koji interaction.

      In this case we are just mocking koji """

  

-     subject = create_test_subject('koji_build', 'nethack-1.2.3-1.el9000')

+     subject = create_test_subject(

+         'brew-build-group',

+         'sha256:0f41e56a1c32519e189ddbcb01d2551e861bd74e603d01769ef5f70d4b30a2dd'

+     )

+     namespace = 'rpms'

  

      serverside_fragment = dedent("""

          --- !Policy
@@ -472,12 +470,10 @@ 

                  assert len(decision) == 1

                  assert isinstance(decision[0], TestResultFailed)

                  f.assert_called_with(

-                     'c3c47a08a66451cb9686c49f040776ed35a0d1bb',

-                     'nethack',

-                     namespace,

                      'https://git.example.com/devops/greenwave-policies/side-tags/raw/'

-                     'master/{pkg_namespace}{pkg_name}.yaml'

+                     'master/0f41e56a1c32519e189ddbcb01d2551e861bd74e603d01769ef5f70d4b30a2dd.yaml'

                  )

+             scm.assert_not_called()

  

  

  def test_remote_rule_policy_with_no_remote_rule_policies_param_defined(tmpdir):
@@ -525,11 +521,8 @@ 

                  assert len(decision) == 1

                  assert isinstance(decision[0], RuleSatisfied)

                  f.assert_called_with(

-                     'c3c47a08a66451cb9686c49f040776ed35a0d1bb',

-                     'nethack',

-                     'rpms',

-                     'https://src.fedoraproject.org/{pkg_namespace}'

-                     '{pkg_name}/raw/{rev}/f/gating.yaml'

+                     'https://src.fedoraproject.org/rpms/nethack/raw/'

+                     'c3c47a08a66451cb9686c49f040776ed35a0d1bb/f/gating.yaml'

                  )

  

  

@@ -121,7 +121,9 @@ 

              response.status_code = 404

              session.request.return_value = response

              retrieve_yaml_remote_rule(

-                 "deadbeaf", "pkg", "", app.config['REMOTE_RULE_POLICIES']['*']

+                 app.config['REMOTE_RULE_POLICIES']['*'].format(

+                     rev='deadbeaf', pkg_name='pkg', pkg_namespace=''

+                 )

              )

  

              expected_call = mock.call(
@@ -141,7 +143,9 @@ 

  

              with pytest.raises(HTTPError) as excinfo:

                  retrieve_yaml_remote_rule(

-                     "deadbeaf", "pkg", "", app.config['REMOTE_RULE_POLICIES']['*']

+                     app.config['REMOTE_RULE_POLICIES']['*'].format(

+                         rev='deadbeaf', pkg_name='pkg', pkg_namespace=''

+                     )

                  )

  

              assert str(excinfo.value) == (

It's safer just create a formatting dict in these branches:

format_dict = {}

if '{pkg_name}' in cur_subject_url ...:
    ...
    format_dict.update(rev=rev, pkg_name=pkg_name, pkg_namespace=pkg_namespace)

if '{subject_id}' in cur_subject_url:
    ...
    format_dict.update(subject_id=subj_id)

response = greenwave.resources.retrieve_yaml_remote_rule(
    cur_subject_url.format(format_dict)
)

Just 1 comment, otherwise it looks good.

rebased onto 8c89050

4 years ago

Commit 23e59dd fixes this pull-request

Pull-Request has been merged by vmaljulin

4 years ago

Pull-Request has been merged by vmaljulin

4 years ago