Skip to content

Commit

Permalink
Improved support for GitLab
Browse files Browse the repository at this point in the history
  • Loading branch information
madbob committed Dec 23, 2023
1 parent 9e34e90 commit 8efb143
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 6 deletions.
56 changes: 55 additions & 1 deletion src/Entity/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,26 @@ public function getVendor(): string
return $this->vendor;
}

public function isGitHub(): array|bool

Check failure on line 277 in src/Entity/Package.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method App\Entity\Package::isGitHub() return type has no value type specified in iterable type array.
{
if (Preg::isMatchStrictGroups('{^(?:git://|git@|https?://)github.com[:/]([^/]+)/(.+?)(?:\.git|/)?$}i', $this->getRepository(), $match)) {
return $match;
}
else {
return false;
}
}

public function isGitLab(): array|bool

Check failure on line 287 in src/Entity/Package.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method App\Entity\Package::isGitLab() return type has no value type specified in iterable type array.
{
if (Preg::isMatchStrictGroups('{^(?:git://|git@|https?://)gitlab.com[:/]([^/]+)/(.+?)(?:\.git|/)?$}i', $this->getRepository(), $match)) {
return $match;
}
else {
return false;
}
}

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

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

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

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

public function setGitHubOpenIssues(int|null $val): void
{
$this->gitHubOpenIssues = $val;
Expand Down Expand Up @@ -467,7 +513,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);

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

View workflow job for this annotation

GitHub Actions / PHPStan

Cannot access offset 1 on non-empty-array|true.

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

View workflow job for this annotation

GitHub Actions / PHPStan

Cannot access offset 2 on non-empty-array|true.
} elseif ($match = $package->isGitLab()) {
$this->updateGitLabInfo($httpDownloader, $io, $package, $match[1], $match[2], $driver);

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

View workflow job for this annotation

GitHub Actions / PHPStan

Cannot access offset 1 on non-empty-array|true.

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

View workflow job for this annotation

GitHub Actions / PHPStan

Cannot access offset 2 on non-empty-array|true.
} 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, '.', ' ')|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 8efb143

Please sign in to comment.