Functions/New-AzDoUserStoryWorkItem.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
Function New-AzDoUserStoryWorkItem
{
<#
    .SYNOPSIS
        Creates a work item of the type User Story
 
    .DESCRIPTION
        Creates a work item of the type User Story
 
    .EXAMPLE
        New-AzDoUserStoryWorkItem -PersonalAccessToken gh5553hiih5lfewahq7n3g7x7oieuothushimanuoch8szn3u2sq -Organisation panzerbjrn -Project "Alpha Devs" -Title "New Story Item"
 
    .EXAMPLE
        This example first get details from another work item, and uses those to place a new item on the same board.
        This also uses <br> to break lines in the descrption field.
         
        $WItem = Get-AzDoUserStoryWorkItem -PersonalAccessToken $PersonalToken -Organisation $OrganizationName -Project $TeamName -WorkItemID 123456
        New-AzDoUserStoryWorkItem -PersonalAccessToken $PAT -Organisation $$Organisation -Project $TeamName -Title "Important Scripting work" -Board $WItem.fields.'System.AreaPath' -Description "Important work <br> Line 2" -AssignedTo $WItem.fields.'System.AssignedTo'.displayName -Verbose -Tags "Tag1","Tag2" -AcceptanceCriteria "Accepted"
         
 
    .PARAMETER PersonalAccessToken
        This is your personal access token from Azuree Devops.
 
    .PARAMETER OrganizationName
        The name of your Azure Devops Organisation
 
    .PARAMETER ProjectName
        The name of your Azure Devops Project or Team
 
    .PARAMETER WorkItemTitle
        The name of your Azure Devops Project or Team
 
    .PARAMETER Board
        The name of your Azure Devops Board you want to add the item to
 
    .PARAMETER Board
        The name of your Azure Devops Board you want to add the item to
 
    .PARAMETER Description
        The content of the description field. Lines can be broken by adding <br>
 
    .PARAMETER AcceptanceCriteria
        The content of the Acceptance Criteria field. Lines can be broken by adding <br>
 
    .PARAMETER Priority
        This is the priority of the item. Default is 3.
 
    .PARAMETER AssignedTo
        This is the person the item is assigned to.
 
    .PARAMETER Tags
        Tags assigned to the work item. These are separated by commas, i.e. "Tag1","Tag2"
 
    .INPUTS
        Input is from command line or called from a script.
 
    .OUTPUTS
        This will output the logfile.
 
    .NOTES
        Version: 0.1
        Author: Lars Panzerbjørn
        Creation Date: 2020.07.31
        Purpose/Change: Initial script development
#>

    [CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='Low')]
    param
    (
        [Parameter(Mandatory)]
        [Alias('PAT')]
        [string]$PersonalAccessToken,

        [Parameter(Mandatory)]
        [Alias('Company')]
        [string]$Organisation,

        [Parameter(Mandatory)]
        [Alias('TeamName')]
        [string]$Project,

        [Parameter(Mandatory)]
        [Alias('Title')]
        [string]$WorkItemTitle,

        [Parameter(Mandatory)]
        [string]$Board,

        [Parameter()][string]$Iteration,

        [Parameter()][string]$Description,
        
        [Parameter()][string]$AcceptanceCriteria,
        
        [Parameter()][ValidateSet(1,2,3,4)][string]$Priority = 3,

        [Parameter()][string]$AssignedTo,
        
        [Parameter()][string[]]$Tags

    )

    BEGIN
    {
        Write-Verbose "Beginning $($MyInvocation.Mycommand)"
        $JsonContentType = 'application/json-patch+json'
        $BaseUri = "https://dev.azure.com/$($Organisation)/"
        $Uri = $BaseUri + "$Project/_apis/wit/workitems/`$User Story?api-version=5.1"

        $Token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PersonalAccessToken)"))
        $Header = @{Authorization = 'Basic ' + $Token;accept=$JsonContentType}
    }

    PROCESS
    {
        Write-Verbose "Processing $($MyInvocation.Mycommand)"

        $Body = @([pscustomobject]@{
                op = "add"
                path = '/fields/System.Title'
                value = $WorkItemTitle
            }
        )
        $Body += @([pscustomobject]@{
                op = "add"
                path = '/fields/System.AreaPath'
                value = $Board
            }
        )
        
        $Body += @([pscustomobject]@{
                op = "add"
                path = '/fields/Microsoft.VSTS.Common.Priority'
                value = $Priority
            }
        )
        
        $Body += @([pscustomobject]@{
                op = "add"
                path = '/fields/System.AssignedTo'
                value = $AssignedTo
            }
        )
        
        #This may need to have project added in front of the iteration.
        IF ($Iteration)
        {
            $Body += @([pscustomobject]@{
                    op = "add"
                    path = '/fields/System.IterationPath'
                    value = $Iteration
                }
            )
        }
        
        IF ($Description)
        {
            $Body += @([pscustomobject]@{
                    op = "add"
                    path = '/fields/System.Description'
                    value = $Description
                }
            )
        }
        
        IF ($AcceptanceCriteria)
        {
            $Body += @([pscustomobject]@{
                    op = "add"
                    path = '/fields/Microsoft.VSTS.Common.AcceptanceCriteria'
                    value = $AcceptanceCriteria
                }
            )
        }
        
        IF ($Tags)
        {
            ForEach ($Tag in $Tags) {$CombiTag += "$Tag;"}
            $Body += @([pscustomobject]@{
                    op = "add"
                    path = '/fields/System.Tags'
                    value = $CombiTag
                }
            )
        }
        $Body = ConvertTo-Json $Body
        $Body
        $Result = Invoke-RestMethod -Uri $uri -Method POST -Headers $Header -ContentType "application/json-patch+json" -Body $Body

    }
    END
    {
        Write-Verbose "Ending $($MyInvocation.Mycommand)"
        #$Body
        $Result
    }
}