Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ name: Integration
on:
pull_request:

env:
DO_NOT_TRACK: 1

jobs:
build:
runs-on: ubuntu-24.04
Expand Down
15 changes: 14 additions & 1 deletion Controller/InstallController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Shopware\WebInstaller\Services\RecoveryManager;
use Shopware\WebInstaller\Services\ReleaseInfoProvider;
use Shopware\WebInstaller\Services\StreamedCommandResponseGenerator;
use Shopware\WebInstaller\Services\TrackingEvent;
use Shopware\WebInstaller\Services\TrackingService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -28,6 +30,7 @@ public function __construct(
private readonly ReleaseInfoProvider $releaseInfoProvider,
private readonly ProjectComposerJsonUpdater $projectComposerJsonUpdater,
private readonly LanguageProvider $languageProvider,
private readonly TrackingService $trackingService,
) {}

#[Route('/install', name: 'install', defaults: ['step' => 2])]
Expand All @@ -45,6 +48,12 @@ public function index(): Response
public function run(Request $request): StreamedResponse
{
$shopwareVersion = $request->query->get('shopwareVersion', '');
$trackingId = $request->getSession()->get('trackingId', '');

$this->trackingService->track(TrackingEvent::InstallStarted, $trackingId, [
'shopware_version' => $shopwareVersion,
]);

$folder = $this->recoveryManager->getProjectDir();

$fs = new Filesystem();
Expand All @@ -61,7 +70,11 @@ public function run(Request $request): StreamedResponse
$shopwareVersion
);

$finish = function (Process $process) use ($request): void {
$finish = function (Process $process) use ($request, $shopwareVersion, $trackingId): void {
$this->trackingService->track($process->isSuccessful() ? TrackingEvent::InstallCompleted : TrackingEvent::InstallFailed, $trackingId, [
'shopware_version' => $shopwareVersion,
]);

$data = [
'success' => $process->isSuccessful(),
];
Expand Down
18 changes: 17 additions & 1 deletion Controller/UpdateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use Shopware\WebInstaller\Services\RecoveryManager;
use Shopware\WebInstaller\Services\ReleaseInfoProvider;
use Shopware\WebInstaller\Services\StreamedCommandResponseGenerator;
use Shopware\WebInstaller\Services\TrackingEvent;
use Shopware\WebInstaller\Services\TrackingService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -31,6 +33,7 @@ public function __construct(
private readonly StreamedCommandResponseGenerator $streamedCommandResponseGenerator,
private readonly ProjectComposerJsonUpdater $projectComposerJsonUpdater,
private readonly LanguageProvider $languageProvider,
private readonly TrackingService $trackingService,
) {}

#[Route('/update', name: 'update', defaults: ['step' => 2], methods: ['GET'])]
Expand Down Expand Up @@ -77,7 +80,15 @@ public function run(Request $request): Response
$version = $request->query->get('shopwareVersion', '');

$shopwarePath = $this->recoveryManager->getShopwareLocation();
$currentVersion = $this->recoveryManager->getCurrentShopwareVersion($shopwarePath);
$composerJsonPath = $shopwarePath . '/composer.json';
$trackingId = $request->getSession()->get('trackingId', '');

$this->trackingService->track(TrackingEvent::UpdateStarted, $trackingId, [
'shopware_version_from' => $currentVersion,
'shopware_version_to' => $version,
'is_flex_project' => $this->recoveryManager->isFlexProject($shopwarePath),
]);

$composerJsonBackup = new FileBackup($composerJsonPath);
$composerJsonBackup->backup();
Expand All @@ -103,10 +114,15 @@ public function run(Request $request): Response
'--no-scripts',
'-v',
'--with-all-dependencies', // update all packages
], function (Process $process) use ($composerJsonBackup): void {
], function (Process $process) use ($composerJsonBackup, $trackingId, $currentVersion, $version): void {
$process->isSuccessful()
? $composerJsonBackup->remove()
: $composerJsonBackup->restore();

$this->trackingService->track($process->isSuccessful() ? TrackingEvent::UpdateCompleted : TrackingEvent::UpdateFailed, $trackingId, [
'shopware_version_from' => $currentVersion,
'shopware_version_to' => $version,
]);
});
}

Expand Down
55 changes: 55 additions & 0 deletions Listener/TrackingListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Shopware\WebInstaller\Listener;

use Shopware\WebInstaller\Services\TrackingEvent;
use Shopware\WebInstaller\Services\TrackingService;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;

