Skip to content

Commit b3bd048

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

File tree

3 files changed

+164
-77
lines changed

3 files changed

+164
-77
lines changed

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

Lines changed: 162 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace Magento\NewRelicReporting\Model\NerdGraph;
99

10-
use Magento\Framework\Exception\LocalizedException;
1110
use Magento\NewRelicReporting\Model\Config;
1211
use Psr\Log\LoggerInterface;
1312

@@ -70,85 +69,26 @@ public function createDeployment(
7069
?string $groupId = null
7170
): false|array {
7271
try {
73-
// Get entity GUID - this is required for NerdGraph deployment tracking
7472
$entityGuid = $this->getEntityGuid();
7573
if (!$entityGuid) {
7674
$this->logger->error('Cannot create NerdGraph deployment: Entity GUID not found');
7775
return false;
7876
}
7977

80-
$mutation = '
81-
mutation CreateDeployment($deployment: ChangeTrackingDeploymentInput!) {
82-
changeTrackingCreateDeployment(deployment: $deployment) {
83-
deploymentId
84-
entityGuid
85-
}
86-
}
87-
';
88-
89-
$variables = [
90-
'deployment' => [
91-
'entityGuid' => $entityGuid,
92-
'version' => $version ?: $this->generateVersion(),
93-
'description' => $description,
94-
'deploymentType' => 'BASIC',
95-
'timestamp' => time() * 1000 // NerdGraph expects milliseconds
96-
]
97-
];
98-
99-
// Add optional fields if provided
100-
if ($changelog) {
101-
$variables['deployment']['changelog'] = $changelog;
102-
}
103-
104-
if ($user) {
105-
$variables['deployment']['user'] = $user;
106-
}
107-
108-
if ($commit) {
109-
$variables['deployment']['commit'] = $commit;
110-
}
111-
112-
if ($deepLink) {
113-
$variables['deployment']['deepLink'] = $deepLink;
114-
}
115-
116-
if ($groupId) {
117-
$variables['deployment']['groupId'] = $groupId;
118-
}
119-
120-
$response = $this->nerdGraphClient->query($mutation, $variables);
121-
122-
$deploymentData = $response['data']['changeTrackingCreateDeployment'] ?? null;
123-
$deployedVersion = $variables['deployment']['version'];
124-
if ($deploymentData) {
125-
$this->logger->info(
126-
'NerdGraph deployment created successfully',
127-
[
128-
'deploymentId' => $deploymentData['deploymentId'],
129-
'entityGuid' => $deploymentData['entityGuid'],
130-
'version' => $deployedVersion,
131-
'description' => $description
132-
]
133-
);
134-
135-
return [
136-
'deploymentId' => $deploymentData['deploymentId'],
137-
'entityGuid' => $deploymentData['entityGuid'],
138-
'version' => $deployedVersion,
139-
'description' => $description,
140-
'changelog' => $changelog,
141-
'user' => $user,
142-
'commit' => $commit,
143-
'deepLink' => $deepLink,
144-
'groupId' => $groupId,
145-
'timestamp' => $variables['deployment']['timestamp']
146-
];
147-
}
78+
$variables = $this->buildDeploymentVariables(
79+
$entityGuid,
80+
$description,
81+
$version,
82+
$changelog,
83+
$user,
84+
$commit,
85+
$deepLink,
86+
$groupId
87+
);
14888

149-
$this->logger->error('NerdGraph deployment creation failed: No deployment data in response');
150-
return false;
89+
$response = $this->nerdGraphClient->query($this->getDeploymentMutation(), $variables);
15190

91+
return $this->processDeploymentResponse($response, $variables, $description, $changelog, $user, $commit, $deepLink, $groupId);
15292
} catch (\Exception $e) {
15393
$this->logger->error('NerdGraph deployment creation failed: ' . $e->getMessage());
15494
return false;
@@ -190,4 +130,154 @@ private function generateVersion(): string
190130
{
191131
return date('Y-m-d_H-i-s') . '_' . substr(hash('sha256', (string)time()), 0, 8);
192132
}
133+
134+
/**
135+
* Get the GraphQL mutation for creating deployment
136+
*
137+
* @return string
138+
*/
139+
private function getDeploymentMutation(): string
140+
{
141+
return '
142+
mutation CreateDeployment($deployment: ChangeTrackingDeploymentInput!) {
143+
changeTrackingCreateDeployment(deployment: $deployment) {
144+
deploymentId
145+
entityGuid
146+
}
147+
}
148+
';
149+
}
150+
151+
/**
152+
* Build deployment variables array
153+
*
154+
* @param string $entityGuid
155+
* @param string $description
156+
* @param string|null $version
157+
* @param string|null $changelog
158+
* @param string|null $user
159+
* @param string|null $commit
160+
* @param string|null $deepLink
161+
* @param string|null $groupId
162+
* @return array
163+
*/
164+
private function buildDeploymentVariables(
165+
string $entityGuid,
166+
string $description,
167+
?string $version,
168+
?string $changelog,
169+
?string $user,
170+
?string $commit,
171+
?string $deepLink,
172+
?string $groupId
173+
): array {
174+
$variables = [
175+
'deployment' => [
176+
'entityGuid' => $entityGuid,
177+
'version' => $version ?: $this->generateVersion(),
178+
'description' => $description,
179+
'deploymentType' => 'BASIC',
180+
'timestamp' => time() * 1000 // NerdGraph expects milliseconds
181+
]
182+
];
183+
184+
$this->addOptionalFields($variables, $changelog, $user, $commit, $deepLink, $groupId);
185+
186+
return $variables;
187+
}
188+
189+
/**
190+
* Add optional fields to deployment variables
191+
*
192+
* @param array $variables
193+
* @param string|null $changelog
194+
* @param string|null $user
195+
* @param string|null $commit
196+
* @param string|null $deepLink
197+
* @param string|null $groupId
198+
* @return void
199+
*/
200+
private function addOptionalFields(
201+
array &$variables,
202+
?string $changelog,
203+
?string $user,
204+
?string $commit,
205+
?string $deepLink,
206+
?string $groupId
207+
): void {
208+
if ($changelog) {
209+
$variables['deployment']['changelog'] = $changelog;
210+
}
211+
212+
if ($user) {
213+
$variables['deployment']['user'] = $user;
214+
}
215+
216+
if ($commit) {
217+
$variables['deployment']['commit'] = $commit;
218+
}
219+
220+
if ($deepLink) {
221+
$variables['deployment']['deepLink'] = $deepLink;
222+
}
223+
224+
if ($groupId) {
225+
$variables['deployment']['groupId'] = $groupId;
226+
}
227+
}
228+
229+
/**
230+
* Process deployment response
231+
*
232+
* @param array $response
233+
* @param array $variables
234+
* @param string $description
235+
* @param string|null $changelog
236+
* @param string|null $user
237+
* @param string|null $commit
238+
* @param string|null $deepLink
239+
* @param string|null $groupId
240+
* @return array|false
241+
*/
242+
private function processDeploymentResponse(
243+
array $response,
244+
array $variables,
245+
string $description,
246+
?string $changelog,
247+
?string $user,
248+
?string $commit,
249+
?string $deepLink,
250+
?string $groupId
251+
): false|array {
252+
$deploymentData = $response['data']['changeTrackingCreateDeployment'] ?? null;
253+
$deployedVersion = $variables['deployment']['version'];
254+
255+
if ($deploymentData) {
256+
$this->logger->info(
257+
'NerdGraph deployment created successfully',
258+
[
259+
'deploymentId' => $deploymentData['deploymentId'],
260+
'entityGuid' => $deploymentData['entityGuid'],
261+
'version' => $deployedVersion,
262+
'description' => $description
263+
]
264+
);
265+
266+
return [
267+
'deploymentId' => $deploymentData['deploymentId'],
268+
'entityGuid' => $deploymentData['entityGuid'],
269+
'version' => $deployedVersion,
270+
'description' => $description,
271+
'changelog' => $changelog,
272+
'user' => $user,
273+
'commit' => $commit,
274+
'deepLink' => $deepLink,
275+
'groupId' => $groupId,
276+
'timestamp' => $variables['deployment']['timestamp']
277+
];
278+
}
279+
280+
$this->logger->error('NerdGraph deployment creation failed: No deployment data in response');
281+
return false;
282+
}
193283
}

app/code/Magento/NewRelicReporting/Model/Observer/ReportSystemCacheFlushToNewRelic.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
/**
1313
* Observer to report system cache flush to New Relic
1414
* Class ReportSystemCacheFlushToNewRelic
15+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
1516
*/
1617
class ReportSystemCacheFlushToNewRelic implements ObserverInterface
1718
{

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,20 @@
88
namespace Magento\NewRelicReporting\Model\NerdGraph;
99

1010
use Magento\Framework\App\Config\MutableScopeConfigInterface;
11-
use Magento\Framework\App\Config\ScopeConfigInterface;
12-
use Magento\Framework\Exception\LocalizedException;
1311
use Magento\Framework\HTTP\LaminasClient;
14-
use Magento\Framework\HTTP\LaminasClientFactory;
1512
use Magento\Framework\ObjectManagerInterface;
16-
use Magento\Framework\Serialize\SerializerInterface;
1713
use Magento\NewRelicReporting\Model\Apm\Deployments;
1814
use Magento\NewRelicReporting\Model\Config;
1915
use Magento\NewRelicReporting\Model\NerdGraph\Client;
2016
use Magento\NewRelicReporting\Model\NerdGraph\DeploymentTracker;
2117
use Magento\TestFramework\Helper\Bootstrap;
2218
use PHPUnit\Framework\TestCase;
23-
use Psr\Log\LoggerInterface;
2419

2520
/**
2621
* Integration test for the complete deployment workflow
2722
*
2823
* @magentoAppIsolation enabled
24+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2925
*/
3026
class DeploymentWorkflowTest extends TestCase
3127
{

0 commit comments

Comments
 (0)