|
7 | 7 |
|
8 | 8 | namespace Magento\NewRelicReporting\Model\NerdGraph; |
9 | 9 |
|
10 | | -use Magento\Framework\Exception\LocalizedException; |
11 | 10 | use Magento\NewRelicReporting\Model\Config; |
12 | 11 | use Psr\Log\LoggerInterface; |
13 | 12 |
|
@@ -70,85 +69,26 @@ public function createDeployment( |
70 | 69 | ?string $groupId = null |
71 | 70 | ): false|array { |
72 | 71 | try { |
73 | | - // Get entity GUID - this is required for NerdGraph deployment tracking |
74 | 72 | $entityGuid = $this->getEntityGuid(); |
75 | 73 | if (!$entityGuid) { |
76 | 74 | $this->logger->error('Cannot create NerdGraph deployment: Entity GUID not found'); |
77 | 75 | return false; |
78 | 76 | } |
79 | 77 |
|
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 | + ); |
148 | 88 |
|
149 | | - $this->logger->error('NerdGraph deployment creation failed: No deployment data in response'); |
150 | | - return false; |
| 89 | + $response = $this->nerdGraphClient->query($this->getDeploymentMutation(), $variables); |
151 | 90 |
|
| 91 | + return $this->processDeploymentResponse($response, $variables, $description, $changelog, $user, $commit, $deepLink, $groupId); |
152 | 92 | } catch (\Exception $e) { |
153 | 93 | $this->logger->error('NerdGraph deployment creation failed: ' . $e->getMessage()); |
154 | 94 | return false; |
@@ -190,4 +130,154 @@ private function generateVersion(): string |
190 | 130 | { |
191 | 131 | return date('Y-m-d_H-i-s') . '_' . substr(hash('sha256', (string)time()), 0, 8); |
192 | 132 | } |
| 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 | + } |
193 | 283 | } |
0 commit comments