Skip to content

Commit

Permalink
use galaxy-importer LegacyRoleLoader
Browse files Browse the repository at this point in the history
No-Issue

Signed-off-by: eamontracey <etracey@redhat.com>
  • Loading branch information
EamonTracey committed Jun 23, 2023
1 parent dd7c39a commit 6f4e92d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 24 deletions.
54 changes: 30 additions & 24 deletions galaxy_ng/app/api/v1/tasks.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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"])
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
35 changes: 35 additions & 0 deletions galaxy_ng/tests/integration/cli/test_legacy_role_lint.py
Original file line number Diff line number Diff line change
@@ -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 == ""

0 comments on commit 6f4e92d

Please sign in to comment.