Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To add a comment using the Python API in Azure DevOps, you can use the following code:

  1. First, import the necessary modules and authenticate to the Azure DevOps organization:
from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication

# Your organization URL
organization_url = 'https://dev.azure.com/myOrganization'

# Personal access token for authentication
personal_access_token = 'myToken'

# Create a connection to the Azure DevOps organization
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)
  1. Specify the team project and work item ID where you want to add a comment:
# The team project where the work item is located
team_project = 'MyProject'

# The ID of the work item
work_item_id = 1234
  1. Define the comment that you want to add:
# The comment text
comment_text = 'This is a comment'
  1. Use the WorkItemTrackingHttpClient to add the comment:
from azure.devops.v6_0.work_item_tracking import WorkItemTrackingHttpClient
from azure.devops.v6_0.work_item_tracking.models import CommentCreate

# Get a reference to the WorkItemTrackingHttpClient
wit_client = connection.clients.get_work_item_tracking_client()

# Create a CommentCreate object with the comment text
comment = CommentCreate(text=comment_text)

# Add the comment to the work item
wit_client.add_comment(comment, team_project, work_item_id)

This will add a new comment to the specified work item in Azure DevOps using the Python API.