Skip to content

Commit

Permalink
Merge pull request #65 from sw360/19-be-more-resilient-when-accessing…
Browse files Browse the repository at this point in the history
…-sw360

feat: be more resilient when accessing SW360
  • Loading branch information
tngraf committed Apr 22, 2024
2 parents 4467ec8 + 20c5da6 commit 5664ba7
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 37 deletions.
14 changes: 7 additions & 7 deletions capycli/project/check_prerequisites.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ class CheckPrerequisites(capycli.common.script_base.ScriptBase):

def get_clearing_state(self, project: Dict[str, Any], href: str) -> str:
"""Returns the clearing state of the given component/release"""
rel = project["linkedReleases"]
rel = project.get("linkedReleases", [])
for key in rel:
if key["release"] == href:
return key["mainlineState"]
return key.get("mainlineState", "")

return ""

Expand All @@ -46,7 +46,7 @@ def get_source_code(self, release: Dict[str, Any]) -> List[Dict[str, Any]]:
att = [
entry
for entry in release["_embedded"]["sw360:attachments"]
if entry["attachmentType"] in ("SOURCE", "SOURCE_SELF")
if entry.get("attachmentType", "") in ("SOURCE", "SOURCE_SELF")
]
return att

Expand Down Expand Up @@ -121,7 +121,7 @@ def check_project_prerequisites(self, id: str, sbom: Optional[Bom]) -> bool:
print_yellow(" No project owner specified!")
has_errors = True
else:
print_green(" Project owner: " + project["projectOwner"])
print_green(" Project owner: " + project.get("projectOwner", "UNKNOWN"))

if not project.get("projectResponsible", None):
print_yellow(" No project responsible specified!")
Expand All @@ -141,7 +141,7 @@ def check_project_prerequisites(self, id: str, sbom: Optional[Bom]) -> bool:
if not project.get("tag", None):
print_yellow(" No tag specified!")
else:
print_green(" Tag: " + project["tag"])
print_green(" Tag: " + project.get("tag", "UNKNOWN"))

if "sw360:projects" in project["_embedded"]:
linked_projects = project["_embedded"]["sw360:projects"]
Expand Down Expand Up @@ -196,14 +196,14 @@ def check_project_prerequisites(self, id: str, sbom: Optional[Bom]) -> bool:

