From 5c07d1d1542558d13dbefe34c856d4b01451d513 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Wed, 18 Dec 2024 11:34:25 +0800 Subject: [PATCH 1/2] Discard unsupported FUNDING.yml URL values --- src/Composer/Repository/Vcs/GitHubDriver.php | 13 +++ .../Test/Repository/Vcs/GitHubDriverTest.php | 96 +++++++++++++++++++ 2 files changed, 109 insertions(+) diff --git a/src/Composer/Repository/Vcs/GitHubDriver.php b/src/Composer/Repository/Vcs/GitHubDriver.php index 97a334f94..f59ce4da8 100644 --- a/src/Composer/Repository/Vcs/GitHubDriver.php +++ b/src/Composer/Repository/Vcs/GitHubDriver.php @@ -287,6 +287,19 @@ class GitHubDriver extends VcsDriver case 'buy_me_a_coffee': $result[$key]['url'] = 'https://www.buymeacoffee.com/' . basename($item['url']); break; + case 'custom': + $bits = parse_url($item['url']); + if ($bits === false) { + unset($result[$key]); + break; + } + + if (!array_key_exists('scheme', $bits) && !array_key_exists('host', $bits)) { + $this->io->writeError('Funding URL '.$item['url'].' not in a supported format.'); + unset($result[$key]); + break; + } + break; } } diff --git a/tests/Composer/Test/Repository/Vcs/GitHubDriverTest.php b/tests/Composer/Test/Repository/Vcs/GitHubDriverTest.php index 9902af644..4be3a3816 100644 --- a/tests/Composer/Test/Repository/Vcs/GitHubDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/GitHubDriverTest.php @@ -237,6 +237,102 @@ class GitHubDriverTest extends TestCase self::assertSame('https://github.com/composer/packagist/tree/feature/3.2-foo', $data['support']['source']); } + /** + * @dataProvider fundingUrlProvider + * @param array|null $expected + */ + public function testFundingFormat(string $funding, ?array $expected): void + { + $repoUrl = 'http://github.com/composer/packagist'; + $repoApiUrl = 'https://api.github.com/repos/composer/packagist'; + $identifier = 'feature/3.2-foo'; + $sha = 'SOMESHA'; + + $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); + $io->expects($this->any()) + ->method('isInteractive') + ->will($this->returnValue(true)); + + $httpDownloader = $this->getHttpDownloaderMock($io, $this->config); + $httpDownloader->expects( + [ + ['url' => $repoApiUrl, 'body' => '{"master_branch": "test_master", "owner": {"login": "composer"}, "name": "packagist"}'], + ['url' => 'https://api.github.com/repos/composer/packagist/contents/composer.json?ref=feature%2F3.2-foo', 'body' => '{"encoding":"base64","content":"'.base64_encode('{"support": {"source": "'.$repoUrl.'" }}').'"}'], + ['url' => 'https://api.github.com/repos/composer/packagist/commits/feature%2F3.2-foo', 'body' => '{"commit": {"committer":{ "date": "2012-09-10"}}}'], + ['url' => 'https://api.github.com/repos/composer/packagist/contents/.github/FUNDING.yml', 'body' => '{"encoding": "base64", "content": "'.base64_encode($funding).'"}'], + ], + true + ); + + $repoConfig = [ + 'url' => $repoUrl, + ]; + + $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, $httpDownloader, $this->getProcessExecutorMock()); + $gitHubDriver->initialize(); + $this->setAttribute($gitHubDriver, 'tags', [$identifier => $sha]); + $this->setAttribute($gitHubDriver, 'branches', ['test_master' => $sha]); + + $data = $gitHubDriver->getComposerInformation($identifier); + + self::assertIsArray($data); + if ($expected === null) { + self::assertArrayNotHasKey('funding', $data); + } else { + self::assertSame(array_values($expected), array_values($data['funding'])); + } + } + + public static function fundingUrlProvider(): array + { + return [ + [ + 'custom: example.com', + null, + ], + [ + 'custom: [example.com]', + null, + ], + [ + 'custom: "https://example.com"', + [ + [ + 'type' => 'custom', + 'url' => 'https://example.com', + ], + ], + ], + [ + 'custom: ["https://example.com"]', + [ + [ + 'type' => 'custom', + 'url' => 'https://example.com', + ], + ], + ], + [ + 'custom: ["https://example.com", example.org]', + [ + [ + 'type' => 'custom', + 'url' => 'https://example.com', + ], + ], + ], + [ + 'custom: [example.net/funding, "https://example.com", example.org]', + [ + [ + 'type' => 'custom', + 'url' => 'https://example.com', + ], + ], + ], + ]; + } + public function testPublicRepositoryArchived(): void { $repoUrl = 'http://github.com/composer/packagist'; From 924527cda6a17b18718c65cfb8f08ba054920619 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 10 Jan 2025 16:42:50 +0100 Subject: [PATCH 2/2] Allow using short form URLs like foo.com if they are very simple --- src/Composer/Repository/Vcs/GitHubDriver.php | 5 +++++ .../Test/Repository/Vcs/GitHubDriverTest.php | 22 +++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/Composer/Repository/Vcs/GitHubDriver.php b/src/Composer/Repository/Vcs/GitHubDriver.php index 31399f190..12f81189d 100644 --- a/src/Composer/Repository/Vcs/GitHubDriver.php +++ b/src/Composer/Repository/Vcs/GitHubDriver.php @@ -301,6 +301,11 @@ class GitHubDriver extends VcsDriver } if (!array_key_exists('scheme', $bits) && !array_key_exists('host', $bits)) { + if (Preg::isMatch('{^[a-z0-9-]++\.[a-z]{2,3}$}', $item['url'])) { + $result[$key]['url'] = 'https://'.$item['url']; + break; + } + $this->io->writeError('Funding URL '.$item['url'].' not in a supported format.'); unset($result[$key]); break; diff --git a/tests/Composer/Test/Repository/Vcs/GitHubDriverTest.php b/tests/Composer/Test/Repository/Vcs/GitHubDriverTest.php index 4be3a3816..ff580ac6a 100644 --- a/tests/Composer/Test/Repository/Vcs/GitHubDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/GitHubDriverTest.php @@ -288,11 +288,21 @@ class GitHubDriverTest extends TestCase return [ [ 'custom: example.com', - null, + [ + [ + 'type' => 'custom', + 'url' => 'https://example.com', + ], + ], ], [ 'custom: [example.com]', - null, + [ + [ + 'type' => 'custom', + 'url' => 'https://example.com', + ], + ], ], [ 'custom: "https://example.com"', @@ -319,6 +329,10 @@ class GitHubDriverTest extends TestCase 'type' => 'custom', 'url' => 'https://example.com', ], + [ + 'type' => 'custom', + 'url' => 'https://example.org', + ], ], ], [ @@ -328,6 +342,10 @@ class GitHubDriverTest extends TestCase 'type' => 'custom', 'url' => 'https://example.com', ], + [ + 'type' => 'custom', + 'url' => 'https://example.org', + ], ], ], ];