Skip to content

Commit

Permalink
Add support to find source repo for a package
Browse files Browse the repository at this point in the history
Signed-off-by: Tushar Goel <[email protected]>
  • Loading branch information
TG1999 committed Jul 26, 2023
1 parent 9e281d2 commit 8e40335
Show file tree
Hide file tree
Showing 12 changed files with 5,078 additions and 28 deletions.
26 changes: 26 additions & 0 deletions minecode/tests/test_maven.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import re

from mock import patch
from unittest import mock

from django.test import TestCase as DjangoTestCase

Expand Down Expand Up @@ -829,3 +830,28 @@ def test_merge_ancestors(self, regen=False):
maven_visitor.merge_ancestors(ancestor_pom_texts, package)
expected_after_loc = self.get_test_loc('maven/pom/pulsar-client-1x-2.5.1-package_after.json')
self.check_expected_results(package.to_dict(), expected_after_loc, regen=regen)

@mock.patch("minecode.visitors.maven.get_pom_text")
def test_get_merged_ancestor_package_from_maven_package(self, get_pom_text_mock, regen=False):
get_pom_text_mock.return_value = ""
ancestor_pom_texts = []
with patch("minecode.visitors.maven.get_ancestry") as mock_get_ancestry:
for loc in [
self.get_test_loc('maven/pom/apache-18.pom'),
self.get_test_loc('maven/pom/pulsar-2.5.1.pom'),
self.get_test_loc('maven/pom/pulsar-client-1x-base-2.5.1.pom')
]:
with open(loc) as f:
pom_text = f.read()
ancestor_pom_texts.append(pom_text)
mock_get_ancestry.return_value = ancestor_pom_texts
db_package = packagedb.models.Package.objects.create(
name="pulsar-client",
namespace="org.apache.pulsar",
version="2.5.1",
type="maven",
download_url="https://repo1.maven.org/maven2/org/apache/pulsar/pulsar-client/2.5.1/pulsar-client-2.5.1.jar",
)
merged_package = maven_visitor.get_merged_ancestor_package_from_maven_package(package=db_package)
expected_loc = self.get_test_loc('maven/pom/pulsar-client-merged-ancestor-package.json')
self.check_expected_results(merged_package.to_dict(), expected_loc, regen=regen)
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"type":"maven",
"namespace":"org.apache.pulsar",
"name":"pulsar-client",
"version":"2.5.1",
"qualifiers":"",
"subpath":"",
"package_sets":[],
"package_content":null,
"primary_language":null,
"description":null,
"release_date":null,
"parties":[],
"keywords":[],
"homepage_url":"https://www.apache.org/",
"download_url":"https://repo1.maven.org/maven2/org/apache/pulsar/pulsar-client/2.5.1/pulsar-client-2.5.1.jar",
"size":null,
"md5":null,
"sha1":null,
"sha256":null,
"sha512":null,
"bug_tracking_url":null,
"code_view_url":null,
"vcs_url":null,
"copyright":null,
"holder":null,
"declared_license_expression":"apache-2.0",
"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":{
"history":[
"Field `declared_license_expression` has been updated using values obtained from the parent POM pkg:maven/org.apache/apache@18",
"Field `homepage_url` has been updated using values obtained from the parent POM pkg:maven/org.apache/apache@18"
]
},
"dependencies":[],
"package_uid":"pkg:maven/org.apache.pulsar/[email protected]?uuid=fixed-uid-done-for-testing-5642512d1758",
"datasource_id":null,
"purl":"pkg:maven/org.apache.pulsar/[email protected]",
"repository_homepage_url":null,
"repository_download_url":null,
"api_data_url":null,
"file_references":[]
}
25 changes: 25 additions & 0 deletions minecode/visitors/maven.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io
import json
import logging
from typing import Dict

import arrow
import requests
Expand Down Expand Up @@ -109,6 +110,8 @@ def get_pom_text(namespace, name, version, qualifiers={}):
field arguments in a string.
"""
# Create URLs using purl fields
if qualifiers and not isinstance(qualifiers, Dict):
return
urls = get_urls(
namespace=namespace,
name=name,
Expand Down Expand Up @@ -152,6 +155,8 @@ def fetch_parent(pom_text):
"""
Return the parent pom text of `pom_text`, or None if `pom_text` has no parent.
"""
if not pom_text:
return
pom = get_maven_pom(text=pom_text)
if (
pom.parent
Expand Down Expand Up @@ -188,6 +193,26 @@ def get_ancestry(pom_text):
return reversed(ancestors)


def get_merged_ancestor_package_from_maven_package(package):
"""
Merge package details of a package with its ancestor pom
and return the merged package.
"""
if not package:
return
pom_text = get_pom_text(
name=package.name,
namespace=package.namespace,
version=package.version,
qualifiers=package.qualifiers,
)
merged_package = merge_ancestors(
ancestor_pom_texts=get_ancestry(pom_text),
package=package,
)
return merged_package


def merge_parent(package, parent_package):
"""
Merge `parent_package` data into `package` and return `package.
Expand Down
Loading

0 comments on commit 8e40335

Please sign in to comment.