Create Issues based on data retrieved from External Systems

Overview

This script makes an external system call and creates a new issue in a selected project, based on information returned.

Example

I am in charge of the company on-boarding Jira project. Every time a new employee is added to our external HR system, I would like Jira issues to be created based on their department. I can use this script to connect to that systems REST API, retrieve the required data, and create issues automatically, saving me the time and effort of manually creating issues.

Good to Know

  • External task type can be defined as a new type to identify what issues has been created by the external system.
  • External system can be customised with the requirements needed. In this example, external system specifications are:
    • No authentication is required. If authentication is needed, can be added in the endpoint call thanks to Unirest HTTP Library.
    • A query parameter named as "timestamp" is needed to specify search date.
    • Result obtained is a JSON objects list, where each object has a property named as "name" and "message". Based on this information, the new issues can be created in Jira.

Requirements

Jira Jira

import java.time.LocalDateTime
import java.time.ZoneOffset

final projectKey = 'TEST'
//External task type name
final externalIssueTypeName = 'External Task'
//External system URL
final externalUrl = 'https://external.system'

//We can compute the time between epoch and one hour before now in order to obtain issues from external by this time.
def timestamp = LocalDateTime.now()
    .minusHours(1)
    .toInstant(ZoneOffset.UTC).toEpochMilli()

def taskType = get('/rest/api/2/issuetype')
    .asObject(List)
    .body
    .find { it['name'] == externalIssueTypeName } ['id']

//We can define here the endpoint to get issues from the external system and the query strings that are needed.
//In this example, we use "since" as a query string parameter where is defined the time by which we want to search.
def results = get("$externalUrl/rest/api/1/results")
    .queryString('since', timestamp)
    .asObject(List)
    .body

//For every result obtained, a new issue is created.
results.each { Map result ->
    post('/rest/api/2/issue')
        .header('Content-Type', 'application/json')
        .body([
            fields: [
                summary    : result.name,
                description: result.message,
                project    : [
                    key: projectKey
                ],
                issuetype  : [
                    id: taskType
                ]
            ]
        ])
        .asString().body
}
Discovered an issue? Report it here

Suggested for you