jirabulkupdatecomponentlead/fixComponentLead.sh
2023-05-08 23:02:22 +00:00

66 lines
2.3 KiB
Bash

#!/bin/bash
jira_base_url="https://jira.someserver.com"
username="userName"
password="passWord! @#"
project_key="projectKey"
# 1. Pull a project's component list
raw_data=$(curl -s -X GET \
-u "$username:$password" \
-H "Content-Type: application/json" \
"$jira_base_url/rest/api/2/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')
# 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
# 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
# 5. Ask for the new username
read -p "Enter the new username: " new_username
# Create a folder for the source username and history.txt file
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
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')
if [ "$component_assignee" = "$old_username" ]; then
components_to_update+="$component"$'\n'
fi
done
# 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')
if [ -z "$component_id" ] || [ -z "$component_name" ]; then
continue
fi
curl -s -X PUT \
-u "$username:$password" \
-H "Content-Type: application/json" \
-d "{\"leadUserName\":\"$new_username\"}" \
"$jira_base_url/rest/api/2/component/$component_id" > /dev/null
# 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) with new assignee $new_username at $timestamp" | tee -a "$history_file"
done