/**
* @internal
*/
class TrackingListener
{
public function __construct(private readonly TrackingService $trackingService) {}

#[AsEventListener(RequestEvent::class, priority: 10)]
public function __invoke(RequestEvent $event): void
{
$request = $event->getRequest();

if (!$event->isMainRequest()) {
return;
}

$session = $request->getSession();

if ($session->has('trackingId')) {
return;
}

$trackingId = bin2hex(random_bytes(16));
$session->set('trackingId', $trackingId);

$referer = $request->headers->get('referer', '');
$source = 'direct';

if ($referer !== '') {
$path = parse_url($referer, \PHP_URL_PATH);

if (\is_string($path) && str_contains($path, '/admin')) {
$source = 'admin';
}
}

$this->trackingService->track(TrackingEvent::Visit, $trackingId, [
'source' => $source,
'language' => $request->getLocale(),
'php_version' => \PHP_VERSION,
'os' => \PHP_OS_FAMILY,
]);
}
}
19 changes: 19 additions & 0 deletions Services/TrackingEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Shopware\WebInstaller\Services;

/**
* @internal
*/
enum TrackingEvent: string
{
case Visit = 'visit';
case InstallStarted = 'install.started';
case InstallCompleted = 'install.completed';
case InstallFailed = 'install.failed';
case UpdateStarted = 'update.started';
case UpdateCompleted = 'update.completed';
case UpdateFailed = 'update.failed';
}
50 changes: 50 additions & 0 deletions Services/TrackingService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Shopware\WebInstaller\Services;

use Composer\Util\Platform;

/**
* @internal
*/
class TrackingService
{
private const DEFAULT_TRACKING_DOMAIN = 'udp.usage.shopware.io';

private \Socket|false $socket;

private string $domain;

public function __construct()
{
$this->socket = @socket_create(\AF_INET, \SOCK_DGRAM, \SOL_UDP);

$domain = Platform::getEnv('SHOPWARE_TRACKING_DOMAIN');
$this->domain = $domain !== false ? $domain : self::DEFAULT_TRACKING_DOMAIN;
}

/**
* @param array<string, string|int|float|bool> $tags
*/
public function track(TrackingEvent $eventName, string $userId, array $tags = []): void
{
if (Platform::getEnv('DO_NOT_TRACK') !== false) {
return;
}

if ($this->socket === false) {
return;
}

$payload = json_encode([
'event' => 'web_installer.' . $eventName->value,
'tags' => $tags,
'user_id' => $userId,
'timestamp' => (new \DateTime())->format(\DateTimeInterface::ATOM),
], \JSON_THROW_ON_ERROR);

@socket_sendto($this->socket, $payload, \strlen($payload), 0, $this->domain, 9000);
}
}
8 changes: 5 additions & 3 deletions Tests/Controller/InstallControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Shopware\WebInstaller\Services\RecoveryManager;
use Shopware\WebInstaller\Services\ReleaseInfoProvider;
use Shopware\WebInstaller\Services\StreamedCommandResponseGenerator;
use Shopware\WebInstaller\Services\TrackingService;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -39,7 +40,7 @@ public function testStartPage(): void
$responseGenerator = $this->createMock(StreamedCommandResponseGenerator::class);
$responseGenerator->method('runJSON')->willReturn(new StreamedResponse());

$controller = new InstallController($recovery, $responseGenerator, $this->createMock(ReleaseInfoProvider::class), $this->createMock(ProjectComposerJsonUpdater::class), $this->createMock(LanguageProvider::class));
$controller = new InstallController($recovery, $responseGenerator, $this->createMock(ReleaseInfoProvider::class), $this->createMock(ProjectComposerJsonUpdater::class), $this->createMock(LanguageProvider::class), $this->createMock(TrackingService::class));
$controller->setContainer($this->buildContainer());

$response = $controller->index();
Expand Down Expand Up @@ -75,7 +76,7 @@ public function testInstall(): void
])
->willReturn(new StreamedResponse());

$controller = new InstallController($recovery, $responseGenerator, $this->createMock(ReleaseInfoProvider::class), $this->createMock(ProjectComposerJsonUpdater::class), $this->createMock(LanguageProvider::class));
$controller = new InstallController($recovery, $responseGenerator, $this->createMock(ReleaseInfoProvider::class), $this->createMock(ProjectComposerJsonUpdater::class), $this->createMock(LanguageProvider::class), $this->createMock(TrackingService::class));
$controller->setContainer($this->buildContainer());

