Skip to content

Commit

Permalink
Improved support for GitLab (#1408)
Browse files Browse the repository at this point in the history
  • Loading branch information
madbob authored Jan 8, 2024
1 parent 175cebe commit 873641c
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 6 deletions.
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
*/
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 @@ public function update(IOInterface $io, Config $config, Package $package, VcsRep
);
}

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 updateGitHubInfo(HttpDownloader $httpDownloader, Package $packa
}
}

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

0 comments on commit 873641c

Please sign in to comment.