Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 28 additions & 0 deletions aboutcode/federatedcode/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env python3
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# FederatedCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/federatedcode for support or download.
# See https://aboutcode.org for more information about AboutCode.org OSS projects.
#

import click

from aboutcode.federatedcode.client import get_package_scan


@click.command()
@click.argument("purl")
def handler(purl):
"""
Get package scan for PURL from FederatedCode git repository.

PURL: PURL to fetch scan result
"""
click.echo(get_package_scan(purl=purl))


if __name__ == "__main__":
handler()
13 changes: 8 additions & 5 deletions aboutcode/federatedcode/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

load_dotenv()

FEDERATEDCODE_GITHUB_ACCOUNT_NAME = os.getenv("FEDERATEDCODE_GITHUB_ACCOUNT_NAME")
FEDERATEDCODE_GIT_RAW_URL = os.getenv(
"FEDERATEDCODE_GIT_RAW_URL",
"https://raw.githubusercontent.com/aboutcode-org/",
)


class ScanNotAvailableError(Exception):
Expand All @@ -28,8 +31,8 @@ class ScanNotAvailableError(Exception):
def get_package_scan(purl: Union[PackageURL, str]):
"""Return the package scan result for a PURL from the FederatedCode Git repository."""

if not FEDERATEDCODE_GITHUB_ACCOUNT_NAME:
raise ValueError("Provide ``FEDERATEDCODE_GITHUB_ACCOUNT_NAME`` in .env file.")
if not FEDERATEDCODE_GIT_RAW_URL:
raise ValueError("Provide ``FEDERATEDCODE_GIT_RAW_URL`` in .env file.")

if isinstance(purl, str):
purl = PackageURL.from_string(purl)
Expand All @@ -45,9 +48,9 @@ def get_package_scan(purl: Union[PackageURL, str]):
version = purl.version
file_name = "scancodeio.json"

url_parts = [FEDERATEDCODE_GITHUB_ACCOUNT_NAME, repo_name, package_dir_path, version, file_name]
url_parts = [repo_name, package_dir_path, version, file_name]

file_url = urljoin("https://raw.githubusercontent.com", "/".join(url_parts))
file_url = urljoin(FEDERATEDCODE_GIT_RAW_URL, "/".join(url_parts))

try:
response = requests.get(file_url)
Expand Down
4 changes: 4 additions & 0 deletions pyproject-aboutcode.federatedcode.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ dependencies = [
"packageurl_python >= 0.15.6",
"aboutcode.hashid>=0.1.0",
"python-dotenv>=1.0.1",
"click>=8.1.7",
"requests>=2.32.3",
]

urls = { Homepage = "https://github.com/aboutcode-org/federatedcode" }

[project.scripts]
federatedcode = "aboutcode.federatedcode.cli:handler"

[tool.bumpversion]
current_version = "0.1.0"
Expand Down