Skip to content

Commit 413e982

Browse files
committed
AC-15461: Migration New Relic from REST v2 to NerdGraph (GraphQL)
1 parent b2f21bc commit 413e982

File tree

11 files changed

+3581
-31
lines changed

11 files changed

+3581
-31
lines changed

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

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
use Laminas\Http\Exception\RuntimeException;
99
use Laminas\Http\Request;
1010
use Magento\Framework\Exception\LocalizedException;
11-
use Magento\Framework\HTTP\LaminasClient;
1211
use Magento\Framework\HTTP\LaminasClientFactory;
1312
use Magento\Framework\Serialize\SerializerInterface;
1413
use Magento\NewRelicReporting\Model\Config;
15-
use Magento\NewRelicReporting\Model\NerdGraph\Client as NerdGraphClient;
14+
use Magento\NewRelicReporting\Model\Config\Source\ApiMode;
1615
use Magento\NewRelicReporting\Model\NerdGraph\DeploymentTracker;
1716
use Psr\Log\LoggerInterface;
1817

@@ -21,10 +20,7 @@
2120
*/
2221
class Deployments
2322
{
24-
/**
25-
* API mode constant for NerdGraph (GraphQL) API
26-
*/
27-
private const NERDGRAPH = 'nerdgraph';
23+
2824
/**
2925
* API URL for New Relic deployments
3026
*/
@@ -43,22 +39,17 @@ class Deployments
4339
/**
4440
* @var LaminasClientFactory $clientFactory
4541
*/
46-
protected $clientFactory;
42+
protected LaminasClientFactory $clientFactory;
4743

4844
/**
4945
* @var SerializerInterface
5046
*/
51-
private $serializer;
52-
53-
/**
54-
* @var NerdGraphClient
55-
*/
56-
private $nerdGraphClient;
47+
private SerializerInterface $serializer;
5748

5849
/**
5950
* @var DeploymentTracker
6051
*/
61-
private $deploymentTracker;
52+
private DeploymentTracker $deploymentTracker;
6253

6354
/**
6455
* Constructor
@@ -67,22 +58,19 @@ class Deployments
6758
* @param LoggerInterface $logger
6859
* @param LaminasClientFactory $clientFactory
6960
* @param SerializerInterface $serializer
70-
* @param NerdGraphClient $nerdGraphClient
7161
* @param DeploymentTracker $deploymentTracker
7262
*/
7363
public function __construct(
7464
Config $config,
7565
LoggerInterface $logger,
7666
LaminasClientFactory $clientFactory,
7767
SerializerInterface $serializer,
78-
NerdGraphClient $nerdGraphClient,
7968
DeploymentTracker $deploymentTracker
8069
) {
8170
$this->config = $config;
8271
$this->logger = $logger;
8372
$this->clientFactory = $clientFactory;
8473
$this->serializer = $serializer;
85-
$this->nerdGraphClient = $nerdGraphClient;
8674
$this->deploymentTracker = $deploymentTracker;
8775
}
8876

@@ -100,6 +88,7 @@ public function __construct(
10088
* @param string|null $groupId Group ID (NerdGraph only)
10189
*
10290
* @return bool|string|array
91+
* @throws LocalizedException
10392
*/
10493
public function setDeployment(
10594
string $description,
@@ -113,7 +102,7 @@ public function setDeployment(
113102
// Check API mode configuration
114103
$apiMode = $this->config->getApiMode();
115104

116-
if ($apiMode === self::NERDGRAPH) {
105+
if ($apiMode === ApiMode::MODE_NERDGRAPH) {
117106
return $this->createNerdGraphDeployment(
118107
$description,
119108
$change,
@@ -137,7 +126,7 @@ public function setDeployment(
137126
* @param string|null $revision
138127
* @return bool|string
139128
*/
140-
private function createV2RestDeployment($description, $change, $user, $revision)
129+
private function createV2RestDeployment(string $description, bool|string $change, bool|string $user, ?string $revision): bool|string
141130
{
142131
$apiUrl = $this->config->getNewRelicApiUrl();
143132
if (empty($apiUrl)) {
@@ -147,7 +136,6 @@ private function createV2RestDeployment($description, $change, $user, $revision)
147136

148137
$apiUrl = sprintf($apiUrl, $this->config->getNewRelicAppId());
149138

150-
/** @var LaminasClient $client */
151139
$client = $this->clientFactory->create();
152140
$client->setUri($apiUrl);
153141
$client->setMethod(Request::METHOD_POST);
@@ -200,7 +188,7 @@ private function createV2RestDeployment($description, $change, $user, $revision)
200188
* @return array|false
201189
* @throws LocalizedException
202190
*/
203-
private function createNerdGraphDeployment($description, $change, $user, $revision, $commit, $deepLink, $groupId)
191+
private function createNerdGraphDeployment(string $description, bool|string $change, bool|string $user, ?string $revision, ?string $commit, ?string $deepLink, ?string $groupId): false|array
204192
{
205193
return $this->deploymentTracker->createDeployment(
206194
$description,

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function query(string $query, array $variables = []): array
8080
$client->setMethod(Request::METHOD_POST);
8181
$client->setHeaders([
8282
'Content-Type' => 'application/json',
83-
'API-Key' => $apiKey
83+
'Api-Key' => $apiKey
8484
]);
8585

8686
$requestBody = [
@@ -185,8 +185,11 @@ public function getEntityGuidFromApplication(?string $appName = null, ?string $a
185185
);
186186
return $firstEntity['guid'];
187187
} catch (\RuntimeException $e) {
188-
$this->logger->error('Failed to get entity GUID: ' . $e->getMessage());
189-
$this->logger->error('Search query was: ' . $searchQuery);
188+
$this->logger->error(sprintf(
189+
'Failed to get entity GUID. Search query: %s. Error: %s',
190+
$searchQuery,
191+
$e->getMessage()
192+
));
190193
return null;
191194
}
192195
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function createDeployment(
6969
?string $commit = null,
7070
?string $deepLink = null,
7171
?string $groupId = null
72-
) {
72+
): false|array {
7373
try {
7474
// Get entity GUID - this is required for NerdGraph deployment tracking
7575
$entityGuid = $this->getEntityGuid();
@@ -121,21 +121,22 @@ public function createDeployment(
121121
$response = $this->nerdGraphClient->query($mutation, $variables);
122122

123123
$deploymentData = $response['data']['changeTrackingCreateDeployment'] ?? null;
124+
$deployedVersion = $variables['deployment']['version'];
124125
if ($deploymentData) {
125126
$this->logger->info(
126127
'NerdGraph deployment created successfully',
127128
[
128129
'deploymentId' => $deploymentData['deploymentId'],
129130
'entityGuid' => $deploymentData['entityGuid'],
130-
'version' => $version,
131+
'version' => $deployedVersion,
131132
'description' => $description
132133
]
133134
);
134135

135136
return [
136137
'deploymentId' => $deploymentData['deploymentId'],
137138
'entityGuid' => $deploymentData['entityGuid'],
138-
'version' => $variables['deployment']['version'],
139+
'version' => $deployedVersion,
139140
'description' => $description,
140141
'changelog' => $changelog,
141142
'user' => $user,
@@ -149,9 +150,6 @@ public function createDeployment(
149150
$this->logger->error('NerdGraph deployment creation failed: No deployment data in response');
150151
return false;
151152

152-
} catch (LocalizedException $e) {
153-
// Re-throw configuration/migration errors so they reach the user
154-
throw $e;
155153
} catch (\Exception $e) {
156154
$this->logger->error('NerdGraph deployment creation failed: ' . $e->getMessage());
157155
return false;

0 commit comments

Comments
 (0)