Skip to content

Commit

Permalink
Add edit support for some legacyrole fields.
Browse files Browse the repository at this point in the history
No-Issue

Signed-off-by: James Tanner <[email protected]>
  • Loading branch information
jctanner committed Oct 20, 2023
1 parent 739f2ee commit b80dc93
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 5 deletions.
50 changes: 46 additions & 4 deletions galaxy_ng/app/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ class Meta:
'download_count',
]

# def update(self, instance, validated_data):

def get_id(self, obj):
return obj.pulp_id

Expand Down Expand Up @@ -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': {
Expand All @@ -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
}
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion galaxy_ng/app/api/v1/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

path(
'roles/<int:pk>/',
LegacyRolesViewSet.as_view({"get": "retrieve", "delete": "destroy"}),
LegacyRolesViewSet.as_view({"get": "retrieve", "delete": "destroy", "put": "update"}),
name='legacy_role-detail'
),

Expand Down
43 changes: 43 additions & 0 deletions galaxy_ng/app/api/v1/viewsets/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,6 +25,7 @@
from galaxy_ng.app.api.v1.serializers import (
LegacyImportSerializer,
LegacyRoleSerializer,
LegacyRoleUpdateSerializer,
LegacyRoleContentSerializer,
LegacyRoleVersionsSerializer,
)
Expand Down Expand Up @@ -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()
Expand Down
88 changes: 88 additions & 0 deletions galaxy_ng/tests/integration/community/test_role_edits.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit b80dc93

Please sign in to comment.