forked from github/spec-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-ado-workitems.ps1
More file actions
508 lines (430 loc) · 17.7 KB
/
create-ado-workitems.ps1
File metadata and controls
508 lines (430 loc) · 17.7 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#!/usr/bin/env pwsh
# Create Azure DevOps work items using Azure CLI with OAuth (no PAT required)
# Requires: Azure CLI with devops extension
param(
[Parameter(Mandatory=$true)]
[string]$SpecFile,
[Parameter(Mandatory=$false)]
[string]$Organization = "",
[Parameter(Mandatory=$false)]
[string]$Project = "",
[Parameter(Mandatory=$false)]
[string]$Stories = "all",
[Parameter(Mandatory=$false)]
[string]$AreaPath = "",
[Parameter(Mandatory=$false)]
[switch]$FromTasks = $false
)
# Check if Azure CLI is installed
if (-not (Get-Command az -ErrorAction SilentlyContinue)) {
Write-Error "Azure CLI not found. Please install from: https://aka.ms/installazurecliwindows"
exit 1
}
# Check if devops extension is installed
$extensions = az extension list --output json | ConvertFrom-Json
if (-not ($extensions | Where-Object { $_.name -eq "azure-devops" })) {
Write-Host "Installing Azure DevOps extension for Azure CLI..."
az extension add --name azure-devops
}
# Check authentication
Write-Host "Checking Azure authentication..."
$account = az account show 2>$null | ConvertFrom-Json
if (-not $account) {
Write-Host "Not authenticated. Running 'az login' with OAuth..."
az login --use-device-code
}
# Validate required parameters
if ([string]::IsNullOrEmpty($Organization)) {
Write-Error "Organization parameter is required. Please provide -Organization parameter."
exit 1
}
if ([string]::IsNullOrEmpty($Project)) {
Write-Error "Project parameter is required. Please provide -Project parameter."
exit 1
}
if ([string]::IsNullOrEmpty($AreaPath)) {
Write-Error "AreaPath parameter is required. Please provide -AreaPath parameter."
exit 1
}
# Save configuration for future reference
$configDir = Join-Path $env:USERPROFILE ".speckit"
$configFile = Join-Path $configDir "ado-config.json"
if (-not (Test-Path $configDir)) {
New-Item -ItemType Directory -Path $configDir -Force | Out-Null
}
$config = @{
Organization = $Organization
Project = $Project
AreaPath = $AreaPath
LastUpdated = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
}
$config | ConvertTo-Json | Set-Content $configFile
Write-Host "Using Azure DevOps configuration:" -ForegroundColor Cyan
Write-Host " Organization: $Organization" -ForegroundColor Yellow
Write-Host " Project: $Project" -ForegroundColor Yellow
Write-Host " Area Path: $AreaPath" -ForegroundColor Yellow
Write-Host ""
# Set defaults for Azure CLI
az devops configure --defaults organization="https://dev.azure.com/$Organization" project="$Project"
# Parse user stories from spec.md
function Get-UserStories {
param([string]$FilePath)
if (-not (Test-Path $FilePath)) {
Write-Error "Spec file not found: $FilePath"
exit 1
}
$content = Get-Content -Path $FilePath -Raw
$parsedStories = [System.Collections.ArrayList]::new()
# Match: ### User Story X - Title (Priority: PX)
$pattern = '###\s+User\s+Story\s+(\d+)\s*-\s*([^\(]+)\s*\(Priority:\s*P(\d+)\)'
$regexMatches = [regex]::Matches($content, $pattern)
foreach ($match in $regexMatches) {
$storyNum = $match.Groups[1].Value
$title = $match.Groups[2].Value.Trim()
$priority = $match.Groups[3].Value
# Extract story content (everything until next ### or ## section)
$startPos = $match.Index
$nextStoryPattern = '###\s+User\s+Story\s+\d+'
$nextMatch = [regex]::Match($content.Substring($startPos + 1), $nextStoryPattern)
if ($nextMatch.Success) {
$endPos = $startPos + $nextMatch.Index + 1
$storyContent = $content.Substring($startPos, $endPos - $startPos)
} else {
# Find next ## level section (Edge Cases, Requirements, etc.)
$endMatch = [regex]::Match($content.Substring($startPos), '\n##\s+(Edge Cases|Requirements|Success Criteria|Assumptions|Out of Scope)')
if ($endMatch.Success) {
$storyContent = $content.Substring($startPos, $endMatch.Index)
} else {
$storyContent = $content.Substring($startPos)
}
}
# Extract sections
$description = ""
if ($storyContent -match '(?s)Priority: P\d+\)\s*\n\s*\n(.+?)(?=\*\*Why this priority|###|##\s+|$)') {
$description = $Matches[1].Trim()
}
$whyPriority = ""
if ($storyContent -match '\*\*Why this priority\*\*:\s*(.+?)(?=\n\n|\*\*Independent Test|###|$)') {
$whyPriority = $Matches[1].Trim()
}
$independentTest = ""
if ($storyContent -match '\*\*Independent Test\*\*:\s*(.+?)(?=\n\n|\*\*Acceptance|###|$)') {
$independentTest = $Matches[1].Trim()
}
$acceptanceCriteria = ""
if ($storyContent -match '(?s)\*\*Acceptance Scenarios\*\*:\s*\n\s*\n(.+?)(?=###|##\s+Edge Cases|##\s+Requirements|$)') {
$acceptanceCriteria = $Matches[1].Trim()
}
[void]$parsedStories.Add([PSCustomObject]@{
Number = $storyNum
Title = $title
Priority = $priority
Description = $description
Why = $whyPriority
Test = $independentTest
Acceptance = $acceptanceCriteria
})
}
return ,$parsedStories
}
# Parse tasks from tasks.md file
function Get-Tasks {
param([string]$FilePath)
if (-not (Test-Path $FilePath)) {
Write-Error "Tasks file not found: $FilePath"
exit 1
}
$content = Get-Content -Path $FilePath -Raw
$parsedTasks = [System.Collections.ArrayList]::new()
# Match: - [ ] TXXX [P] [Story] Description
$pattern = '-\s*\[\s*\]\s+T(\d+)\s+(?:\[P\]\s+)?(?:\[([^\]]+)\]\s+)?(.+)'
$regexMatches = [regex]::Matches($content, $pattern)
Write-Verbose "Found $($regexMatches.Count) task matches in tasks file"
foreach ($match in $regexMatches) {
$taskNum = $match.Groups[1].Value
$story = $match.Groups[2].Value.Trim()
$description = $match.Groups[3].Value.Trim()
# Default priority to 2 (medium) for tasks
$priority = 2
# If story tag exists, extract priority (US1=P1, etc.)
if ($story -match 'US(\d+)') {
$priority = [int]$Matches[1]
if ($priority -gt 4) { $priority = 4 }
}
# Title as task number + description (truncate if too long)
$title = "T$taskNum - $description"
if ($title.Length -gt 100) {
$title = $title.Substring(0, 97) + "..."
}
# Full description includes story tag
$fullDescription = $description
if (-not [string]::IsNullOrEmpty($story)) {
$fullDescription = "[$story] $description"
}
[void]$parsedTasks.Add([PSCustomObject]@{
Number = $taskNum
Title = $title
Priority = $priority
Description = $fullDescription
Story = $story
})
}
return ,$parsedTasks
}
# Filter stories based on selection
function Get-SelectedStories {
param([array]$AllStories, [string]$Selection)
if ($Selection -eq "all" -or [string]::IsNullOrEmpty($Selection)) {
return $AllStories
}
$selectedNumbers = @()
$parts = $Selection -split ','
foreach ($part in $parts) {
$part = $part.Trim()
if ($part -match '^(\d+)-(\d+)$') {
$start = [int]$Matches[1]
$end = [int]$Matches[2]
$selectedNumbers += $start..$end
}
elseif ($part -match '^\d+$') {
$selectedNumbers += [int]$part
}
}
return $AllStories | Where-Object { $selectedNumbers -contains [int]$_.Number }
}
Write-Host ""
Write-Host "=============================================="
if ($FromTasks) {
Write-Host "Azure DevOps Work Items from Tasks"
} else {
Write-Host "Azure DevOps Work Item Creation (OAuth)"
}
Write-Host "=============================================="
Write-Host "Organization: $Organization"
Write-Host "Project: $Project"
Write-Host "File: $SpecFile"
Write-Host ""
$featureName = Split-Path (Split-Path $SpecFile -Parent) -Leaf
# Parse and filter items (tasks or stories)
if ($FromTasks) {
$allStories = Get-Tasks -FilePath $SpecFile
$itemType = "Task"
$itemLabel = "tasks"
} else {
$allStories = Get-UserStories -FilePath $SpecFile
$itemType = "User Story"
$itemLabel = "user stories"
}
$selectedStories = Get-SelectedStories -AllStories $allStories -Selection $Stories
Write-Host "Found $($allStories.Count) $itemLabel"
Write-Host "Syncing $($selectedStories.Count) $itemLabel"
Write-Host ""
# Show preview of items to be created
Write-Host "Items to be created:" -ForegroundColor Cyan
Write-Host ""
foreach ($story in $selectedStories) {
Write-Host " [$($story.Number)] P$($story.Priority) - $($story.Title)" -ForegroundColor Yellow
if (-not $FromTasks) {
$desc = $story.Description.Substring(0, [Math]::Min(80, $story.Description.Length))
if ($story.Description.Length -gt 80) { $desc += "..." }
Write-Host " $desc" -ForegroundColor Gray
} else {
Write-Host " Story: $($story.Story)" -ForegroundColor Gray
}
}
Write-Host ""
$createdItems = @()
# Load parent user story mapping for tasks
$parentMapping = @{}
if ($FromTasks) {
$mappingFile = Join-Path (Split-Path $SpecFile -Parent) ".speckit\azure-devops-mapping.json"
if (Test-Path $mappingFile) {
$mapping = Get-Content $mappingFile -Raw | ConvertFrom-Json
foreach ($item in $mapping.workItems) {
# Map story number to work item ID (e.g., "1" -> workItemId)
if ($item.StoryNumber -match '^\d+$') {
$parentMapping[$item.StoryNumber] = $item.WorkItemId
}
}
Write-Host "Loaded parent user story mappings: $($parentMapping.Count) stories" -ForegroundColor Green
Write-Host ""
} else {
Write-Host "Warning: No user story mapping found. Tasks will be created without parent links." -ForegroundColor Yellow
Write-Host "Run the script on spec.md first to create user stories, then create tasks." -ForegroundColor Yellow
Write-Host ""
}
}
foreach ($story in $selectedStories) {
if ($FromTasks) {
$workItemTitle = $story.Title
$fullDescription = $story.Description
$tags = "spec-kit;$featureName;task"
if ($story.Story) {
$tags += ";$($story.Story)"
}
Write-Host "Creating Task $($story.Number): $($story.Description.Substring(0, [Math]::Min(60, $story.Description.Length)))..."
} else {
$workItemTitle = "User Story $($story.Number) - $($story.Title)"
$fullDescription = $story.Description
if ($story.Why) {
$fullDescription += "`n`n**Why this priority**: $($story.Why)"
}
if ($story.Test) {
$fullDescription += "`n`n**Independent Test**: $($story.Test)"
}
$tags = "spec-kit;$featureName;user-story"
Write-Host "Creating User Story $($story.Number): $($story.Title)..."
}
# Create work item using Azure CLI
try {
# Escape special characters in field values
# For title: escape quotes by doubling them for Azure CLI
$cleanTitle = $workItemTitle -replace '"', '""'
$cleanDesc = $fullDescription -replace '"', '\"' -replace '\r?\n', ' '
# Build field arguments
$fieldArgs = @(
"System.Description=$cleanDesc"
"Microsoft.VSTS.Common.Priority=$($story.Priority)"
"System.Tags=$tags"
"System.AssignedTo=" # Explicitly leave unassigned
)
# Add Original Estimate for Tasks (required field in Azure DevOps)
if ($FromTasks) {
$fieldArgs += "Microsoft.VSTS.Scheduling.OriginalEstimate=0"
}
# Add acceptance criteria only for user stories
if (-not $FromTasks -and $story.Acceptance) {
$cleanAcceptance = $story.Acceptance -replace '"', '\"' -replace '\r?\n', ' '
$fieldArgs += "Microsoft.VSTS.Common.AcceptanceCriteria=$cleanAcceptance"
}
if ($AreaPath) {
$fieldArgs += "System.AreaPath=$AreaPath"
}
# Build complete command arguments array
$azArgs = @(
'boards', 'work-item', 'create'
'--type', $itemType
'--title', $cleanTitle
'--organization', "https://dev.azure.com/$Organization"
'--project', $Project
'--fields'
) + $fieldArgs + @('--output', 'json')
Write-Verbose "Total args: $($azArgs.Count)"
Write-Verbose "Args: $($azArgs -join ' | ')"
# Execute command
$result = & az @azArgs 2>&1
$resultString = $result | Out-String
if ($LASTEXITCODE -eq 0 -and $resultString -notmatch "ERROR") {
try {
$workItem = $resultString | ConvertFrom-Json
} catch {
Write-Host " [FAIL] Failed to parse response"
Write-Host " Error: $_"
Write-Host ""
continue
}
$workItemId = $workItem.id
$workItemUrl = "https://dev.azure.com/$Organization/$Project/_workitems/edit/$workItemId"
Write-Host " [OK] Created work item #$workItemId"
Write-Host " -> $workItemUrl"
Write-Host ""
$createdItems += [PSCustomObject]@{
StoryNumber = $story.Number
Title = $story.Title
Priority = "P$($story.Priority)"
WorkItemId = $workItemId
WorkItemUrl = $workItemUrl
ParentStoryNumber = if ($FromTasks) { $story.Story } else { $null }
Status = "Created"
}
} else {
Write-Host " [FAIL] Failed to create work item"
Write-Host " Error: $resultString"
Write-Host ""
}
}
catch {
Write-Host " [ERROR] Error: $_"
Write-Host ""
}
}
# Display summary
if ($createdItems.Count -gt 0) {
Write-Host ""
Write-Host "=============================================="
Write-Host "[SUCCESS] Azure DevOps Sync Complete"
Write-Host "=============================================="
Write-Host ""
Write-Host "Organization: $Organization"
Write-Host "Project: $Project"
Write-Host "Feature: $featureName"
$selectionLabel = if ($FromTasks) { "tasks" } else { "user stories" }
$selectedCount = if ($null -ne $selectedStories) { $selectedStories.Count } else { 0 }
Write-Host "Created: $($createdItems.Count) of $selectedCount $selectionLabel"
Write-Host ""
Write-Host "Created Work Items:"
Write-Host ""
foreach ($item in $createdItems) {
Write-Host " [$($item.StoryNumber)] $($item.Title) ($($item.Priority))"
Write-Host " Work Item: #$($item.WorkItemId)"
Write-Host " Link: $($item.WorkItemUrl)"
Write-Host ""
}
Write-Host "View in Azure DevOps:"
Write-Host " Boards: https://dev.azure.com/$Organization/$Project/_boards"
Write-Host " Work Items: https://dev.azure.com/$Organization/$Project/_workitems"
Write-Host ""
# Link tasks to parent user stories if FromTasks mode
if ($FromTasks -and $parentMapping.Count -gt 0) {
Write-Host "Linking tasks to parent user stories..." -ForegroundColor Cyan
Write-Host ""
foreach ($item in $createdItems) {
if ($item.ParentStoryNumber) {
# Extract story number from "US1" format
$storyNum = $null
if ($item.ParentStoryNumber -match 'US(\d+)') {
$storyNum = $Matches[1]
} elseif ($item.ParentStoryNumber -match '^\d+$') {
$storyNum = $item.ParentStoryNumber
}
if ($storyNum -and $parentMapping.ContainsKey($storyNum)) {
$parentId = $parentMapping[$storyNum]
Write-Host " Linking Task #$($item.WorkItemId) -> User Story #$parentId..." -NoNewline
$linkArgs = @(
'boards', 'work-item', 'relation', 'add'
'--id', $item.WorkItemId
'--relation-type', 'Parent'
'--target-id', $parentId
'--organization', "https://dev.azure.com/$Organization"
'--output', 'json'
)
$linkResult = & az @linkArgs 2>&1 | Out-String
if ($LASTEXITCODE -eq 0) {
Write-Host " [OK]" -ForegroundColor Green
} else {
Write-Host " [FAIL]" -ForegroundColor Yellow
Write-Host " Error: $linkResult" -ForegroundColor Gray
}
}
}
}
Write-Host ""
}
Write-Host ""
# Save mapping
$mappingDir = Join-Path (Split-Path $SpecFile -Parent) ".speckit"
if (-not (Test-Path $mappingDir)) {
New-Item -ItemType Directory -Path $mappingDir -Force | Out-Null
}
$mappingFile = Join-Path $mappingDir "azure-devops-mapping.json"
$mapping = @{
feature = $featureName
organization = $Organization
project = $Project
syncDate = Get-Date -Format "o"
workItems = $createdItems
}
$mapping | ConvertTo-Json -Depth 10 | Out-File -FilePath $mappingFile -Encoding UTF8
Write-Host "Mapping saved: $mappingFile"
Write-Host ""
}