Compare commits

...

3 Commits

2 changed files with 22 additions and 13 deletions

View File

@@ -3,14 +3,9 @@
from sk.devops import Organization from sk.devops import Organization
from sk.azure import get_token from sk.azure import get_token
token = get_token( org = Organization("https://dev.azure.com/mcovsandbox", token=get_token())
tenant_id="a7740229-47b6-45de-ad22-83721462b1bf", item = org["ADO Sandbox"]["ado-auth-lab"]["/"]
client_id="840671c4-5dc4-40e5-aab9-7c3a07bbd652",
pem_path="./slawek-mba.pem"
)
org = Organization("https://dev.azure.com/mcovsandbox", token=token) # Let's list all python files in this folder
for child in item.get_child_items(pattern="*.py", recurse=True):
# print(org.projects["bafe0cf1-6c97-4088-864a-ea6dc02b2727"].repositories["feac266f-84d2-41bc-839b-736925a85eaa"].items["/generate-pat.py"]) print(f'- {child.path}')
print(org["ADO Sandbox"]["ado-auth-lab"]["/container"].url) # type: ignore[attr-defined]
print(org["ADO Sandbox"]["ado-auth-lab"]["/generate-pat.py"].url) # type: ignore[attr-defined]

View File

@@ -1,4 +1,5 @@
from __future__ import annotations from __future__ import annotations
import pathlib
import requests import requests
import urllib.parse import urllib.parse
from uuid import UUID from uuid import UUID
@@ -273,7 +274,7 @@ class Item():
def path(self): def path(self):
return self._path # type: ignore[attr-defined] return self._path # type: ignore[attr-defined]
def get_child_items(self) -> list[Item]: def get_child_items(self, pattern: str | None = None, recurse: bool = False) -> list[Item]:
"""Get child items if this item is a folder.""" """Get child items if this item is a folder."""
if self.git_object_type != "tree": # type: ignore[attr-defined] if self.git_object_type != "tree": # type: ignore[attr-defined]
raise ValueError("Child items can only be fetched for folder items.") raise ValueError("Child items can only be fetched for folder items.")
@@ -283,12 +284,15 @@ class Item():
path=f"{self._repository.project.id}/_apis/git/repositories/{self._repository.id}/items", path=f"{self._repository.project.id}/_apis/git/repositories/{self._repository.id}/items",
params={ params={
"scopePath": self.path, "scopePath": self.path,
"recursionLevel": "oneLevel" "recursionLevel": "oneLevel" if not recurse else "full"
} }
).json().get("value", []) ).json().get("value", [])
child_items = [] child_items = []
for obj in objects: for obj in objects:
i = Item(repository=self._repository, path=obj.get("path")) obj_path = obj.get("path")
if pattern and not pathlib.PurePath(obj_path).match(pattern):
continue
i = Item(repository=self._repository, path=obj_path)
i.from_json(obj) # type: ignore[attr-defined] i.from_json(obj) # type: ignore[attr-defined]
child_items.append(i) child_items.append(i)
return child_items return child_items
@@ -299,5 +303,15 @@ class Item():
self._children = self.get_child_items() self._children = self.get_child_items()
return self._children return self._children
def __getitem__(self, key: str) -> Item:
if self.git_object_type != "tree": # type: ignore[attr-defined]
raise ValueError("Child items can only be accessed for folder items.")
if not key.startswith("/"):
key = pathlib.Path(self.path).joinpath(key).absolute().as_posix()
for child in self.children:
if child.path == key:
return child
raise KeyError(f"Child item with path '{key}' not found.")
def __str__(self): def __str__(self):
return f"Item(path=\"{self._path}\" type={self.git_object_type})" # type: ignore[attr-defined] return f"Item(path=\"{self._path}\" type={self.git_object_type})" # type: ignore[attr-defined]