Skip to content

Commit 7a8cfbf

Browse files
committed
Updated scripts logic from main.
1 parent cd0c120 commit 7a8cfbf

4 files changed

Lines changed: 66 additions & 6 deletions

File tree

.vortex/tooling/src/notify-newrelic

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ namespace DrevOps\VortexTooling;
1313
require_once __DIR__ . '/helpers.php';
1414
execute_override(basename(__FILE__));
1515

16+
// -----------------------------------------------------------------------------
17+
18+
// Flag to enable New Relic notifications.
19+
//
20+
// Set to "true" (not "1") in environments where New Relic is configured.
21+
$newrelic_enabled = getenv('VORTEX_NOTIFY_NEWRELIC_ENABLED') ?: getenv('NEWRELIC_ENABLED');
22+
1623
// New Relic notification project name.
1724
$notify_project = getenv_required('VORTEX_NOTIFY_NEWRELIC_PROJECT', 'VORTEX_NOTIFY_PROJECT');
1825

@@ -77,6 +84,11 @@ $newrelic_endpoint = getenv_default('VORTEX_NOTIFY_NEWRELIC_ENDPOINT', 'https://
7784

7885
// ------------------------------------------------------------------------------
7986

87+
if (empty($newrelic_enabled)) {
88+
info('New Relic is not enabled. Set NEWRELIC_ENABLED or VORTEX_NOTIFY_NEWRELIC_ENABLED in your environment.');
89+
quit();
90+
}
91+
8092
info('Started New Relic notification.');
8193

8294
// Skip if this is a pre-deployment event (New Relic only for post-deployment).

.vortex/tooling/src/notify-slack

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,21 @@ if ($notify_event === 'pre_deployment') {
8484
// Build the message title.
8585
$title = sprintf('%s: %s', $event_label, $notify_project);
8686

87+
// Build fields array - conditionally include Environment and Login for post_deployment only.
88+
$fields = [
89+
['title' => 'Deployment', 'value' => $notify_label, 'short' => TRUE],
90+
['title' => 'Time', 'value' => $timestamp, 'short' => TRUE],
91+
];
92+
93+
// Only include Environment and Login links for post-deployment notifications.
94+
// Pre-deployment notifications should not show these as the site is not yet available.
95+
if ($notify_event !== 'pre_deployment') {
96+
array_splice($fields, 1, 0, [
97+
['title' => 'Environment', 'value' => sprintf('<%s|View Site>', $notify_env_url), 'short' => TRUE],
98+
['title' => 'Login', 'value' => sprintf('<%s|Login Here>', $notify_login_url), 'short' => TRUE],
99+
]);
100+
}
101+
87102
// Build payload.
88103
$data = [
89104
'username' => $slack_username,
@@ -93,12 +108,7 @@ $data = [
93108
'color' => $color,
94109
'fallback' => $slack_fallback_message,
95110
'title' => $title,
96-
'fields' => [
97-
['title' => 'Deployment', 'value' => $notify_label, 'short' => TRUE],
98-
['title' => 'Environment', 'value' => sprintf('<%s|View Site>', $notify_env_url), 'short' => TRUE],
99-
['title' => 'Login', 'value' => sprintf('<%s|Login Here>', $notify_login_url), 'short' => TRUE],
100-
['title' => 'Time', 'value' => $timestamp, 'short' => TRUE],
101-
],
111+
'fields' => $fields,
102112
'ts' => time(),
103113
],
104114
],

.vortex/tooling/tests/Unit/NotifyNewrelicTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ protected function setUp(): void {
2121
parent::setUp();
2222

2323
$this->envSetMultiple([
24+
'VORTEX_NOTIFY_NEWRELIC_ENABLED' => 'true',
2425
'VORTEX_NOTIFY_NEWRELIC_PROJECT' => 'test-project',
2526
'VORTEX_NOTIFY_NEWRELIC_USER_KEY' => 'NRAK-TEST123456',
2627
'VORTEX_NOTIFY_NEWRELIC_LABEL' => 'main',
@@ -33,6 +34,31 @@ protected function setUp(): void {
3334
]);
3435
}
3536

37+
public function testNotificationSkippedWhenNotEnabled(): void {
38+
$this->envUnset('VORTEX_NOTIFY_NEWRELIC_ENABLED');
39+
40+
$this->runScriptEarlyPass('src/notify-newrelic', 'New Relic is not enabled');
41+
}
42+
43+
public function testNotificationEnabledWithFallbackVariable(): void {
44+
$this->envUnset('VORTEX_NOTIFY_NEWRELIC_ENABLED');
45+
$this->envSet('NEWRELIC_ENABLED', 'true');
46+
$this->envSet('VORTEX_NOTIFY_NEWRELIC_REVISION', 'v1.0.0');
47+
48+
$this->mockRequestPost(
49+
'https://api.newrelic.com/v2/applications/12345678/deployments.json',
50+
$this->callback(fn(): true => TRUE),
51+
['Api-Key: NRAK-TEST123456', 'Content-Type: application/json'],
52+
10,
53+
['status' => 201]
54+
);
55+
56+
$output = $this->runScript('src/notify-newrelic');
57+
58+
$this->assertStringContainsString('Started New Relic notification', $output);
59+
$this->assertStringContainsString('Finished New Relic notification', $output);
60+
}
61+
3662
public function testSuccessfulNotificationWithProvidedAppId(): void {
3763
$this->envSet('VORTEX_NOTIFY_NEWRELIC_REVISION', 'v1.2.3');
3864

.vortex/tooling/tests/Unit/NotifySlackTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ public function testSuccessfulNotificationPostDeployment(): void {
4141
$this->assertEquals(':rocket:', $payload['icon_emoji']);
4242
$this->assertEquals('good', $payload['attachments'][0]['color']);
4343
$this->assertStringContainsString('Deployment Complete', $payload['attachments'][0]['title']);
44+
// Verify fields - post-deployment should have all 4 fields.
45+
$fields = $payload['attachments'][0]['fields'];
46+
$this->assertCount(4, $fields, 'Post-deployment should have 4 fields');
47+
$this->assertEquals('Deployment', $fields[0]['title']);
48+
$this->assertEquals('Environment', $fields[1]['title']);
49+
$this->assertEquals('Login', $fields[2]['title']);
50+
$this->assertEquals('Time', $fields[3]['title']);
4451
$this->assertEquals('test-project', $this->getFieldValue($payload, 'Deployment'));
4552
return TRUE;
4653
}),
@@ -68,6 +75,11 @@ public function testSuccessfulNotificationPreDeployment(): void {
6875
// Verify pre-deployment styling.
6976
$this->assertEquals('#808080', $payload['attachments'][0]['color']);
7077
$this->assertStringContainsString('Deployment Starting', $payload['attachments'][0]['title']);
78+
// Verify fields - should only have Deployment and Time, not Environment or Login.
79+
$fields = $payload['attachments'][0]['fields'];
80+
$this->assertCount(2, $fields, 'Pre-deployment should only have 2 fields');
81+
$this->assertEquals('Deployment', $fields[0]['title']);
82+
$this->assertEquals('Time', $fields[1]['title']);
7183
return TRUE;
7284
}),
7385
['Content-Type: application/json'],

0 commit comments

Comments
 (0)