|
| 1 | +import numpy as np |
| 2 | +from sysmlv2_client import SysMLV2Client, SysMLV2Error, SysMLV2NotFoundError |
| 3 | +from typing import Any, Dict |
| 4 | + |
| 5 | +minimal_payload = [ |
| 6 | + { |
| 7 | + "payload": { |
| 8 | + "@type": "Empty", |
| 9 | + "@id": "8dd9e99d-ec41-44d8-bb18-a1d849d3aff5", |
| 10 | + "elementId": "8dd9e99d-ec41-44d8-bb18-a1d849d3aff5", |
| 11 | + "qualifiedName": "", |
| 12 | + "isImpliedIncluded": True, |
| 13 | + "isLibraryElement": False, |
| 14 | + "ownedElement": [], |
| 15 | + "ownedRelationship": [] |
| 16 | + } |
| 17 | + } |
| 18 | +] |
| 19 | +def create_sysml_project(client: SysMLV2Client, name:str, description:str="project description"): |
| 20 | + created_project = None |
| 21 | + example_project_id = None |
| 22 | + commit_id = None |
| 23 | + new_project_data = { |
| 24 | + "@type": "Project", |
| 25 | + "name": name, |
| 26 | + "description": description |
| 27 | + } |
| 28 | + try: |
| 29 | + created_project = client.create_project(new_project_data) |
| 30 | + # Store the ID for later use |
| 31 | + example_project_id = created_project.get('@id') |
| 32 | + # if not example_project_id: |
| 33 | + # print("\n*** WARNING: Could not extract project ID ('@id') from response! Subsequent steps might fail. ***") |
| 34 | + # else: |
| 35 | + # #create empty commit so we can branch off an empty commit initially |
| 36 | + # commit_response, commit_id = commit_to_project(client, example_project_id, minimal_payload) |
| 37 | + except SysMLV2Error as e: |
| 38 | + print(f"Error creating project: {e}") |
| 39 | + return created_project, example_project_id, commit_id |
| 40 | + |
| 41 | + |
| 42 | +def get_project_by_name(client: SysMLV2Client, name: str): |
| 43 | + try: |
| 44 | + projects = client.get_projects() |
| 45 | + if projects: |
| 46 | + for project in projects: |
| 47 | + proj_id = project.get('@id', 'N/A') |
| 48 | + proj_name = project.get('name', 'N/A') |
| 49 | + if proj_name == name: |
| 50 | + return project, proj_id |
| 51 | + # If no project matched |
| 52 | + return None, None |
| 53 | + else: |
| 54 | + print(" (No projects found)") |
| 55 | + return None, None |
| 56 | + except SysMLV2Error as e: |
| 57 | + print(f"Error getting projects: {e}") |
| 58 | + return None, None |
| 59 | + |
| 60 | +def commit_to_project(client: SysMLV2Client, proj_id:str, payload:str, commit_msg:str = "default commit message"): |
| 61 | + commit1_data = { |
| 62 | + "@type": "Commit", |
| 63 | + "description": commit_msg, |
| 64 | + "change": payload |
| 65 | + } |
| 66 | + |
| 67 | + try: |
| 68 | + commit1_response = client.create_commit(proj_id, commit1_data) |
| 69 | + commit1_id = commit1_response.get('@id') |
| 70 | + if not commit1_id: |
| 71 | + print("\n*** WARNING: Could not extract commit ID ('@id') from response! ***") |
| 72 | + return None, None |
| 73 | + else: |
| 74 | + return commit1_response, commit1_id |
| 75 | + except SysMLV2Error as e: |
| 76 | + print(f"Error creating commit 1: {e}") |
| 77 | + return None, None |
| 78 | + |
| 79 | + |
| 80 | +def _get_default_branch_id(project: Dict[str, Any]) -> str | None: |
| 81 | + default_branch = project.get('defaultBranch') |
| 82 | + if isinstance(default_branch, dict): |
| 83 | + return default_branch.get('@id') |
| 84 | + return None |
| 85 | + |
| 86 | +def _get_referenced_commit_id(branch: Dict[str, Any]) -> str | None: |
| 87 | + referenced_commit = branch.get('referencedCommit') |
| 88 | + if isinstance(referenced_commit, dict): |
| 89 | + return referenced_commit.get('@id') |
| 90 | + return None |
| 91 | + |
| 92 | + |
| 93 | +def get_last_commit_from_project(client: SysMLV2Client, proj_obj: Dict[str, Any]): |
| 94 | + default_branch_id = _get_default_branch_id(proj_obj) |
| 95 | + proj_id = proj_obj.get('@id') |
| 96 | + response_data = client._request(method="GET", endpoint=f"/projects/{proj_id}/branches/{default_branch_id}", expected_status=200) |
| 97 | + commit_id = _get_referenced_commit_id(response_data) |
| 98 | + return commit_id |
| 99 | + |
| 100 | +def create_branch(client: SysMLV2Client, proj_id:str, commit_id:str, branch_name:str): |
| 101 | + #create branch from that commit |
| 102 | + branch_data = { |
| 103 | +# "@type": "Branch", |
| 104 | + "name": branch_name, |
| 105 | + "head": {"@id": commit_id} |
| 106 | + } |
| 107 | + print (branch_data) |
| 108 | + try: |
| 109 | + created_branch = client.create_branch(proj_id, branch_data) |
| 110 | + example_branch_id = created_branch.get('@id') |
| 111 | + if not example_branch_id: |
| 112 | + print("\n*** WARNING: Could not extract branch ID ('@id') from response! ***") |
| 113 | + return None, None |
| 114 | + else: |
| 115 | + return created_branch, example_branch_id |
| 116 | + except SysMLV2Error as e: |
| 117 | + print(f"Error creating branch: {e}") |
| 118 | + return None, None |
| 119 | + |
0 commit comments