Skip to content

Commit 96b9334

Browse files
committed
Code enhancement based on the PR review comments #1763
Signed-off-by: Chin Yeung Li <tli@nexb.com>
1 parent f3401de commit 96b9334

3 files changed

Lines changed: 45 additions & 56 deletions

File tree

scanpipe/pipelines/scan_maven_package.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131

3232
class ScanMavenPackage(ScanSinglePackage, DeployToDevelop):
3333
"""
34-
Scan a single Maven package and run a D2D scan.
34+
Scan a single Maven package and perform a deployment to development
35+
relation scan.
3536
3637
This pipeline takes a Maven PURL as input, fetches the binary and
3738
source archives (if they exist), and then performs scans for package
@@ -92,8 +93,8 @@ def extract_input(self):
9293
self.extract_inputs_to_codebase_directory()
9394

9495
def maven_d2d_steps(self):
96+
"""Run D2D steps for Maven projects."""
9597
if self.d2d_enabled:
96-
"""Run D2D steps for Maven projects."""
9798
self.collect_and_create_codebase_resources()
9899
self.fingerprint_codebase_directories()
99100
self.flag_empty_files()
@@ -163,16 +164,13 @@ def fetch_and_scan_remote_pom(self):
163164
scanning_errors = fetch_and_scan_remote_pom(
164165
self.purl, self.scan_output_location
165166
)
166-
if scanning_errors:
167-
for scanning_error in scanning_errors:
168-
for resource_path, errors in scanning_error.items():
169-
self.project.add_error(
170-
description="\n".join(errors),
171-
model=self.pipeline_name,
172-
details={
173-
"resource_path": resource_path.removeprefix("codebase/")
174-
},
175-
)
167+
for scanning_error in scanning_errors:
168+
for resource_path, errors in scanning_error.items():
169+
self.project.add_error(
170+
description="\n".join(errors),
171+
model=self.pipeline_name,
172+
details={"resource_path": resource_path.removeprefix("codebase/")},
173+
)
176174

177175
def update_package_license_from_resource_if_missing(self):
178176
"""Update PACKAGE license from the license detected in RESOURCES if missing."""

scanpipe/pipes/maven.py

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,26 @@ def check_input_and_return_purl(project):
6363

6464

6565
def fetch_inputs(purl):
66-
# Fetch the bianry and source for the given input purl and return the
67-
# location of the fetched tarballs
66+
"""Fetch the binary and source for the given input purl"""
6867
purl_str = PackageURL.to_string(purl)
69-
purl_bin = purl_str
70-
purl_src = f"{purl_str}?classifier=sources"
7168

72-
try:
73-
package = fetch.fetch_url(url=purl_bin)
74-
purl_bin_path = [package.path]
75-
except Exception as e:
76-
logger.info("Failed to fetch binary package: %s", e)
77-
purl_bin_path = []
78-
79-
try:
80-
package_src = fetch.fetch_url(url=purl_src)
81-
purl_src_path = [package_src.path]
82-
except Exception as e:
83-
logger.info("Failed to fetch source package: %s", e)
84-
purl_src_path = []
69+
purl_bin_path = fetch_path(purl_str, "binary")
70+
purl_src_path = fetch_path(f"{purl_str}?classifier=sources", "source")
8571

8672
if not purl_bin_path and not purl_src_path:
8773
err_msg = f"No source or binary could be resolved for {purl}."
8874
raise ValueError(err_msg)
8975

90-
return purl_src_path, purl_bin_path
76+
return [purl_src_path], [purl_bin_path]
77+
78+
79+
def fetch_path(url, package_type):
80+
"""Fetch the url and return the location of the fetched tarball"""
81+
try:
82+
return fetch.fetch_url(url=url).path
83+
except Exception as e:
84+
logger.info("Failed to fetch %s package: %s", package_type, e)
85+
return None
9186

9287

9388
def fetch_and_scan_remote_pom(input_purl, scan_output_location):
@@ -97,7 +92,7 @@ def fetch_and_scan_remote_pom(input_purl, scan_output_location):
9792
# Return and do nothing if data has pom.xml
9893
for file in data["files"]:
9994
if "pom.xml" in file["path"]:
100-
return
95+
return []
10196
packages = data.get("packages", [])
10297

10398
pom_url = get_pom_url(input_purl)
@@ -141,33 +136,31 @@ def get_pom_url(input_purl):
141136

142137
def download_pom_file(pom_url):
143138
"""Fetch the pom file from the input pom_url"""
144-
pom_file_dict = {}
145-
139+
# PO: Could we use fetchcode to fetch instead? Yes, we could, but
140+
# the issue is do we want to. Following is the code if we switch to
141+
# fetchcode which seems making things complicated, OR we can move
142+
# the "fetch_http" to fetchcode and use it.
143+
"""
144+
import os
145+
import fetchcode
146+
downloaded_pom = fetchcode.fetch(pom_url)
147+
location = str(downloaded_pom.location)
148+
path = location + ".pom"
149+
# The fetch function from fetchcode save the file as /tmp/name
150+
# without an extension. We need to add the ".pom" extension so that
151+
# the package scan can work properly for this file.
152+
os.rename(location, path)
153+
"""
146154
try:
147-
# PO: Could we use fetchcode to fetch instead? Yes, we could, but
148-
# the issue is do we want to. Following is the code if we switch to
149-
# fetchcode which seems making things complicated, OR we can move
150-
# the "fetch_http" to fetchcode and use it.
151-
"""
152-
import os
153-
import fetchcode
154-
downloaded_pom = fetchcode.fetch(pom_url)
155-
location = str(downloaded_pom.location)
156-
path = location + ".pom"
157-
# The fetch function from fetchcode save the file as /tmp/name
158-
# without an extension. We need to add the ".pom" extension so that
159-
# the package scan can work properly for this file.
160-
os.rename(location, path)
161-
"""
162155
downloaded_pom = fetch.fetch_http(pom_url)
163-
path = str(downloaded_pom.path)
164-
165-
pom_file_dict["pom_file_path"] = path
166-
pom_file_dict["output_path"] = path + "-output.json"
167-
pom_file_dict["pom_url"] = pom_url
168156
except requests.RequestException:
169157
# Return an empty dictionary
170-
pass
158+
return {}
159+
path = str(downloaded_pom.path)
160+
pom_file_dict = {}
161+
pom_file_dict["pom_file_path"] = path
162+
pom_file_dict["output_path"] = path + "-output.json"
163+
pom_file_dict["pom_url"] = pom_url
171164
return pom_file_dict
172165

173166

scanpipe/views.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
from contextlib import suppress
2929

3030
from django.apps import apps
31-
from django.conf import settings
3231
from django.contrib import messages
3332
from django.contrib.auth.mixins import LoginRequiredMixin
3433
from django.core.exceptions import ObjectDoesNotExist
@@ -2352,7 +2351,6 @@ class DiscoveredPackageDetailsView(
23522351
],
23532352
"icon_class": "fa-solid fa-bug",
23542353
"template": "scanpipe/tabset/tab_vulnerabilities.html",
2355-
"tab_context": {"VULNERABLECODE_URL": settings.VULNERABLECODE_URL},
23562354
},
23572355
"extra_data": {
23582356
"fields": ["extra_data"],

0 commit comments

Comments
 (0)