Implemented item indexer on Repository class.
This commit is contained in:
46
devops.py
46
devops.py
@@ -1,3 +1,4 @@
|
|||||||
|
from __future__ import annotations
|
||||||
import requests
|
import requests
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
@@ -120,7 +121,7 @@ class Organization(DevOps):
|
|||||||
list_url="_apis/projects")
|
list_url="_apis/projects")
|
||||||
return self._projects
|
return self._projects
|
||||||
|
|
||||||
def __getitem__(self, key: str) -> 'Project':
|
def __getitem__(self, key: str) -> Project:
|
||||||
for project in self.projects:
|
for project in self.projects:
|
||||||
if project.id == key or project.name == key:
|
if project.id == key or project.name == key:
|
||||||
return project
|
return project
|
||||||
@@ -157,10 +158,18 @@ class Project(DevOps):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def repositories(self):
|
def repositories(self):
|
||||||
return self._entities(
|
if not hasattr(self, "_repositories"):
|
||||||
entity_class=Repository,
|
self._repositories = self._entities(
|
||||||
key_name="id",
|
entity_class=Repository,
|
||||||
list_url=f"{self._id}/_apis/git/repositories")
|
key_name="id",
|
||||||
|
list_url=f"{self._id}/_apis/git/repositories")
|
||||||
|
return self._repositories
|
||||||
|
|
||||||
|
def __getitem__(self, key: str) -> Repository:
|
||||||
|
for repo in self.repositories:
|
||||||
|
if repo.id == key or repo.name == key:
|
||||||
|
return repo
|
||||||
|
raise KeyError(f"Repository with ID or name '{key}' not found.")
|
||||||
|
|
||||||
@auto_properties({
|
@auto_properties({
|
||||||
"name": "name",
|
"name": "name",
|
||||||
@@ -209,16 +218,23 @@ class Repository(DevOps):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def items(self):
|
def items(self):
|
||||||
# GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/items?api-version=7.1
|
if not hasattr(self, "_items"):
|
||||||
return self._entities(
|
self._items = self._entities(
|
||||||
entity_class=Item,
|
entity_class=Item,
|
||||||
key_name="path",
|
key_name="path",
|
||||||
list_url=f"{self._project.id}/_apis/git/repositories/{self._id}/items",
|
list_url=f"{self._project.id}/_apis/git/repositories/{self._id}/items",
|
||||||
params={
|
params={
|
||||||
"scopePath": "/",
|
"scopePath": "/",
|
||||||
"recursionLevel": "oneLevel"
|
"recursionLevel": "oneLevel"
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
return self._items
|
||||||
|
|
||||||
|
def __getitem__(self, key: str) -> Item:
|
||||||
|
for item in self.items:
|
||||||
|
if item.path == key:
|
||||||
|
return item
|
||||||
|
raise KeyError(f"Item with path '{key}' not found.")
|
||||||
|
|
||||||
@auto_properties({
|
@auto_properties({
|
||||||
"object_id": "objectId",
|
"object_id": "objectId",
|
||||||
|
|||||||
@@ -4,4 +4,5 @@ from devops import Organization
|
|||||||
org = Organization("https://dev.azure.com/mcovsandbox")
|
org = Organization("https://dev.azure.com/mcovsandbox")
|
||||||
|
|
||||||
# print(org.projects["bafe0cf1-6c97-4088-864a-ea6dc02b2727"].repositories["feac266f-84d2-41bc-839b-736925a85eaa"].items["/generate-pat.py"])
|
# print(org.projects["bafe0cf1-6c97-4088-864a-ea6dc02b2727"].repositories["feac266f-84d2-41bc-839b-736925a85eaa"].items["/generate-pat.py"])
|
||||||
print(org["ADO Sandbox"])
|
print(org["ADO Sandbox"]["ado-auth-lab"]["/container"].url)
|
||||||
|
print(org["ADO Sandbox"]["ado-auth-lab"]["/generate-pat.py"].url)
|
||||||
|
|||||||
60
tests.py
60
tests.py
@@ -1,20 +1,19 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import unittest
|
import unittest
|
||||||
from devops import Organization, Repository, Project, Item
|
from azure.identity import DefaultAzureCredential
|
||||||
|
from devops import DEVOPS_SCOPE, Organization, Repository, Project, Item
|
||||||
|
|
||||||
# Setup the Organization object outside the test class to speed up tests.
|
# Get the token outside the test class to speed up tests.
|
||||||
# Each Unit test instantinates the class, so doing it here avoids repeated authentication.
|
# Each Unit test instantinates the class, so doing it here avoids repeated authentication.
|
||||||
org = Organization("https://dev.azure.com/mcovsandbox")
|
token = DefaultAzureCredential().get_token(DEVOPS_SCOPE).token
|
||||||
|
|
||||||
class Test01(unittest.TestCase):
|
class Test01(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.org = org
|
self.org = Organization("https://dev.azure.com/mcovsandbox", token=token)
|
||||||
|
|
||||||
def test_01(self):
|
def test_01(self):
|
||||||
"""Listing projects in the organization"""
|
"""Listing projects in the organization"""
|
||||||
org = self.org
|
self.assertGreater(len(list(self.org.projects)), 0)
|
||||||
projects = list(org.projects)
|
|
||||||
self.assertGreater(len(projects), 0)
|
|
||||||
def test_02(self):
|
def test_02(self):
|
||||||
"""Getting a specific project by ID (object instantiation)"""
|
"""Getting a specific project by ID (object instantiation)"""
|
||||||
project = Project(self.org, id="bafe0cf1-6c97-4088-864a-ea6dc02b2727")
|
project = Project(self.org, id="bafe0cf1-6c97-4088-864a-ea6dc02b2727")
|
||||||
@@ -33,7 +32,7 @@ class Test01(unittest.TestCase):
|
|||||||
|
|
||||||
class Test02(unittest.TestCase):
|
class Test02(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.org = org
|
self.org = Organization("https://dev.azure.com/mcovsandbox", token=token)
|
||||||
|
|
||||||
def test_01(self):
|
def test_01(self):
|
||||||
"""Listing repositories in a project"""
|
"""Listing repositories in a project"""
|
||||||
@@ -41,9 +40,29 @@ class Test02(unittest.TestCase):
|
|||||||
repos = list(project.repositories)
|
repos = list(project.repositories)
|
||||||
self.assertGreater(len(repos), 0)
|
self.assertGreater(len(repos), 0)
|
||||||
|
|
||||||
|
def test_02(self):
|
||||||
|
"""Getting a specific repository by ID (object instantiation)"""
|
||||||
|
repo = Repository(Project(self.org, id="bafe0cf1-6c97-4088-864a-ea6dc02b2727"), id="feac266f-84d2-41bc-839b-736925a85eaa")
|
||||||
|
self.assertEqual(repo.id, "feac266f-84d2-41bc-839b-736925a85eaa")
|
||||||
|
self.assertEqual(repo.name, "ado-auth-lab")
|
||||||
|
|
||||||
|
def test_03(self):
|
||||||
|
"""Getting a specific repository by name using project indexing (object retrieval)"""
|
||||||
|
project = Project(self.org, id="bafe0cf1-6c97-4088-864a-ea6dc02b2727")
|
||||||
|
repo = project["ado-auth-lab"]
|
||||||
|
self.assertEqual(repo.id, "feac266f-84d2-41bc-839b-736925a85eaa")
|
||||||
|
self.assertEqual(repo.name, "ado-auth-lab")
|
||||||
|
|
||||||
|
def test_04(self):
|
||||||
|
"""Getting a specific repository by ID using project indexing (object retrieval)"""
|
||||||
|
project = Project(self.org, id="bafe0cf1-6c97-4088-864a-ea6dc02b2727")
|
||||||
|
repo = project["feac266f-84d2-41bc-839b-736925a85eaa"]
|
||||||
|
self.assertEqual(repo.id, "feac266f-84d2-41bc-839b-736925a85eaa")
|
||||||
|
self.assertEqual(repo.name, "ado-auth-lab")
|
||||||
|
|
||||||
class Test03(unittest.TestCase):
|
class Test03(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.org = org
|
self.org = Organization("https://dev.azure.com/mcovsandbox", token=token)
|
||||||
|
|
||||||
def test_01(self):
|
def test_01(self):
|
||||||
"""Getting details of a specific item in a repository"""
|
"""Getting details of a specific item in a repository"""
|
||||||
@@ -58,9 +77,29 @@ class Test03(unittest.TestCase):
|
|||||||
children = list(docs_item.children)
|
children = list(docs_item.children)
|
||||||
self.assertGreater(len(children), 0)
|
self.assertGreater(len(children), 0)
|
||||||
|
|
||||||
|
def test_03(self):
|
||||||
|
"""Getting a specific item from a repository using indexing"""
|
||||||
|
repo = Repository(Project(self.org, id="bafe0cf1-6c97-4088-864a-ea6dc02b2727"), id="feac266f-84d2-41bc-839b-736925a85eaa")
|
||||||
|
item = repo["/container"]
|
||||||
|
self.assertEqual(item.path, "/container")
|
||||||
|
self.assertTrue(item.is_folder)
|
||||||
|
|
||||||
|
def test_04(self):
|
||||||
|
"""Getting a specific item from a repository using indexing"""
|
||||||
|
repo = Repository(Project(self.org, id="bafe0cf1-6c97-4088-864a-ea6dc02b2727"), id="feac266f-84d2-41bc-839b-736925a85eaa")
|
||||||
|
item = repo["/generate-pat.py"]
|
||||||
|
self.assertEqual(item.path, "/generate-pat.py")
|
||||||
|
self.assertFalse(item.is_folder)
|
||||||
|
|
||||||
|
def test_05(self):
|
||||||
|
"""Attempting to get a non-existent item from a repository using indexing"""
|
||||||
|
repo = Repository(Project(self.org, id="bafe0cf1-6c97-4088-864a-ea6dc02b2727"), id="feac266f-84d2-41bc-839b-736925a85eaa")
|
||||||
|
with self.assertRaises(KeyError):
|
||||||
|
repo["/non-existent-file.txt"] # This file does not exist
|
||||||
|
|
||||||
class Test04(unittest.TestCase):
|
class Test04(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.org = org
|
self.org = Organization("https://dev.azure.com/mcovsandbox", token=token)
|
||||||
|
|
||||||
def test_01(self):
|
def test_01(self):
|
||||||
"""Getting details of a specific item in a repository"""
|
"""Getting details of a specific item in a repository"""
|
||||||
@@ -75,6 +114,5 @@ class Test04(unittest.TestCase):
|
|||||||
children = list(docs_item.children)
|
children = list(docs_item.children)
|
||||||
self.assertGreater(len(children), 0)
|
self.assertGreater(len(children), 0)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Reference in New Issue
Block a user