Compare commits
3 Commits
89ba272003
...
8b2a06551a
| Author | SHA1 | Date | |
|---|---|---|---|
| 8b2a06551a | |||
| 89b9f8d6e6 | |||
| d6b58f0b51 |
15
harvester.py
15
harvester.py
@@ -3,14 +3,9 @@
|
||||
from sk.devops import Organization
|
||||
from sk.azure import get_token
|
||||
|
||||
token = get_token(
|
||||
tenant_id="a7740229-47b6-45de-ad22-83721462b1bf",
|
||||
client_id="840671c4-5dc4-40e5-aab9-7c3a07bbd652",
|
||||
pem_path="./slawek-mba.pem"
|
||||
)
|
||||
org = Organization("https://dev.azure.com/mcovsandbox", token=get_token())
|
||||
item = org["ADO Sandbox"]["ado-auth-lab"]["/"]
|
||||
|
||||
org = Organization("https://dev.azure.com/mcovsandbox", token=token)
|
||||
|
||||
# print(org.projects["bafe0cf1-6c97-4088-864a-ea6dc02b2727"].repositories["feac266f-84d2-41bc-839b-736925a85eaa"].items["/generate-pat.py"])
|
||||
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]
|
||||
# Let's list all python files in this folder
|
||||
for child in item.get_child_items(pattern="*.py", recurse=True):
|
||||
print(f'- {child.path}')
|
||||
|
||||
20
sk/devops.py
20
sk/devops.py
@@ -1,4 +1,5 @@
|
||||
from __future__ import annotations
|
||||
import pathlib
|
||||
import requests
|
||||
import urllib.parse
|
||||
from uuid import UUID
|
||||
@@ -273,7 +274,7 @@ class Item():
|
||||
def path(self):
|
||||
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."""
|
||||
if self.git_object_type != "tree": # type: ignore[attr-defined]
|
||||
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",
|
||||
params={
|
||||
"scopePath": self.path,
|
||||
"recursionLevel": "oneLevel"
|
||||
"recursionLevel": "oneLevel" if not recurse else "full"
|
||||
}
|
||||
).json().get("value", [])
|
||||
child_items = []
|
||||
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]
|
||||
child_items.append(i)
|
||||
return child_items
|
||||
@@ -299,5 +303,15 @@ class Item():
|
||||
self._children = self.get_child_items()
|
||||
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):
|
||||
return f"Item(path=\"{self._path}\" type={self.git_object_type})" # type: ignore[attr-defined]
|
||||
|
||||
Reference in New Issue
Block a user