jirabulkupdatecomponentlead/fixComponentLead.sh

113 lines
4.6 KiB
Bash

#!/bin/bash
# 1. Update your base URL for Jira Cloud
jira_base_url="https://<your-domain>.atlassian.net"
# 2. Use email + API token for Jira Cloud
username="your_email@example.com"
api_token="YOUR_API_TOKEN"
# 3. Your project key
project_key="PROJECTKEY"
# -----------------------------------------------------------------------------
# STEP A: Pull a project's component list
# -----------------------------------------------------------------------------
raw_data=$(curl -s -X GET \
-u "$username:$api_token" \
-H "Content-Type: application/json" \
"$jira_base_url/rest/api/3/project/$project_key/components")
# -----------------------------------------------------------------------------
# STEP B: Create a mapping of each component's ID, name, and lead info
# -----------------------------------------------------------------------------
# For display, we'll store "leadDisplayName” and "leadAccountId”.
# We'll store the "leadAccountId” in the field `assignee` to mirror the old script,
# but be aware that it's really the account ID in Jira Cloud.
component_data=$(echo "$raw_data" \
| jq -r '.[]
| { id: .id,
name: .name,
leadDisplayName: .lead.displayName,
assignee: .lead.accountId
}
| @base64')
# -----------------------------------------------------------------------------
# STEP C: Show a comma-separated list of lead **display names**
# and ask which "username” we want to replace
# -----------------------------------------------------------------------------
# NOTE: In Jira Cloud, "username” is replaced by "accountId”.
# But to preserve the same "prompt” style, we'll list display names.
usernames=$(echo "$raw_data" \
| jq -r '[.[].lead.displayName]
| unique
| join(", ")')
echo "Current leads (by display name): $usernames"
read -p "Enter the display name you want to replace: " old_display_name
# -----------------------------------------------------------------------------
# STEP D: Check if the entered display name is in the list
# -----------------------------------------------------------------------------
if ! echo "$usernames" | grep -qw "$old_display_name"; then
echo "That display name was not found in the list. Please select from the list above."
exit 1
fi
# -----------------------------------------------------------------------------
# STEP E: Ask for the new user's **account ID**
# -----------------------------------------------------------------------------
read -p "Enter the new user's email address: " new_user_email
user_search_json=$(curl -s -G -u "$username:$api_token" \
--data-urlencode "query=$new_user_email" \
"$jira_base_url/rest/api/3/users/search")
new_account_id=$(echo "$user_search_json" | jq -r '.[0].accountId')
# Create a folder named after the old display name for history and logs
mkdir -p "$old_display_name"
history_file="$old_display_name/history.txt"
# -----------------------------------------------------------------------------
# STEP F: Identify components where the existing lead's display name
# matches the one we want to replace
# -----------------------------------------------------------------------------
components_to_update=""
IFS=$'\n'
for component_b64 in $component_data; do
component_json=$(echo "$component_b64" | base64 --decode)
lead_display_name=$(echo "$component_json" | jq -r '.leadDisplayName')
if [ "$lead_display_name" = "$old_display_name" ]; then
components_to_update+="$component_json"$'\n'
fi
done
# -----------------------------------------------------------------------------
# STEP G: Update those component leads in Jira Cloud
# -----------------------------------------------------------------------------
for component in $components_to_update; do
component_id=$(echo "$component" | jq -r '.id')
component_name=$(echo "$component" | jq -r '.name')
if [ -z "$component_id" ] || [ -z "$component_name" ]; then
continue
fi
# Note that in Jira Cloud, you must specify "leadAccountId" in the body
# to change a component's lead. "leadUserName" is ignored/invalid in Cloud.
curl -s -X PUT \
-u "$username:$api_token" \
-H "Content-Type: application/json" \
-d "{\"leadAccountId\":\"$new_account_id\"}" \
"$jira_base_url/rest/api/3/component/$component_id" > /dev/null
# -----------------------------------------------------------------------------
# STEP H: Log the update
# -----------------------------------------------------------------------------
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo "Updated component '$component_name' ($component_id) -> new lead $new_account_id at $timestamp" \
| tee -a "$history_file"
done