Skip to content

Commit b0000d6

Browse files
author
SimonDeeb
committed
refactore workitems cmdlets
1 parent 6e7a188 commit b0000d6

4 files changed

Lines changed: 259 additions & 73 deletions

File tree

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
function Get-AzDoWorkItem {
2+
[CmdletBinding(SupportsShouldProcess)]
23
param (
34
# Collection Uri of the organization
45
[Parameter(Mandatory)]
@@ -9,42 +10,56 @@ function Get-AzDoWorkItem {
910
# Name of the project where the team is located
1011
[Parameter(Mandatory)]
1112
[string]
12-
$ProjectName
13+
$ProjectName,
14+
15+
# WorkItem Id to get
16+
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
17+
[int[]]
18+
$WorkItemId
1319
)
1420

1521
begin {
1622
Write-Verbose "Starting 'Get-AzDoWorkItem' function."
1723
$result = New-Object System.Collections.Generic.List[System.Object]
24+
$Uri = "$CollectionUri/$ProjectName/_apis/wit/workitems/{0}"
1825
}
1926

2027
process {
2128
$params = @{
22-
uri = "$CollectionUri/$ProjectName/_apis/wit/workitems/Issues"
23-
method = 'GET'
29+
method = 'GET'
2430
version = '7.1-preview.3'
2531
}
2632

27-
$WorkItems = Invoke-AzDoRestMethod @params
33+
foreach ($id in $WorkItemId) {
34+
$id = [string]$id
35+
$params.uri = $Uri -f $id
36+
try {
37+
$result += Invoke-AzDoRestMethod @params
38+
} catch {
39+
Write-AzdoError -Message $_
40+
}
41+
}
2842
}
2943

3044
end {
3145
Write-Verbose "Ending 'Get-AzDoWorkItem' function."
32-
$WorkItems | ForEach-Object {
33-
[PSCustomObject]@{
34-
Id = $_.id
35-
Title = $_.fields.'System.Title'
36-
AreaPath = $_.fields.'System.AreaPath'
37-
IterationPath = $_.fields.'System.IterationPath'
38-
TeamProject = $_.fields.'System.TeamProject'
39-
WorkItemType = $_.fields.'System.WorkItemType'
40-
State = $_.fields.'System.State'
41-
Reason = $_.fields.'System.Reason'
42-
AssignedTo = $_.fields.'System.AssignedTo'.displayName
43-
CreatedDate = $_.fields.'System.CreatedDate'
44-
CreatedBy = $_.fields.'System.CreatedBy'.displayName
45-
Url = $_.url
46+
if ($result) {
47+
$result | ForEach-Object {
48+
[PSCustomObject]@{
49+
Id = $_.id
50+
Title = $_.fields.'System.Title'
51+
AreaPath = $_.fields.'System.AreaPath'
52+
IterationPath = $_.fields.'System.IterationPath'
53+
TeamProject = $_.fields.'System.TeamProject'
54+
WorkItemType = $_.fields.'System.WorkItemType'
55+
State = $_.fields.'System.State'
56+
Reason = $_.fields.'System.Reason'
57+
AssignedTo = $_.fields.'System.AssignedTo'.displayName
58+
CreatedDate = $_.fields.'System.CreatedDate'
59+
CreatedBy = $_.fields.'System.CreatedBy'.displayName
60+
Url = $_.url
61+
}
4662
}
4763
}
4864
}
49-
5065
}

AzureDevOpsPowerShell/Public/Api/Work/WorkItems/New-AzDoWorkItem.ps1

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,19 @@ function New-AzDoWorkItem {
1414

1515
# Work item object (could be a hashtable or a custom object)
1616
# template: @{
17-
# Title = "Test Work Item 2"
18-
# Description = "This is a test work item created by the script"
19-
# AreaPath = "DevOps Automation"
20-
# IterationPath = "DevOps Automation"
21-
# TeamProject = "DevOps Automation"
22-
# WorkItemType = "Task"
23-
# State = "To Do"
24-
# Reason = "Added to backlog"
25-
# ParentId = "3"
17+
# Title = "Test Work Item 2" (required)
18+
# WorkItemType = "Task" (required)
19+
# Description = "This is a test work item." (optional)
20+
# AreaPath = "DevOps Automation" (optional)
21+
# IterationPath = "DevOps Automation" (optional)
22+
# TeamProject = "DevOps Automation" (optional)
23+
# State = "To Do" (optional)
24+
# Reason = "Added to backlog" (optional)
25+
# ParentId = "3" (optional)
2626
# }
2727
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
2828
[object[]]
2929
$WorkItem
30-
3130
)
3231

3332
begin {
@@ -38,47 +37,49 @@ function New-AzDoWorkItem {
3837
process {
3938
$WorkItem | ForEach-Object {
4039
$body = @(
41-
@{
42-
op = 'add'
43-
path = '/fields/System.Title'
44-
value = $_.Title
45-
},
46-
@{
47-
op = 'add'
48-
path = '/fields/System.AreaPath'
49-
value = $_.AreaPath
50-
},
51-
@{
52-
op = 'add'
53-
path = '/fields/System.IterationPath'
54-
value = $_.IterationPath
55-
},
56-
@{
57-
op = 'add'
58-
path = '/fields/System.TeamProject'
59-
value = $_.TeamProject
60-
},
61-
@{
62-
op = 'add'
63-
path = '/fields/System.WorkItemType'
64-
value = $_.WorkItemType
65-
},
66-
@{
67-
op = 'add'
68-
path = '/fields/System.State'
69-
value = $_.State
70-
},
71-
@{
72-
op = 'add'
73-
path = '/fields/System.Reason'
74-
value = $_.Reason
75-
},
76-
@{
77-
op = 'add'
78-
path = '/fields/System.Description'
79-
value = $_.Description
80-
}
81-
)
40+
@{
41+
op = 'add'
42+
path = '/fields/System.Title'
43+
value = $_.Title
44+
},
45+
@{
46+
op = 'add'
47+
path = '/fields/System.WorkItemType'
48+
value = $_.WorkItemType
49+
}
50+
)
51+
52+
if ($_.Description) {
53+
$body += @{
54+
op = 'add'
55+
path = '/fields/System.Description'
56+
value = $_.Description
57+
}
58+
}
59+
60+
if ($_.AreaPath) {
61+
$body += @{
62+
op = 'add'
63+
path = '/fields/System.AreaPath'
64+
value = $_.AreaPath
65+
}
66+
}
67+
68+
if ($_.IterationPath) {
69+
$body += @{
70+
op = 'add'
71+
path = '/fields/System.IterationPath'
72+
value = $_.IterationPath
73+
}
74+
}
75+
76+
if ($_.TeamProject) {
77+
$body += @{
78+
op = 'add'
79+
path = '/fields/System.TeamProject'
80+
value = $_.TeamProject
81+
}
82+
}
8283

8384
if ($_.ParentId) {
8485
$body += @{
@@ -102,7 +103,6 @@ function New-AzDoWorkItem {
102103
try {
103104
$result += Invoke-AzDoRestMethod @params
104105
} catch {
105-
Write-Host "$CollectionUri/$ProjectName/_apis/wit/workitems/$ParentId"
106106
Write-AzDoError -message $_.Exception.Message
107107
}
108108
}
@@ -113,9 +113,9 @@ function New-AzDoWorkItem {
113113
if ($result) {
114114
$result | ForEach-Object {
115115
[PSCustomObject]@{
116-
Id = $_.id
116+
Id = $_.id
117117
Name = $_.fields.'System.Title'
118-
Url = $_.url
118+
Url = $_.url
119119
}
120120
}
121121
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
function Remove-AzDoWorkItem {
2+
[CmdletBinding(SupportsShouldProcess)]
3+
param (
4+
# Collection Uri of the organization
5+
[Parameter(Mandatory)]
6+
[ValidateScript({ Validate-CollectionUri -CollectionUri $_ })]
7+
[string]
8+
$CollectionUri,
9+
10+
# Name of the project where the team is located
11+
[Parameter(Mandatory)]
12+
[string]
13+
$ProjectName,
14+
15+
# WorkItem Id to remove
16+
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
17+
[int[]]
18+
$WorkItemId
19+
)
20+
21+
begin {
22+
Write-Verbose "Starting 'Remove-AzDoWorkItem' function."
23+
$Uri = "$CollectionUri/$ProjectName/_apis/wit/workitems/{0}"
24+
}
25+
26+
process {
27+
$params = @{
28+
method = 'DELETE'
29+
version = '7.1-preview.3'
30+
}
31+
32+
foreach ($id in $WorkItemId) {
33+
$id = [string]$id
34+
$params.uri = $Uri -f $id
35+
try {
36+
Invoke-AzDoRestMethod @params
37+
Write-Host "Work item $id deleted successfully."
38+
} catch {
39+
Write-AzdoError -Message $_
40+
}
41+
}
42+
}
43+
44+
end {
45+
Write-Verbose "Ending 'Remove-AzDoWorkItem' function."
46+
}
47+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
function Set-AzDoWorkItem {
2+
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')]
3+
param (
4+
# Collection Uri of the organization
5+
[Parameter(Mandatory)]
6+
[ValidateScript({ Validate-CollectionUri -CollectionUri $_ })]
7+
[string]
8+
$CollectionUri,
9+
10+
# Name of the project where the team is located
11+
[Parameter(Mandatory)]
12+
[string]
13+
$ProjectName,
14+
15+
# Work item object (could be a hashtable or a custom object)
16+
# template: @{
17+
# WorkItemId = 1 (required)
18+
# Title = "Test Work Item 2" (optional)
19+
# Description = "This is a test work item." (optional)
20+
# AreaPath = "DevOps Automation" (optional)
21+
# IterationPath = "DevOps Automation" (optional)
22+
# TeamProject = "DevOps Automation" (optional)
23+
# ParentId = 3 (optional)
24+
# }
25+
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
26+
[object[]]
27+
$WorkItem
28+
)
29+
30+
begin {
31+
Write-Verbose "Starting 'Set-AzDoWorkItem' function."
32+
$result = New-Object System.Collections.Generic.List[System.Object]
33+
}
34+
35+
process {
36+
$WorkItem | ForEach-Object {
37+
$body = @(
38+
@{
39+
op = 'test'
40+
path = '/id'
41+
value = $_.WorkItemId
42+
}
43+
)
44+
45+
if ($_.Title) {
46+
$body += @{
47+
op = 'add'
48+
path = '/fields/System.Title'
49+
value = $_.Title
50+
}
51+
}
52+
53+
if ($_.Description) {
54+
$body += @{
55+
op = 'add'
56+
path = '/fields/System.Description'
57+
value = $_.Description
58+
}
59+
}
60+
61+
if ($_.AreaPath) {
62+
$body += @{
63+
op = 'add'
64+
path = '/fields/System.AreaPath'
65+
value = $_.AreaPath
66+
}
67+
}
68+
69+
if ($_.IterationPath) {
70+
$body += @{
71+
op = 'add'
72+
path = '/fields/System.IterationPath'
73+
value = $_.IterationPath
74+
}
75+
}
76+
77+
if ($_.TeamProject) {
78+
$body += @{
79+
op = 'add'
80+
path = '/fields/System.TeamProject'
81+
value = $_.TeamProject
82+
}
83+
}
84+
85+
if ($_.ParentId) {
86+
$body += @{
87+
op = 'add'
88+
path = '/relations/-'
89+
value = @{
90+
rel = 'System.LinkTypes.Hierarchy-Reverse'
91+
url = "$CollectionUri/$ProjectName/_apis/wit/workitems/$($_.ParentId)"
92+
}
93+
}
94+
}
95+
96+
$params = @{
97+
uri = "$CollectionUri/$ProjectName/_apis/wit/workitems/$($_.WorkItemId)"
98+
method = 'PATCH'
99+
version = '7.1-preview.3'
100+
body = $body
101+
}
102+
103+
if ($PSCmdlet.ShouldProcess($CollectionUri, "Setting work item: $($PSStyle.Bold)$($_.WorkItemId)$($PSStyle.Reset)")) {
104+
try {
105+
$result += Invoke-AzDoRestMethod @params
106+
} catch {
107+
Write-AzDoError -message $_.Exception.Message
108+
}
109+
}
110+
}
111+
}
112+
113+
end {
114+
if ($result) {
115+
$result | ForEach-Object {
116+
[PSCustomObject]@{
117+
Id = $_.id
118+
Name = $_.fields.'System.Title'
119+
Url = $_.url
120+
}
121+
}
122+
}
123+
}
124+
}

0 commit comments

Comments
 (0)