Skip to content

Commit

Permalink
Composer support string (#11386)
Browse files Browse the repository at this point in the history
* GitHubDriver: fix support is set to string

* GitLabDriver: fix support is set to string

* BitbucketDriver: fix support is set to string

* Fix PHPStan
  • Loading branch information
glaubinix committed Mar 20, 2023
1 parent 5d2d513 commit 685a2e6
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 39 deletions.
3 changes: 3 additions & 0 deletions src/Composer/Repository/Vcs/GitBitbucketDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ public function getComposerInformation(string $identifier): ?array

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,
Expand Down
3 changes: 3 additions & 0 deletions src/Composer/Repository/Vcs/GitHubDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ public function getComposerInformation(string $identifier): ?array

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);
Expand Down
3 changes: 3 additions & 0 deletions src/Composer/Repository/Vcs/GitLabDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ public function getComposerInformation(string $identifier): ?array

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);
Expand Down
81 changes: 42 additions & 39 deletions tests/Composer/Test/Repository/Vcs/GitBitbucketDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

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;
Expand All @@ -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;
Expand All @@ -46,9 +48,7 @@ protected function setUp(): void
],
]);

$this->httpDownloader = $this->getMockBuilder('Composer\Util\HttpDownloader')
->disableOriginalConstructor()
->getMock();
$this->httpDownloader = $this->getHttpDownloaderMock($this->io, $this->config);;
}

protected function tearDown(): void
Expand Down Expand Up @@ -83,15 +83,9 @@ public function testGetRootIdentifierWrongScmType(): void
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']);

Expand All @@ -109,32 +103,13 @@ public function testDriver(): GitBitbucketDriver
'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',
Expand Down Expand Up @@ -218,6 +193,34 @@ public function testInitializeInvalidRepositoryUrl(): void
$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(
Expand Down
38 changes: 38 additions & 0 deletions tests/Composer/Test/Repository/Vcs/GitHubDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,44 @@ public function testPublicRepository2(): void
$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';
Expand Down
27 changes: 27 additions & 0 deletions tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,22 @@ public function testInitializeWithPortNumber(): void
$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');
Expand Down Expand Up @@ -631,4 +647,15 @@ public function testProtocolOverrideRepositoryUrlGeneration(): void
$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);
}
}

0 comments on commit 685a2e6

Please sign in to comment.