DEV Community

Arief Warazuhudien
Arief Warazuhudien

Posted on

Example of a shell script that uses cURL to retrieve & submit all the configurations for a project in JIRA

Example of a shell script that uses cURL to retrieve all the configurations for a project in JIRA, including boards and workflows:

#!/bin/bash

# JIRA API URL and project key
JIRA_URL="https://your-jira-instance.com"
PROJECT_KEY="YOUR_PROJECT_KEY"

# JIRA API authentication
USERNAME="your-username"
API_TOKEN="your-api-token"

# Step 1: Authenticate and obtain the access token
AUTH_HEADER="Authorization: Basic $(echo -n "$USERNAME:$API_TOKEN" | base64)"
ACCESS_TOKEN=$(curl -s -H "$AUTH_HEADER" "$JIRA_URL/rest/api/2/myself" | jq -r '.key')

# Step 2: Get the project ID
PROJECT_ID=$(curl -s -H "$AUTH_HEADER" "$JIRA_URL/rest/api/2/project/$PROJECT_KEY" | jq -r '.id')

# Step 3: Retrieve the project details
PROJECT_DETAILS=$(curl -s -H "$AUTH_HEADER" "$JIRA_URL/rest/api/2/project/$PROJECT_ID")

# Step 4: Get the workflow scheme ID
WORKFLOW_SCHEME_ID=$(echo "$PROJECT_DETAILS" | jq -r '.issueTypes[].workflowId')

# Step 5: Retrieve the workflow scheme details
WORKFLOW_SCHEME_DETAILS=$(curl -s -H "$AUTH_HEADER" "$JIRA_URL/rest/api/2/workflowscheme/$WORKFLOW_SCHEME_ID")

# Step 6: Process and store the workflow details
WORKFLOW_DETAILS=$(echo "$WORKFLOW_SCHEME_DETAILS" | jq -r '.values[].workflow')

# Step 7: Retrieve all boards for the project
BOARD_DETAILS=$(curl -s -H "$AUTH_HEADER" "$JIRA_URL/rest/agile/1.0/board?projectKeyOrId=$PROJECT_ID")

# Step 8: Process and store the board details
BOARD_IDS=$(echo "$BOARD_DETAILS" | jq -r '.values[].id')

# Output the configurations
echo "Project Details:"
echo "$PROJECT_DETAILS"
echo ""
echo "Workflow Scheme Details:"
echo "$WORKFLOW_SCHEME_DETAILS"
echo ""
echo "Workflow Details:"
echo "$WORKFLOW_DETAILS"
echo ""
echo "Board Details:"
echo "$BOARD_DETAILS"

Enter fullscreen mode Exit fullscreen mode

To submit all the configurations retrieved from one project to a new blank project in JIRA, you can use the JIRA API to create the necessary configurations. Here's an example of a shell script that demonstrates this process:

#!/bin/bash

# JIRA API URL and project keys
JIRA_URL="https://your-jira-instance.com"
SOURCE_PROJECT_KEY="SOURCE_PROJECT_KEY"
TARGET_PROJECT_KEY="TARGET_PROJECT_KEY"

# JIRA API authentication
USERNAME="your-username"
API_TOKEN="your-api-token"

# Step 1: Authenticate and obtain the access token
AUTH_HEADER="Authorization: Basic $(echo -n "$USERNAME:$API_TOKEN" | base64)"
ACCESS_TOKEN=$(curl -s -H "$AUTH_HEADER" "$JIRA_URL/rest/api/2/myself" | jq -r '.key')

# Step 2: Get the source project ID
SOURCE_PROJECT_ID=$(curl -s -H "$AUTH_HEADER" "$JIRA_URL/rest/api/2/project/$SOURCE_PROJECT_KEY" | jq -r '.id')

# Step 3: Retrieve the project details
PROJECT_DETAILS=$(curl -s -H "$AUTH_HEADER" "$JIRA_URL/rest/api/2/project/$SOURCE_PROJECT_ID")

# Step 4: Get the workflow scheme ID
WORKFLOW_SCHEME_ID=$(echo "$PROJECT_DETAILS" | jq -r '.issueTypes[].workflowId')

# Step 5: Retrieve the workflow scheme details
WORKFLOW_SCHEME_DETAILS=$(curl -s -H "$AUTH_HEADER" "$JIRA_URL/rest/api/2/workflowscheme/$WORKFLOW_SCHEME_ID")

# Step 6: Process and store the workflow details
WORKFLOW_DETAILS=$(echo "$WORKFLOW_SCHEME_DETAILS" | jq -r '.values[].workflow')

# Step 7: Retrieve all boards for the project
BOARD_DETAILS=$(curl -s -H "$AUTH_HEADER" "$JIRA_URL/rest/agile/1.0/board?projectKeyOrId=$SOURCE_PROJECT_ID")

# Step 8: Process and store the board details
BOARD_IDS=$(echo "$BOARD_DETAILS" | jq -r '.values[].id')

# Step 9: Create a new project
NEW_PROJECT_ID=$(curl -s -X POST -H "$AUTH_HEADER" -H "Content-Type: application/json" -d '{
  "key": "'"$TARGET_PROJECT_KEY"'",
  "name": "New Project",
  "projectTypeKey": "business",
  "projectTemplateKey": "com.atlassian.jira-core-project-templates:jira-core-project-management",
  "description": "New project created from template",
  "lead": "'"$USERNAME"'",
  "assigneeType": "PROJECT_LEAD",
  "avatarId": 10137
}' "$JIRA_URL/rest/api/2/project" | jq -r '.id')

# Step 10: Update the workflow scheme for the new project
curl -s -X PUT -H "$AUTH_HEADER" -H "Content-Type: application/json" -d '{
  "update": {
    "issueTypeMappings": [{
      "issueTypeId": "10001",
      "workflow": {
        "name": "New Workflow",
        "description": "New workflow created from template",
        "steps": [
          {
            "id": "1",
            "name": "To Do"
          },
          {
            "id": "2",
            "name": "In Progress"
          },
          {
            "id": "3",
            "name": "Done"
          }
        ],
        "transitions": [
          {
            "id": "11",
            "name": "Start Progress",
            "from": ["1"],
            "to": "2"
          },
          {
            "id": "21",
            "name": "Resolve Issue",
            "from": ["2"],
            "to": "3"
          }
        ]
      }
    }]
  }
}' "$JIRA_URL/rest/api/2/workflowscheme/$NEW_PROJECT_ID"

# Step 11: Copy boards to the new project
for BOARD_ID in $(echo "$BOARD_IDS"); do
  curl -s -X POST -H "$AUTH_HEADER" -H "Content-Type: application/json" -d '{
    "name": "New Board",
    "type": "scrum",
    "projectIds": ["'"$NEW_PROJECT_ID"'"],
    "filterId": "'"$BOARD_ID"'"
  }' "$JIRA_URL/rest/agile/1.0/board"
done

echo "New project with key '$TARGET_PROJECT_KEY' has been created and configured."

Enter fullscreen mode Exit fullscreen mode

Top comments (0)