From b254689e1e60625b4b6b4cc5307f5a7465b23a5e Mon Sep 17 00:00:00 2001 From: Cody Cook Date: Mon, 8 May 2023 23:13:51 +0000 Subject: [PATCH] Add a CLI-driven PHP version. --- fixComponentLead-cli.php | 95 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 fixComponentLead-cli.php diff --git a/fixComponentLead-cli.php b/fixComponentLead-cli.php new file mode 100644 index 0000000..8a15f3d --- /dev/null +++ b/fixComponentLead-cli.php @@ -0,0 +1,95 @@ + [ + 'method' => 'GET', + 'header' => [ + "Content-Type: application/json", + "Authorization: Basic $auth" + ] + ] +]; + +$context = stream_context_create($opts); +$raw_data = file_get_contents($url, false, $context); +$components = json_decode($raw_data); + +// 2. Create a mapping of component ID, component name, and the assignee name +$component_data = []; +foreach ($components as $component) { + $component_data[] = [ + 'id' => $component->id, + 'name' => $component->name, + 'assignee' => $component->assignee->name + ]; +} + +// 3. Show a comma-separated list of the usernames and ask which username we want to replace +$usernames = array_unique(array_column($component_data, 'assignee')); +echo "Usernames: " . implode(', ', $usernames) . PHP_EOL; +echo "Enter the username you want to replace: "; +$old_username = trim(fgets(STDIN)); + +// 4. Check if the entered username is in the list +if (!in_array($old_username, $usernames)) { + echo "Username not found in the list. Please select a name from the list." . PHP_EOL; + exit(1); +} + +// 5. Ask for the new username +echo "Enter the new username: "; +$new_username = trim(fgets(STDIN)); + +// Create a folder for the source username and history.txt file +$history_folder = $old_username; +if (!file_exists($history_folder)) { + mkdir($history_folder); +} +$history_file = $history_folder . "/history.txt"; + +// 6. Create a new mapping of the IDs and new assignees we will be changing +$components_to_update = []; +foreach ($component_data as $component) { + if ($component['assignee'] === $old_username) { + $components_to_update[] = $component; + } +} + +// 7. Push an update to the Jira Server API to update those component leads +foreach ($components_to_update as $component) { + $component_id = $component['id']; + $component_name = $component['name']; + + $url = $jira_base_url . "/rest/api/2/component/$component_id"; + $post_data = json_encode(["leadUserName" => $new_username]); + + $opts = [ + 'http' => [ + 'method' => 'PUT', + 'header' => [ + "Content-Type: application/json", + "Authorization: Basic $auth" + ], + 'content' => $post_data + ] + ]; + + $context = stream_context_create($opts); + file_get_contents($url, false, $context); + + // Save the changed component ID and timestamp to the history file + $timestamp = date('Y-m-d H:i:s'); + $history_entry = "Updated component $component_name ($component_id) with new assignee $new_username at $timestamp" . PHP_EOL; + file_put_contents($history_file, $history_entry, FILE_APPEND); +} + +?>