Skip to content

Commit

Permalink
Discard unsupported FUNDING.yml URL values
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Dec 18, 2024
1 parent fb397ac commit 5c07d1d
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Composer/Repository/Vcs/GitHubDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,19 @@ private function getFundingInfo()
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('<warning>Funding URL '.$item['url'].' not in a supported format.</warning>');
unset($result[$key]);
break;
}
break;
}
}

Expand Down
96 changes: 96 additions & 0 deletions tests/Composer/Test/Repository/Vcs/GitHubDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,102 @@ public function testInvalidSupportData(): void
self::assertSame('https://github.com/composer/packagist/tree/feature/3.2-foo', $data['support']['source']);
}

/**
* @dataProvider fundingUrlProvider
* @param array<array{type: string, url: string}>|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';
Expand Down

0 comments on commit 5c07d1d

Please sign in to comment.