@@ -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! ***" )
0 commit comments