1

Similar to the question posted here, I am trying to manage Azure DevOps work items via python.

The accepted answer refers to the official Azure DevOps Python API docs and some sample code which seems like it might be out of date.

Here is a simplified version of the script I am trying to run:

from azure.devops.connection import Connection
from azure.devops.v7_1.work_item_tracking import JsonPatchOperation
from msrest.authentication import BasicAuthentication

personal_access_token = "MY_PAT"
organization_url = f'https://dev.azure.com/{MY_ORG}'

# Create a connection to the org
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)

core_client = connection.clients.get_core_client()  # core client --> used to get project
project = core_client.get_project(MY_PROJECT_ID)
wit_client = connection.clients.get_work_item_tracking_client()  # work item tracking client

# this code is recycled from the azure example code posted above
def _create_work_item_field_patch_operation(op, field, value):
    path = '/fields/{field}'.format(field=field)
    return _create_patch_operation(op=op, path=path, value=value)

def _create_patch_operation(op, path, value):
    patch_operation = JsonPatchOperation()
    patch_operation.op = op
    patch_operation.path = path
    patch_operation.value = value
    patch_operation._from = None
    return patch_operation

patch_document = []
patch_document.append(_create_work_item_field_patch_operation('add', 'System.Title', "Testing"))
patch_document.append(_create_work_item_field_patch_operation('add', 'System.Description', "Hoping to create a new work item."))
wit_client.create_work_item(document=patch_document, project=project, type="Bug")

When I execute this code, I get the following error:

azure.devops.exceptions.AzureDevOpsClientRequestError: Operation returned a 400 status code.

I am wondering if anybody has insight into why I am receiving a 400 status code.

1 Answer 1

1

The problem with the code above was that I was passing in the TeamProject object as opposed to the project name or UUID.

So for example, it should be this:

...

wit_client.create_work_item(document=patch_document, project=MY_PROJECT_ID, type="Bug")

Doing so will result in the work item to be created as expected.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.