Skip to content

Commit 23dd884

Browse files
committed
implement optional deletion when committing
1 parent d0a93c5 commit 23dd884

2 files changed

Lines changed: 76 additions & 9 deletions

File tree

src/sysml_api/api_lib.py

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,75 @@ def get_project_by_name(client: SysMLV2Client, name: str):
3838
print(f"Error getting projects: {e}")
3939
return None, None
4040

41-
def commit_to_project(client: SysMLV2Client, proj_id:str, payload:str, commit_msg:str = "default commit message"):
42-
commit1_data = {
43-
"@type": "Commit",
44-
"description": commit_msg,
45-
"change": payload
46-
}
41+
def delete_project_data (client: SysMLV2Client, proj_id:str, branch_id:str = None):
42+
# Get the latest commit
43+
commits = client.list_commits(proj_id)
44+
if not commits:
45+
return
46+
latest = commits[0] if isinstance(commits, list) else commits
47+
commit_id = latest.get("@id") or latest.get("id")
48+
if not commit_id:
49+
return
50+
51+
# Fetch elements for that commit
52+
elements = client.list_elements(proj_id, commit_id)
53+
print(f"_delete_project_data: Found {len(elements)} elements to delete")
54+
55+
if not elements:
56+
return
57+
58+
# include elements in the change attribute which have an identity but no payload
59+
# {
60+
# "@type": "Commit",
61+
# "description": "Commit 1: Create initial elements",
62+
# "change": [
63+
# {
64+
# "identity": {
65+
# "@id": "4b0d767c-318f-4160-8a5f-2a76a3c5a65a"
66+
# }
67+
# }
68+
change_payload = [
69+
{
70+
"identity": {
71+
"@id": elem["@id"]
72+
}
73+
}
74+
for elem in elements
75+
if "@id" in elem
76+
]
77+
commit = {"@type": "Commit", "description": f"delete all elements", "change": change_payload}
78+
print (commit)
79+
80+
try:
81+
resp = client.create_commit(proj_id, commit, branch_id=branch_id)
82+
commit1_id = resp.get('@id')
83+
if not commit1_id:
84+
print("\n*** WARNING: Could not extract commit ID ('@id') from response! ***")
85+
return None, None
86+
else:
87+
return resp, commit1_id
88+
except SysMLV2Error as e:
89+
print(f"Error creating commit 1: {e}")
90+
return None, None
91+
92+
def commit_to_project(client: SysMLV2Client, proj_id:str, payload:str, commit_msg:str = "default commit message", user_commit_data=None, delete_project_data:bool = False, branch_id=None):
93+
if user_commit_data is None:
94+
commit1_data = {
95+
"@type": "Commit",
96+
"description": commit_msg,
97+
"change": payload
98+
}
99+
else:
100+
commit1_data = user_commit_data
101+
102+
if delete_project_data:
103+
resp, commit_del_id = delete_project_data (client, proj_id, branch_id=branch_id)
104+
if not commit_del_id:
105+
print("\n*** WARNING: Could not delete project data before commit ***")
106+
return None, None
47107

48108
try:
49-
commit1_response = client.create_commit(proj_id, commit1_data)
109+
commit1_response = client.create_commit(proj_id, commit1_data, branch_id=branch_id)
50110
commit1_id = commit1_response.get('@id')
51111
if not commit1_id:
52112
print("\n*** WARNING: Could not extract commit ID ('@id') from response! ***")

src/sysmlv2_client/client.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,15 @@ def get_owned_elements(self, project_id: str, element_id: str, commit_id: str =
126126
else:
127127
return []
128128

129-
def create_commit(self, project_id: str, commit_data: Dict[str, Any]) -> Dict[str, Any]:
130-
endpoint = f"/projects/{project_id}/commits"
129+
def create_commit(self, project_id: str, commit_data: Dict[str, Any], branch_id:str = None) -> Dict[str, Any]:
130+
if branch_id is None:
131+
# this takes the default branch
132+
endpoint = f"/projects/{project_id}/commits"
133+
else:
134+
endpoint = f"/projects/{project_id}/commits?branchId={branch_id}"
135+
#print (">>> DEBUG create_commit")
136+
#print (endpoint)
137+
#print (commit_data)
131138
return self._request(
132139
method="POST",
133140
endpoint=endpoint,

0 commit comments

Comments
 (0)