Automate Jira Issue Creation with AtlasCLI for Jira

The Starware
4 min readMay 12, 2023

Written by Faik Burak Türedi

Sometimes you need to create lots of similar issues using programmatically. Here, we will create periodic Jira issues but you can perform lots of other automation tasks using AtlasCLI for Jira.

Sample command from AtlasCLI for Jira

Prerequisites

  1. Ensure you have the AtlasCLI for Jira installed on your system.
  2. Make sure you have a working Jira instance and proper credentials to create issues in the desired project.

Create Issue Template

Create a JSON template file named `cron_issue_template.json` compatible with the Jira REST API to create an issue on Jira. We use the `$` notation for variables in the template to make it easier to replace them with actual values using tools like awk and sed. This allows for different issues to be created with varying content, such as automatically adding the iteration number, the current month, or week to the summary:

{
"fields": {
"project": {
"key": "$TEST_PKEY"
},
"issuetype": {
"name": "Task"
},
"description": {
"type": "doc",
"version": 1,
"content": [
{
"type": "paragraph",
"content": [
{
"text": "$DESCRIPTION",
"type": "text"
}
]
}
]
},
"summary": "$SUMMARY",
"priority": {
"id": "3"
},
"customfield_12345": {
"value": "$CUSTOM_FIELD_VALUE"
}
}
}

Please note that, custom field values can be different than as shown above. You might need to take a look at the Jira REST API.

Using AtlasCLI to Create an Issue

If you have valid json file to create an issue, you can can use it as follows with AtlasCLI;

jira issue create < your_json_file.json

Creating a Script for Automation

jira issue create” command can only create a single issue, but we can use all the power of your shell to invoke it in any frequency you want. Lets prepare a script file and trigger it from CRON.

To create a cron job, we need to write a bash script. Here is the steps to create a bash script for a cron job.

  1. Create a new file named `your_cron_issue.sh` and open it in a text editor.
  2. Add the following content to the script, replacing `YOUR_PROJECT_KEY`, `YOUR_DESCRIPTION`, and `YOUR_SUMMARY` with your desired values:
#!/bin/bash
# Load environment variables
. $HOME/.zshrc
# Define variables
test_pkey="YOUR_PROJECT_KEY"
description="YOUR_DESCRIPTION"
custom_field_value="YOUR_CUSTOM_FIELD_VALUE"
input_file="./cron_issue_template.json"
output_file="./last_crontab_issue_template.json"

WEEK_NUMBER=$(date +'%U')
YEAR=$(date +'%Y')
summary="Weekly status planning for week $WEEK_NUMBER of $YEAR"

# Update the JSON template
awk -v test_pkey="$test_pkey" -v description="$description" -v summary="$summary" -v custom_field_value="$custom_field_value" '{gsub(/\$TEST_PKEY/,test_pkey); gsub(/\$DESCRIPTION/,description); gsub(/\$CUSTOM_FIELD_VALUE/,custom_field_value); gsub(/\$SUMMARY/,summary); print}' "$input_file" > "$output_file"
# Create the Jira issue
jira issue create < "$output_file"

3. Make the script using “chmod +x your_cron_issue.sh

Scheduling with CRON for Periodic Issue Creation

  1. Type `crontab -e` to open your crontab file for editing.
  2. Add the following line to the end of the file (change the path to match the location of the `your_cron_issue.sh` file on your system):
*/5 * * * * /path/to/your/your_cron_issue.sh

This will schedule the script to run every 5 minutes. You can adjust the interval to suit your needs.

We have done, but you can continue to read to understand how the script works.

How the Script Works

The script we provided for periodic issue creation in Jira follows these main steps:

  1. Load environment variables: The script begins by loading environment variables from the user’s shell environment.
  2. Define variables: The script then defines the necessary variables for the JSON template, such as the project key, summary, and description. These variables are used to replace the placeholders in the JSON template.
  3. Update the JSON template: Using the awk command (you can also use sed instead), the script replaces the placeholders in the JSON template with the corresponding values from the variables defined earlier. The updated JSON template is saved to a new file.
  4. Create the Jira issue: Finally, the script uses the “jira issue create” command to create a new issue in Jira based on the updated JSON template.

This script automates the process of creating Jira issues periodically by following these steps. It’s designed to be flexible and can be customized to suit your specific needs.

Notes on awk and sed

We have shown you how to update the JSON template using awk in the script. However, if you prefer to use awk or sed to update the template directly, you can follow these instructions.

Using awk

awk -v variable_name1="value1" -v variable_name2="value2" ... '{gsub(/\$VARIABLE_NAME1/,value1); gsub(/\$VARIABLE_NAME2/,value2); ... print}' input_file > output_file

For example, to update the `$TEST_PKEY`, `$DESCRIPTION`, and `$SUMMARY` variables in the `cron_issue_template.json` file, use the following command:

awk -v test_pkey="YOUR_PROJECT_KEY" -v description="YOUR_DESCRIPTION" -v summary="YOUR_SUMMARY" '{gsub(/\$TEST_PKEY/,test_pkey); gsub(/\$DESCRIPTION/,description); gsub(/\$SUMMARY/,summary); print}' cron_issue_template.json > updated_cron_issue_template.json

The updated JSON template will be saved to the `updated_cron_issue_template.json` file.

Using sed

sed 's/\$VARIABLE_NAME1/value1/g; s/\$VARIABLE_NAME2/value2/g; ...' input_file > output_file

For example, to update the `$TEST_PKEY`, `$DESCRIPTION`, and `$SUMMARY` variables in the `cron_issue_template.json` file, use the following command:

sed 's/\$TEST_PKEY/YOUR_PROJECT_KEY/g; s/\$DESCRIPTION/YOUR_DESCRIPTION/g; s/\$SUMMARY/YOUR_SUMMARY/g' cron_issue_template.json > updated_cron_issue_template.json

Replace `YOUR_PROJECT_KEY`, `YOUR_DESCRIPTION`, and `YOUR_SUMMARY` with your desired values. The updated JSON template will be saved to the `updated_cron_issue_template.json` file.

--

--

The Starware is a software development company specialized in add-on development on Atlassian Platform.