Last active
October 2, 2021 08:43
-
-
Save sdwheeler/99173f6d3358db2b8ff34f3d85a4545d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function New-DevOpsWorkItem { | |
param( | |
[Parameter(Mandatory=$true)] | |
[string]$title, | |
[Parameter(Mandatory=$true)] | |
[string]$description, | |
[int]$parentId, | |
[string[]]$tags, | |
[ValidateSet('Task','User%20Story')] | |
[string]$wiType = 'Task', | |
[ValidateSet( | |
'ProjectName\TeamName', | |
'ProjectName\TeamName\Management\AreaPath1', | |
'ProjectName\TeamName\Management\AreaPath1\SubArea1', | |
'ProjectName\TeamName\Management\AreaPath1\SubArea2', | |
'ProjectName\TeamName\Management\AreaPath1\SubArea3', | |
'ProjectName\TeamName\Management\AreaPath1\SubArea4' | |
)] | |
[string]$areapath='ProjectName\TeamName\Management\AreaPath1', | |
[ValidateSet( | |
'ProjectName\Future', | |
'ProjectName\CY2019\12_2019', | |
'ProjectName\CY2020\01_2020', | |
'ProjectName\CY2020\02_2020', | |
'ProjectName\CY2020\03_2020', | |
'ProjectName\CY2020\04_2020', | |
'ProjectName\CY2020\05_2020', | |
'ProjectName\CY2020\06_2020', | |
'ProjectName\CY2020\07_2020', | |
'ProjectName\CY2020\08_2020', | |
'ProjectName\CY2020\09_2020', | |
'ProjectName\CY2020\10_2020' | |
)] | |
[string]$iterationpath='ProjectName\CY2019\12_2019', | |
[ValidateSet('user1','user2','user3','user4','user5')] | |
[string]$assignee='user1' | |
) | |
# You must create a Personal Access Token (PAT) in Azure DevOps | |
# This script expects the PAT to be stored in an environment variable | |
$username = ' ' | |
$password = ConvertTo-SecureString $env:DEVOPS_OAUTH_TOKEN -AsPlainText -Force | |
$cred = [PSCredential]::new($username, $password) | |
$vsuri = 'https://dev.azure.com' | |
$org = 'orgname' | |
$project = 'ProjectName' | |
$apiurl = "$vsuri/$org/$project/_apis/wit/workitems/$" + $wiType +"?api-version=5.1" | |
$widata = [System.Collections.Generic.List[psobject]]::new() | |
$field = New-Object -type PSObject -prop @{ | |
op = "add" | |
path = "/fields/System.Title" | |
value = $title | |
} | |
$widata.Add($field) | |
$field = New-Object -type PSObject -prop @{ | |
op = "add" | |
path = "/fields/System.AreaPath" | |
value = $areapath | |
} | |
$widata.Add($field) | |
$field = New-Object -type PSObject -prop @{ | |
op = "add" | |
path = "/fields/System.IterationPath" | |
value = $iterationpath | |
} | |
$widata.Add($field) | |
if ($parentId -ne 0) { | |
$field = New-Object -type PSObject -prop @{ | |
op = "add" | |
path = "/relations/-" | |
value = @{ | |
rel = 'System.LinkTypes.Hierarchy-Reverse' | |
url = "$vsuri/$org/$project/_apis/wit/workitems/$parentId" | |
} | |
} | |
$widata.Add($field) | |
} | |
if ($tags.count -ne 0) { | |
$field = New-Object -type PSObject -prop @{ | |
op = "add" | |
path = "/fields/System.Tags" | |
value = $tags -join '; ' | |
} | |
$widata.Add($field) | |
} | |
$field = New-Object -type PSObject -prop @{ | |
op = "add" | |
path = "/fields/System.AssignedTo" | |
value = $assignee + '@microsoft.com' | |
} | |
$widata.Add($field) | |
$field = New-Object -type PSObject -prop @{ | |
op = "add" | |
path = "/fields/System.Description" | |
value = $description | |
} | |
$widata.Add($field) | |
$query = ConvertTo-Json $widata | |
$params = @{ | |
uri = $apiurl | |
Authentication = 'Basic' | |
Credential = $cred | |
Method = 'Post' | |
ContentType = 'application/json-patch+json' | |
Body = $query | |
} | |
#$params | |
$results = irm @params | |
$results | | |
select @{l='Id';e={$_.Id}}, | |
@{l='State'; e={$_.fields.'System.State'}}, | |
@{l='Parent';e={$_.fields.'System.Parent'}}, | |
@{l='AssignedTo'; e={$_.fields.'System.AssignedTo'.displayName}}, | |
@{l='AreaPath';e={$_.fields.'System.AreaPath'}}, | |
@{l='IterationPath'; e={$_.fields.'System.IterationPath'}}, | |
@{l='Title';e={$_.fields.'System.Title'}}, | |
@{l='AttachedFiles'; e={$_.fields.'System.AttachedFileCount'}}, | |
@{l='ExternalLinks';e={$_.fields.'System.ExternalLinkCount'}}, | |
@{l='HyperLinks'; e={$_.fields.'System.HyperLinkCount'}}, | |
@{l='Reason';e={$_.fields.'System.Reason'}}, | |
@{l='RelatedLinks'; e={$_.fields.'System.RelatedLinkCount'}}, | |
@{l='RemoteLinks';e={$_.fields.'System.RemoteLinkCount'}}, | |
@{l='Tags'; e={$_.fields.'System.Tags'}}, | |
@{l='Description';e={$_.fields.'System.Description'}} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment