Created
April 30, 2024 17:37
-
-
Save tkapin/c470796ec8cc0772e2ba967f56544aaa 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
param( | |
[Parameter(Mandatory = $true)] | |
[string] $InputCsvPath, | |
[switch] $DoExecute | |
) | |
Set-StrictMode -version 3.0 | |
Function ExecuteCommand([System.Management.Automation.ScriptBlock] $sb) { | |
$sbExpanded = $ExecutionContext.InvokeCommand.ExpandString($sb).Trim() | |
Write-Debug "Executing '$sbExpanded' ..." | |
& $sb | |
if ($LASTEXITCODE -ne 0) { | |
throw "Failed to execute '$sbExpanded', exit code $LASTEXITCODE" | |
} | |
Write-Debug "Successfully executed '$sbExpanded'" | |
} | |
Function GetFiedId($fields, $name) { | |
return ($fields.fields | Where-Object { $_.name -eq $name }).id | |
} | |
Function SetGitHubProjectItemSingleSelectOptionField($itemId, $projectId, $fieldId, $optionId) { | |
ExecuteCommand {gh project item-edit --id $itemId --project-id $projectId --field-id $fieldId --single-select-option-id $optionId} | |
} | |
Function GetFieldValues($fields, $name) { | |
$areas = @{} | |
($fields.fields | Where-Object { $_.name -eq $name }).options | ForEach-Object { $areas.Add($_.name, $_.id) } | |
return $areas | |
} | |
try { | |
# hard code project constants for the board (could be also automated and cached) | |
# gh project list --owner dotnet -L 1000 --format json | |
$projectNumber = -1 | |
$projectId = "<FILL_IN_PROJECT_ID>" | |
# get project board fields | |
$fields = ExecuteCommand({gh project field-list $projectNumber --owner dotnet --format json}) | ConvertFrom-Json | |
$areaId = GetFiedId $fields "Area" | |
$areas = GetFieldValues $fields "Area" | |
$sizeId = GetFiedId $fields "Size" | |
$sizes = GetFieldValues $fields "Size" | |
$items = ExecuteCommand({gh project item-list $projectNumber --owner dotnet -L 10000 --format json}) | ConvertFrom-Json | |
foreach ($line in Get-Content $InputCsvPath) { | |
$line = $line.Trim() | |
if ($line -eq "") { | |
continue | |
} | |
$columns = $line.Split(",") | |
$issueUrl = $columns[0].Trim() | |
$area = $columns[1].Trim() | |
$size = $columns[2].Trim() | |
$item = $items.items | Where-Object { $_.content.url -eq $issueUrl } | |
if ($null -eq $item) { | |
throw "Failed to find board item for '$issueUrl'" | |
} | |
$itemId = $item.id | |
$itemTitle = $item.content.title | |
Write-Host "Setting '$area' and '$size' on '$itemTitle' ($issueUrl) ..." | |
if ($DoExecute) { | |
SetGitHubProjectItemSingleSelectOptionField $itemId $projectId $areaId $areas[$area] | |
SetGitHubProjectItemSingleSelectOptionField $itemId $projectId $sizeId $sizes[$size] | |
} | |
} | |
} | |
catch [Exception] { | |
Write-Error "Error: $($_.Exception.Message)" | |
exit 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment