In my previous post I talked about creating Azure DevOps(ADO) Task from ITSM Incident. I am going to talk about another use case today. We will create ADO User Story work item from ITSM Change Management Ticket. As mentioned in my previous post, Engineering team is using ADO to track their activities, where as rest of business uses ITSM system to track all there Change Management process in the organization. We need to create a corresponding ADO User Story from the change request entered by the requester. Note, this is just an example use case, you may have some other reason to implement this automation at your work place.
Before we start, we need to identify all required fields we would like to map to the user story from the change request. For my example, I will capture following fields from the change request ticket to update in the newly created user story. You may have different required fields. Feel free to change it accordingly!
Change Number | ITSM Change Request Number |
Requester | Requester of the Change |
Category | Category of Change – Infrastructure, Code etc. |
Sub-Category | Sub Category – Windows, Linux etc. |
Priority | Change Priority |
System Type | Type of system – Production, Non-Production etc. |
Business Value | Why we are making this change – Maintenance,Enhancement etc. |
Start Date | Start date and time of the implementation of the change |
End Date | End date and time of the implementation of the change |
Short Description | Short description of the change |
Description | Detailed description of the change |
Following table shows how I am going to map above mentioned fields with ADO user story fields. Again, decide your own fields based on requirements.
Title | Change Number, Short Description |
Description | Change Number, Requester, Category, Sub-Category, System Type, Business Value, Start Date, End Date, Description |
Priority | Numeric Value corresponding to Change Priority 1,2,3 etc. |
Tags | Category,Sub-Category |
Before we start, as prerequisite, we need a Personal Access Token(PAT) with correct set of permissions to authenticate and authorize on the ADO API. You may use SSH Key or an alternate method to generate credentials. Please refer my previous post on ADO Task creation process to get the steps to generate PAT. In addition, you need your Organization Name and Project Name where we will be creating this work item ready.
With that, let’s generate the authorization header to make REST API calls to ADO API using following code block.
$pacToken = "abcdesomesamplejunkentriesdontuseititwillnotwork"
$organizationName = "MyTestOrg"
$projectName = "MyTestProject"
$adoHeader = @{Authorization=("Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "",$pacToken))))}
$adoUserStoryUri = "https://dev.azure.com/$organizationName/$projectName/_apis/wit/workitems/`$User Story?api-version=5.1"
Now, let’s create the main script block to create ADO User story.This is very much similar to the ADO task creation script block with few changes . I am assuming you already collected all required ITSM change request information to use while updating user story details.
#Data retrieved from Change request
$number = "CHG12345" #Enter your change number here
$requester = "Arindam Hazra" #Enter Requester name, I will share a separate script on how to retrieve these informations from ServiceNow using Powershell
$cat = "Infrastructure"
$subcat = "Windows"
$shortDescription = "Patch Winsdows Server - abc"
$description = "Patch Windows Server - abc. Please use the time window 01/20/2019 20:00 - 01/20/2020 22:00. Make sure to stop services for SQL Server, xyz application before startin."
$businessVal = "Maintenance"
$priority = 2
$systemType = "Production"
$startDate = "2020-01-20 20:00"
$endDate = "2020-01-20 22:00"
#Define ADO vaiables
$title = "$number : $shortDescription"
$comments = "Change Number : $number <br />"
$comments += "Opened By : $requester <br />"
$comments += "Categoty : $cat <br />"
$comments += "SubCategory : $subcat <br />"
$comments += "System Type : $systemType <br />"
$comments += "Business Value : $businessVal <br />"
$comments += "Change Start Date : $startDate <br />"
$comments += "Cange End Date : $endDate <br />"
$comments += "Description : <br />"
$comments += $description
$comments = $comments.replace('"',"'")
$impact = $priority
#Generate JSON body to make the REST API call
$body="[
{
`"op`": `"add`",
`"path`": `"/fields/System.Title`",
`"value`": `"$title`"
},
{
`"op`": `"add`",
`"path`": `"/fields/System.Description`",
`"value`": `"$comments`"
},
{
`"op`": `"add`",
`"path`": `"/fields/Microsoft.VSTS.Common.Priority`",
`"value`": `"$impact`"
},
{
`"op`": `"add`",
`"path`": `"/fields/System.Tags`",
`"value`": `"$cat;$subcat`"
}
]"
Invoke-RestMethod -Uri $adoUserStoryUri -ContentType "application/json-patch+json" -Body $body -headers $adoHeader -Method POST
Once the script runs successfully, you will receive response like below :
Now, check in Azure DevOps. You should see a User Story has been created in the Project name you provided :
That’s it! we have created an User story in ADO using Powershell!