1
0
Fork 0

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
pull/11398/head
Stephan 2023-03-20 19:18:19 +00:00 committed by GitHub
parent 5d2d513f97
commit 685a2e6be2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 116 additions and 39 deletions

View File

@ -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,

View File

@ -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);

View File

@ -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);

View File

@ -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(

View File

@ -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';

View File

@ -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);
}
}