91 lines
2.8 KiB
PowerShell
91 lines
2.8 KiB
PowerShell
|
function Add-JiraComment {
|
||
|
<#
|
||
|
.SYNOPSIS
|
||
|
Add the specified comment body to the specified Jira ticket
|
||
|
|
||
|
.PARAMETER TicketNumber
|
||
|
The Jira ticket number
|
||
|
|
||
|
.PARAMETER Message
|
||
|
The content of the message
|
||
|
|
||
|
.PARAMETER Credential
|
||
|
Optional. Credentials to talk to Jira. Expected to be stored in a user-environment-variable for the default case.
|
||
|
#>
|
||
|
[CmdletBinding()]
|
||
|
[OutputType([void])]
|
||
|
param (
|
||
|
[Parameter(Mandatory = $true, Position = 0)]
|
||
|
[string]$TicketNumber,
|
||
|
[Parameter(Mandatory = $true, Position = 1)]
|
||
|
[string]$Message,
|
||
|
[Parameter(Mandatory = $false, Position = 2)]
|
||
|
[PSCredential]$Credential
|
||
|
)
|
||
|
|
||
|
$logLead = (Get-LogLeadName)
|
||
|
|
||
|
if ($null -eq $Credential) {
|
||
|
$Credential = (Get-CredentialFromEnvironmentVariables)
|
||
|
}
|
||
|
|
||
|
if ($null -eq $Credential) {
|
||
|
Write-Error "$logLead : Can not talk to Jira without credentials. Returning."
|
||
|
return
|
||
|
}
|
||
|
|
||
|
$headers = (Get-BasicAuthWebHeader -Credential $Credential)
|
||
|
$headers["Content-Type"] = "application/json"
|
||
|
|
||
|
$url = (Get-JiraBaseUrl)
|
||
|
# Use this URL to ensure the ticket number as provided exists
|
||
|
$jiraUrlTicket = (Join-UrlComponents -BaseUrl $url -Path "/rest/api/latest/issue/$TicketNumber")
|
||
|
|
||
|
$arguments = @{
|
||
|
UseBasicParsing = $true
|
||
|
Headers = $headers
|
||
|
Uri = $jiraUrlTicket
|
||
|
Method = 'Get'
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
$response = Invoke-RestMethod @arguments
|
||
|
if (![string]::IsNullOrWhiteSpace($response.key)) {
|
||
|
# In case the ticket has been moved, let's goto the right value
|
||
|
$TicketNumber = $response.key
|
||
|
}
|
||
|
} catch {
|
||
|
Write-Host (Get-LastWebRequestErrorText)
|
||
|
Write-ErrorObject -ErrorItem $PSItem
|
||
|
Write-Error "$logLead : Could not verify ticket. Ensure proper credentials and try again, or verify the ticket number is correct."
|
||
|
return
|
||
|
}
|
||
|
|
||
|
# Use this URL to post the comment to the body
|
||
|
$jiraUrlComment = (Join-UrlComponents -BaseUrl $url -Path "/rest/api/latest/issue/$TicketNumber/comment")
|
||
|
|
||
|
$body = @{
|
||
|
body = $Message
|
||
|
}
|
||
|
|
||
|
$arguments = @{
|
||
|
UseBasicParsing = $true
|
||
|
Headers = $headers
|
||
|
Uri = $jiraUrlComment
|
||
|
Body = (ConvertTo-Json $body -Depth 10)
|
||
|
Method = 'Post'
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
$response = (Invoke-RestMethod @arguments)
|
||
|
if (![string]::IsNullOrWhiteSpace($response.key)) {
|
||
|
# In case the ticket has been moved, let's goto the right value
|
||
|
$TicketNumber = $response.key
|
||
|
}
|
||
|
} catch {
|
||
|
Write-Host (Get-LastWebRequestErrorText)
|
||
|
Write-ErrorObject -ErrorItem $PSItem
|
||
|
Write-Error "$logLead : Could not add comment to ticket. Ensure proper credentials and try again, or verify the ticket number is correct."
|
||
|
return
|
||
|
}
|
||
|
}
|