Creating a program that will automatically create PBIs in AzureDevOps for my organization. I cant quite figure out how to make a script reference the APIs AzureDevOps have created. I'm simply wanting to have the program take my token and create a work item in vsts and assign it to myself with a customized title. I have installed azure CLI and have downloaded the azure-devops-python-samples files, but theres not a good way for me to reference a way to create_work_item. I guess I need a tutorial on how to take the github files and simply have another program reference this way of making a PBI. I'm fairly new to all of this, so someone that can explain how to do this would be really helpful!
TLDR: Point A - Have a title and user_assignment saved as variables in another file Point B - Reference these variables and make a PBI for my org.
I figure it would be helpful to have my code in this question:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import base64
import json
import requests
#from bugcrowdapipull.py import xbug_title
pat = 'PAT HERE'
authorization = str(base64.b64encode(bytes(':'+pat, 'ascii')), 'ascii')
headers = {
'Accept': 'application/json',
'Authorization': 'Basic '+authorization
}
url="https://PROJECT.visualstudio.com/_apis/projects?api-version=5.1"
response = requests.get(url, headers=headers)
print(response)
def create_work_item(self, document, project, type='Bug', validate_only=None, bypass_rules=None, suppress_notifications=None, expand=None):
"""CreateWorkItem.
[Preview API] Creates a single work item.
:param :class:`<[JsonPatchOperation]> <azure.devops.v6_0.work_item_tracking.models.[JsonPatchOperation]>` document: The JSON Patch document representing the work item
:param str project: Project ID or project name
:param str type: The work item type of the work item to create
:param bool validate_only: Indicate if you only want to validate the changes without saving the work item
:param bool bypass_rules: Do not enforce the work item type rules on this update
:param bool suppress_notifications: Do not fire any notifications for this change
:param str expand: The expand parameters for work item attributes. Possible options are { None, Relations, Fields, Links, All }.
:rtype: :class:`<WorkItem> <azure.devops.v6_0.work_item_tracking.models.WorkItem>`
"""
route_values = {}
if project is not None:
route_values['project'] = self._serialize.url('project', project, 'str')
if type is not None:
route_values['type'] = self._serialize.url('type', type, 'str')
query_parameters = {}
if validate_only is not None:
query_parameters['validateOnly'] = self._serialize.query('validate_only', validate_only, 'bool')
if bypass_rules is not None:
query_parameters['bypassRules'] = self._serialize.query('bypass_rules', bypass_rules, 'bool')
if suppress_notifications is not None:
query_parameters['suppressNotifications'] = self._serialize.query('suppress_notifications', suppress_notifications, 'bool')
if expand is not None:
query_parameters['$expand'] = self._serialize.query('expand', expand, 'str')
content = self._serialize.body(document, '[JsonPatchOperation]')
response = self._send(http_method='POST',
location_id='62d3d110-0047-428c-ad3c-4fe872c91c74',
version='6.0-preview.3',
route_values=route_values,
query_parameters=query_parameters,
content=content,
media_type='application/json-patch+json')
return self._deserialize('WorkItem', response)
print('\n\n')
You can use both python api client library and Restful api to create a work items. Please check below examples:
1, Use python api client library. Please check create_work_item method for more information.
from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
from azure.devops.v5_1.py_pi_api import JsonPatchOperation
token = 'PAT'
team_instance = 'https://dev.azure.com/{OrganizationName}'
credentials = BasicAuthentication("", token)
connection = Connection(base_url=team_instance, creds=credentials)
wit_client = connection.clients.get_work_item_tracking_client()
def createworkitem():
documents = []
# add title field
documents.append(JsonPatchOperation(from_=None,op='add',path="/fields/System.Title",value="i am created by python client"))
# add assignTo field
documents.append(JsonPatchOperation(from_=None,op='add',path="/fields/System.AssignedTo",value="User DisplayName"))
result = wit_client.create_work_item(documents, "{ProjectName}", 'Product Backlog Item', validate_only=None, bypass_rules=None, suppress_notifications=None, expand=None)
print(result)
if __name__ == "__main__":
createworkitem()
2, Use Restful API, Work Item- Create:
import requests
import base64
if __name__ == "__main__":
pat = '{PAT}'
authorization = str(base64.b64encode(bytes(':'+pat, 'ascii')), 'ascii')
headers = {
'Content-Type': 'application/json-patch+json', #specify the content-type
'Authorization': 'Basic '+authorization
}
url="https://dev.azure.com/{OrganizationName}/{ProjectName}/_apis/wit/workitems/$Product Backlog Item?api-version=5.1"
body = [{
"op": "add",
"path": "/fields/System.Title",
"value": "Sample from python api"
},
{
"op": "add",
"path": "/fields/System.AssignedTo",
"value": "User Name"
}]
response = requests.post(url, json = body, headers=headers)