Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved support for GitLab #1408

Merged
merged 3 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion src/Entity/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,30 @@ public function getVendor(): string
return $this->vendor;
}

/**
* @return array<string> Vendor and package name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the return type is wrong. It is array<string>|false

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and the naming looks weird as the name makes us think it should return a boolean.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah true, I'll fix those

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And f1c5a5a 🤦🏻

*/
public function isGitHub(): array|false
{
if (Preg::isMatchStrictGroups('{^(?:git://|git@|https?://)github.com[:/]([^/]+)/(.+?)(?:\.git|/)?$}i', $this->getRepository(), $match)) {
return $match;
}

return false;
}

/**
* @return array<string> Vendor and package name
*/
public function isGitLab(): array|false
{
if (Preg::isMatchStrictGroups('{^(?:git://|git@|https?://)gitlab.com[:/]([^/]+)/(.+?)(?:\.git|/)?$}i', $this->getRepository(), $match)) {
return $match;
}

return false;
}

/**
* Get package name without vendor
*/
Expand Down Expand Up @@ -334,6 +358,18 @@ public function getGitHubStars(): int|null
return $this->gitHubStars;
}

public function getGitHubStarsUrl(): string|null
{
if ($this->isGitHub()) {
return $this->getBrowsableRepository() . '/stargazers';
}
if ($this->isGitLab()) {
return $this->getBrowsableRepository() . '/-/starrers';
}

return null;
}

public function setGitHubWatches(int|null $val): void
{
$this->gitHubWatches = $val;
Expand All @@ -354,6 +390,18 @@ public function getGitHubForks(): int|null
return $this->gitHubForks;
}

public function getGitHubForksUrl(): string|null
{
if ($this->isGitHub()) {
return $this->getBrowsableRepository() . '/forks';
}
if ($this->isGitLab()) {
return $this->getBrowsableRepository() . '/-/forks';
}

return null;
}

public function setGitHubOpenIssues(int|null $val): void
{
$this->gitHubOpenIssues = $val;
Expand Down Expand Up @@ -467,7 +515,15 @@ public function getBrowsableRepository(): string
return Preg::replace('{^(?:git@|https://|git://)bitbucket.org[:/](.+?)(?:\.git)?$}i', 'https://bitbucket.org/$1', $this->repository);
}

return Preg::replace('{^(git://github.com/|[email protected]:)}', 'https://github.com/', $this->repository);
if (Preg::isMatch('{(://|@)github.com[:/]}i', $this->repository)) {
return Preg::replace('{^(git://github.com/|[email protected]:)}', 'https://github.com/', $this->repository);
}

if (Preg::isMatch('{(://|@)gitlab.com[:/]}i', $this->repository)) {
return Preg::replace('{^(git://gitlab.com/|[email protected]:)}', 'https://gitlab.com/', $this->repository);
}

return $this->repository;
}

public function addVersion(Version $version): void
Expand Down
33 changes: 32 additions & 1 deletion src/Package/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Composer\Pcre\Preg;
use Composer\Repository\VcsRepository;
use Composer\Repository\Vcs\GitHubDriver;
use Composer\Repository\Vcs\GitLabDriver;
use Composer\Repository\Vcs\VcsDriverInterface;
use Composer\Repository\InvalidRepositoryException;
use Composer\Util\ErrorHandler;
Expand Down Expand Up @@ -241,8 +242,10 @@
);
}

if (Preg::isMatchStrictGroups('{^(?:git://|git@|https?://)github.com[:/]([^/]+)/(.+?)(?:\.git|/)?$}i', $package->getRepository(), $match)) {
if ($match = $package->isGitHub()) {
$this->updateGitHubInfo($httpDownloader, $package, $match[1], $match[2], $driver);
} elseif ($match = $package->isGitLab()) {
$this->updateGitLabInfo($httpDownloader, $io, $package, $match[1], $match[2], $driver);
} else {
$this->updateReadme($io, $package, $driver);
}
Expand Down Expand Up @@ -604,6 +607,34 @@
}
}

private function updateGitLabInfo(HttpDownloader $httpDownloader, IOInterface $io, Package $package, string $owner, string $repo, VcsDriverInterface $driver): void
{
// GitLab provides a generic URL for the original formatted README,
// which requires further elaboration. Here we use the already existing
// function to handle it, and back here to populate the other available
// metadata
$this->updateReadme($io, $package, $driver);

if (!$driver instanceof GitLabDriver) {
return;
}

$repoData = $driver->getRepoData();

Check failure on line 622 in src/Package/Updater.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to an undefined method Composer\Repository\Vcs\GitLabDriver::getRepoData().

if (isset($repoData['star_count']) && is_numeric($repoData['star_count'])) {
$package->setGitHubStars((int) $repoData['star_count']);
}
if (isset($repoData['forks_count']) && is_numeric($repoData['forks_count'])) {
$package->setGitHubForks((int) $repoData['forks_count']);
}
if (isset($repoData['open_issues_count']) && is_numeric($repoData['open_issues_count'])) {
$package->setGitHubOpenIssues((int) $repoData['open_issues_count']);
}

// GitLab does not include a "watch" feature
$package->setGitHubWatches(null);
}

/**
* Prepare the readme by stripping elements and attributes that are not supported .
*/
Expand Down
8 changes: 4 additions & 4 deletions templates/package/view_package.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@
{{ securityAdvisories|number_format(0, '.', '&#8201;')|raw }}
</p>
{% endif %}
{% if 'github.com' in repoUrl and package.gitHubStars is not null %}
{% if package.gitHubStars is not null %}
<p>
<span>
<a href="{{ repoUrl }}/stargazers">Stars</a>:
<a href="{{ package.gitHubStarsUrl }}">Stars</a>:
</span>
{{ package.gitHubStars|number_format(0, '.', '&#8201;')|raw }}
</p>
Expand All @@ -238,10 +238,10 @@
</span> {{ package.gitHubWatches|number_format(0, '.', '&#8201;')|raw }}
</p>
{% endif %}
{% if 'github.com' in repoUrl and package.gitHubForks is not null %}
{% if package.gitHubForks is not null %}
<p>
<span>
<a href="{{ repoUrl }}/forks">Forks</a>:
<a href="{{ package.gitHubForksUrl }}">Forks</a>:
</span>
{{ package.gitHubForks|number_format(0, '.', '&#8201;')|raw }}
</p>
Expand Down
Loading