Skip to content

Commit

Permalink
Leave one old provider after update (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
karniv00l authored Sep 24, 2020
1 parent 0a06fa3 commit 31ad73b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/Service/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,27 @@ public function providers(string $version, string $hash): Option
*/
public function latestProvider(): Option
{
$providers = [];
foreach ($this->filesystem->listContents($this->name.'/provider') as $file) {
if ($file['type'] === 'file' && $file['extension'] === 'json') {
preg_match('/\$(?<hash>.+)$/', $file['filename'], $matches);

if (($hash = $matches['hash']) !== null) {
return $this->fetchMetadata(
sprintf('%s/provider/provider-latest$%s.json', $this->url, $hash),
$hash
);
}
if ($file['type'] === 'file' && $file['extension'] === 'json' && strpos($file['filename'], '$') !== false) {
$providers[$file['timestamp']] = $file;
}
}

return Option::none();
if ($providers === []) {
return Option::none();
}

ksort($providers);
$provider = array_pop($providers);

preg_match('/\$(?<hash>.+)$/', $provider['filename'], $matches);
$hash = $matches['hash'];

return $this->fetchMetadata(
sprintf('%s/provider/provider-latest$%s.json', $this->url, $hash),
$hash
);
}

/**
Expand Down Expand Up @@ -228,11 +235,19 @@ private function updateLatestProvider(array $files): void
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES);
$basePath = sprintf('%s/provider', $this->name);

$oldProviders = [];
foreach ($this->filesystem->listContents($basePath) as $file) {
if ($file['type'] !== 'file' || $file['extension'] !== 'json') {
continue;
}

$oldProviders[$file['timestamp']] = $file;
}

krsort($oldProviders);
array_shift($oldProviders);

foreach ($oldProviders as $file) {
$this->filesystem->delete($file['path']);
}

Expand Down
32 changes: 32 additions & 0 deletions tests/Functional/Controller/ProxyControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,38 @@ public function testPackagesAction(): void
', $this->client->getResponse()->getContent());
}

public function testPackagesActionMissingProvider(): void
{
$providerBasePath = __DIR__.'/../../Resources/packagist.org/provider';
$oldProviderName = $providerBasePath.'/provider-latest$bf7274d469c9a2c4b4d0babeeb112b40a3afd19a9887adb342671818360ae326.json';
$newProviderName = $providerBasePath.'/file.json';

rename($oldProviderName, $newProviderName);

$this->client->request('GET', '/packages.json', [], [], [
'HTTP_HOST' => 'repo.repman.wip',
]);

rename($newProviderName, $oldProviderName);

self::assertMatchesPattern('
{
"notify-batch": "http://repo.repman.wip/downloads",
"providers-url": "/p/%package%$%hash%.json",
"metadata-url": "/p2/%package%.json",
"search": "https://packagist.org/search.json?q=%query%&type=%type%",
"mirrors": [
{
"dist-url": "@[email protected]()",
"preferred": true
}
],
"providers-lazy-url": "/p/%package%",
"provider-includes": []
}
', $this->client->getResponse()->getContent());
}

public function testProviderLazyAction(): void
{
$response = $this->contentFromStream(fn () => $this->client->request('GET', '/p/buddy-works/repman', [], [], [
Expand Down

0 comments on commit 31ad73b

Please sign in to comment.