Skip to content
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

Merged
merged 6 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions packagedb/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,17 @@ def filter_by_checksums(self, request, *args, **kwargs):

qs = Package.objects.filter(q)
paginated_qs = self.paginate_queryset(qs)
enhance_package_data = request.query_params.get('enhance_package_data', False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the enhance_package_data flag should be sent along with the data in the post request, rather than using query_params here

Suggested change
enhance_package_data = request.query_params.get('enhance_package_data', False)
enhance_package_data = data.get('enhance_package_data', False)

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)

Expand Down Expand Up @@ -609,16 +620,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
Expand Down
90 changes: 87 additions & 3 deletions packagedb/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,92 @@ def setUp(self):
self.package3 = Package.objects.create(**self.package_data3)
self.package3.refresh_from_db()

self.package_data4= {
Copy link
Member

Choose a reason for hiding this comment

The 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():
Expand Down Expand Up @@ -448,14 +526,20 @@ def test_package_api_filter_by_checksums(self):
'testsha1',
'testsha1-2',
'testsha1-3',
'testsha1-4',
'testsha1-6',
]
data = {
'sha1': sha1s
}
enhanced_response = self.client.post('/api/packages/filter_by_checksums/?enhance_package_data=true', 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=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.check_expected_results(enhanced_response.data['results'], expected, fields_to_remove=["url", "uuid", "resources", "package_sets",], regen=True)
self.check_expected_results(enhanced_response.data['results'], expected, fields_to_remove=["url", "uuid", "resources", "package_sets",], regen=False)

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=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.check_expected_results(response.data['results'], expected, fields_to_remove=["url", "uuid", "resources", "package_sets",], regen=True)
self.check_expected_results(response.data['results'], expected, fields_to_remove=["url", "uuid", "resources", "package_sets",], regen=False)



class PackageApiReindexingTestCase(JsonBasedTesting, TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
[
{
"type":"generic",
"namespace":"generic",
"name":"foo",
"version":"12.34",
"qualifiers":"test_qual=qual",
"subpath":"test_subpath",
"package_content":null,
"primary_language":null,
"description":null,
"release_date":null,
"parties":[],
"keywords":[],
"homepage_url":null,
"download_url":"http://example.com",
"size":101,
"md5":"testmd5",
"sha1":"testsha1",
"sha256":null,
"sha512":null,
"bug_tracking_url":null,
"code_view_url":null,
"vcs_url":null,
"copyright":null,
"holder":null,
"declared_license_expression":null,
"declared_license_expression_spdx":null,
"license_detections":[],
"other_license_expression":null,
"other_license_expression_spdx":null,
"other_license_detections":[],
"extracted_license_statement":null,
"notice_text":null,
"source_packages":[],
"extra_data":{},
"dependencies":[],
"package_uid":"pkg:generic/generic/foo@12.34?test_qual=qual&uuid=fixed-uid-done-for-testing-5642512d1758#test_subpath",
"datasource_id":null,
"purl":"pkg:generic/generic/foo@12.34?test_qual=qual#test_subpath",
"repository_homepage_url":null,
"repository_download_url":null,
"api_data_url":null,
"file_references":[]
},
{
"type":"npm",
"namespace":"example",
"name":"bar",
"version":"56.78",
"qualifiers":"",
"subpath":"",
"package_content":null,
"primary_language":null,
"description":null,
"release_date":null,
"parties":[],
"keywords":[],
"homepage_url":null,
"download_url":"http://somethingelse.org",
"size":100,
"md5":"testmd5-2",
"sha1":"testsha1-2",
"sha256":null,
"sha512":null,
"bug_tracking_url":null,
"code_view_url":null,
"vcs_url":null,
"copyright":null,
"holder":null,
"declared_license_expression":null,
"declared_license_expression_spdx":null,
"license_detections":[],
"other_license_expression":null,
"other_license_expression_spdx":null,
"other_license_detections":[],
"extracted_license_statement":null,
"notice_text":null,
"source_packages":[],
"extra_data":{},
"dependencies":[],
"package_uid":"pkg:npm/example/bar@56.78?uuid=fixed-uid-done-for-testing-5642512d1758",
"datasource_id":null,
"purl":"pkg:npm/example/bar@56.78",
"repository_homepage_url":null,
"repository_download_url":null,
"api_data_url":null,
"file_references":[]
},
{
"type":"jar",
"namespace":"sample",
"name":"baz",
"version":"90.12",
"qualifiers":"",
"subpath":"",
"package_content":null,
"primary_language":null,
"description":null,
"release_date":null,
"parties":[],
"keywords":[],
"homepage_url":null,
"download_url":"http://anotherexample.com",
"size":100,
"md5":"testmd5-3",
"sha1":"testsha1-3",
"sha256":null,
"sha512":null,
"bug_tracking_url":null,
"code_view_url":null,
"vcs_url":null,
"copyright":null,
"holder":null,
"declared_license_expression":null,
"declared_license_expression_spdx":null,
"license_detections":[],
"other_license_expression":null,
"other_license_expression_spdx":null,
"other_license_detections":[],
"extracted_license_statement":null,
"notice_text":null,
"source_packages":[],
"extra_data":{},
"dependencies":[],
"package_uid":"pkg:jar/sample/baz@90.12?uuid=fixed-uid-done-for-testing-5642512d1758",
"datasource_id":null,
"purl":"pkg:jar/sample/baz@90.12",
"repository_homepage_url":null,
"repository_download_url":null,
"api_data_url":null,
"file_references":[]
},
{
"type":"jar",
"namespace":"sample",
"name":"baz",
"version":"90.123",
"qualifiers":"",
"subpath":"",
"package_content":"binary",
"primary_language":null,
"description":null,
"release_date":null,
"parties":[],
"keywords":[],
"homepage_url":null,
"download_url":"http://anothersample.com",
"size":100,
"md5":"testmd5-3",
"sha1":"testsha1-4",
"sha256":null,
"sha512":null,
"bug_tracking_url":null,
"code_view_url":null,
"vcs_url":null,
"copyright":null,
"holder":null,
"declared_license_expression":null,
"declared_license_expression_spdx":null,
"license_detections":[],
"other_license_expression":null,
"other_license_expression_spdx":null,
"other_license_detections":[],
"extracted_license_statement":null,
"notice_text":null,
"source_packages":[],
"extra_data":{},
"dependencies":[],
"package_uid":"pkg:jar/sample/baz@90.123?uuid=fixed-uid-done-for-testing-5642512d1758",
"datasource_id":null,
"purl":"pkg:jar/sample/baz@90.123",
"repository_homepage_url":null,
"repository_download_url":null,
"api_data_url":null,
"file_references":[]
},
{
"type":"maven",
"namespace":"fooo",
"name":"baz",
"version":"90.123",
"qualifiers":"",
"subpath":"",
"package_content":"binary",
"primary_language":null,
"description":null,
"release_date":null,
"parties":[],
"keywords":[],
"homepage_url":null,
"download_url":"http://test-maven-11.com",
"size":100,
"md5":"testmd5-11",
"sha1":"testsha1-6",
"sha256":null,
"sha512":null,
"bug_tracking_url":null,
"code_view_url":null,
"vcs_url":null,
"copyright":"BACC",
"holder":null,
"declared_license_expression":"MIT",
"declared_license_expression_spdx":null,
"license_detections":[],
"other_license_expression":null,
"other_license_expression_spdx":null,
"other_license_detections":[],
"extracted_license_statement":null,
"notice_text":null,
"source_packages":[],
"extra_data":{
"enhanced_by":[
"pkg:github/glue/cat@90.123",
"pkg:maven/foot/baz@90.123?classifier=source"
]
},
"dependencies":[],
"package_uid":"pkg:maven/fooo/baz@90.123?uuid=fixed-uid-done-for-testing-5642512d1758",
"datasource_id":null,
"purl":"pkg:maven/fooo/baz@90.123",
"repository_homepage_url":null,
"repository_download_url":null,
"api_data_url":null,
"file_references":[]
}
]
Loading
Loading