Skip to content

Commit 331ab3b

Browse files
committed
AC-15461: Migration New Relic from REST v2 to NerdGraph (GraphQL)
1 parent b3bd048 commit 331ab3b

File tree

7 files changed

+41
-29
lines changed

7 files changed

+41
-29
lines changed

app/code/Magento/NewRelicReporting/Model/Apm/Deployments.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ public function __construct(
7979
* Supports both v2 REST and NerdGraph APIs based on configuration
8080
*
8181
* @param string $description
82-
* @param bool|string $change
83-
* @param bool|string $user
82+
* @param string|null $change
83+
* @param string|null $user
8484
* @param string|null $revision
8585
* @param string|null $commit Git commit hash (NerdGraph only)
8686
* @param string|null $deepLink Deep link URL (NerdGraph only)
@@ -90,8 +90,8 @@ public function __construct(
9090
*/
9191
public function setDeployment(
9292
string $description,
93-
bool|string $change = false,
94-
bool|string $user = false,
93+
?string $change = null,
94+
?string $user = null,
9595
?string $revision = null,
9696
?string $commit = null,
9797
?string $deepLink = null,
@@ -178,15 +178,15 @@ private function createV2RestDeployment(string $description, bool|string $change
178178
* Create deployment using NerdGraph (GraphQL) API
179179
*
180180
* @param string $description
181-
* @param bool|string $change
182-
* @param bool|string $user
181+
* @param string|null $change
182+
* @param string|null $user
183183
* @param string|null $revision
184184
* @param string|null $commit
185185
* @param string|null $deepLink
186186
* @param string|null $groupId
187187
* @return array|false
188188
*/
189-
private function createNerdGraphDeployment(string $description, bool|string $change, bool|string $user, ?string
189+
private function createNerdGraphDeployment(string $description, ?string $change, ?string $user, ?string
190190
$revision, ?string $commit, ?string $deepLink, ?string $groupId): false|array
191191
{
192192
return $this->deploymentTracker->createDeployment(

app/code/Magento/NewRelicReporting/Model/NerdGraph/Client.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ public function __construct(
6363
* @param string $query The GraphQL query string
6464
* @param array $variables Optional variables for the query
6565
* @return array The decoded response data
66-
* @throws \RuntimeException on request or response errors
66+
* @throws RuntimeException on request or response errors
6767
*/
6868
public function query(string $query, array $variables = []): array
6969
{
7070
if (!$this->config->isNewRelicEnabled()) {
71-
throw new \RuntimeException('New Relic is not enabled');
71+
throw new RuntimeException('New Relic is not enabled');
7272
}
7373
try {
7474
// Use the same API key field for both v2 REST and NerdGraph modes
@@ -92,7 +92,7 @@ public function query(string $query, array $variables = []): array
9292
$response = $client->send();
9393
} catch (RuntimeException $e) {
9494
$this->logger->error('NerdGraph API request failed: ' . $e->getMessage());
95-
throw new \RuntimeException('NerdGraph API request failed: ' . $e->getMessage(), 0, $e);
95+
throw new RuntimeException('NerdGraph API request failed: ' . $e->getMessage(), 0, $e);
9696
}
9797

9898
if ($response->getStatusCode() !== 200) {
@@ -102,7 +102,7 @@ public function query(string $query, array $variables = []): array
102102
$response->getBody()
103103
);
104104
$this->logger->error($errorMsg);
105-
throw new \RuntimeException($errorMsg);
105+
throw new RuntimeException($errorMsg);
106106
}
107107

108108
$responseData = $this->serializer->unserialize($response->getBody());
@@ -114,7 +114,7 @@ public function query(string $query, array $variables = []): array
114114
);
115115
$errorMsg = 'NerdGraph GraphQL errors: ' . implode(', ', $errorMessages);
116116
$this->logger->error($errorMsg);
117-
throw new \RuntimeException($errorMsg);
117+
throw new RuntimeException($errorMsg);
118118
}
119119

120120
return $responseData;
@@ -135,7 +135,6 @@ public function getEntityGuidFromApplication(?string $appName = null, ?string $a
135135
$searchQuery = 'type = \'APPLICATION\'';
136136

137137
if ($appName) {
138-
// safer than addslashes
139138
$searchQuery .= sprintf(" AND name = '%s'", str_replace("'", "\\'", $appName));
140139
} elseif ($appId) {
141140
$searchQuery .= sprintf(" AND appId = '%s'", str_replace("'", "\\'", $appId));
@@ -184,7 +183,7 @@ public function getEntityGuidFromApplication(?string $appName = null, ?string $a
184183
' (GUID: ' . $firstEntity['guid'] . ')'
185184
);
186185
return $firstEntity['guid'];
187-
} catch (\RuntimeException $e) {
186+
} catch (RuntimeException $e) {
188187
$this->logger->error(sprintf(
189188
'Failed to get entity GUID. Search query: %s. Error: %s',
190189
$searchQuery,

app/code/Magento/NewRelicReporting/Model/NerdGraph/DeploymentTracker.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,16 @@ public function createDeployment(
8888

8989
$response = $this->nerdGraphClient->query($this->getDeploymentMutation(), $variables);
9090

91-
return $this->processDeploymentResponse($response, $variables, $description, $changelog, $user, $commit, $deepLink, $groupId);
91+
return $this->processDeploymentResponse(
92+
$response,
93+
$variables,
94+
$description,
95+
$changelog,
96+
$user,
97+
$commit,
98+
$deepLink,
99+
$groupId
100+
);
92101
} catch (\Exception $e) {
93102
$this->logger->error('NerdGraph deployment creation failed: ' . $e->getMessage());
94103
return false;
@@ -251,7 +260,7 @@ private function processDeploymentResponse(
251260
): false|array {
252261
$deploymentData = $response['data']['changeTrackingCreateDeployment'] ?? null;
253262
$deployedVersion = $variables['deployment']['version'];
254-
263+
255264
if ($deploymentData) {
256265
$this->logger->info(
257266
'NerdGraph deployment created successfully',

app/code/Magento/NewRelicReporting/Test/Unit/Console/Command/DeployMarkerTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@
77

88
namespace Magento\NewRelicReporting\Test\Unit\Console\Command;
99

10+
use Exception;
1011
use Magento\Framework\Exception\LocalizedException;
1112
use Magento\NewRelicReporting\Console\Command\DeployMarker;
1213
use Magento\NewRelicReporting\Model\Apm\Deployments;
1314
use Magento\NewRelicReporting\Model\Apm\DeploymentsFactory;
1415
use Magento\NewRelicReporting\Model\Config;
1516
use Magento\NewRelicReporting\Model\ServiceShellUser;
16-
use PHPUnit\Framework\MockObject\Exception;
1717
use PHPUnit\Framework\MockObject\MockObject;
1818
use PHPUnit\Framework\TestCase;
19-
use Symfony\Component\Console\Exception\ExceptionInterface;
2019
use Symfony\Component\Console\Input\InputInterface;
2120
use Symfony\Component\Console\Output\BufferedOutput;
2221

@@ -62,10 +61,10 @@ class DeployMarkerTest extends TestCase
6261

6362
protected function setUp(): void
6463
{
65-
$this->deploymentsFactoryMock = $this->createMock(DeploymentsFactory::class); // @phpstan-ignore-line
64+
$this->deploymentsFactoryMock = $this->createMock(DeploymentsFactory::class);
6665
$this->deploymentMock = $this->createMock(Deployments::class);
67-
$this->configMock = $this->createMock(Config::class); // @phpstan-ignore-line
68-
$this->serviceShellUserMock = $this->createMock(ServiceShellUser::class); // @phpstan-ignore-line
66+
$this->configMock = $this->createMock(Config::class);
67+
$this->serviceShellUserMock = $this->createMock(ServiceShellUser::class);
6968
$this->inputMock = $this->createMock(InputInterface::class);
7069
$this->output = new BufferedOutput();
7170

@@ -364,7 +363,7 @@ public function testExecuteWithGenericException()
364363

365364
$this->deploymentMock->expects($this->once())
366365
->method('setDeployment')
367-
->willThrowException(new \Exception($exceptionMessage));
366+
->willThrowException(new Exception($exceptionMessage));
368367

369368
$result = $this->command->run($this->inputMock, $this->output);
370369

app/code/Magento/NewRelicReporting/Test/Unit/Model/NerdGraph/ClientTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ class ClientTest extends TestCase
6060

6161
protected function setUp(): void
6262
{
63-
$this->httpClientFactoryMock = $this->createMock(LaminasClientFactory::class); // @phpstan-ignore-line
64-
$this->serializerMock = $this->createMock(SerializerInterface::class); // @phpstan-ignore-line
65-
$this->configMock = $this->createMock(Config::class); // @phpstan-ignore-line
66-
$this->loggerMock = $this->createMock(LoggerInterface::class); // @phpstan-ignore-line
63+
$this->httpClientFactoryMock = $this->createMock(LaminasClientFactory::class);
64+
$this->serializerMock = $this->createMock(SerializerInterface::class);
65+
$this->configMock = $this->createMock(Config::class);
66+
$this->loggerMock = $this->createMock(LoggerInterface::class);
6767
$this->httpClientMock = $this->createMock(LaminasClient::class);
6868
$this->responseMock = $this->createMock(Response::class);
6969

app/code/Magento/NewRelicReporting/Test/Unit/Model/NerdGraph/DeploymentTrackerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ class DeploymentTrackerTest extends TestCase
4747
*/
4848
protected function setUp(): void
4949
{
50-
$this->nerdGraphClientMock = $this->createMock(Client::class); // @phpstan-ignore-line
51-
$this->configMock = $this->createMock(Config::class); // @phpstan-ignore-line
52-
$this->loggerMock = $this->createMock(LoggerInterface::class); // @phpstan-ignore-line
50+
$this->nerdGraphClientMock = $this->createMock(Client::class);
51+
$this->configMock = $this->createMock(Config::class);
52+
$this->loggerMock = $this->createMock(LoggerInterface::class);
5353

5454
$this->deploymentTracker = new DeploymentTracker(
5555
$this->nerdGraphClientMock,

dev/tests/integration/testsuite/Magento/NewRelicReporting/Model/NerdGraph/DeploymentWorkflowTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@
88
namespace Magento\NewRelicReporting\Model\NerdGraph;
99

1010
use Magento\Framework\App\Config\MutableScopeConfigInterface;
11+
use Magento\Framework\Exception\LocalizedException;
1112
use Magento\Framework\HTTP\LaminasClient;
13+
use Magento\Framework\HTTP\LaminasClientFactory;
1214
use Magento\Framework\ObjectManagerInterface;
15+
use Magento\Framework\Serialize\SerializerInterface;
1316
use Magento\NewRelicReporting\Model\Apm\Deployments;
1417
use Magento\NewRelicReporting\Model\Config;
1518
use Magento\NewRelicReporting\Model\NerdGraph\Client;
1619
use Magento\NewRelicReporting\Model\NerdGraph\DeploymentTracker;
1720
use Magento\TestFramework\Helper\Bootstrap;
1821
use PHPUnit\Framework\TestCase;
22+
use Psr\Log\LoggerInterface;
1923

2024
/**
2125
* Integration test for the complete deployment workflow
@@ -57,6 +61,7 @@ class DeploymentWorkflowTest extends TestCase
5761

5862
protected function setUp(): void
5963
{
64+
/** @phpstan-ignore-next-line */
6065
$this->objectManager = Bootstrap::getObjectManager();
6166
$this->config = $this->objectManager->get(Config::class);
6267
$this->mutableScopeConfig = $this->objectManager->get(MutableScopeConfigInterface::class);

0 commit comments

Comments
 (0)