Skip to content

Commit

Permalink
use galaxy-importer to import legacy roles
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 Aug 1, 2023
1 parent 1bb8082 commit 901b35a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 25 deletions.
52 changes: 28 additions & 24 deletions galaxy_ng/app/api/v1/tasks.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import copy
import datetime
import logging
import os
import tempfile

from django.db import transaction

from galaxy_importer.utils import markup as markup_utils
from galaxy_importer.config import Config
from galaxy_importer.legacy_role import import_legacy_role

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 +60,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 +72,12 @@ 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:
# galaxy-importer requires importing legacy roles from the role's parent directory.
os.chdir(tmp_path)

# galaxy-importer wants the role's directory to be the name of the role.
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 +108,15 @@ def legacy_role_import(
logger.debug(f'GITHUB_REFERENCE: {github_reference}')
logger.debug(f'GITHUB_COMMIT: {github_commit}')

# Parse legacy role with galaxy-importer.
importer_config = Config()
result = import_legacy_role(checkout_path, namespace.name, importer_config, logger)
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 +130,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
52 changes: 51 additions & 1 deletion galaxy_ng/tests/integration/cli/test_community.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
SocialGithubClient
)

from ..utils.legacy import cleanup_social_user
from ..utils.legacy import clean_all_roles, cleanup_social_user


pytestmark = pytest.mark.qa # noqa: F821
Expand Down Expand Up @@ -171,6 +171,56 @@ def test_import_role_as_not_owner(ansible_config):
assert ds.get('count') == 0


@pytest.mark.deployment_community
def test_import_role_metadata(ansible_config):
"""Test role serializer fields after import with galaxy-importer>=0.4.11."""

github_user = "jctannerTEST"
github_repo = "role1"
role_name = "role1"
role_url = f"v1/roles/?github_user={github_user}&name={role_name}"

# Cleanup all roles.
clean_all_roles(ansible_config)

config = ansible_config(github_user)
client = SocialGithubClient(config=config)
client.login()
token = client.get_hub_token()
assert token is not None

import_pid = ansible_galaxy(
f"role import {github_user} {github_repo}",
ansible_config=config,
token=token,
cleanup=False,
check_retcode=False
)
assert import_pid.returncode == 0

result = client.get(role_url).json()["results"][0]

for field in [
"commit", "created", "description", "download_count", "github_branch", "github_repo",
"github_user", "id", "modified", "name", "summary_fields", "upstream_id", "username"
]:
assert field in result

summary_fields = result["summary_fields"]
assert summary_fields["dependencies"] == list()
assert summary_fields["namespace"]["name"] == "jctannerTEST"
assert summary_fields["provider_namespace"]["name"] == "jctannerTEST"
assert summary_fields["repository"]["name"] == "role1"
assert summary_fields["tags"] == list()

assert len(summary_fields["versions"]) == 1
for field in [
"active", "commit_date", "commit_sha", "created", "download_url",
"modified", "name", "release_date", "url", "version"
]:
assert field in summary_fields["versions"][0]


@pytest.mark.deployment_community
def test_delete_role_as_not_owner(ansible_config):
""" Tests role delete with non-owner """
Expand Down

0 comments on commit 901b35a

Please sign in to comment.