Skip to content

Commit

Permalink
Merge pull request #773 from heiglandreas/allowNonRepoBadgesToWrokWit…
Browse files Browse the repository at this point in the history
…houtRepo

Remove repo-access when not required
  • Loading branch information
JellyBellyDev authored Apr 26, 2022
2 parents 225f812 + 9098c13 commit 5c282e7
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
12 changes: 8 additions & 4 deletions src/Badge/Infrastructure/Package/PackageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(
*
* @throws UnexpectedValueException
*/
public function fetchByRepository(string $repository): Package
public function fetchByRepository(string $repository, bool $withDefaultBranch = false): Package
{
$apiPackage = $this->packagistClient->get($repository);
if (!$apiPackage instanceof ApiPackage) {
Expand All @@ -48,11 +48,15 @@ public function fetchByRepository(string $repository): Package

$repositoryInfo = Repository::createFromRepositoryUrl($apiPackage->getRepository());

$defaultBranch = $this->clientStrategy->getDefaultBranch($repositoryInfo);

/** @var Package $class */
$class = self::$packageClass;

return $class::createFromApi($apiPackage, ['default_branch' => $defaultBranch]);
if (true === $withDefaultBranch) {
$defaultBranch = $this->clientStrategy->getDefaultBranch($repositoryInfo);

return $class::createFromApi($apiPackage, ['default_branch' => $defaultBranch]);
}

return $class::createFromApi($apiPackage);
}
}
2 changes: 1 addition & 1 deletion src/Badge/Model/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private function __construct(ApiPackage $apiPackage, array $repoGitHubData)
*
* @param array{default_branch: string} $repoGitHubData
*/
public static function createFromApi(ApiPackage $apiPackage, array $repoGitHubData): self
public static function createFromApi(ApiPackage $apiPackage, array $repoGitHubData = ['default_branch' => '']): self
{
return new self($apiPackage, $repoGitHubData);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Badge/Model/PackageRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ interface PackageRepositoryInterface
*
* @throws UnexpectedValueException
*/
public function fetchByRepository(string $repository): Package;
public function fetchByRepository(string $repository, bool $withDefaultBranch = false): Package;
}
5 changes: 5 additions & 0 deletions src/Badge/Model/UseCase/BaseCreatePackagistImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ protected function fetchPackage(string $repository): Package
return $this->packageRepository->fetchByRepository($repository);
}

protected function fetchPackageWithRepo(string $repository): Package
{
return $this->packageRepository->fetchByRepository($repository, true);
}

/**
* @throws InvalidArgumentException
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Badge/Model/UseCase/CreateGitAttributesBadge.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function __construct(
public function createGitAttributesBadge(string $repository, string $format = Badge::DEFAULT_FORMAT, string $style = Badge::DEFAULT_STYLE): CacheableBadge
{
try {
$package = $this->fetchPackage($repository);
$package = $this->fetchPackageWithRepo($repository);
$repo = \str_replace('.git', '', $package->getRepository());
} catch (\Exception) {
return $this->createDefaultBadge($format, $style);
Expand Down

0 comments on commit 5c282e7

Please sign in to comment.