From 6f4e92da13edc688402f5d8ed8f2d88d20ef00d9 Mon Sep 17 00:00:00 2001 From: eamontracey Date: Tue, 30 May 2023 14:55:24 -0400 Subject: [PATCH] use galaxy-importer LegacyRoleLoader No-Issue Signed-off-by: eamontracey --- galaxy_ng/app/api/v1/tasks.py | 54 ++++++++++--------- .../integration/cli/test_legacy_role_lint.py | 35 ++++++++++++ 2 files changed, 65 insertions(+), 24 deletions(-) create mode 100644 galaxy_ng/tests/integration/cli/test_legacy_role_lint.py diff --git a/galaxy_ng/app/api/v1/tasks.py b/galaxy_ng/app/api/v1/tasks.py index 6435fc32e1..b190011acb 100644 --- a/galaxy_ng/app/api/v1/tasks.py +++ b/galaxy_ng/app/api/v1/tasks.py @@ -1,17 +1,21 @@ import copy import datetime import logging +import os import tempfile from django.db import transaction -from galaxy_importer.utils import markup as markup_utils +try: + from galaxy_importer.config import Config + from galaxy_importer.loaders import LegacyRoleLoader +except: + pass from galaxy_ng.app.models.auth import User from galaxy_ng.app.utils.galaxy import upstream_role_iterator from galaxy_ng.app.utils.git import get_tag_commit_hash from galaxy_ng.app.utils.git import get_tag_commit_date -import galaxy_ng.app.utils.roles as roles_utils from galaxy_ng.app.api.v1.models import LegacyNamespace from galaxy_ng.app.api.v1.models import LegacyRole @@ -59,7 +63,6 @@ def legacy_role_import( if not github_reference: github_reference = None - role_name = alternate_role_name or github_repo.replace('ansible-role-', '') if LegacyNamespace.objects.filter(name=github_user).count() == 0: logger.debug(f'CREATE NEW NAMESPACE {github_user}') namespace, _ = LegacyNamespace.objects.get_or_create(name=github_user) @@ -72,7 +75,8 @@ def legacy_role_import( logger.debug(f'USE EXISTING NAMESPACE {github_user}') namespace = LegacyNamespace.objects.filter(name=github_user).first() - with tempfile.TemporaryDirectory() as checkout_path: + with tempfile.TemporaryDirectory() as tmp_path: + checkout_path = os.path.join(tmp_path, github_repo) clone_url = f'https://github.com/{github_user}/{github_repo}' gitrepo = Repo.clone_from(clone_url, checkout_path, multi_options=["--recurse-submodules"]) @@ -103,6 +107,18 @@ def legacy_role_import( logger.debug(f'GITHUB_REFERENCE: {github_reference}') logger.debug(f'GITHUB_COMMIT: {github_commit}') + # parse legacy role with galaxy-importer + result = LegacyRoleLoader( + checkout_path, + namespace.name, + cfg=Config(), + logger=logger, + ).load() + galaxy_info = result.metadata.galaxy_info + logger.debug(f'TAGS: {galaxy_info.galaxy_tags}') + + role_name = result.name or alternate_role_name or github_repo.replace('ansible-role-', '') + # check if this namespace/name/version has already been imported old = LegacyRole.objects.filter(namespace=namespace, name=role_name).first() if old is not None: @@ -116,33 +132,23 @@ def legacy_role_import( ) raise Exception(msg) - role_meta = roles_utils.get_path_role_meta(checkout_path) - role_tags = role_meta.get('galaxy_info', {}).get('galaxy_tags', []) - if role_tags is None: - role_tags = [] - logger.debug(f'TAGS: {role_tags}') - - # use the importer to grok the readme - readme = markup_utils.get_readme_doc_file(checkout_path) - if not readme: - raise Exception("No role readme found.") - readme_html = markup_utils.get_html(readme) - - galaxy_info = role_meta.get('galaxy_info', {}) new_full_metadata = { 'imported': datetime.datetime.now().isoformat(), 'clone_url': clone_url, - 'tags': role_tags, + 'tags': galaxy_info.galaxy_tags, 'commit': github_commit, 'github_repo': github_repo, 'github_reference': github_reference, - 'issue_tracker_url': clone_url + '/issues', - 'dependencies': [], + 'issue_tracker_url': galaxy_info.issue_tracker_url or clone_url + '/issues', + 'dependencies': result.metadata.dependencies, 'versions': [], - 'description': galaxy_info.get('description', ''), - 'license': galaxy_info.get('galaxy_info', {}).get('license', ''), - 'readme': readme, - 'readme_html': readme_html + 'description': galaxy_info.description or '', + 'license': galaxy_info.license or '', + 'min_ansible_version': galaxy_info.min_ansible_version or '', + 'min_ansible_container_version': galaxy_info.min_ansible_container_version or '', + 'platforms': galaxy_info.platforms, + 'readme': result.readme_file, + 'readme_html': result.readme_html } # Make the object diff --git a/galaxy_ng/tests/integration/cli/test_legacy_role_lint.py b/galaxy_ng/tests/integration/cli/test_legacy_role_lint.py new file mode 100644 index 0000000000..1e4eb1debe --- /dev/null +++ b/galaxy_ng/tests/integration/cli/test_legacy_role_lint.py @@ -0,0 +1,35 @@ +import pytest + +from ..utils import ansible_galaxy, SocialGithubClient +from ..utils.legacy import clean_all_roles + + +@pytest.mark.community_only +def test_import_role_lint(ansible_config, caplog): + """Test that ansible-lint runs when importing a legacy role.""" + + github_user = "jctannerTEST" + github_repo = "role1" + + # Cleanup all roles. + clean_all_roles(ansible_config) + + # Login as jctannerTEST. + test_config = ansible_config(github_user) + test_client = SocialGithubClient(config=test_config) + test_client.login() + token = test_client.get_hub_token() + assert token is not None + + # Import jctannerTEST role1 as jctannerTEST. + import_pid = ansible_galaxy( + f"role import {github_user} {github_repo}", + ansible_config=test_config, + token=token, + force_token=True, + cleanup=False, + check_retcode=False + ) + assert import_pid.returncode == 0 + + assert caplog.text == ""