From 67f9997fcbc67d203b1ecf26729f5d56290fdf3f Mon Sep 17 00:00:00 2001 From: Cody Cook Date: Wed, 22 Jan 2025 12:57:25 -0800 Subject: [PATCH 1/3] Edit for cloud --- fixComponentLead.sh | 111 ++++++++++++++++++++++++++++++-------------- 1 file changed, 77 insertions(+), 34 deletions(-) diff --git a/fixComponentLead.sh b/fixComponentLead.sh index 5e94484..349b640 100644 --- a/fixComponentLead.sh +++ b/fixComponentLead.sh @@ -1,51 +1,89 @@ #!/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://.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 \ - -u "$username:$password" \ + -u "$username:$api_token" \ -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(", ")') -echo "Usernames: $usernames" -read -p "Enter the username you want to replace: " old_username +# ----------------------------------------------------------------------------- +# 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(", ")') -# 4. Check if the entered username is in the list -if ! echo "$usernames" | grep -qw "$old_username"; then - echo "Username not found in the list. Please select a name from the list." +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 -# 5. Ask for the new username -read -p "Enter the new username: " new_username +# ----------------------------------------------------------------------------- +# STEP E: Ask for the new user’s **account ID** +# ----------------------------------------------------------------------------- +# Because Cloud requires accountId to assign a lead, prompt for accountId +# (not a username). +read -p "Enter the new user's account ID: " new_account_id -# Create a folder for the source username and history.txt file -mkdir -p "$old_username" -history_file="$old_username/history.txt" +# 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" -# 6. Create a new mapping of the IDs and new assignees we will be changing +# ----------------------------------------------------------------------------- +# 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=$(echo "$component_b64" | base64 --decode) - component_assignee=$(echo "$component" | jq -r '.assignee') + component_json=$(echo "$component_b64" | base64 --decode) + lead_display_name=$(echo "$component_json" | jq -r '.leadDisplayName') - if [ "$component_assignee" = "$old_username" ]; then - components_to_update+="$component"$'\n' + if [ "$lead_display_name" = "$old_display_name" ]; then + components_to_update+="$component_json"$'\n' fi 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 component_id=$(echo "$component" | jq -r '.id') component_name=$(echo "$component" | jq -r '.name') @@ -54,13 +92,18 @@ for component in $components_to_update; do 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:$password" \ + -u "$username:$api_token" \ -H "Content-Type: application/json" \ - -d "{\"leadUserName\":\"$new_username\"}" \ - "$jira_base_url/rest/api/2/component/$component_id" > /dev/null + -d "{\"leadAccountId\":\"$new_account_id\"}" \ + "$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') - echo "Updated component $component_name ($component_id) with new assignee $new_username at $timestamp" | tee -a "$history_file" -done \ No newline at end of file + echo "Updated component '$component_name' ($component_id) -> new lead $new_account_id at $timestamp" \ + | tee -a "$history_file" +done From 8e5b6acad8f898e6a58820a7df77fda1c9232bac Mon Sep 17 00:00:00 2001 From: Cody Cook Date: Wed, 22 Jan 2025 12:58:49 -0800 Subject: [PATCH 2/3] Fix unicodes --- fixComponentLead.sh | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/fixComponentLead.sh b/fixComponentLead.sh index 349b640..cbb211e 100644 --- a/fixComponentLead.sh +++ b/fixComponentLead.sh @@ -21,9 +21,9 @@ raw_data=$(curl -s -X GET \ # ----------------------------------------------------------------------------- # 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. +# 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, @@ -35,10 +35,10 @@ component_data=$(echo "$raw_data" \ # ----------------------------------------------------------------------------- # STEP C: Show a comma-separated list of lead **display names** -# and ask which “username” we want to replace +# 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. +# 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 @@ -56,7 +56,7 @@ if ! echo "$usernames" | grep -qw "$old_display_name"; then fi # ----------------------------------------------------------------------------- -# STEP E: Ask for the new user’s **account ID** +# STEP E: Ask for the new user's **account ID** # ----------------------------------------------------------------------------- # Because Cloud requires accountId to assign a lead, prompt for accountId # (not a username). @@ -67,7 +67,7 @@ mkdir -p "$old_display_name" history_file="$old_display_name/history.txt" # ----------------------------------------------------------------------------- -# STEP F: Identify components where the existing lead’s display name +# STEP F: Identify components where the existing lead's display name # matches the one we want to replace # ----------------------------------------------------------------------------- components_to_update="" @@ -93,7 +93,7 @@ for component in $components_to_update; do 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. + # 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" \ From 6be1bc053929662e6e34c1c50d5a01487736f573 Mon Sep 17 00:00:00 2001 From: Cody Cook Date: Wed, 22 Jan 2025 13:00:19 -0800 Subject: [PATCH 3/3] Fix the lookup of email ID --- fixComponentLead.sh | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/fixComponentLead.sh b/fixComponentLead.sh index cbb211e..d7dd1cf 100644 --- a/fixComponentLead.sh +++ b/fixComponentLead.sh @@ -58,9 +58,12 @@ fi # ----------------------------------------------------------------------------- # STEP E: Ask for the new user's **account ID** # ----------------------------------------------------------------------------- -# Because Cloud requires accountId to assign a lead, prompt for accountId -# (not a username). -read -p "Enter the new user's account ID: " new_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"