96 lines
2.9 KiB
PHP
96 lines
2.9 KiB
PHP
<?php
|
|
|
|
$jira_base_url = "https://jira.someserver.com";
|
|
$username = "userName";
|
|
$password = "passWord! @#";
|
|
$project_key = "projectKey";
|
|
|
|
// 1. Pull a project's component list
|
|
$url = $jira_base_url . "/rest/api/2/project/$project_key/components";
|
|
$auth = base64_encode($username . ':' . $password);
|
|
|
|
$opts = [
|
|
'http' => [
|
|
'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);
|
|
}
|
|
|
|
?>
|