Applied from_json() to object initialization.
This commit is contained in:
125
devops.py
125
devops.py
@@ -77,70 +77,53 @@ class Organization:
|
|||||||
|
|
||||||
# Return a list of Project instances
|
# Return a list of Project instances
|
||||||
projects_data = r.json().get("value", [])
|
projects_data = r.json().get("value", [])
|
||||||
return [
|
|
||||||
Project(self,
|
|
||||||
id=proj.get("id"),
|
|
||||||
name=proj.get("name"),
|
|
||||||
url=proj.get("url"),
|
|
||||||
description=proj.get("description"),
|
|
||||||
) for proj in projects_data
|
|
||||||
]
|
|
||||||
|
|
||||||
@auto_properties({"name": "name", "url": "url", "description": "description"})
|
project_list = []
|
||||||
class Project:
|
for proj in projects_data:
|
||||||
def __init__(self,
|
p = Project(self, project_id=proj.get("id"))
|
||||||
org: Organization,
|
p.from_json(proj)
|
||||||
id: str,
|
project_list.append(p)
|
||||||
name: str | None = None,
|
|
||||||
url: str | None = None,
|
return project_list
|
||||||
description: str | None = None
|
|
||||||
):
|
@auto_properties({
|
||||||
|
"name": "name",
|
||||||
|
"url": "url",
|
||||||
|
"description": "description"
|
||||||
|
})
|
||||||
|
class Project():
|
||||||
|
def _get(self, project_id: str):
|
||||||
|
r = self._org.get(f"_apis/projects/{project_id}")
|
||||||
|
self.from_json(r.json())
|
||||||
|
|
||||||
|
def __init__(self, org: Organization, project_id: str, **kwargs):
|
||||||
self._org = org
|
self._org = org
|
||||||
|
|
||||||
# Check, if the id is a valid UUID
|
|
||||||
try:
|
try:
|
||||||
self._id = str(UUID(id))
|
self._id = str(UUID(project_id))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise ValueError(f"Invalid project ID: {self._id}")
|
raise ValueError(f"Invalid project ID: {project_id}")
|
||||||
|
|
||||||
if name is not None and url is not None and name != "" and url != "":
|
self.set_auto_properties(**kwargs)
|
||||||
self._name = name
|
|
||||||
self._url = url
|
|
||||||
self._description = description if description is not None else "" # Ensure description is a string
|
|
||||||
else:
|
|
||||||
# Fetch project details from the API
|
|
||||||
r = org.get(f"_apis/projects/{self._id}")
|
|
||||||
proj_data = r.json()
|
|
||||||
self._id = proj_data.get("id", "")
|
|
||||||
self._name = proj_data.get("name", "")
|
|
||||||
self._url = proj_data.get("url", "")
|
|
||||||
self._description = proj_data.get("description", "")
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"Project(name={self._name}, id={self._id})"
|
return f"Project(name={self._name}, id={self._id})"
|
||||||
|
|
||||||
def get(self, path: str, params: dict = {}):
|
def get_path(self, path: str, params: dict = {}):
|
||||||
return self._org.get(f"{self._id}/{path.lstrip('/')}", params=params)
|
return self._org.get(f"{self._id}/{path.lstrip('/')}", params=params)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def repositories(self):
|
def repositories(self):
|
||||||
r = self.get(f"_apis/git/repositories")
|
r = self.get_path(f"_apis/git/repositories")
|
||||||
|
|
||||||
repos_data = r.json().get("value", [])
|
repos_data = r.json().get("value", [])
|
||||||
# Return a list of Repository instances
|
|
||||||
return [
|
repository_list = []
|
||||||
Repository(self,
|
for repo in repos_data:
|
||||||
id_or_name=repo.get("id"),
|
r = Repository(self, id_or_name=repo.get("id"))
|
||||||
name=repo.get("name"),
|
r.from_json(repo)
|
||||||
url=repo.get("url"),
|
repository_list.append(r)
|
||||||
default_branch=repo.get("defaultBranch"),
|
|
||||||
disabled=repo.get("isDisabled"),
|
return repository_list
|
||||||
in_maintenance=repo.get("isInMaintenance"),
|
|
||||||
remote_url=repo.get("remoteUrl"),
|
|
||||||
ssh_url=repo.get("sshUrl"),
|
|
||||||
web_url=repo.get("webUrl"),
|
|
||||||
) for repo in repos_data
|
|
||||||
]
|
|
||||||
|
|
||||||
@auto_properties({
|
@auto_properties({
|
||||||
"name": "name",
|
"name": "name",
|
||||||
@@ -154,7 +137,8 @@ class Project:
|
|||||||
})
|
})
|
||||||
class Repository:
|
class Repository:
|
||||||
def _get(self, repo_name: str):
|
def _get(self, repo_name: str):
|
||||||
r = self._project.get(f"_apis/git/repositories/{urllib.parse.quote(repo_name)}")
|
r = self._project.get_path(f"_apis/git/repositories/{urllib.parse.quote(repo_name)}")
|
||||||
|
self._id = r.json().get("id", None)
|
||||||
self.from_json(r.json())
|
self.from_json(r.json())
|
||||||
|
|
||||||
def __init__(self,_project: Project, id_or_name: str, **kwargs):
|
def __init__(self,_project: Project, id_or_name: str, **kwargs):
|
||||||
@@ -166,6 +150,11 @@ class Repository:
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
# Id not available, use API to get the repository object
|
# Id not available, use API to get the repository object
|
||||||
self._get(id_or_name)
|
self._get(id_or_name)
|
||||||
|
# Successfully retrieved the repository by name,
|
||||||
|
# throw an error if automatic properties were set and
|
||||||
|
# id_or_name was not set to repository id
|
||||||
|
if kwargs:
|
||||||
|
raise ValueError("Automatic properties cannot be set when retrieving by name.")
|
||||||
return
|
return
|
||||||
|
|
||||||
# set other properties if provided
|
# set other properties if provided
|
||||||
@@ -174,34 +163,38 @@ class Repository:
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"Repository(name={self.name}, id={self._id})"
|
return f"Repository(name={self.name}, id={self._id})"
|
||||||
|
|
||||||
def _get_path(self, path: str, params: dict = {}):
|
def get_path(self, path: str, params: dict = {}):
|
||||||
return self._project.get(f"_apis/git/repositories/{self._id}/{path.lstrip('/')}", params=params)
|
return self._project.get_path(f"_apis/git/repositories/{self._id}/{path.lstrip('/')}", params=params)
|
||||||
|
|
||||||
def _get_items(self, scope_path: str = "/", recursion_level: str = "oneLevel"):
|
def _get_items(self, scope_path: str = "/", recursion_level: str = "oneLevel"):
|
||||||
r = self._get_path(f"items", params={"scopePath": scope_path, "recursionLevel": recursion_level})
|
r = self.get_path(f"items", params={"scopePath": scope_path, "recursionLevel": recursion_level})
|
||||||
return r.json()
|
return r.json()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def items(self):
|
def items(self):
|
||||||
# GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/items?api-version=7.1
|
# GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/items?api-version=7.1
|
||||||
|
|
||||||
items_data = self._get_items().get("value", [])
|
|
||||||
# Return a list of Item instances
|
# Return a list of Item instances
|
||||||
return [
|
items_data = self._get_items().get("value", [])
|
||||||
Item(self,
|
|
||||||
path=item.get("path"),
|
|
||||||
object_id=item.get("objectId"),
|
|
||||||
git_object_type=item.get("gitObjectType"),
|
|
||||||
commit_id=item.get("commitId"),
|
|
||||||
is_folder=item.get("isFolder"),
|
|
||||||
url=item.get("url"),
|
|
||||||
) for item in items_data
|
|
||||||
]
|
|
||||||
|
|
||||||
@auto_properties({"object_id": "objectId", "git_object_type": "gitObjectType", "commit_id": "commitId", "is_folder": "isFolder", "url": "url"})
|
item_list = []
|
||||||
|
for item in items_data:
|
||||||
|
i = Item(self, path=item.get("path"))
|
||||||
|
i.from_json(item)
|
||||||
|
item_list.append(i)
|
||||||
|
|
||||||
|
return item_list
|
||||||
|
|
||||||
|
@auto_properties({
|
||||||
|
"object_id": "objectId",
|
||||||
|
"git_object_type": "gitObjectType",
|
||||||
|
"commit_id": "commitId",
|
||||||
|
"is_folder": "isFolder",
|
||||||
|
"url": "url"
|
||||||
|
})
|
||||||
class Item:
|
class Item:
|
||||||
def _get(self, path):
|
def _get(self, path):
|
||||||
r = self._repository._get_path(f"items/{urllib.parse.quote(path)}")
|
r = self._repository.get_path(f"items/{urllib.parse.quote(path)}")
|
||||||
|
|
||||||
for name in self.__auto_properties__:
|
for name in self.__auto_properties__:
|
||||||
setattr(self, f"_{name}", r.json().get(name, None))
|
setattr(self, f"_{name}", r.json().get(name, None))
|
||||||
|
|||||||
Reference in New Issue
Block a user