Compare commits

..

3 commits
main ... cloud

Author SHA1 Message Date
6be1bc0539 Fix the lookup of email ID 2025-01-22 13:00:19 -08:00
8e5b6acad8 Fix unicodes 2025-01-22 12:58:49 -08:00
67f9997fcb Edit for cloud 2025-01-22 12:57:25 -08:00

View file

@ -1,51 +1,92 @@
#!/bin/bash #!/bin/bash
jira_base_url="https://jira.someserver.com"
username="userName"
password="passWord! @#"
project_key="projectKey"
# 1. Update your base URL for Jira Cloud
jira_base_url="https://<your-domain>.atlassian.net"
# 1. Pull a project's component list # 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 \ raw_data=$(curl -s -X GET \
-u "$username:$password" \ -u "$username:$api_token" \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
"$jira_base_url/rest/api/2/project/$project_key/components") "$jira_base_url/rest/api/3/project/$project_key/components")
# 2. Create a mapping of component ID, component name, and the assignee name # -----------------------------------------------------------------------------
component_data=$(echo "$raw_data" | jq -r '.[] | {id: .id, name: .name, assignee: .assignee.name} | @base64') # 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')
# 3. Show a comma-separated list of the usernames and ask which username we want to replace # -----------------------------------------------------------------------------
usernames=$(echo "$raw_data" | jq -r '[.[].assignee.name] | unique | join(", ")') # STEP C: Show a comma-separated list of lead **display names**
echo "Usernames: $usernames" # and ask which "username” we want to replace
read -p "Enter the username you want to replace: " old_username # -----------------------------------------------------------------------------
# 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(", ")')
# 4. Check if the entered username is in the list echo "Current leads (by display name): $usernames"
if ! echo "$usernames" | grep -qw "$old_username"; then read -p "Enter the display name you want to replace: " old_display_name
echo "Username not found in the list. Please select a name from the list."
# -----------------------------------------------------------------------------
# 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 exit 1
fi fi
# 5. Ask for the new username # -----------------------------------------------------------------------------
read -p "Enter the new username: " new_username # 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")
# Create a folder for the source username and history.txt file new_account_id=$(echo "$user_search_json" | jq -r '.[0].accountId')
mkdir -p "$old_username"
history_file="$old_username/history.txt"
# 6. Create a new mapping of the IDs and new assignees we will be changing # 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="" components_to_update=""
IFS=$'\n' IFS=$'\n'
for component_b64 in $component_data; do for component_b64 in $component_data; do
component=$(echo "$component_b64" | base64 --decode) component_json=$(echo "$component_b64" | base64 --decode)
component_assignee=$(echo "$component" | jq -r '.assignee') lead_display_name=$(echo "$component_json" | jq -r '.leadDisplayName')
if [ "$component_assignee" = "$old_username" ]; then if [ "$lead_display_name" = "$old_display_name" ]; then
components_to_update+="$component"$'\n' components_to_update+="$component_json"$'\n'
fi fi
done done
# -----------------------------------------------------------------------------
# 7. Push an update to the Jira Server API to update those component leads # STEP G: Update those component leads in Jira Cloud
# -----------------------------------------------------------------------------
for component in $components_to_update; do for component in $components_to_update; do
component_id=$(echo "$component" | jq -r '.id') component_id=$(echo "$component" | jq -r '.id')
component_name=$(echo "$component" | jq -r '.name') component_name=$(echo "$component" | jq -r '.name')
@ -54,13 +95,18 @@ for component in $components_to_update; do
continue continue
fi 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 \ curl -s -X PUT \
-u "$username:$password" \ -u "$username:$api_token" \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-d "{\"leadUserName\":\"$new_username\"}" \ -d "{\"leadAccountId\":\"$new_account_id\"}" \
"$jira_base_url/rest/api/2/component/$component_id" > /dev/null "$jira_base_url/rest/api/3/component/$component_id" > /dev/null
# Save the changed component ID and timestamp to the history file # -----------------------------------------------------------------------------
# STEP H: Log the update
# -----------------------------------------------------------------------------
timestamp=$(date '+%Y-%m-%d %H:%M:%S') timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo "Updated component $component_name ($component_id) with new assignee $new_username at $timestamp" | tee -a "$history_file" echo "Updated component '$component_name' ($component_id) -> new lead $new_account_id at $timestamp" \
| tee -a "$history_file"
done done