-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_lib.py
More file actions
100 lines (88 loc) · 3.48 KB
/
api_lib.py
File metadata and controls
100 lines (88 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import numpy as np
from sysmlv2_client import SysMLV2Client, SysMLV2Error, SysMLV2NotFoundError
from typing import Any, Dict
def create_sysml_project(client: SysMLV2Client, name:str, description:str="project description"):
created_project = None
example_project_id = None
commit_id = None
new_project_data = {
"@type": "Project",
"name": name,
"description": description
}
try:
created_project = client.create_project(new_project_data)
# Store the ID for later use
example_project_id = created_project.get('@id')
except SysMLV2Error as e:
print(f"Error creating project: {e}")
return created_project, example_project_id, commit_id
def get_project_by_name(client: SysMLV2Client, name: str):
try:
projects = client.get_projects()
if projects:
for project in projects:
proj_id = project.get('@id', 'N/A')
proj_name = project.get('name', 'N/A')
if proj_name == name:
return project, proj_id
# If no project matched
return None, None
else:
print(" (No projects found)")
return None, None
except SysMLV2Error as e:
print(f"Error getting projects: {e}")
return None, None
def commit_to_project(client: SysMLV2Client, proj_id:str, payload:str, commit_msg:str = "default commit message"):
commit1_data = {
"@type": "Commit",
"description": commit_msg,
"change": payload
}
try:
commit1_response = client.create_commit(proj_id, commit1_data)
commit1_id = commit1_response.get('@id')
if not commit1_id:
print("\n*** WARNING: Could not extract commit ID ('@id') from response! ***")
return None, None
else:
return commit1_response, commit1_id
except SysMLV2Error as e:
print(f"Error creating commit 1: {e}")
return None, None
def _get_default_branch_id(project: Dict[str, Any]) -> str | None:
default_branch = project.get('defaultBranch')
if isinstance(default_branch, dict):
return default_branch.get('@id')
return None
def _get_referenced_commit_id(branch: Dict[str, Any]) -> str | None:
referenced_commit = branch.get('referencedCommit')
if isinstance(referenced_commit, dict):
return referenced_commit.get('@id')
return None
def get_last_commit_from_project(client: SysMLV2Client, proj_obj: Dict[str, Any]):
default_branch_id = _get_default_branch_id(proj_obj)
proj_id = proj_obj.get('@id')
response_data = client._request(method="GET", endpoint=f"/projects/{proj_id}/branches/{default_branch_id}", expected_status=200)
commit_id = _get_referenced_commit_id(response_data)
return commit_id
def create_branch(client: SysMLV2Client, proj_id:str, commit_id:str, branch_name:str):
#create branch from that commit
branch_data = {
# "@type": "Branch",
"name": branch_name,
"head": {"@id": commit_id}
}
print (branch_data)
try:
created_branch = client.create_branch(proj_id, branch_data)
example_branch_id = created_branch.get('@id')
if not example_branch_id:
print("\n*** WARNING: Could not extract branch ID ('@id') from response! ***")
return None, None
else:
return created_branch, example_branch_id
except SysMLV2Error as e:
print(f"Error creating branch: {e}")
return None, None