Last active
October 16, 2017 08:41
-
-
Save lipkau/a9846f014af6a92f050fe39e18ab5869 to your computer and use it in GitHub Desktop.
[AtlassianPS] [JiraPS] Upload changes to Jira Issues
This file contains 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
[{ | |
"key": "TV-24", | |
"fields": { | |
"summary": "My new Summary" | |
} | |
}] |
This file contains 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( | |
# Path to the JSON file with the information to upload. | |
[Parameter(Mandatory = $true)] | |
[ValidateScript( { Test-Path $_ })] | |
[string]$InputFile, | |
# Credentials to use for the connection with Jira. | |
[Parameter(Mandatory = $true)] | |
[PSCredential]$Credential, | |
# Server to connect to. | |
# Edit the default value or overwrite it when calling the script. | |
[Parameter()] | |
[ValidateNotNullOrEmpty()] | |
$ApiURi = 'https://myserver.com' | |
) | |
# region Functions | |
function ConvertTo-HashTable { | |
param( | |
[Parameter(Mandatory = $true)] | |
[Object]$InputObject, | |
[switch]$Recurse | |
) | |
begin { | |
$hash = @{} | |
$InputObject.PSObject.properties | Foreach-Object { | |
$value = $_.Value | |
if ($Recurse -and $value.gettype().Name -eq "PSCustomObject") { $value = ConvertTo-HashTable $value} | |
$hash[$_.Name] = $value | |
} | |
Write-Output $hash | |
} | |
} | |
# endregion Functions | |
# region Setup | |
Import-Module JiraPS -Force | |
Set-JiraConfigServer $ApiURi | |
$null = New-JiraSession -Credential $Credential | |
# Write log to: | |
$logFile = "$PSScriptRoot\Upload_$(Get-Date -format "yyyyMMdd.HHmmss").log" | |
$importFile = Get-Content -Path $InputFile -Raw | |
Write-Output "Uploading File Content . . . [File] : $InputFile" | |
Out-File -FilePath $logFile -Force -InputObject @" | |
Uploading content of: $InputFile | |
--------------------------------- | |
"@ | |
$changedIssues = ConvertFrom-Json $importFile | |
# endregion Setup | |
# region Main | |
$i =0 | |
$failures = @() | |
foreach ($change in $changedIssues) { | |
Write-Progress -Activity "Uploading changes" -status "Processing $($change.key)" -percentComplete ($i / $changedIssues.count * 100) | |
$i++ | |
"Processing: $($change.key) " | Out-File -FilePath $logFile -Append -NoNewLine | |
try { | |
$Issue = Get-JiraIssue $change.key -ErrorAction Stop | |
Set-JiraIssue $Issue -Fields (ConvertTo-HashTable $change.fields) -ErrorAction Stop | |
"[OK]" | Out-File -FilePath $logFile -Append | |
} | |
catch { | |
$failures += $change | |
"[FAILED]" | Out-File -FilePath $logFile -Append | |
} | |
} | |
# endregion Main | |
# region Evaluate-Result | |
Write-Host "" | |
If ($failures.Count -gt 0) { | |
Write-Warning "$($failures.Count) issues failed to be updated. Check `$failures ." | |
} | |
Write-Host "Script finished!" | |
# endregion Evaluate-Result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment