Skip to content

Commit 989eda2

Browse files
committed
chore: update project version to 2.1.10 and upgrade dependencies
- Incremented project version to `2.1.10` in `composer.json`. - Updated `composer.lock` with new content hash and upgraded dependencies, including `nikic/php-parser` to `v5.7.0` and `phpunit/phpunit` to `9.6.31`. - Enhanced `ApiClientV2` to utilize GuzzleHttp Client for API requests and added header building functionality. - Updated `ReporterFactory` to pass additional parameters to the internal reporter creation methods.
1 parent 411590c commit 989eda2

File tree

4 files changed

+162
-22
lines changed

4 files changed

+162
-22
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"scripts": {
4141
"test": "phpunit"
4242
},
43-
"version": "2.1.9",
43+
"version": "2.1.10",
4444
"config": {
4545
"platform": {
4646
"php": "8.0"

composer.lock

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Client/ApiClientV2.php

Lines changed: 142 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Qase\PhpCommons\Client;
66

77
use Exception;
8+
use GuzzleHttp\Client;
89
use Qase\APIClientV2\Api\ResultsApi;
910
use Qase\APIClientV2\Configuration;
1011
use Qase\APIClientV2\Model\CreateResultsRequestV2;
@@ -22,12 +23,14 @@
2223
use Qase\PhpCommons\Models\Relation;
2324
use Qase\PhpCommons\Models\Result;
2425
use Qase\PhpCommons\Models\Step;
26+
use Qase\PhpCommons\Utils\HostInfo;
2527

2628
class ApiClientV2 extends ApiClientV1
2729
{
2830
private Configuration $clientV2Config;
31+
private Client $clientV2;
2932

30-
public function __construct(LoggerInterface $logger, TestopsConfig $config)
33+
public function __construct(LoggerInterface $logger, TestopsConfig $config, string $framework = "", string $reporterName = "", array $hostData = [])
3134
{
3235
parent::__construct($logger, $config);
3336

@@ -40,6 +43,12 @@ public function __construct(LoggerInterface $logger, TestopsConfig $config)
4043
} else {
4144
$this->clientV2Config->setHost('https://api-' . $host . '/v2');
4245
}
46+
47+
// Create GuzzleHttp Client with default headers
48+
$headers = $this->buildHeaders($framework, $reporterName, $hostData);
49+
$this->clientV2 = new Client([
50+
'headers' => $headers
51+
]);
4352
}
4453

4554
public function sendResults(string $code, int $runId, array $results): void
@@ -56,7 +65,7 @@ public function sendResults(string $code, int $runId, array $results): void
5665

5766
$this->logger->debug("Send results to project: " . json_encode($model));
5867

59-
$resultsApi = new ResultsApi($this->client, $this->clientV2Config);
68+
$resultsApi = new ResultsApi($this->clientV2, $this->clientV2Config);
6069
$resultsApi->createResultsV2($code, $runId, $model);
6170
} catch (Exception $e) {
6271
$this->logger->error("Error send results to project: " . $code . ', run: ' . $runId);
@@ -168,4 +177,135 @@ public function runUpdateExternalIssue(string $code, string $type, array $links)
168177
// Delegate to parent class (ApiClientV1) implementation
169178
parent::runUpdateExternalIssue($code, $type, $links);
170179
}
180+
181+
/**
182+
* Build X-Client and X-Platform headers based on HostInfo data
183+
*
184+
* @param string $framework Framework name
185+
* @param string $reporterName Reporter name
186+
* @param array $hostData Host data from HostInfo
187+
* @return array Headers array
188+
*/
189+
private function buildHeaders(string $framework = "", string $reporterName = "", array $hostData = []): array
190+
{
191+
$headers = [];
192+
193+
// If hostData is empty, try to get it from HostInfo (fallback for backward compatibility)
194+
if (empty($hostData)) {
195+
$hostInfo = new HostInfo();
196+
$hostData = $hostInfo->getHostInfo($framework, $reporterName);
197+
}
198+
199+
// Build X-Client header
200+
$xClientParts = [];
201+
202+
if (!empty($reporterName)) {
203+
$xClientParts[] = 'reporter=' . $reporterName;
204+
}
205+
206+
if (!empty($hostData['reporter'])) {
207+
$reporterVersion = $this->normalizeVersion($hostData['reporter']);
208+
if (!empty($reporterVersion)) {
209+
$xClientParts[] = 'reporter_version=v' . $reporterVersion;
210+
}
211+
}
212+
213+
if (!empty($framework)) {
214+
$xClientParts[] = 'framework=' . $framework;
215+
}
216+
217+
if (!empty($hostData['framework'])) {
218+
$frameworkVersion = $this->normalizeVersion($hostData['framework']);
219+
if (!empty($frameworkVersion)) {
220+
$xClientParts[] = 'framework_version=v' . $frameworkVersion;
221+
}
222+
}
223+
224+
if (!empty($hostData['apiClientV1'])) {
225+
$clientV1Version = $this->normalizeVersion($hostData['apiClientV1']);
226+
if (!empty($clientV1Version)) {
227+
$xClientParts[] = 'client_version_v1=v' . $clientV1Version;
228+
}
229+
}
230+
231+
if (!empty($hostData['apiClientV2'])) {
232+
$clientV2Version = $this->normalizeVersion($hostData['apiClientV2']);
233+
if (!empty($clientV2Version)) {
234+
$xClientParts[] = 'client_version_v2=v' . $clientV2Version;
235+
}
236+
}
237+
238+
if (!empty($hostData['commons'])) {
239+
$commonsVersion = $this->normalizeVersion($hostData['commons']);
240+
if (!empty($commonsVersion)) {
241+
$xClientParts[] = 'core_version=v' . $commonsVersion;
242+
}
243+
}
244+
245+
if (!empty($xClientParts)) {
246+
$headers['X-Client'] = implode(';', $xClientParts);
247+
}
248+
249+
// Build X-Platform header
250+
$xPlatformParts = [];
251+
252+
if (!empty($hostData['system'])) {
253+
$osName = ucfirst($hostData['system']);
254+
$xPlatformParts[] = 'os=' . $osName;
255+
}
256+
257+
if (!empty($hostData['arch'])) {
258+
$xPlatformParts[] = 'arch=' . $hostData['arch'];
259+
}
260+
261+
if (!empty($hostData['php'])) {
262+
$xPlatformParts[] = 'php=' . $hostData['php'];
263+
}
264+
265+
if (!empty($hostData['composer'])) {
266+
$xPlatformParts[] = 'composer=' . $hostData['composer'];
267+
}
268+
269+
if (!empty($xPlatformParts)) {
270+
$headers['X-Platform'] = implode(';', $xPlatformParts);
271+
}
272+
273+
return $headers;
274+
}
275+
276+
/**
277+
* Normalize version string by removing constraints and prefixes
278+
*
279+
* @param string $version Version string from composer.json/composer.lock
280+
* @return string Normalized version (e.g., "1.0.0" from "^1.0.0" or "v1.0.0")
281+
*/
282+
private function normalizeVersion(string $version): string
283+
{
284+
if (empty($version)) {
285+
return '';
286+
}
287+
288+
// Remove version constraints (^, ~, >=, etc.)
289+
$version = preg_replace('/^[^0-9]*/', '', $version);
290+
291+
// Remove 'v' prefix if present
292+
$version = ltrim($version, 'v');
293+
294+
// Extract version number (e.g., "1.0.0" from "1.0.0.0" or "1.0.0-dev")
295+
if (preg_match('/^(\d+\.\d+\.\d+)/', $version, $matches)) {
296+
return $matches[1];
297+
}
298+
299+
// If no match, try to extract at least major.minor
300+
if (preg_match('/^(\d+\.\d+)/', $version, $matches)) {
301+
return $matches[1] . '.0';
302+
}
303+
304+
// If still no match, try to extract at least major
305+
if (preg_match('/^(\d+)/', $version, $matches)) {
306+
return $matches[1] . '.0.0';
307+
}
308+
309+
return '';
310+
}
171311
}