source = self.get_source_code(release)
for source_info in source:
source_name = source_info["filename"]
source_name = source_info.get("filename", "")
if "-SOURCES.JAR" in source_name.upper():
print_yellow(
" Source " +
source_name +
" seems to be from Maven!")
if bom_sha1:
if bom_sha1 != source_info["sha1"]:
if bom_sha1 != source_info.get("sha1", ""):
print_red(
" SHA1 for source " +
source_name +
Expand Down
2 changes: 1 addition & 1 deletion capycli/project/create_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def upload_attachments(self, attachments: List[Dict[str, Any]]) -> None:
filename = os.path.basename(attachment['file'])
upload = True
for project_attachment in project_attachments:
if project_attachment['filename'] == filename:
if project_attachment.get('filename', '') == filename:
print_yellow(
" Attachment file " + filename +
" already exists! Please check manually")
Expand Down
4 changes: 2 additions & 2 deletions capycli/project/create_readme.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,8 @@ def read_cli_files(self, config: Dict[str, Any]) -> List[CliFile]:
"""Reads all CLI files"""
cli_files: List[CliFile] = []
unique_components = []
for file in config["Components"]:
component_name = file["ComponentName"]
for file in config.get("Components", []):
component_name = file.get("ComponentName", "")
if component_name not in unique_components:
unique_components.append(component_name)
else:
Expand Down
14 changes: 7 additions & 7 deletions capycli/project/get_license_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def get_cli_files_for_release(self, release: Dict[str, Any],
attachment = self.client.get_attachment_by_url(att_href)
if not attachment:
continue
if not attachment["attachmentType"] == "COMPONENT_LICENSE_INFO_XML":
if not attachment.get("attachmentType", "") == "COMPONENT_LICENSE_INFO_XML":
continue

release_id = self.client.get_id_from_href(release["_links"]["self"]["href"])
Expand Down Expand Up @@ -151,12 +151,12 @@ def get_project_info(
for cli_file in cli_files:
comp = {}
comp["ComponentName"] = component_name
comp["CliFile"] = cli_file["filename"]
comp["CreatedBy"] = cli_file["createdBy"]
comp["CreatedOn"] = cli_file["createdOn"]
comp["CheckedBy"] = cli_file["checkedBy"]
comp["CheckedTeam"] = cli_file["checkedTeam"]
comp["CheckStatus"] = cli_file["checkStatus"]
comp["CliFile"] = cli_file.get("filename", "")
comp["CreatedBy"] = cli_file.get("createdBy", "")
comp["CreatedOn"] = cli_file.get("createdOn", "")
comp["CheckedBy"] = cli_file.get("checkedBy", "")
comp["CheckedTeam"] = cli_file.get("checkedTeam", "")
comp["CheckStatus"] = cli_file.get("checkStatus", "")

count += 1
if count > 1:
Expand Down
16 changes: 8 additions & 8 deletions capycli/project/show_ecc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -------------------------------------------------------------------------------
# Copyright (c) 2022-2023 Siemens
# Copyright (c) 2022-2024 Siemens
# All Rights Reserved.
# Author: thomas.graf@siemens.com
#
Expand Down Expand Up @@ -29,9 +29,9 @@ def show_project_status(self, result: Dict[str, Any]) -> None:

print_text(" Project name: " + result["Name"] + ", " + result["Version"])
if "ProjectResponsible" in result:
print(" Project responsible: " + result["ProjectResponsible"])
print_text(" Project owner: " + result["ProjectOwner"])
print_text(" Clearing state: " + result["ClearingState"])
print(" Project responsible: " + result.get("ProjectResponsible", "Unknown"))
print_text(" Project owner: " + result.get("ProjectOwner", "Unknown"))
print_text(" Clearing state: " + result.get("ClearingState", "Unknown"))

if len(result["Projects"]) > 0:
print("\n Linked projects: ")
Expand Down Expand Up @@ -92,10 +92,10 @@ def get_project_status(self, project_id: str) -> Dict[str, Any]:

result["Name"] = self.project["name"]
result["Version"] = self.project["version"]
result["ProjectOwner"] = self.project["projectOwner"]
result["ProjectResponsible"] = self.project["projectResponsible"]
result["SecurityResponsibles"] = self.project["securityResponsibles"]
result["BusinessUnit"] = self.project["businessUnit"]
result["ProjectOwner"] = self.project.get("projectOwner", "Unknown")
result["ProjectResponsible"] = self.project.get("projectResponsible", "Unknown")
result["SecurityResponsibles"] = self.project.get("securityResponsibles", [])
result["BusinessUnit"] = self.project.get("businessUnit", "Unknown")
result["Tag"] = self.project["tag"]
if "clearingState" in self.project:
result["ClearingState"] = self.project["clearingState"]
Expand Down
6 changes: 3 additions & 3 deletions capycli/project/show_licenses.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -------------------------------------------------------------------------------
# Copyright (c) 2019-23 Siemens
# Copyright (c) 2019-24 Siemens
# All Rights Reserved.
# Author: thomas.graf@siemens.com
#
Expand Down Expand Up @@ -142,8 +142,8 @@ def show_licenses(self, id: str) -> None:
return

print_text(" Project name: " + project["name"] + ", " + project["version"])
print_text(" Project owner: " + project["projectOwner"])
print_text(" Clearing state: " + project["clearingState"])
print_text(" Project owner: " + project.get("projectOwner", "???"))
print_text(" Clearing state: " + project.get("clearingState", "???"))
if self.nodelete:
print_text(" Temp folder", tempfolder, "will not get deleted.")

Expand Down
16 changes: 8 additions & 8 deletions capycli/project/show_project.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -------------------------------------------------------------------------------
# Copyright (c) 2019-23 Siemens
# Copyright (c) 2019-24 Siemens
# All Rights Reserved.
# Author: thomas.graf@siemens.com
#
Expand Down Expand Up @@ -28,10 +28,10 @@ def get_clearing_state(self, proj: Optional[Dict[str, Any]], href: str) -> str:
if not proj:
return ""

rel = proj["linkedReleases"]
rel = proj.get("linkedReleases", [])
for key in rel:
if key["release"] == href:
return key["mainlineState"]
if key.get("release", "") == href:
return key.get("mainlineState", "???")

return ""

Expand All @@ -42,8 +42,8 @@ def show_project_status(self, result: Dict[str, Any]) -> None:
print_text(" Project name: " + result["Name"] + ", " + result["Version"])
if "ProjectResponsible" in result:
print_text(" Project responsible: " + result["ProjectResponsible"])
print_text(" Project owner: " + result["ProjectOwner"])
print_text(" Clearing state: " + result["ClearingState"])
print_text(" Project owner: " + result.get("ProjectOwner", "???"))
print_text(" Clearing state: " + result.get("ClearingState", "???"))

if len(result["Projects"]) > 0:
print_text("\n Linked projects: ")
Expand Down Expand Up @@ -138,8 +138,8 @@ def get_project_status(self, project_id: str) -> Dict[str, Any]:
sys.exit(ResultCode.RESULT_ERROR_ACCESSING_SW360)

# capycli.common.json_support.print_json(release_details)
rel_item["ClearingState"] = release_details["clearingState"]
rel_item["ReleaseMainlineState"] = release_details.get("mainlineState", "")
rel_item["ClearingState"] = release_details.get("clearingState", "???")
rel_item["ReleaseMainlineState"] = release_details.get("mainlineState", "???")
rel_item["SourceAvailable"] = "False"
if "externalIds" in release_details:
rel_item["ExternalIds"] = release_details["externalIds"]
Expand Down
8 changes: 7 additions & 1 deletion capycli/project/show_vulnerabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,13 @@ def display_project(self, project: Optional[Dict[str, Any]], pid: str = "") -> D

# 2022-07-01: SW360 changed "sw360:vulnerabilityDToes" to "sw360:vulnerabilityDTOes" - arrgghhh
if "sw360:vulnerabilityDTOes" not in vuls["_embedded"]:
return report
if "sw360:vulnerabilityDTes" not in vuls["_embedded"]:
vuls["_embedded"]["sw360:vulnerabilityDTOes"] = vuls["_embedded"]["sw360:vulnerabilityDTes"]
elif "sw360:vulnerabilities" not in vuls["_embedded"]:
vuls["_embedded"]["sw360:vulnerabilityDTOes"] = vuls["_embedded"]["sw360:vulnerabilities"]

if "sw360:vulnerabilityDTOes" not in vuls["_embedded"]:
return report

report["Vulnerabilities"] = vuls["_embedded"]["sw360:vulnerabilityDTOes"]

Expand Down

0 comments on commit 5664ba7

Please sign in to comment.