From b80dc93c56a61b0c88cde67df4c078bb38bc891d Mon Sep 17 00:00:00 2001 From: James Tanner Date: Fri, 20 Oct 2023 19:14:48 -0400 Subject: [PATCH] Add edit support for some legacyrole fields. No-Issue Signed-off-by: James Tanner --- galaxy_ng/app/api/v1/serializers.py | 50 ++++++++++- galaxy_ng/app/api/v1/urls.py | 2 +- galaxy_ng/app/api/v1/viewsets/roles.py | 43 +++++++++ .../integration/community/test_role_edits.py | 88 +++++++++++++++++++ 4 files changed, 178 insertions(+), 5 deletions(-) create mode 100644 galaxy_ng/tests/integration/community/test_role_edits.py diff --git a/galaxy_ng/app/api/v1/serializers.py b/galaxy_ng/app/api/v1/serializers.py index 61abc710b7..3aa9358840 100644 --- a/galaxy_ng/app/api/v1/serializers.py +++ b/galaxy_ng/app/api/v1/serializers.py @@ -217,6 +217,8 @@ class Meta: 'download_count', ] + # def update(self, instance, validated_data): + def get_id(self, obj): return obj.pulp_id @@ -304,6 +306,15 @@ def get_summary_fields(self, obj): 'pulp_href': pulp_href } + # FIXME - repository is a bit hacky atm + repository = {} + if obj.full_metadata.get('repository'): + repository = obj.full_metadata.get('repository') + if not repository.get('name'): + repository['name'] = obj.full_metadata.get('github_repo') + if not repository.get('original_name'): + repository['original_name'] = obj.full_metadata.get('github_repo') + return { 'dependencies': dependencies, 'namespace': { @@ -312,10 +323,7 @@ def get_summary_fields(self, obj): 'avatar_url': f'https://github.com/{obj.namespace.name}.png' }, 'provider_namespace': provider_ns, - 'repository': { - 'name': obj.name, - 'original_name': obj.full_metadata.get('github_repo') - }, + 'repository': repository, 'tags': tags, 'versions': versions } @@ -327,6 +335,40 @@ def get_download_count(self, obj): return 0 +class LegacyRoleRepositoryUpdateSerializer(serializers.Serializer): + name = serializers.CharField(required=False, allow_blank=False, max_length=50) + original_name = serializers.CharField(required=False, allow_blank=False, max_length=50) + + def is_valid(self, raise_exception=False): + # Check for any unexpected fields + extra_fields = set(self.initial_data.keys()) - set(self.fields.keys()) + if extra_fields: + self._errors = {field: ["Unexpected field."] for field in extra_fields} + else: + # Continue with the original validation logic + super(serializers.Serializer, self).is_valid(raise_exception=raise_exception) + + return not bool(self._errors) + + +class LegacyRoleUpdateSerializer(serializers.Serializer): + github_user = serializers.CharField(required=False, allow_blank=False, max_length=50) + github_repo = serializers.CharField(required=False, allow_blank=False, max_length=50) + github_branch = serializers.CharField(required=False, allow_blank=False, max_length=50) + repository = LegacyRoleRepositoryUpdateSerializer(required=False) + + def is_valid(self, raise_exception=False): + # Check for any unexpected fields + extra_fields = set(self.initial_data.keys()) - set(self.fields.keys()) + if extra_fields: + self._errors = {field: ["Unexpected field."] for field in extra_fields} + else: + # Continue with the original validation logic + super(serializers.Serializer, self).is_valid(raise_exception=raise_exception) + + return not bool(self._errors) + + class LegacyRoleContentSerializer(serializers.ModelSerializer): readme = serializers.SerializerMethodField() diff --git a/galaxy_ng/app/api/v1/urls.py b/galaxy_ng/app/api/v1/urls.py index 5c010f616f..df11e8b94a 100644 --- a/galaxy_ng/app/api/v1/urls.py +++ b/galaxy_ng/app/api/v1/urls.py @@ -36,7 +36,7 @@ path( 'roles//', - LegacyRolesViewSet.as_view({"get": "retrieve", "delete": "destroy"}), + LegacyRolesViewSet.as_view({"get": "retrieve", "delete": "destroy", "put": "update"}), name='legacy_role-detail' ), diff --git a/galaxy_ng/app/api/v1/viewsets/roles.py b/galaxy_ng/app/api/v1/viewsets/roles.py index 09aa914104..4028d82cc5 100644 --- a/galaxy_ng/app/api/v1/viewsets/roles.py +++ b/galaxy_ng/app/api/v1/viewsets/roles.py @@ -8,6 +8,7 @@ from rest_framework import mixins from rest_framework import viewsets +from rest_framework import status from rest_framework.response import Response from rest_framework.settings import perform_import from rest_framework.pagination import PageNumberPagination @@ -24,6 +25,7 @@ from galaxy_ng.app.api.v1.serializers import ( LegacyImportSerializer, LegacyRoleSerializer, + LegacyRoleUpdateSerializer, LegacyRoleContentSerializer, LegacyRoleVersionsSerializer, ) @@ -94,6 +96,47 @@ def list(self, request): return super().list(request) + def update(self, request, pk=None): + role = self.get_object() + serializer = LegacyRoleUpdateSerializer(role, data=request.data, partial=True) + if not serializer.is_valid(): + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + + changed = {} + for key, newval in serializer.validated_data.items(): + + # repositories are special ... + if key == 'repository': + if not role.full_metadata.get('repository'): + changed['repository'] = {} + role.full_metadata['repository'] = {} + for subkey, subval in newval.items(): + print(f'{key}.{subkey} {role.full_metadata[key].get(subkey)} --> {subval}') + if role.full_metadata.get(key, {}).get(subkey) != subval: + if key not in changed: + changed[key] = {} + role.full_metadata[key][subkey] = subval + changed[key][subkey] = subval + continue + + print(f'{key} {role.full_metadata.get(key)} --> {newval}') + if role.full_metadata.get(key) != newval: + role.full_metadata[key] = newval + changed[key] = newval + + # TODO - get rid of github_reference? + if key == 'github_branch': + key = 'github_reference' + if role.full_metadata.get(key) != newval: + role.full_metadata[key] = newval + changed[key] = newval + + # only save if changes made + if changed: + role.save() + + return Response(changed, status=204) + def destroy(self, request, pk=None): """Delete a single role.""" role = LegacyRole.objects.filter(id=pk).first() diff --git a/galaxy_ng/tests/integration/community/test_role_edits.py b/galaxy_ng/tests/integration/community/test_role_edits.py new file mode 100644 index 0000000000..a79321a8ef --- /dev/null +++ b/galaxy_ng/tests/integration/community/test_role_edits.py @@ -0,0 +1,88 @@ +import os +# import subprocess +import tempfile + +import pytest + +# from ..utils import ansible_galaxy, get_client, SocialGithubClient +from ..utils import ansible_galaxy, get_client +from ..utils.legacy import clean_all_roles, cleanup_social_user + + +@pytest.mark.deployment_community +def test_community_legacy_role_edit(ansible_config): + + # namespace_name = painless + # github_user = painless-software + # github_repository = ansible-role-software + # role_name = software + # install fqn = painless.software + # github_branch = main + + admin_config = ansible_config("admin") + admin_client = get_client( + config=admin_config, + request_token=False, + require_auth=True + ) + + namespace_v3name = 'jctannertest' + namespace_v1name = 'jctannerTEST' + github_user = 'jctannerTEST' + github_repo = 'role1' + role_name = 'role1' + branch = 'master' + + # cleanup + clean_all_roles(ansible_config) + cleanup_social_user(namespace_v3name, ansible_config) + cleanup_social_user(namespace_v1name, ansible_config) + + # creat the v3 namespace + v3_ns = admin_client( + '/api/_ui/v1/namespaces/', method='POST', args={'name': namespace_v3name, 'groups': []} + ) + assert v3_ns['name'] == namespace_v3name, v3_ns + + # make the legacy ns + v1_ns = admin_client('/api/v1/namespaces/', method='POST', args={'name': namespace_v1name}) + assert v1_ns['name'] == namespace_v1name, v1_ns + + # bind the v3 namespace to the v1 namespace + v3_bind = { + 'id': v3_ns['id'] + } + admin_client(f"/api/v1/namespaces/{v1_ns['id']}/providers/", method='POST', args=v3_bind) + + # import jctanerTEST role1 + pid = ansible_galaxy( + f"role import {github_user} {github_repo} --branch={branch}", + ansible_config=admin_config, + force_token=True, + cleanup=False, + check_retcode=False + ) + assert pid.returncode == 0, pid.stdout.decode('utf-8') + + # find the new role ... + resp = admin_client(f'/api/v1/roles/?owner__username={namespace_v1name}&name={role_name}') + assert resp['count'] == 1 + role = resp['results'][0] + assert role['summary_fields']['namespace']['name'] == namespace_v1name + assert role['summary_fields']['provider_namespace']['name'] == namespace_v3name + assert role['name'] == role_name + assert role['github_user'] == github_user + assert role['github_repo'] == github_repo + assert role['github_branch'] == branch + + role_id = role['id'] + + # change the branch ... + admin_client( + f'/api/v1/roles/{role_id}/', + method='PUT', + args={'github_branch': 'fakebranch'} + ) + newds = admin_client(f'/api/v1/roles/{role_id}/') + + import epdb; epdb.st()