$request = new Request();
Expand Down Expand Up @@ -190,7 +191,8 @@ private function createInstallControllerAndRequestAndTemporaryDirectory(
$responseGenerator,
$this->createMock(ReleaseInfoProvider::class),
$this->createMock(ProjectComposerJsonUpdater::class),
$this->createMock(LanguageProvider::class)
$this->createMock(LanguageProvider::class),
$this->createMock(TrackingService::class),
);
$installController->setContainer($this->buildContainer());

Expand Down
11 changes: 11 additions & 0 deletions Tests/Controller/UpdateControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Shopware\WebInstaller\Services\RecoveryManager;
use Shopware\WebInstaller\Services\ReleaseInfoProvider;
use Shopware\WebInstaller\Services\StreamedCommandResponseGenerator;
use Shopware\WebInstaller\Services\TrackingService;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -48,6 +49,7 @@ public function testRedirectWhenNotInstalled(): void
$this->createMock(StreamedCommandResponseGenerator::class),
$this->createMock(ProjectComposerJsonUpdater::class),
$this->createMock(LanguageProvider::class),
$this->createMock(TrackingService::class),
);

$controller->setContainer($this->buildContainer());
Expand All @@ -74,6 +76,7 @@ public function testRedirectToFinishWhenNoUpdateThere(): void
$this->createMock(StreamedCommandResponseGenerator::class),
$this->createMock(ProjectComposerJsonUpdater::class),
$this->createMock(LanguageProvider::class),
$this->createMock(TrackingService::class),
);
$controller->setContainer($this->buildContainer());

Expand All @@ -99,6 +102,7 @@ public function testUpdateThereRendersTemplate(): void
$this->createMock(StreamedCommandResponseGenerator::class),
$this->createMock(ProjectComposerJsonUpdater::class),
$this->createMock(LanguageProvider::class),
$this->createMock(TrackingService::class),
);
$controller->setContainer($this->buildContainer());

Expand Down Expand Up @@ -145,6 +149,7 @@ public function testMigrateFlex(): void
$this->createMock(StreamedCommandResponseGenerator::class),
$this->createMock(ProjectComposerJsonUpdater::class),
$this->createMock(LanguageProvider::class),
$this->createMock(TrackingService::class),
);

$controller->setContainer($this->buildContainer());
Expand Down Expand Up @@ -182,6 +187,7 @@ public function testPrepare(): void
$responseGenerator,
$this->createMock(ProjectComposerJsonUpdater::class),
$this->createMock(LanguageProvider::class),
$this->createMock(TrackingService::class),
);
$controller->setContainer($this->buildContainer());

Expand Down Expand Up @@ -220,6 +226,7 @@ public function testFinishUpdate(): void
$responseGenerator,
$this->createMock(ProjectComposerJsonUpdater::class),
$this->createMock(LanguageProvider::class),
$this->createMock(TrackingService::class),
);
$controller->setContainer($this->buildContainer());

Expand Down Expand Up @@ -277,6 +284,7 @@ public function testUpdateChangesComposerJSON(string $shopwareVersion): void
$responseGenerator,
$this->createMock(ProjectComposerJsonUpdater::class),
$this->createMock(LanguageProvider::class),
$this->createMock(TrackingService::class),
);
$controller->setContainer($this->buildContainer());

Expand Down Expand Up @@ -334,6 +342,7 @@ public function testUpdateToRC(): void
$responseGenerator,
$this->createMock(ProjectComposerJsonUpdater::class),
$this->createMock(LanguageProvider::class),
$this->createMock(TrackingService::class),
);
$controller->setContainer($this->buildContainer());

Expand Down Expand Up @@ -407,6 +416,7 @@ public function testUpdateChangesComposerJSONInTestMode(): void
$responseGenerator,
$this->createMock(ProjectComposerJsonUpdater::class),
$this->createMock(LanguageProvider::class),
$this->createMock(TrackingService::class),
);
$controller->setContainer($this->buildContainer());

Expand Down Expand Up @@ -457,6 +467,7 @@ public function testResetConfig(): void
$responseGenerator,
$this->createMock(ProjectComposerJsonUpdater::class),
$this->createMock(LanguageProvider::class),
$this->createMock(TrackingService::class),
);
$controller->setContainer($this->buildContainer());

Expand Down