Skip to content

Commit 5fa52a6

Browse files
committed
feat: new method dulicate_project to create a copy of an exisiting project
1 parent e05fc17 commit 5fa52a6

5 files changed

Lines changed: 91 additions & 2 deletions

File tree

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# SW360 Base Library for Python
22

3+
## 1.1.0
4+
* New method `dulicate_project` to create a copy of an exisiting project.
5+
36
## 1.0.0
47
* **New Features**:
58
* `get_projects_by_tag` added.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "sw360"
3-
version = "1.0.0"
3+
version = "1.1.0"
44
description = "Python interface to the SW360 software component catalogue"
55
authors = ["Thomas Graf <thomas.graf@siemens.com>",
66
"Gernot Hillier <gernot.hillier@siemens.com>"]

sw360/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
# SPDX-License-Identifier: MIT
88
# -------------------------------------------------------------------------------
99

10-
__version__ = (1, 0, 0)
10+
__version__ = (1, 1, 0)
1111

1212
from .sw360_api import SW360, SW360Error # noqa: F401

sw360/sw360_api.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,38 @@ def get_users_of_project(self, project_id):
576576
resp = self.api_get(self.url + "resource/api/projects/usedBy/" + project_id)
577577
return resp
578578

579+
def duplicate_project(self, project_id: str, new_version: str):
580+
"""Create a copy of an exisiting project.
581+
582+
API endpoint: GET /projects/duplicate/{id}
583+
584+
:param project_id: the id of the exisiting project
585+
:type project_id: string
586+
:param new_version: the version of the new project
587+
:type new_version: string
588+
:return: the newly created project
589+
:rtype: JSON object
590+
:raises SW360Error: if there is a negative HTTP response
591+
"""
592+
593+
if not project_id:
594+
raise SW360Error(message="No project id provided!")
595+
596+
project_details = {}
597+
project_details["version"] = new_version
598+
# force clearing state to OPEN
599+
project_details["clearingState"] = "OPEN"
600+
601+
url = self.url + "resource/api/projects/duplicate/" + project_id
602+
response = requests.post(
603+
url, json=project_details, headers=self.api_headers
604+
)
605+
606+
if response.ok:
607+
return response.json()
608+
609+
raise SW360Error(response, url)
610+
579611
# ----- Releases ---------------------------------------------------------
580612

581613
def get_release(self, release_id):

tests/test_sw360_projects.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,60 @@ def test_get_users_of_project(self):
768768

769769
lib.get_users_of_project("123")
770770

771+
@responses.activate
772+
def test_duplicate_project(self):
773+
lib = self.get_logged_in_lib()
774+
responses.add(
775+
responses.POST,
776+
url=self.MYURL + "resource/api/projects/duplicate/007",
777+
json={
778+
# server returns complete project, here we only mock a part of it
779+
'name': 'ExistingProduct',
780+
'version': '42',
781+
'clearingState': 'OPEN',
782+
'_links': {
783+
'self': {
784+
'href': self.MYURL+'resource/api/projects/0206'
785+
}
786+
},
787+
},
788+
match=[
789+
responses.json_params_matcher({
790+
"version": "42",
791+
'clearingState': 'OPEN',
792+
})
793+
]
794+
)
795+
result = lib.duplicate_project("007", "42")
796+
self.assertIsNotNone(result)
797+
self.assertTrue("clearingState" in result)
798+
self.assertEqual("OPEN", result["clearingState"])
799+
self.assertTrue("version" in result)
800+
self.assertEqual("42", result["version"])
801+
802+
@responses.activate
803+
def test_duplicate_project_no_id(self):
804+
lib = self.get_logged_in_lib()
805+
806+
with self.assertRaises(SW360Error) as context:
807+
lib.duplicate_project(None, "42")
808+
809+
self.assertEqual("No project id provided!", context.exception.message)
810+
811+
@responses.activate
812+
def test_duplicate_project_failed(self):
813+
lib = self.get_logged_in_lib()
814+
815+
responses.add(
816+
responses.POST,
817+
url=self.MYURL + "resource/api/projects/duplicate/123",
818+
body="4",
819+
status=404,
820+
)
821+
822+
with self.assertRaises(SW360Error) as context:
823+
lib.duplicate_project("123", "42")
824+
771825

772826
if __name__ == "__main__":
773827
unittest.main()

0 commit comments

Comments
 (0)