The Starware

Copy All Versions from One Jira Project to Another

In the previous article we have showed how to copy Jira project components from one project to another, in this article we will show how to…

In the previous article we have showed how to copy Jira project components from one project to another, in this article we will show how to copy project versions. Different than last time, we will prepare a small shell script file to perform it. This script will take two project keys, source project key and destination project key and copy all versions from source project to target project. If the version already exists in the destination project, it will just log it as an error and continue to create other versions. This enables you to run this script multiple times to keep two projects in synch. The script will not only create a version with the same name, but it will also match the source Jira project version’s released, archived status and release and start dates. Here is the shell script.

#!/bin/bash

# Check if correct number of arguments are passed
if [ "$#" -ne 2 ]; then
    echo "Usage: $0 <source_project_key> <destination_project_key>"
    exit 1
fi

# Source and destination project keys from command line arguments
SOURCE_PROJECT_KEY="$1"
DESTINATION_PROJECT_KEY="$2"

# Fetch all versions from the source project and skip the header line and --- below headers
versions=$(jira project-version get-all --project="$SOURCE_PROJECT_KEY" | tail -n +3)

# Read through each line to get details
echo "$versions" | while IFS= read -r line; do
    # Read fields using awk by treating multiple spaces as a single delimiter
    read -r id name archived released startDate releaseDate <<<$(echo $line | awk -v OFS="\t" '{$1=$1; print $1, $2, $3, $4, $5, $6}')

    # Construct the command to create version in the destination project
    cmd="jira project-version create -p $DESTINATION_PROJECT_KEY --name \"$name\""
    [ "$archived" == "true" ] && cmd+=" --archived"
    [ "$released" == "true" ] && cmd+=" --released"
    [ -n "$startDate" ] && [[ "$startDate" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]] && cmd+=" --startDate $startDate"
    [ -n "$releaseDate" ] && [[ "$releaseDate" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]] && cmd+=" --releaseDate $releaseDate"

    # Execute the command
    echo "Creating version $name in project $DESTINATION_PROJECT_KEY..."
    eval $cmd
done

echo "Version copying complete."

Example Usage:

./copy-versions ERP NP