To interact with **work items** in **Azure DevOps** using Python, you can use the **Azure DevOps Python API**. The library allows you to create, read, update, and query work items. Below is a guide on how to get started:
### Steps to Access Work Items in Azure DevOps Using Python:
1. **Install the Azure DevOps Python API**:
You can install the `azure-devops` Python package using pip:
```bash
pip install azure-devops
```
2. **Set Up Personal Access Token (PAT)**:
- Go to your Azure DevOps account.
- Navigate to **User Settings** > **Personal Access Tokens**.
- Create a new token with the appropriate permissions (e.g., work items).
3. **Connect to Azure DevOps**:
Use the `Connection` object from the `azure.devops.connection` module to establish a connection to your Azure DevOps organization.
### Code Example to Query Work Items:
```python
from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
from azure.devops.v6_0.work_item_tracking.models import Wiql
# Personal Access Token and Organization URL
personal_access_token = 'YOUR_PERSONAL_ACCESS_TOKEN'
organization_url = 'https://dev.azure.com/YOUR_ORGANIZATION'
# Create a connection to the Azure DevOps organization
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)
# Get the work item tracking client
wit_client = connection.clients.get_work_item_tracking_client()
# Query to get work items
wiql = Wiql(query="SELECT [System.Id], [System.Title], [System.State] FROM WorkItems WHERE [System.WorkItemType] = 'Bug' AND [System.State] = 'Active' ORDER BY [System.CreatedDate] DESC")
# Execute the query
wiql_results = wit_client.query_by_wiql(wiql).work_items
# Fetch work item details
work_item_ids = [wi.id for wi in wiql_results]
if work_item_ids:
work_items = wit_client.get_work_items(work_item_ids)
for work_item in work_items:
print(f"ID: {work_item.id}, Title: {work_item.fields['System.Title']}, State: {work_item.fields['System.State']}")
else:
print("No work items found.")
```
### Key Parts of the Code:
1. **Personal Access Token (PAT)**: Used for authentication.
2. **Connection**: Created using the `Connection` class and the `BasicAuthentication` object.
3. **Query Work Items (WIQL)**: The `Wiql` class allows you to write queries similar to SQL.
4. **Execute the Query**: The `query_by_wiql` method executes the query and returns the result.
5. **Retrieve Work Item Details**: Use the `get_work_items` method to fetch the details of the queried work items.
### Creating a New Work Item:
If you want to **create** a new work item, you can do so using the following code:
```python
from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
# Create a connection
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)
# Get the work item tracking client
wit_client = connection.clients.get_work_item_tracking_client()
# Create a new work item
document = [
{
"op": "add",
"path": "/fields/System.Title",
"value": "New Bug from API"
},
{
"op": "add",
"path": "/fields/System.Description",
"value": "Description of the bug"
}
]
project = 'YOUR_PROJECT_NAME'
work_item_type = 'Bug'
new_work_item = wit_client.create_work_item(document, project, work_item_type)
print(f"Created work item with ID: {new_work_item.id}")
```
### Summary:
- **Get Work Items**: You can query work items using WIQL queries.
- **Create Work Items**: You can create new work items by defining the required fields.
- **Update Work Items**: Similarly, you can use the `update_work_item` method to modify existing work items.
These tools can be extremely useful for automating DevOps processes, such as managing sprints, tracking bugs, and monitoring development progress.
From Blogger iPhone client