Skip to content

Fixed getReleaseAssetsForPackage() for repositories with names that do not match the package name. #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 14, 2024
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
25 changes: 25 additions & 0 deletions src/DependencyResolver/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

use function array_key_exists;
use function array_map;
use function array_slice;
use function explode;
use function implode;
use function parse_url;
use function str_contains;
use function str_starts_with;

/**
* @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks
Expand Down Expand Up @@ -71,4 +77,23 @@ public function prettyNameAndVersion(): string
{
return $this->name . ':' . $this->version;
}

public function githubOrgAndRepository(): string
{
if ($this->downloadUrl === null || str_contains($this->downloadUrl, '/' . $this->name . '/')) {
return $this->name;
}

if (! str_starts_with($this->downloadUrl, 'https://api.github.com/repos/')) {
return $this->name;
}

$parsed = parse_url($this->downloadUrl);
if ($parsed === false || ! array_key_exists('path', $parsed)) {
return $this->name;
}

// Converts https://api.github.com/repos/<user>/<repository>/zipball/<sha>" to "<user>/<repository>"
return implode('/', array_slice(explode('/', $parsed['path']), 2, 2));
}
}
3 changes: 1 addition & 2 deletions src/Downloading/GithubPackageReleaseAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ private function selectMatchingReleaseAsset(TargetPlatform $targetPlatform, Pack
/** @return list<array{name: non-empty-string, browser_download_url: non-empty-string, ...}> */
private function getReleaseAssetsForPackage(Package $package): array
{
// @todo confirm prettyName will always match the repo name - it might not
$request = AddAuthenticationHeader::withAuthHeaderFromComposer(
new Request('GET', $this->githubApiBaseUrl . '/repos/' . $package->name . '/releases/tags/' . $package->version),
new Request('GET', $this->githubApiBaseUrl . '/repos/' . $package->githubOrgAndRepository() . '/releases/tags/' . $package->version),
$package,
$this->authHelper,
);
Expand Down
39 changes: 39 additions & 0 deletions test/unit/DependencyResolver/PackageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

use Composer\Package\CompletePackage;
use Php\Pie\DependencyResolver\Package;
use Php\Pie\ExtensionName;
use Php\Pie\ExtensionType;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

#[CoversClass(Package::class)]
Expand Down Expand Up @@ -38,4 +41,40 @@ public function testFromComposerCompletePackageWithExtensionName(): void
self::assertSame('vendor/foo:1.2.3', $package->prettyNameAndVersion());
self::assertNull($package->downloadUrl);
}

/**
* @return array<string, array{0: string, 1: string|null, 2: string}>
*
* @psalm-suppress PossiblyUnusedMethod https://github.com/psalm/psalm-plugin-phpunit/issues/131
*/
public function githubOrgAndRepoFromPackage(): array
{
return [
'noDownloadUrl' => ['foo/bar', null, 'foo/bar'],
'gitlabMatchingPackage' => ['foo/bar', 'https://gitlab.com/api/v4/projects/foo%2Fbar/repository/archive.zip?sha=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'foo/bar'],
'gitlabDifferentPackage' => ['foo/bar', 'https://gitlab.com/api/v4/projects/abc%2Fdef/repository/archive.zip?sha=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'foo/bar'],
'githubMatchingPackage' => ['foo/bar', 'https://api.github.com/repos/foo/bar/zipball/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'foo/bar'],
'githubDifferentPackage' => ['foo/bar', 'https://api.github.com/repos/abc/def/zipball/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'abc/def'],
'githubIncompleteUrl' => ['foo/bar', 'https://api.github.com/', 'foo/bar'],
];
}

#[DataProvider('githubOrgAndRepoFromPackage')]
public function testGithubOrgAndRepo(string $composerPackageName, string|null $downloadUrl, string $expectedGithubOrgAndRepo): void
{
$package = new Package(
ExtensionType::PhpModule,
ExtensionName::normaliseFromString('foo'),
$composerPackageName,
'1.2.3',
$downloadUrl,
[],
null,
'1.2.3.0',
true,
true,
);

self::assertSame($expectedGithubOrgAndRepo, $package->githubOrgAndRepository());
}
}