Skip to content

Commit d4b3424

Browse files
committed
Add FederatedCode client to fetch package scan
Signed-off-by: Keshav Priyadarshi <[email protected]>
1 parent 3f1d369 commit d4b3424

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# FederatedCode is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+
# See https://github.com/nexB/federatedcode for support or download.
7+
# See https://aboutcode.org for more information about AboutCode.org OSS projects.
8+
#
9+
10+
import os
11+
from typing import Union
12+
from urllib.parse import urljoin
13+
14+
import requests
15+
from aboutcode.hashid import get_package_base_dir
16+
from dotenv import load_dotenv
17+
from packageurl import PackageURL
18+
19+
load_dotenv()
20+
21+
FEDERATEDCODE_GITHUB_ACCOUNT_NAME = os.getenv("FEDERATEDCODE_GITHUB_ACCOUNT_NAME")
22+
23+
24+
class ScanNotAvailableError(Exception):
25+
pass
26+
27+
28+
def get_package_scan(purl: Union[PackageURL, str]):
29+
"""Return the package scan result for a PURL from the FederatedCode Git repository."""
30+
31+
if not FEDERATEDCODE_GITHUB_ACCOUNT_NAME:
32+
raise ValueError("Provide ``FEDERATEDCODE_GITHUB_ACCOUNT_NAME`` in .env file.")
33+
34+
if isinstance(purl, str):
35+
purl = PackageURL.from_string(purl)
36+
37+
if not purl.version:
38+
raise ValueError("Missing version in PURL.")
39+
40+
package_path = get_package_base_dir(purl=purl)
41+
package_path_parts = package_path.parts
42+
43+
repo_name = f"{package_path_parts[0]}/refs/heads/main"
44+
package_dir_path = "/".join(package_path_parts[1:])
45+
version = purl.version
46+
file_name = "scancodeio.json"
47+
48+
url_parts = [FEDERATEDCODE_GITHUB_ACCOUNT_NAME, repo_name, package_dir_path, version, file_name]
49+
50+
file_url = urljoin("https://raw.githubusercontent.com", "/".join(url_parts))
51+
52+
try:
53+
response = requests.get(file_url)
54+
response.raise_for_status()
55+
return response.json()
56+
except requests.exceptions.HTTPError as err:
57+
if response.status_code == 404:
58+
raise ScanNotAvailableError(f"No scan available for {purl!s}")
59+
raise err

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ mypy-extensions==1.0.0
5151
nh3==0.2.15
5252
oauthlib==3.2.2
5353
openpyxl==3.1.2
54-
packageurl-python==0.11.1
54+
packageurl-python==0.15.6
5555
packaging==23.1
5656
pathspec==0.11.2
5757
Pillow==9.5.0

setup.cfg

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ install_requires =
8282
jwcrypto>=1.5.0
8383
mypy-extensions>=1.0.0
8484
oauthlib>=3.2.2
85-
packageurl-python>=0.11.1
85+
packageurl-python>=0.15.6
8686
packaging>=23.1
8787
pathspec>=0.11.2
8888
Pillow>=9.5.0
@@ -105,6 +105,17 @@ install_requires =
105105
unidiff>=0.7.5
106106
urllib3>=2.0.3
107107
wrapt>=1.15.0
108+
109+
# Schema
110+
django-ninja>=1.2.1
111+
pydantic>=2.8.2
112+
113+
# Pipeline
114+
aboutcode.pipeline>=0.1.0
115+
116+
# aboutcode.federatedcode.client
117+
aboutcode.hashid>=0.1.0
118+
python-dotenv>=1.0.1
108119

109120

110121
[options.extras_require]

0 commit comments

Comments
 (0)