From 685a2e6be2537ed2f5008b877ce12bb5049c6507 Mon Sep 17 00:00:00 2001 From: Stephan Date: Mon, 20 Mar 2023 19:18:19 +0000 Subject: [PATCH] Composer support string (#11386) * GitHubDriver: fix support is set to string * GitLabDriver: fix support is set to string * BitbucketDriver: fix support is set to string * Fix PHPStan --- .../Repository/Vcs/GitBitbucketDriver.php | 3 + src/Composer/Repository/Vcs/GitHubDriver.php | 3 + src/Composer/Repository/Vcs/GitLabDriver.php | 3 + .../Repository/Vcs/GitBitbucketDriverTest.php | 81 ++++++++++--------- .../Test/Repository/Vcs/GitHubDriverTest.php | 38 +++++++++ .../Test/Repository/Vcs/GitLabDriverTest.php | 27 +++++++ 6 files changed, 116 insertions(+), 39 deletions(-) diff --git a/src/Composer/Repository/Vcs/GitBitbucketDriver.php b/src/Composer/Repository/Vcs/GitBitbucketDriver.php index c3b8dd8c4..ff9aecafc 100644 --- a/src/Composer/Repository/Vcs/GitBitbucketDriver.php +++ b/src/Composer/Repository/Vcs/GitBitbucketDriver.php @@ -153,6 +153,9 @@ class GitBitbucketDriver extends VcsDriver if ($composer !== null) { // specials for bitbucket + if (isset($composer['support']) && !is_array($composer['support'])) { + $composer['support'] = []; + } if (!isset($composer['support']['source'])) { $label = array_search( $identifier, diff --git a/src/Composer/Repository/Vcs/GitHubDriver.php b/src/Composer/Repository/Vcs/GitHubDriver.php index 98dbf989a..122b03fa7 100644 --- a/src/Composer/Repository/Vcs/GitHubDriver.php +++ b/src/Composer/Repository/Vcs/GitHubDriver.php @@ -174,6 +174,9 @@ class GitHubDriver extends VcsDriver if ($composer !== null) { // specials for github + if (isset($composer['support']) && !is_array($composer['support'])) { + $composer['support'] = []; + } if (!isset($composer['support']['source'])) { $label = array_search($identifier, $this->getTags()) ?: array_search($identifier, $this->getBranches()) ?: $identifier; $composer['support']['source'] = sprintf('https://%s/%s/%s/tree/%s', $this->originUrl, $this->owner, $this->repository, $label); diff --git a/src/Composer/Repository/Vcs/GitLabDriver.php b/src/Composer/Repository/Vcs/GitLabDriver.php index 77b183e0b..1e13154c5 100644 --- a/src/Composer/Repository/Vcs/GitLabDriver.php +++ b/src/Composer/Repository/Vcs/GitLabDriver.php @@ -167,6 +167,9 @@ class GitLabDriver extends VcsDriver if (null !== $composer) { // specials for gitlab (this data is only available if authentication is provided) + if (isset($composer['support']) && !is_array($composer['support'])) { + $composer['support'] = []; + } if (!isset($composer['support']['source']) && isset($this->project['web_url'])) { $label = array_search($identifier, $this->getTags(), true) ?: array_search($identifier, $this->getBranches(), true) ?: $identifier; $composer['support']['source'] = sprintf('%s/-/tree/%s', $this->project['web_url'], $label); diff --git a/tests/Composer/Test/Repository/Vcs/GitBitbucketDriverTest.php b/tests/Composer/Test/Repository/Vcs/GitBitbucketDriverTest.php index 3a35cd28e..5050b0fb4 100644 --- a/tests/Composer/Test/Repository/Vcs/GitBitbucketDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/GitBitbucketDriverTest.php @@ -14,6 +14,8 @@ namespace Composer\Test\Repository\Vcs; use Composer\Config; use Composer\Repository\Vcs\GitBitbucketDriver; +use Composer\Repository\Vcs\GitHubDriver; +use Composer\Test\Mock\HttpDownloaderMock; use Composer\Test\TestCase; use Composer\Util\Filesystem; use Composer\Util\ProcessExecutor; @@ -28,7 +30,7 @@ class GitBitbucketDriverTest extends TestCase private $io; /** @var Config */ private $config; - /** @var \Composer\Util\HttpDownloader&\PHPUnit\Framework\MockObject\MockObject */ + /** @var HttpDownloaderMock */ private $httpDownloader; /** @var string */ private $home; @@ -46,9 +48,7 @@ class GitBitbucketDriverTest extends TestCase ], ]); - $this->httpDownloader = $this->getMockBuilder('Composer\Util\HttpDownloader') - ->disableOriginalConstructor() - ->getMock(); + $this->httpDownloader = $this->getHttpDownloaderMock($this->io, $this->config);; } protected function tearDown(): void @@ -83,15 +83,9 @@ class GitBitbucketDriverTest extends TestCase self::expectException('RuntimeException'); self::expectExceptionMessage('https://bitbucket.org/user/repo.git does not appear to be a git repository, use https://bitbucket.org/user/repo but remember that Bitbucket no longer supports the mercurial repositories. https://bitbucket.org/blog/sunsetting-mercurial-support-in-bitbucket'); - $this->httpDownloader->expects($this->once()) - ->method('get') - ->with( - $url = 'https://api.bitbucket.org/2.0/repositories/user/repo?fields=-project%2C-owner', - [] - ) - ->willReturn( - new Response(['url' => $url], 200, [], '{"scm":"hg","website":"","has_wiki":false,"name":"repo","links":{"branches":{"href":"https:\/\/api.bitbucket.org\/2.0\/repositories\/user\/repo\/refs\/branches"},"tags":{"href":"https:\/\/api.bitbucket.org\/2.0\/repositories\/user\/repo\/refs\/tags"},"clone":[{"href":"https:\/\/user@bitbucket.org\/user\/repo","name":"https"},{"href":"ssh:\/\/hg@bitbucket.org\/user\/repo","name":"ssh"}],"html":{"href":"https:\/\/bitbucket.org\/user\/repo"}},"language":"php","created_on":"2015-02-18T16:22:24.688+00:00","updated_on":"2016-05-17T13:20:21.993+00:00","is_private":true,"has_issues":false}') - ); + $this->httpDownloader->expects([ + ['url' => 'https://api.bitbucket.org/2.0/repositories/user/repo?fields=-project%2C-owner', 'body' => '{"scm":"hg","website":"","has_wiki":false,"name":"repo","links":{"branches":{"href":"https:\/\/api.bitbucket.org\/2.0\/repositories\/user\/repo\/refs\/branches"},"tags":{"href":"https:\/\/api.bitbucket.org\/2.0\/repositories\/user\/repo\/refs\/tags"},"clone":[{"href":"https:\/\/user@bitbucket.org\/user\/repo","name":"https"},{"href":"ssh:\/\/hg@bitbucket.org\/user\/repo","name":"ssh"}],"html":{"href":"https:\/\/bitbucket.org\/user\/repo"}},"language":"php","created_on":"2015-02-18T16:22:24.688+00:00","updated_on":"2016-05-17T13:20:21.993+00:00","is_private":true,"has_issues":false}'] + ], true); $driver = $this->getDriver(['url' => 'https://bitbucket.org/user/repo.git']); @@ -109,32 +103,13 @@ class GitBitbucketDriverTest extends TestCase 'https://api.bitbucket.org/2.0/repositories/user/repo/src/main/composer.json', 'https://api.bitbucket.org/2.0/repositories/user/repo/commit/main?fields=date', ]; - $this->httpDownloader->expects($this->any()) - ->method('get') - ->withConsecutive( - [ - $urls[0], [], - ], - [ - $urls[1], [], - ], - [ - $urls[2], [], - ], - [ - $urls[3], [], - ], - [ - $urls[4], [], - ] - ) - ->willReturnOnConsecutiveCalls( - new Response(['url' => $urls[0]], 200, [], '{"mainbranch": {"name": "main"}, "scm":"git","website":"","has_wiki":false,"name":"repo","links":{"branches":{"href":"https:\/\/api.bitbucket.org\/2.0\/repositories\/user\/repo\/refs\/branches"},"tags":{"href":"https:\/\/api.bitbucket.org\/2.0\/repositories\/user\/repo\/refs\/tags"},"clone":[{"href":"https:\/\/user@bitbucket.org\/user\/repo.git","name":"https"},{"href":"ssh:\/\/git@bitbucket.org\/user\/repo.git","name":"ssh"}],"html":{"href":"https:\/\/bitbucket.org\/user\/repo"}},"language":"php","created_on":"2015-02-18T16:22:24.688+00:00","updated_on":"2016-05-17T13:20:21.993+00:00","is_private":true,"has_issues":false}'), - new Response(['url' => $urls[1]], 200, [], '{"values":[{"name":"1.0.1","target":{"hash":"9b78a3932143497c519e49b8241083838c8ff8a1"}},{"name":"1.0.0","target":{"hash":"d3393d514318a9267d2f8ebbf463a9aaa389f8eb"}}]}'), - new Response(['url' => $urls[2]], 200, [], '{"values":[{"name":"main","target":{"hash":"937992d19d72b5116c3e8c4a04f960e5fa270b22"}}]}'), - new Response(['url' => $urls[3]], 200, [], '{"name": "user/repo","description": "test repo","license": "GPL","authors": [{"name": "Name","email": "local@domain.tld"}],"require": {"creator/package": "^1.0"},"require-dev": {"phpunit/phpunit": "~4.8"}}'), - new Response(['url' => $urls[4]], 200, [], '{"date": "2016-05-17T13:19:52+00:00"}') - ); + $this->httpDownloader->expects([ + ['url' => $urls[0], 'body' => '{"mainbranch": {"name": "main"}, "scm":"git","website":"","has_wiki":false,"name":"repo","links":{"branches":{"href":"https:\/\/api.bitbucket.org\/2.0\/repositories\/user\/repo\/refs\/branches"},"tags":{"href":"https:\/\/api.bitbucket.org\/2.0\/repositories\/user\/repo\/refs\/tags"},"clone":[{"href":"https:\/\/user@bitbucket.org\/user\/repo.git","name":"https"},{"href":"ssh:\/\/git@bitbucket.org\/user\/repo.git","name":"ssh"}],"html":{"href":"https:\/\/bitbucket.org\/user\/repo"}},"language":"php","created_on":"2015-02-18T16:22:24.688+00:00","updated_on":"2016-05-17T13:20:21.993+00:00","is_private":true,"has_issues":false}'], + ['url' => $urls[1], 'body' => '{"values":[{"name":"1.0.1","target":{"hash":"9b78a3932143497c519e49b8241083838c8ff8a1"}},{"name":"1.0.0","target":{"hash":"d3393d514318a9267d2f8ebbf463a9aaa389f8eb"}}]}'], + ['url' => $urls[2], 'body' => '{"values":[{"name":"main","target":{"hash":"937992d19d72b5116c3e8c4a04f960e5fa270b22"}}]}'], + ['url' => $urls[3], 'body' => '{"name": "user/repo","description": "test repo","license": "GPL","authors": [{"name": "Name","email": "local@domain.tld"}],"require": {"creator/package": "^1.0"},"require-dev": {"phpunit/phpunit": "~4.8"}}'], + ['url' => $urls[4], 'body' => '{"date": "2016-05-17T13:19:52+00:00"}'], + ], true); $this->assertEquals( 'main', @@ -218,6 +193,34 @@ class GitBitbucketDriverTest extends TestCase $driver->initialize(); } + public function testInvalidSupportData(): void + { + $repoUrl = 'https://bitbucket.org/user/repo.git'; + + $driver = $this->getDriver(['url' => $repoUrl]); + + $urls = [ + 'https://api.bitbucket.org/2.0/repositories/user/repo?fields=-project%2C-owner', + 'https://api.bitbucket.org/2.0/repositories/user/repo/src/main/composer.json', + 'https://api.bitbucket.org/2.0/repositories/user/repo/commit/main?fields=date', + 'https://api.bitbucket.org/2.0/repositories/user/repo/refs/tags?pagelen=100&fields=values.name%2Cvalues.target.hash%2Cnext&sort=-target.date', + 'https://api.bitbucket.org/2.0/repositories/user/repo/refs/branches?pagelen=100&fields=values.name%2Cvalues.target.hash%2Cvalues.heads%2Cnext&sort=-target.date', + ]; + $this->httpDownloader->expects([ + ['url' => $urls[0], 'body' => '{"mainbranch": {"name": "main"}, "scm":"git","website":"","has_wiki":false,"name":"repo","links":{"branches":{"href":"https:\/\/api.bitbucket.org\/2.0\/repositories\/user\/repo\/refs\/branches"},"tags":{"href":"https:\/\/api.bitbucket.org\/2.0\/repositories\/user\/repo\/refs\/tags"},"clone":[{"href":"https:\/\/user@bitbucket.org\/user\/repo.git","name":"https"},{"href":"ssh:\/\/git@bitbucket.org\/user\/repo.git","name":"ssh"}],"html":{"href":"https:\/\/bitbucket.org\/user\/repo"}},"language":"php","created_on":"2015-02-18T16:22:24.688+00:00","updated_on":"2016-05-17T13:20:21.993+00:00","is_private":true,"has_issues":false}'], + ['url' => $urls[1], 'body' => '{"support": "' . $repoUrl . '"}'], + ['url' => $urls[2], 'body' => '{"date": "2016-05-17T13:19:52+00:00"}'], + ['url' => $urls[3], 'body' => '{"values":[{"name":"1.0.1","target":{"hash":"9b78a3932143497c519e49b8241083838c8ff8a1"}},{"name":"1.0.0","target":{"hash":"d3393d514318a9267d2f8ebbf463a9aaa389f8eb"}}]}'], + ['url' => $urls[4], 'body' => '{"values":[{"name":"main","target":{"hash":"937992d19d72b5116c3e8c4a04f960e5fa270b22"}}]}'], + ], true); + + $driver->getRootIdentifier(); + $data = $driver->getComposerInformation('main'); + + $this->assertIsArray($data); + $this->assertSame('https://bitbucket.org/user/repo/src/937992d19d72b5116c3e8c4a04f960e5fa270b22/?at=main', $data['support']['source']); + } + public function testSupports(): void { $this->assertTrue( diff --git a/tests/Composer/Test/Repository/Vcs/GitHubDriverTest.php b/tests/Composer/Test/Repository/Vcs/GitHubDriverTest.php index 68cf5e295..8431a1b49 100644 --- a/tests/Composer/Test/Repository/Vcs/GitHubDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/GitHubDriverTest.php @@ -199,6 +199,44 @@ class GitHubDriverTest extends TestCase $this->assertArrayNotHasKey('abandoned', $data); } + public function testInvalidSupportData(): 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": "'.$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("custom: https://example.com").'"}'], + ], + 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); + + $this->assertIsArray($data); + $this->assertSame('https://github.com/composer/packagist/tree/feature/3.2-foo', $data['support']['source']); + } + public function testPublicRepositoryArchived(): void { $repoUrl = 'http://github.com/composer/packagist'; diff --git a/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php b/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php index 2f9dae2b9..9d163f567 100644 --- a/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php @@ -244,6 +244,22 @@ JSON; $this->assertEquals($url, $driver->getUrl()); } + public function testInvalidSupportData(): void + { + $driver = $this->testInitialize($repoUrl = 'https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v4/projects/mygroup%2Fmyproject'); + $this->setAttribute($driver, 'branches', ['main' => 'SOMESHA']); + $this->setAttribute($driver, 'tags', []); + + $this->httpDownloader->expects([ + ['url' => 'https://gitlab.com/api/v4/projects/mygroup%2Fmyproject/repository/files/composer%2Ejson/raw?ref=SOMESHA', 'body' => '{"support": "'.$repoUrl.'" }'], + ], true); + + $data = $driver->getComposerInformation('main'); + + $this->assertIsArray($data); + $this->assertSame('https://gitlab.com/mygroup/myproject/-/tree/main', $data['support']['source']); + } + public function testGetDist(): void { $driver = $this->testInitialize('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v4/projects/mygroup%2Fmyproject'); @@ -631,4 +647,15 @@ JSON; $driver->initialize(); $this->assertEquals('https://gitlab.com/mygroup/myproject.git', $driver->getRepositoryUrl(), 'Repository URL matches config request for http not git'); } + + /** + * @param object $object + * @param mixed $value + */ + protected function setAttribute($object, string $attribute, $value): void + { + $attr = new \ReflectionProperty($object, $attribute); + $attr->setAccessible(true); + $attr->setValue($object, $value); + } }