Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug triggering #117, leading to loosing components during "bom map" #119

Merged
merged 2 commits into from
Jan 24, 2025
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
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
to SPECIFIC of to the value given by `-pms`. Now **existing** Project Mainline States are kept.
* `project create` has a new parameter `--copy_from` which allows to first create a copy of the given
project and then update the releases based on the contents of the given SBOM.
* fix for `bom map` loosing SBOM items when it tries to map to invalid SW360 releases

## 2.6.0

Expand Down
16 changes: 12 additions & 4 deletions capycli/bom/map_bom.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,13 @@ def map_bom_item_no_cache(self, component: Component) -> MapResult:

# Sorted alternatives in descending version order
# Please note: the release list sometimes contain just the href but no version
rel_list = sorted(rel_list,
key=lambda x: "version" in x and ComparableVersion(
x.get("version", "")), reverse=True) # type: ignore
try:
rel_list = sorted(rel_list,
key=lambda x: "version" in x and ComparableVersion(
x.get("version", "")), reverse=True) # type: ignore
except ValueError:
pass # we can live with an unsorted list

for relref in rel_list:
href = relref["_links"]["self"]["href"]
real_release = self.client.get_release_by_url(href)
Expand Down Expand Up @@ -779,7 +783,11 @@ def create_updated_bom(self, old_bom: Bom, result: List[MapResult]) -> Bom:
newbom.components.add(newitem)

# Sorted alternatives in descending version order
item.releases = sorted(item.releases, key=lambda x: ComparableVersion(x['Version']), reverse=True)
try:
item.releases = sorted(item.releases, key=lambda x: ComparableVersion(x['Version']), reverse=True)
except ValueError:
pass # we can live with an unsorted list

for match_item in item.releases:
if self.is_good_match(match_item["MapResult"]):
newitem = self.update_bom_item(item.component, match_item)
Expand Down
1 change: 1 addition & 0 deletions capycli/common/comparable_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def __init__(self, version: str) -> None:
self.parts = self.parse(version)
except Exception:
LOG.warning("Unable to parse version %s", version)
raise # pass on to caller as object is useless without self.parts

@staticmethod
def parse(version: str) -> List[Tuple[bool, int | str]]:
Expand Down
34 changes: 33 additions & 1 deletion tests/test_bom_map2.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def test_map_bom_item_nocache_mixed_match(self) -> None:

res = self.app.map_bom_item_no_cache(bomitem)
assert res.result == MapResult.FULL_MATCH_BY_NAME_AND_VERSION
# TODO see #25: assert len(res.releases) == 1
# TODO see #118: assert len(res.releases) == 1

component_matches = {"_embedded": {"sw360:components": [
{"name": "Mail",
Expand All @@ -191,6 +191,38 @@ def test_map_bom_item_nocache_mixed_match(self) -> None:
assert res.result == MapResult.FULL_MATCH_BY_NAME_AND_VERSION
assert len(res.releases) == 1

@responses.activate
def test_map_bom_item_nocache_invalid_version(self) -> None:
bomitem = Component(
name="mail",
version="1.4")
component_matches = {"_embedded": {"sw360:components": [
{"name": "mail",
"_links": {"self": {"href": SW360_BASE_URL + 'components/b001'}}}]}}
component_data1 = {"_embedded": {"sw360:releases": [
{"version": "1.4",
"_links": {"self": {"href": SW360_BASE_URL + 'releases/1111'}}},
{"version": "1.0._ME-2",
"_links": {"self": {"href": SW360_BASE_URL + 'releases/1112'}}}]}}
release_data1 = {"name": "mail", "version": "1.4", "_links": {
"self": {"href": SW360_BASE_URL + 'releases/1111'},
"sw360:component": {"href": SW360_BASE_URL + "components/b001"}}}
release_data2 = {"name": "Mail", "version": "1.0._ME-2", "_links": {
"self": {"href": SW360_BASE_URL + 'releases/1112'},
"sw360:component": {"href": SW360_BASE_URL + "components/b002"}}}
responses.add(responses.GET, SW360_BASE_URL + 'components?name=mail',
json=component_matches)
responses.add(responses.GET, SW360_BASE_URL + 'components/b001',
json=component_data1)
responses.add(responses.GET, SW360_BASE_URL + 'releases/1111',
json=release_data1)
responses.add(responses.GET, SW360_BASE_URL + 'releases/1112',
json=release_data2)

res = self.app.map_bom_item_no_cache(bomitem)
assert res.result == MapResult.FULL_MATCH_BY_NAME_AND_VERSION
assert len(res.releases) == 1

# ----------------- map_bom_item_no_cache purl cases --------------------

@responses.activate
Expand Down
Loading