ps/Modules/Cole.PowerShell.Developer/Public/New-JiraTicket.ps1
2023-05-30 22:51:22 -07:00

95 lines
2.8 KiB
PowerShell

function New-JiraTicket {
<#
.SYNOPSIS
Create a new Jira ticket (issue)
.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)]
[string]$Project,
[Parameter(Mandatory = $true)]
[string]$Type,
[Parameter(Mandatory = $true)]
[string]$Message,
[Parameter(Mandatory = $false)]
[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")
$arguments = @{
UseBasicParsing = $true
Headers = $headers
Uri = $jiraUrlTicket
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 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")
$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
}
}
Set-Alias -Name New-JiraIssue -Value New-JiraTicket