diff --git a/fixComponentLead.sh b/fixComponentLead.sh index d7dd1cf..5e94484 100644 --- a/fixComponentLead.sh +++ b/fixComponentLead.sh @@ -1,92 +1,51 @@ #!/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" -# 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 -# ----------------------------------------------------------------------------- +# 1. Pull a project's component list raw_data=$(curl -s -X GET \ - -u "$username:$api_token" \ + -u "$username:$password" \ -H "Content-Type: application/json" \ - "$jira_base_url/rest/api/3/project/$project_key/components") + "$jira_base_url/rest/api/2/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') +# 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 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(", ")') +# 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 -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." +# 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." 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") +# 5. Ask for the new username +read -p "Enter the new username: " new_username -new_account_id=$(echo "$user_search_json" | jq -r '.[0].accountId') +# 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" - -# ----------------------------------------------------------------------------- -# STEP F: Identify components where the existing lead's display name -# matches the one we want to replace -# ----------------------------------------------------------------------------- +# 6. Create a new mapping of the IDs and new assignees we will be changing 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') + component=$(echo "$component_b64" | base64 --decode) + component_assignee=$(echo "$component" | jq -r '.assignee') - if [ "$lead_display_name" = "$old_display_name" ]; then - components_to_update+="$component_json"$'\n' + if [ "$component_assignee" = "$old_username" ]; then + components_to_update+="$component"$'\n' fi done -# ----------------------------------------------------------------------------- -# STEP G: Update those component leads in Jira Cloud -# ----------------------------------------------------------------------------- + +# 7. Push an update to the Jira Server API to update those component leads for component in $components_to_update; do component_id=$(echo "$component" | jq -r '.id') component_name=$(echo "$component" | jq -r '.name') @@ -95,18 +54,13 @@ 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:$api_token" \ + -u "$username:$password" \ -H "Content-Type: application/json" \ - -d "{\"leadAccountId\":\"$new_account_id\"}" \ - "$jira_base_url/rest/api/3/component/$component_id" > /dev/null + -d "{\"leadUserName\":\"$new_username\"}" \ + "$jira_base_url/rest/api/2/component/$component_id" > /dev/null - # ----------------------------------------------------------------------------- - # STEP H: Log the update - # ----------------------------------------------------------------------------- + # Save the changed component ID and timestamp to the history file 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 + 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