-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option to get enhanced package data in API #157
Changes from 5 commits
0937a62
b34edf8
d96a719
d510cd7
4815a74
5cc124c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -503,7 +503,7 @@ def filter_by_checksums(self, request, *args, **kwargs): | |||
data = dict(request.data) | ||||
unsupported_fields = [] | ||||
for field, value in data.items(): | ||||
if field not in ('md5', 'sha1', 'sha256', 'sha512'): | ||||
if field not in ('md5', 'sha1', 'sha256', 'sha512', 'enhance_package_data'): | ||||
unsupported_fields.append(field) | ||||
|
||||
if unsupported_fields: | ||||
|
@@ -517,11 +517,24 @@ def filter_by_checksums(self, request, *args, **kwargs): | |||
for field, value in data.items(): | ||||
# We create this intermediate dictionary so we can modify the field | ||||
# name to have __in at the end | ||||
if field in ["enhance_package_data"]: | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
continue | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
d = {f'{field}__in': value} | ||||
q |= Q(**d) | ||||
|
||||
qs = Package.objects.filter(q) | ||||
paginated_qs = self.paginate_queryset(qs) | ||||
enhance_package_data = data.get("enhance_package_data", False) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this, since we're going to pop
Suggested change
|
||||
if enhance_package_data: | ||||
package_data = [] | ||||
for package in qs: | ||||
enhanced_package = get_enhanced_package(package=package) | ||||
if enhanced_package: | ||||
package_data.append(enhanced_package) | ||||
else: | ||||
package_data.append(package.to_dict()) | ||||
paginated_response = self.get_paginated_response(package_data) | ||||
return paginated_response | ||||
serializer = PackageAPISerializer(paginated_qs, many=True, context={'request': request}) | ||||
return self.get_paginated_response(serializer.data) | ||||
|
||||
|
@@ -609,16 +622,9 @@ def _get_enhanced_package(package, packages): | |||
Return a mapping of package data based on `package` and Packages in | ||||
`packages`. | ||||
""" | ||||
mixing = False | ||||
package_data = {} | ||||
package_data = package.to_dict() | ||||
for peer in packages: | ||||
if peer == package: | ||||
mixing = True | ||||
package_data = package.to_dict() | ||||
continue | ||||
if not mixing: | ||||
continue | ||||
if peer.package_content == package.package_content: | ||||
if peer.package_content >= package.package_content: | ||||
# We do not want to mix data with peers of the same package content | ||||
continue | ||||
enhanced = False | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -265,14 +265,92 @@ def setUp(self): | |
self.package3 = Package.objects.create(**self.package_data3) | ||
self.package3.refresh_from_db() | ||
|
||
self.package_data4= { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you should add two new maven packages, one of them a binary package, the other a source_archive package. These packages should have the same purl fields (type, namespace, name, version) with the qualifiers being different. On the binary package, leave out license information, and on the source archive package, have license information. We want to see the enhanced package data returned for the binary package in the test results when we enable the option. |
||
'type': 'jar', | ||
'namespace': 'sample', | ||
'name': 'Baz', | ||
'version': '90.123', | ||
'qualifiers': '', | ||
'subpath': '', | ||
'download_url': 'http://anothersample.com', | ||
'filename': 'Baz.zip', | ||
'sha1': 'testsha1-4', | ||
'md5': 'testmd5-3', | ||
'size': 100, | ||
'package_content': PackageContentType.BINARY, | ||
} | ||
self.package4 = Package.objects.create(**self.package_data4) | ||
self.package4.refresh_from_db() | ||
|
||
self.package_data5= { | ||
'type': 'maven', | ||
'namespace': 'foot', | ||
'name': 'baz', | ||
'version': '90.123', | ||
'qualifiers': 'classifier=source', | ||
'subpath': '', | ||
'download_url': 'http://test-maven.com', | ||
'filename': 'Baz.zip', | ||
'sha1': 'testsha1-5', | ||
'md5': 'testmd5-11', | ||
'size': 100, | ||
'package_content': PackageContentType.SOURCE_ARCHIVE, | ||
'declared_license_expression': 'MIT', | ||
} | ||
|
||
self.package5 = Package.objects.create(**self.package_data5) | ||
self.package5.refresh_from_db() | ||
|
||
self.package_data6= { | ||
'type': 'maven', | ||
'namespace': 'fooo', | ||
'name': 'baz', | ||
'version': '90.123', | ||
'qualifiers': '', | ||
'subpath': '', | ||
'download_url': 'http://test-maven-11.com', | ||
'filename': 'Baz.zip', | ||
'sha1': 'testsha1-6', | ||
'md5': 'testmd5-11', | ||
'size': 100, | ||
'package_content': PackageContentType.BINARY, | ||
} | ||
|
||
self.package6 = Package.objects.create(**self.package_data6) | ||
self.package6.refresh_from_db() | ||
|
||
self.package_data7= { | ||
'type': 'github', | ||
'namespace': 'glue', | ||
'name': 'cat', | ||
'version': '90.123', | ||
'qualifiers': '', | ||
'subpath': '', | ||
'download_url': 'http://test-maven-111.com', | ||
'filename': 'Baz.zip', | ||
'sha1': 'testsha1-7', | ||
'md5': 'testmd5-11', | ||
'size': 100, | ||
'copyright': 'BACC', | ||
'package_content': PackageContentType.SOURCE_REPO, | ||
} | ||
|
||
self.package7 = Package.objects.create(**self.package_data7) | ||
self.package7.refresh_from_db() | ||
|
||
self.packageset_1 = PackageSet.objects.create() | ||
self.packageset_1.packages.add(self.package6) | ||
self.packageset_1.packages.add(self.package5) | ||
self.packageset_1.packages.add(self.package7) | ||
|
||
self.test_url = 'http://testserver/api/packages/{}/' | ||
|
||
self.client = APIClient() | ||
|
||
def test_package_api_list_endpoint(self): | ||
response = self.client.get('/api/packages/') | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(3, response.data.get('count')) | ||
self.assertEqual(7, response.data.get('count')) | ||
|
||
def test_package_api_list_endpoint_filter(self): | ||
for key, value in self.package_data.items(): | ||
|
@@ -448,14 +526,21 @@ def test_package_api_filter_by_checksums(self): | |
'testsha1', | ||
'testsha1-2', | ||
'testsha1-3', | ||
'testsha1-4', | ||
'testsha1-6', | ||
] | ||
data = { | ||
'sha1': sha1s | ||
'sha1': sha1s, | ||
} | ||
response = self.client.post('/api/packages/filter_by_checksums/', data=data) | ||
self.assertEqual(3, response.data['count']) | ||
self.assertEqual(5, response.data['count']) | ||
expected = self.get_test_loc('api/package-filter_by_checksums-expected.json') | ||
self.check_expected_results(response.data['results'], expected, fields_to_remove=["url", "uuid", "resources"], regen=False) | ||
self.check_expected_results(response.data['results'], expected, fields_to_remove=["url", "uuid", "resources", "package_sets",], regen=False) | ||
data["enhance_package_data"] = True | ||
enhanced_response = self.client.post('/api/packages/filter_by_checksums/', data=data) | ||
self.assertEqual(5, len(enhanced_response.data['results'])) | ||
expected = self.get_test_loc('api/package-filter_by_checksums-enhanced-package-data-expected.json') | ||
self.check_expected_results(enhanced_response.data['results'], expected, fields_to_remove=["url", "uuid", "resources", "package_sets",], regen=False) | ||
|
||
|
||
class PackageApiReindexingTestCase(JsonBasedTesting, TestCase): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to pop
enhance_package_data
fromdata
before iterating through it, then useenhance_package_data
afterwards.