Compare commits

..

2 Commits

3 changed files with 56 additions and 41 deletions

View File

@@ -1,6 +1,7 @@
import requests
import urllib.parse
from uuid import UUID
from azure.identity import DefaultAzureCredential
DEVOPS_SCOPE = "https://app.vssps.visualstudio.com/.default"
DEVOPS_API_VERSION = "7.1"
@@ -105,7 +106,9 @@ class DevOps():
return entities_list
class Organization(DevOps):
def __init__(self, org_url: str, token: str, api_version: str = DEVOPS_API_VERSION):
def __init__(self, org_url: str, token: str | None = None, api_version: str = DEVOPS_API_VERSION):
if token is None:
token = DefaultAzureCredential().get_token(DEVOPS_SCOPE).token
super().__init__(org_url, token, api_version)
@property

View File

@@ -3,44 +3,5 @@
from devops import Organization, Project, Repository, Item, DEVOPS_SCOPE
from azure.identity import DefaultAzureCredential
from json import dumps
import tests
org = Organization("https://dev.azure.com/mcovsandbox", DefaultAzureCredential().get_token(DEVOPS_SCOPE).token)
# Listing projects in the organization
print(f"Projects in the organization {org._org_url}:")
for project in org.projects:
print(f"- {project.name} (ID: {project.id})")
print()
ado_sandbox = Project(org, id="bafe0cf1-6c97-4088-864a-ea6dc02b2727")
print(f"Listing repositories in project: {ado_sandbox.name}")
for repo in ado_sandbox.repositories:
print(f"- {repo.name} (ID: {repo.id})")
print()
repo = Repository(ado_sandbox, id="ado-auth-lab")
print(f"Repository name: {repo.name} and URL: {repo.web_url} (fetched by name)\n")
repo = Repository(ado_sandbox, id="feac266f-84d2-41bc-839b-736925a85eaa")
print(f"Repository name: {repo.name} and URL: {repo.web_url} (fetched by direct ID)\n")
print(f"Listing items in the {repo.name} repository:")
for item in repo.items:
print(f"{item.path} ({item.git_object_type}): {item.commit_id}")
print()
print("Getting specific item details:")
item = Item(repo, path="/generate-pat.py")
print(f"Item path: {item.path}")
print(f"Item type: {item.git_object_type}")
print(f"Item commit ID: {item.commit_id}")
print(f"Item URL: {item.url}")
print()
print("Listing items under the /container folder:")
docs_item = Item(repo, path="/container")
for sub_item in docs_item.children:
print(f"{sub_item.path} ({sub_item.git_object_type}): {sub_item.commit_id}")
print()

51
tests.py Executable file
View File

@@ -0,0 +1,51 @@
#!/usr/bin/env python3
import unittest
from devops import Organization, Repository, Project, Item
# Setup the Organization object outside the test class to speed up tests.
# Each Unit test instantinates the class, so doing it here avoids repeated authentication.
org = Organization("https://dev.azure.com/mcovsandbox")
class TestDevOps(unittest.TestCase):
def setUp(self):
self.org = org
def test_listing_projects_in_organization(self):
org = self.org
projects = list(org.projects)
self.assertGreater(len(projects), 0)
def test_listing_repositories_in_project(self):
project = Project(self.org, id="bafe0cf1-6c97-4088-864a-ea6dc02b2727")
repos = list(project.repositories)
self.assertGreater(len(repos), 0)
def test_fetching_repository_by_name_or_id(self):
project = Project(self.org, id="bafe0cf1-6c97-4088-864a-ea6dc02b2727")
self.assertIsNotNone(project)
repo_by_name = Repository(project, id="ado-auth-lab")
self.assertIsNotNone(repo_by_name)
self.assertEqual(repo_by_name.name, "ado-auth-lab")
self.assertEqual(repo_by_name.id, "feac266f-84d2-41bc-839b-736925a85eaa")
repo_by_id = Repository(project, id="feac266f-84d2-41bc-839b-736925a85eaa")
self.assertEqual(repo_by_id.name, "ado-auth-lab")
self.assertEqual(repo_by_id.id, "feac266f-84d2-41bc-839b-736925a85eaa")
def test_listing_items_in_repository(self):
repo = Repository(Project(self.org, id="bafe0cf1-6c97-4088-864a-ea6dc02b2727"), id="feac266f-84d2-41bc-839b-736925a85eaa")
items = list(repo.items)
self.assertGreater(len(items), 0)
def test_getting_specific_item_details(self):
item = Item(Repository(Project(self.org, id="bafe0cf1-6c97-4088-864a-ea6dc02b2727"), id="feac266f-84d2-41bc-839b-736925a85eaa"), path="/generate-pat.py")
self.assertEqual(item.path, "/generate-pat.py")
self.assertIsNotNone(item.commit_id)
def test_listing_descendant_items_in_folder(self):
repo = Repository(Project(self.org, id="bafe0cf1-6c97-4088-864a-ea6dc02b2727"), id="feac266f-84d2-41bc-839b-736925a85eaa")
docs_item = Item(repo, path="/container")
children = list(docs_item.children)
self.assertGreater(len(children), 0)
if __name__ == "__main__":
unittest.main()