Skip to content

Commit 6e788a5

Browse files
committed
feat: add get_recent_components
1 parent 828df68 commit 6e788a5

3 files changed

Lines changed: 73 additions & 11 deletions

File tree

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
* more REST API endpoints implemented:
1111
* `get_recent_releases`
1212
* `get_recent_components`
13+
* `get_all_moderation_requests`
14+
* `get_moderation_requests_by_state`
15+
* `get_moderation_request`
1316

1417
## V1.6.0
1518

sw360/components.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,10 @@ def get_components_by_type(self, component_type: str) -> List[Dict[str, Any]]:
8484
"""
8585

8686
resp = self.api_get(self.url + "resource/api/components?type=" + component_type)
87-
if not resp:
88-
return []
89-
90-
if "_embedded" not in resp:
91-
return []
92-
93-
if "sw360:components" not in resp["_embedded"]:
94-
return []
87+
if resp and ("_embedded" in resp) and ("sw360:components" in resp["_embedded"]):
88+
return resp["_embedded"]["sw360:components"]
9589

96-
return resp["_embedded"]["sw360:components"]
90+
return []
9791

9892
def get_component(self, component_id: str) -> Optional[Dict[str, Any]]:
9993
"""Get information of about a component
@@ -304,4 +298,7 @@ def get_recent_components(self) -> Optional[Dict[str, Any]]:
304298
"""
305299
url = self.url + "resource/api/components/recentComponents"
306300
resp = self.api_get(url)
301+
if resp and ("_embedded" in resp) and ("sw360:components" in resp["_embedded"]):
302+
return resp["_embedded"]["sw360:components"]
303+
307304
return resp

tests/test_sw360_components.py

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -839,11 +839,73 @@ def xtest_get_component_real(self) -> None:
839839
lib.login_api()
840840
# c = lib.get_component("eaba2f0416e000e8ca5b2ccb4400633e")
841841
# c = lib.get_components_by_external_id( "package-url", "pkg:nuget/Tethys.Logging")
842-
c = lib.api_get("https://sw360.siemens.com/resource/api/components/searchByExternalIds?package-url=pkg:nuget/Tethys.Logging") # noqa
842+
c = lib.api_get("https://my.server.com/resource/api/components/searchByExternalIds?package-url=pkg:nuget/Tethys.Logging") # noqa
843843
print(c)
844844

845+
@responses.activate
846+
def test_get_recent_components(self) -> None:
847+
lib = SW360(self.MYURL, self.MYTOKEN, False)
848+
lib.force_no_session = True
849+
self._add_login_response()
850+
actual = lib.login_api()
851+
self.assertTrue(actual)
852+
853+
responses.add(
854+
method=responses.GET,
855+
url=self.MYURL + "resource/api/components/recentComponents",
856+
body='''{
857+
"_embedded": {
858+
"sw360:components": [
859+
{
860+
"id": "ff6f1b5b212b4f93b306e2cceca4f64d",
861+
"name": "intl-listformat",
862+
"description": "n/a",
863+
"componentType": "OSS",
864+
"visbility": "EVERYONE",
865+
"mainLicenseIds": [],
866+
"_links": {
867+
"self": {
868+
"href": "https://my.server.com/resource/api/components/ff"
869+
}
870+
}
871+
},
872+
{
873+
"id": "f916b35d6c864014bd8823c45615aeab",
874+
"name": "fields-metadata-plugin",
875+
"description": "n/a",
876+
"componentType": "OSS",
877+
"visbility": "EVERYONE",
878+
"mainLicenseIds": [],
879+
"_links": {
880+
"self": {
881+
"href": "https://my.server.com/resource/api/components/f9"
882+
}
883+
}
884+
}
885+
]
886+
},
887+
"_links": {
888+
"curies": [
889+
{
890+
"href": "https://my.server.com/resource/docs/{rel}.html",
891+
"name": "sw360",
892+
"templated": true
893+
}
894+
]
895+
}
896+
}''',
897+
status=200,
898+
content_type="application/json",
899+
adding_headers={"Authorization": "Token " + self.MYTOKEN},
900+
)
901+
902+
components = lib.get_recent_components()
903+
self.assertIsNotNone(components)
904+
self.assertEqual(2, len(components))
905+
self.assertEqual("intl-listformat", components[0]["name"])
906+
self.assertEqual("OSS", components[0]["componentType"])
907+
845908

846909
if __name__ == "__main__":
847-
# unittest.main()
848910
x = Sw360TestComponents()
849911
x.test_get_all_components_with_fields_and_paging()

0 commit comments

Comments
 (0)