Skip to content

Commit

Permalink
Merge pull request #30 from wunderio/feature/notification-site-env
Browse files Browse the repository at this point in the history
Notifications: JSON: Site & Env: Include
  • Loading branch information
ragnarkurmwunder authored Jun 16, 2023
2 parents 638a649 + fb84a51 commit d750d80
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ As an alternative there is Warden, but it lacks highly configurable alerting.
On hourly basis it logs the differences of the statuses of modules like this (if there are any changes):

```
---- -------------- ------------- ---------- ---------------------------------------------------------------------
---- -------------- ------------- ---------- ------------------------------------------------------------------------------------------------------
ID Date Type Severity Message
---- -------------- ------------- ---------- ---------------------------------------------------------------------
1 01/Jul 15:43 updates_log Info updates_log={"project":"drupal","old":"CURRENT","new":"NOT_SECURE"}==
---- -------------- ------------- ---------- ---------------------------------------------------------------------
---- -------------- ------------- ---------- ------------------------------------------------------------------------------------------------------
1 01/Jul 15:43 updates_log Info updates_log={"project":"drupal","old":"CURRENT","new":"NOT_SECURE","site":"example.com","env:"prod"}==
---- -------------- ------------- ---------- ------------------------------------------------------------------------------------------------------
```

`old` and `new` denote statuses.
Expand Down Expand Up @@ -87,6 +87,8 @@ updates_log={
project: "webform",
old: "NOT_CURRENT",
new: "CURRENT"
site: "example.com"
env: "prod"
}==
```

Expand All @@ -97,7 +99,7 @@ Every state change will have its own log entry.
The module also logs "Statistics" once in 24h that gives a quick overview about how many modules there are and in what statuses.
```
updates_log_statistics={
"updates_log": "2.0.1",
"updates_log": "2.4.0",
"last_check_epoch": 1672835445,
"last_check_human": "2023-01-04T12:30:450GMT",
"last_check_ago": 16,
Expand Down
25 changes: 21 additions & 4 deletions src/UpdatesLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ class UpdatesLog {
*/
private ExtensionList $extensionList;

/**
* The Site name.
*
* @var string
*/
private string $site;

/**
* The Env name.
*
* @var string
*/
private string $env;

/**
* UpdatesLog constructor.
*
Expand Down Expand Up @@ -92,6 +106,9 @@ public function __construct(
$this->updateProcessor = $updateProcessor;
$this->lastUpdated = $state->get('update.last_check', 0);
$this->extensionList = $moduleExtensionList;

$this->site = $this->getSite();
$this->env = $this->getEnv();
}

/*
Expand Down Expand Up @@ -168,13 +185,11 @@ public function runStatistics(
}

$version = $this->getVersion();
$site = $this->getSite();
$env = $this->getEnv();
$statistics = $this->generateStatistics(
$statuses,
$version,
$site,
$env
$this->site,
$this->env
);

// https://www.drupal.org/project/drupal/issues/2920285
Expand Down Expand Up @@ -292,6 +307,8 @@ public function logDiff(array $statuses): void {
'project' => $project,
'old' => $status['old'],
'new' => $status['new'],
'site' => $this->site,
'env' => $this->env,
];
try {
$json = json_encode($log, JSON_THROW_ON_ERROR);
Expand Down
8 changes: 7 additions & 1 deletion tests/src/Kernel/LogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\Tests\updates_log\Kernel;

use Drupal\Core\Database\Connection;
use Drupal\Core\Site\Settings;
use Drupal\KernelTests\KernelTestBase;
use Drupal\updates_log\UpdatesLog;

Expand Down Expand Up @@ -43,6 +44,11 @@ class LogTest extends KernelTestBase {
protected function setUp(): void {
parent::setUp();

new Settings([
'updates_log_site' => 'site',
'updates_log_env' => 'env',
]);

$this->installConfig(['updates_log']);
$this->installSchema('dblog', ['watchdog']);
$this->updatesLogService = \Drupal::service('updates_log.updates_logger');
Expand All @@ -64,7 +70,7 @@ public function testLogDiff(): void {

$this->assertEquals('updates_log', $log->type);
$this->assertEquals('updates_log=@placeholder==', $log->message);
$this->assertEquals('a:1:{s:12:"@placeholder";s:44:"{"project":"drupal","old":"old","new":"new"}";}', $log->variables);
$this->assertEquals('a:1:{s:12:"@placeholder";s:70:"{"project":"drupal","old":"old","new":"new","site":"site","env":"env"}";}', $log->variables);
$this->assertEquals(6, $log->severity);
$this->assertGreaterThan(time() - 5, $log->timestamp);
}
Expand Down
2 changes: 1 addition & 1 deletion updates_log.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ description: "Log module update info"
core: 8.x
core_version_requirement: ^8 || ^9 || ^10
package: Development
version: 2.3.1
version: 2.4.0
dependencies:
- drupal:update

0 comments on commit d750d80

Please sign in to comment.