Hosted JIRA open/update ticket action

Doug 1126
Viewed 1

Not really a question as much as sharing a script a customer shared with us that runs an Execute Script (Powershell) action from monitors which updates JIRA tickets with updated information. You'll note they use Custom Properties to control which JIRA user is used.

# Define variables
$jiraDomain = "XXXXXXXXX.atlassian.net"
$jiraUser = $act.CustomProp("JIRAUSERNAME")
$jiraToken = $act.CustomProp("JIRAAPITOKEN")
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("${jiraUser}:${jiraToken}"))

# Get accountId for reporter
$userUrl = "https://$jiraDomain/rest/api/3/user/search?query=$jiraUser"
$userResponse = Invoke-RestMethod -Uri $userUrl -Method Get -Headers @{
    Authorization = "Basic $base64AuthInfo"
    Accept = "application/json"
}
$accountId = $userResponse[0].accountId

# Use MonitorTitle as the key identifier for storage and retrieval
$monitorTitleKey = $act.MonitorTitle
$storedIssueKey = $act.CustomProp($monitorTitleKey)
$issueKey = $null
$issueExists = $false

# Check if the stored issue exists in JIRA
if (-not [string]::IsNullOrWhiteSpace($storedIssueKey)) {
    $checkUrl = "https://$jiraDomain/rest/api/2/issue/$storedIssueKey"
    try {
        $checkResponse = Invoke-RestMethod -Uri $checkUrl -Method Get -Headers @{
            Authorization = "Basic $base64AuthInfo"
            Accept = "application/json"
        }
        $issueExists = $true
        $issueKey = $storedIssueKey
        Write-Host "`n--- Existing JIRA Issue Found: $issueKey ---`n"
    }
    catch {
        Write-Host "`n--- Stored Issue Key Not Found in JIRA: $storedIssueKey ---`n"
    }
}

# Create a new JIRA issue if needed
if (-not $issueExists) {
    $createUrl = "https://$jiraDomain/rest/api/2/issue"
    $issueBody = @{
        fields = @{
            project = @{ key = "RSG" }
            summary = "$($act.Machine) - $($act.MonitorTitle)"
            description = "Monitor triggered at $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')`nDetails:`n$($act.Details)"
            issuetype = @{ name = "Server Monitor Alert" }
            reporter = @{ accountId = "$accountId" }
        }
    } | ConvertTo-Json -Depth 5

    try {
        $createResponse = Invoke-RestMethod -Uri $createUrl -Method Post -Headers @{
            Authorization = "Basic $base64AuthInfo"
            Accept = "application/json"
            "Content-Type" = "application/json"
        } -Body $issueBody

        $issueKey = $createResponse.key
        $act.SetComputerCustomPropByID(0, $monitorTitleKey, $issueKey)
        Write-Host "`n--- Created New JIRA Issue: $issueKey ---`n"
    }
    catch {
        Write-Host "`n--- Error Creating Issue ---`n"
        Write-Host $_.Exception.Message
        if ($_.ErrorDetails) {
            Write-Host $_.ErrorDetails.Message
        }
        return
    }
}

# Add a comment to the issue
$commentUrl = "https://$jiraDomain/rest/api/2/issue/$issueKey/comment"
$commentBody = @{
    body = "Monitor triggered at $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')`nMonitor Title: $($act.MonitorTitle)`nSeconds in Error: $($act.SecondsInError)`nDetails: $($act.Details)"
} | ConvertTo-Json -Depth 3

try {
    $commentResponse = Invoke-RestMethod -Uri $commentUrl -Method Post -Headers @{
        Authorization = "Basic $base64AuthInfo"
        Accept = "application/json"
        "Content-Type" = "application/json"
    } -Body $commentBody
    Write-Host "`n--- Comment Added to $issueKey ---`n"
    $commentResponse | ConvertTo-Json -Depth 10
}
catch {
    Write-Host "`n--- Error Adding Comment ---`n"
    Write-Host $_.Exception.Message
    if ($_.ErrorDetails) {
        Write-Host $_.ErrorDetails.Message
    }
}

# Transition the issue to status ID 2 if it's not already there
$transitionId = "2"

try {
    $statusCheckUrl = "https://$jiraDomain/rest/api/2/issue/$issueKey"
    $statusResponse = Invoke-RestMethod -Uri $statusCheckUrl -Method Get -Headers @{
        Authorization = "Basic $base64AuthInfo"
        Accept = "application/json"
    }

    $currentStatusId = $statusResponse.fields.status.id
    Write-Host "`n--- Current Status ID: $currentStatusId ---`n"

    if ($currentStatusId -ne $transitionId) {
        $transitionUrl = "https://$jiraDomain/rest/api/2/issue/$issueKey/transitions"
        $transitionBody = @{
            transition = @{
                id = $transitionId
            }
        } | ConvertTo-Json -Depth 3

        $transitionResponse = Invoke-RestMethod -Uri $transitionUrl -Method Post -Headers @{
            Authorization = "Basic $base64AuthInfo"
            Accept = "application/json"
            "Content-Type" = "application/json"
        } -Body $transitionBody

        Write-Host "`n--- Issue Transitioned to ID $transitionId ---`n"
        $transitionResponse | ConvertTo-Json -Depth 10
    }
    else {
        Write-Host "`n--- Issue Already in Desired Status (ID $transitionId) ---`n"
    }
}
catch {
    Write-Host "`n--- Error Checking or Transitioning Issue ---`n"
    Write-Host $_.Exception.Message
    if ($_.ErrorDetails) {
        Write-Host $_.ErrorDetails.Message
    }
}
0 Answers