Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Adds users to the notification list. #86

Merged
merged 3 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 82 additions & 9 deletions src/Commands/NotificationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use AcquiaCli\Cli\Config;
use AcquiaCloudApi\Connector\Client;
use AcquiaCloudApi\Endpoints\Notifications;
use AcquiaCloudApi\Endpoints\Applications;
use AcquiaCloudApi\Endpoints\Organizations;
use Symfony\Component\Console\Helper\Table;
use AcquiaCloudApi\Exception\ApiErrorException;

/**
* Class NotificationsCommand
Expand All @@ -26,16 +28,20 @@ class NotificationsCommand extends AcquiaCommand
* A leading "~" in the field indicates the field should be sorted in a descending order.
*
* @command notification:list
* @option details Whether to show more details in the notication list (slower).
* @aliases n:l
*/
public function notificationList(
Config $config,
Client $client,
Applications $applicationsAdapter,
Organizations $organizationsAdapter,
Notifications $notificationsAdapter,
$uuid,
$limit = 50,
$filter = null,
$sort = '~created_at'
$sort = '~created_at',
$options = ['details']
) {

// Allows for limits and sort criteria.
Expand All @@ -58,19 +64,66 @@ public function notificationList(
$format = $extraConfig['format'];
$timezone = new \DateTimeZone($tz);

/**
* If we are using the details option, override the headers as we will
* be using a different number of columns.
*
* We will also be making THREE additional API calls which is why details
* nestled in as an option rather than the default, with a warning that
* it will be slower.
*
* We have to get both admins and members as separate API calls as there
* is no single call to source all users in an organisation.
*
* The array_reduce() allows us to go from an ArrayObject of MemberResponses
* to a simple associative array keyed on user UUID with a value of their email.
*
* @TODO find a way to store the application object further up the chain
* as we can call on that where needed.
*/
if ($options['details']) {
$table->setHeaders(['UUID', 'User', 'Created', 'Name', 'Status']);

$application = $applicationsAdapter->get($uuid);
$orgUuid = $application->organization->uuid;

$admins = $organizationsAdapter->getAdmins($orgUuid);
$members = $organizationsAdapter->getMembers($orgUuid);

$users = $admins->getArrayCopy() + $members->getArrayCopy();
$uuids = array_reduce($users, function ($result, $member) {
$result[$member->uuid] = $member->mail;
return $result;
}, []);
}

foreach ($notifications as $notification) {
$createdDate = new \DateTime($notification->created_at);
$createdDate->setTimezone($timezone);

$rows = [
$notification->uuid,
$createdDate->format($format),
$notification->label,
$notification->status,
];

/**
* Again only fires if we've used the details option.
* There is a chance that an operation will occur without a user attached.
* This could happen if an automated task/Acquia support person does something.
* The code here finds the user by UUID and presents their email.
*/
if ($options['details']) {
$author = $notification->context->author->uuid;
$mail = isset($uuids[$author]) ? $uuids[$author] : 'N/A';
array_splice($rows, 1, 0, $mail);
}

$table
->addRows(
[
[
$notification->uuid,
$createdDate->format($format),
$notification->label,
$notification->status,
],
$rows
]
);
}
Expand All @@ -88,8 +141,14 @@ public function notificationList(
* @aliases n:i
* @throws \Exception
*/
public function notificationInfo(Config $config, Notifications $notificationsAdapter, $uuid, $notificationUuid)
{
public function notificationInfo(
Config $config,
Applications $applicationsAdapter,
Organizations $organizationsAdapter,
Notifications $notificationsAdapter,
$uuid,
$notificationUuid
) {

$extraConfig = $config->get('extraconfig');
$tz = $extraConfig['timezone'];
Expand All @@ -103,7 +162,21 @@ public function notificationInfo(Config $config, Notifications $notificationsAda
$completedDate = new \DateTime($notification->completed_at);
$completedDate->setTimezone($timezone);

// @TODO Find a way to store the application object earlier to remove this call.
$application = $applicationsAdapter->get($uuid);
$orgUuid = $application->organization->uuid;
$admins = $organizationsAdapter->getAdmins($orgUuid);
$members = $organizationsAdapter->getMembers($orgUuid);
$users = $admins->getArrayCopy() + $members->getArrayCopy();
$uuids = array_reduce($users, function ($result, $member) {
$result[$member->uuid] = $member->mail;
return $result;
}, []);
$author = $notification->context->author->uuid;
$mail = isset($uuids[$author]) ? $uuids[$author] : 'N/A';

$this->say(sprintf('ID: %s', $notification->uuid));
$this->say(sprintf('User: %s', $mail));
$this->say(sprintf('Event: %s', $notification->event));
$this->say(sprintf('Description: %s', htmlspecialchars_decode($notification->description)));
$this->say(sprintf('Status: %s', $notification->status));
Expand Down
1 change: 1 addition & 0 deletions tests/AcquiaCliTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ public static function getFixtureMap()
'get' => 'Applications/getAllApplications.json',
],
'/applications/a47ac10b-58cc-4372-a567-0e02b2c3d470' => [
'get' => 'Applications/getApplication.json',
'put' => 'Applications/renameApplication.json'
],
'/applications/a47ac10b-58cc-4372-a567-0e02b2c3d470/environments' => [
Expand Down
13 changes: 13 additions & 0 deletions tests/Commands/NotificationCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,19 @@ public function notificationProvider()
+--------------------------------------+---------------------+-----------------------------------+-----------+
| 1bd3487e-71d1-4fca-a2d9-5f969b3d35c1 | 2019-07-30 06:47:13 | Application added to recents list | completed |
+--------------------------------------+---------------------+-----------------------------------+-----------+
TABLE;

$getAllNotifictionsDetails = <<<TABLE
+--------------------------------------+------+---------------------+-----------------------------------+-----------+
| UUID | User | Created | Name | Status |
+--------------------------------------+------+---------------------+-----------------------------------+-----------+
| 1bd3487e-71d1-4fca-a2d9-5f969b3d35c1 | N/A | 2019-07-30 06:47:13 | Application added to recents list | completed |
+--------------------------------------+------+---------------------+-----------------------------------+-----------+
TABLE;

$getNotification = <<<INFO
> ID: f4b37e3c-1g96-4ed4-ad20-3081fe0f9545
> User: N/A
> Event: DatabaseBackupCreated
> Description: A "sample_db" database backup has been created for "Dev".
> Status: completed
Expand All @@ -40,6 +49,10 @@ public function notificationProvider()
['notification:list', 'devcloud:devcloud2'],
$getAllNotifictions . PHP_EOL
],
[
['notification:list', 'devcloud:devcloud2', '--details'],
$getAllNotifictionsDetails . PHP_EOL
],
[
['notification:info', 'devcloud:devcloud2', 'f4b37e3c-1g96-4ed4-ad20-3081fe0f9545'],
$getNotification . PHP_EOL
Expand Down