src/Reporters/ReporterFactory.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public static function create(String $framework = "", String $reporterName = "")
2727
$hostData = $hostInfo->getHostInfo($framework, $reporterName);
2828
$logger->debug("Host data: " . json_encode($hostData));
2929
$state = new StateManager();
30-
$reporter = self::createInternalReporter($logger, $config, $state);
31-
$fallbackReporter = self::createInternalReporter($logger, $config, $state, true);
30+
$reporter = self::createInternalReporter($logger, $config, $state, false, $framework, $reporterName, $hostData);
31+
$fallbackReporter = self::createInternalReporter($logger, $config, $state, true, $framework, $reporterName, $hostData);
3232

3333
// Create status mapping utility
3434
$statusMapping = new StatusMapping($logger);
@@ -37,12 +37,12 @@ public static function create(String $framework = "", String $reporterName = "")
3737
return new CoreReporter($logger, $reporter, $fallbackReporter, $config->getRootSuite(), $statusMapping);
3838
}
3939

40-
private static function createInternalReporter(LoggerInterface $logger, QaseConfig $config, StateInterface $state, bool $fallback = false): ?InternalReporterInterface
40+
private static function createInternalReporter(LoggerInterface $logger, QaseConfig $config, StateInterface $state, bool $fallback = false, string $framework = "", string $reporterName = "", array $hostData = []): ?InternalReporterInterface
4141
{
4242
$mode = $fallback ? $config->getFallback() : $config->getMode();
4343

4444
if ($mode === 'testops') {
45-
return self::prepareTestopsReporter($logger, $config, $state);
45+
return self::prepareTestopsReporter($logger, $config, $state, $framework, $reporterName, $hostData);
4646
}
4747

4848
if ($mode === 'report') {
@@ -52,9 +52,9 @@ private static function createInternalReporter(LoggerInterface $logger, QaseConf
5252
return null;
5353
}
5454

55-
private static function prepareTestopsReporter(LoggerInterface $logger, QaseConfig $config, StateInterface $state): InternalReporterInterface
55+
private static function prepareTestopsReporter(LoggerInterface $logger, QaseConfig $config, StateInterface $state, string $framework = "", string $reporterName = "", array $hostData = []): InternalReporterInterface
5656
{
57-
$client = new ApiClientV2($logger, $config->testops);
57+
$client = new ApiClientV2($logger, $config->testops, $framework, $reporterName, $hostData);
5858
return new TestOpsReporter($client, $config, $state, $logger);
5959
}
6060
}

0 commit comments

Comments
 (0)