Jira Integration

The LEAPWORK public REST API makes it very easy for DevOps, IT operations, and developers to integrate LEAPWORK with any third-party system. This article will instruct you how to automatically create bugs in JIRA when automation flows in LEAPWORK fails.

The following is a Windows Powershell script that runs a pre-defined LEAPWORK schedule, polls for the results until they are available, and then loops through all failed cases and creates bugs in JIRA as appropriate.

Please note that the script contains no error handling or logging mechanisms. It is meant only to demonstrate the core functionality of integrating LEAPWORK with JIRA.

Download sample PowerShell script.


# LEAPWORK REST API example: Run a schedule, iterate through the results and create bugs in JIRA.# 
#
# Author: Claus Topholt.


# Function that finds a schedule in LEAPWORK based on a title, runs it and polls for the results.
function RunScheduleAndGetResults($schedule)
{
    Write-Host "Getting id for schedule '$schedule'."

    # Get the id of the schedule.
    $runScheduleId = "";
    $headers = @{}
    $headers.Add("AccessKey","bTyGAd0UGL70JFQg")
    $runSchedules = Invoke-WebRequest -ContentType "application/json" -Headers $headers "http://localhost:9001/api/v3/schedules" | ConvertFrom-Json
    foreach($runScheduleItem in $runSchedules)
    {
        if ($runScheduleItem.title -eq $schedule) { $runScheduleId = $runScheduleItem.id }
    }
    if ($runScheduleId -eq "") { throw "Could not find schedule '$schedule'." }

    Write-Host "Running the schedule."

    # Run the schedule now.
    $timestamp = [DateTime]::UtcNow.ToString("ddMMyyyy HHmmss")
    Start-Sleep 1
    $runNow = Invoke-WebRequest -Method PUT -ContentType "application/json" -Headers $headers "http://localhost:9001/api/v3/schedules/$runScheduleId/runNow"
    if ($runNow.StatusCode -ne 200) { throw "Could not run schedule." }
    $runNowResponse=$runNow.Content | ConvertFrom-Json
    $runId=$runNowResponse.RunId
    # Get the result, keep polling every 5 seconds until a new result is returned.
    do
    {
        Start-Sleep -Seconds 5

        Write-Host "Polling for run results."

        $runResult = Invoke-WebRequest -ContentType "application/json" -Headers $headers "http://localhost:9001/api/v3/run/$runId" | ConvertFrom-Json         

    } 
    while ($runResult.Status -ne 'Finished')

    Write-Host "Results received."

    return $runResult
}


# Function that creates a bug with a specific title in JIRA.
function CreateBug($bugTitle, $bugDescription)
{
    # Create an authorization header using basic auth.
    $lp = "USERNAME:PASSWORD"
    $lpEncoded = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes($lp));
    $header = @{"Authorization" = "Basic $lpEncoded"; "Content-Type" = "application/json"}

    # Create json issue.
    $body = '{ "fields": { "project": { "key": "TEST0" }, "summary": "' + $bugTitle + '", "description": "' + $bugDescription + '", "issuetype": { "name": "Bug" } } } '

    # Create bug.
    Invoke-RestMethod -Headers $header -Method Post -Body $body "https://YOURINSTANCE.atlassian.net/rest/api/2/issue/"
}


# Run the LEAPWORK schedule "My Test Schedule" and get the results.
$runResult = RunScheduleAndGetResults("My Test Schedule")

# If there are any failed cases in the results, iterate through them.
if ($runResult.Failed -gt 0)
{
    Write-Host "Found $($runResult.Failed) failed case(s)."
    $headers = @{}
    $headers.Add("AccessKey","bTyGAd0UGL70JFQg")
    $runId=$runResult.RunId
    $runItemIds = Invoke-WebRequest -ContentType "application/json" -Headers $headers http://localhost:9001/api/v3/run/$runId/runItemIds | ConvertFrom-Json 

    $rootPath =$runResult.RunFolderPath
    foreach ($runItemId in $runItemIds.RunItemIds)
    {   
   
    $runItems = Invoke-WebRequest -ContentType "application/json" -Headers $headers http://localhost:9001/api/v3/runItems/$runItemId | ConvertFrom-Json 

        if($runItems.FlowInfo.Status -eq 'Failed')
        {

         # Create a title for the bug.
            $bugTitle = "LEAPWORK: " + $runItems.FlowInfo.FlowTitle

            # Create a description that contains the log messages.
            $newline = "\r\n";
           
            $keyFrames = Invoke-WebRequest -ContentType "application/json" -Headers $headers http://localhost:9001/api/v3/runItems/$runItemId/keyframes/1 | ConvertFrom-Json 

            $bugDescription = "Log from LEAPWORK:$newline $newline"
            foreach ($keyframe in $keyFrames)
            {
                if ($keyframe.Level -ge 1)
                {
                    $keyframeTimestamp = get-date($keyframe.Timestamp.LocalDateTime) -Format "dd-MM-yyyy HH:mm:ss"
                    $bugDescription += "$keyframeTimestamp - $($keyframe.LogMessage) $newline"
                }
            }

            # Add path to video and screenshots.
            $mediaPath = Join-Path -Path $rootPath  "$($runItems.RunItemId)"
            $videoPath = Join-Path -Path $mediaPath  "$($runItems.RunItemId).avi"
            $videoPath = $videoPath.Replace('\', '\\')
            $screenshotsPath = Join-Path -Path $mediaPath "Screenshots"
            $screenshotsPath = $screenshotsPath.Replace('\', '\\')
            $bugDescription += "$newline Video: $videoPath $newline"
            $bugDescription += "$newline Screenshots (if any): $screenshotsPath $newline"

            # Create or update bug in JIRA.
            CreateBug $bugTitle $bugDescription
        }
    }

}
else
{
    Write-Host "No failed cases found."
}

After running, cases will be created in JIRA and can be managed from the web interface:

jra

The above script uses basic authentication to authorize access to JIRA. For information about how to use the JIRA REST API, please see the links:

You can explore the endpoints mentioned above by going to this url: http://localhost:9001/help/index if you have the Controller installed on your own computer.

Explore the full documentation of the LEAPWORK REST API.

If you have any questions, please contact priority support on prioritysupport@leapwork.com.