diff --git a/fixComponentLead.sh b/fixComponentLead.sh index 5e94484..d7dd1cf 100644 --- a/fixComponentLead.sh +++ b/fixComponentLead.sh @@ -1,51 +1,92 @@ #!/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** +# ----------------------------------------------------------------------------- +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 -mkdir -p "$old_username" -history_file="$old_username/history.txt" +new_account_id=$(echo "$user_search_json" | jq -r '.[0].accountId') -# 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="" 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 +95,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