Hey there, fellow dev! Ready to supercharge your workflow with Azure DevOps API? Let's dive into building a slick integration using Python and the azure-devops package. Trust me, it's easier than you might think!
Before we jump in, make sure you've got:
pip install azure-devops
)Let's get this party started! First, import the goods and set up your connection:
from azure.devops.connection import Connection from msrest.authentication import BasicAuthentication # Your organization URL and PAT organization_url = "https://dev.azure.com/your-organization" personal_access_token = "your-pat-here" # Create a connection object credentials = BasicAuthentication('', personal_access_token) connection = Connection(base_url=organization_url, creds=credentials)
Now that we're connected, let's flex those API muscles!
core_client = connection.clients.get_core_client() projects = core_client.get_projects() for project in projects: print(f"Project Name: {project.name}")
wit_client = connection.clients.get_work_item_tracking_client() work_item = wit_client.get_work_item(id=42) # Replace with your work item ID print(f"Work Item Title: {work_item.fields['System.Title']}")
build_client = connection.clients.get_build_client() builds = build_client.get_builds(project="YourProjectName") for build in builds: print(f"Build ID: {build.id}, Status: {build.status}")
Ready to level up? Let's tackle some more complex operations.
wiql = "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.TeamProject] = 'YourProjectName' AND [System.WorkItemType] = 'Bug' AND [System.State] = 'Active'" query_results = wit_client.query_by_wiql(wiql).work_items for item in query_results: work_item = wit_client.get_work_item(item.id) print(f"ID: {work_item.id}, Title: {work_item.fields['System.Title']}")
new_work_item = [ { "op": "add", "path": "/fields/System.Title", "value": "New bug found" }, { "op": "add", "path": "/fields/System.Description", "value": "This is a newly created bug." } ] created_item = wit_client.create_work_item(document=new_work_item, project="YourProjectName", type="Bug") print(f"Created Work Item ID: {created_item.id}")
git_client = connection.clients.get_git_client() pull_requests = git_client.get_pull_requests("YourProjectName", "YourRepositoryName") for pr in pull_requests: print(f"PR Title: {pr.title}, Status: {pr.status}")
Don't let errors catch you off guard! Wrap your API calls in try-except blocks:
from azure.devops.exceptions import AzureDevOpsServiceError try: projects = core_client.get_projects() except AzureDevOpsServiceError as e: print(f"Oops! Something went wrong: {e}")
Remember to handle rate limits gracefully and never, ever hardcode your PAT. Use environment variables or a secure secret management system.
Let's put it all together with a real-world scenario. Here's a script that updates the status of all active bugs to "In Progress":
wiql = "Select [System.Id] From WorkItems Where [System.TeamProject] = 'YourProjectName' AND [System.WorkItemType] = 'Bug' AND [System.State] = 'Active'" query_results = wit_client.query_by_wiql(wiql).work_items for item in query_results: update = [ { "op": "add", "path": "/fields/System.State", "value": "In Progress" } ] wit_client.update_work_item(document=update, id=item.id) print(f"Updated work item {item.id}")
And there you have it! You're now equipped to harness the power of Azure DevOps API with Python. Remember, this is just scratching the surface. The azure-devops package offers a ton more functionality, so don't be afraid to explore and experiment.
Keep coding, keep automating, and most importantly, keep having fun with it!
Hit a snag? Here are some common hiccups and how to solve them:
Now go forth and conquer those Azure DevOps tasks with your newfound Python powers!