1
0
Fork 0

Fix VcsDrivers initialization of tags and branches, and VcsRepository swallowing exceptions (#10319)

* Drivers: avoid early initialize of tags and branches

* VcsRepository: do not continue if fetching root information fails because of unexpected exceptions

Also rethrow 5XX exception in addition to select 4XX exceptions
pull/10327/head
Stephan 2021-11-30 13:33:38 +00:00 committed by GitHub
parent 9efe4290b6
commit f5ffedfe60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 16 deletions

View File

@ -293,7 +293,7 @@ class GitBitbucketDriver extends VcsDriver
} }
if (null === $this->tags) { if (null === $this->tags) {
$this->tags = array(); $tags = array();
$resource = sprintf( $resource = sprintf(
'%s?%s', '%s?%s',
$this->tagsUrl, $this->tagsUrl,
@ -311,7 +311,7 @@ class GitBitbucketDriver extends VcsDriver
while ($hasNext) { while ($hasNext) {
$tagsData = $this->fetchWithOAuthCredentials($resource)->decodeJson(); $tagsData = $this->fetchWithOAuthCredentials($resource)->decodeJson();
foreach ($tagsData['values'] as $data) { foreach ($tagsData['values'] as $data) {
$this->tags[$data['name']] = $data['target']['hash']; $tags[$data['name']] = $data['target']['hash'];
} }
if (empty($tagsData['next'])) { if (empty($tagsData['next'])) {
$hasNext = false; $hasNext = false;
@ -319,6 +319,8 @@ class GitBitbucketDriver extends VcsDriver
$resource = $tagsData['next']; $resource = $tagsData['next'];
} }
} }
$this->tags = $tags;
} }
return $this->tags; return $this->tags;
@ -334,7 +336,7 @@ class GitBitbucketDriver extends VcsDriver
} }
if (null === $this->branches) { if (null === $this->branches) {
$this->branches = array(); $branches = array();
$resource = sprintf( $resource = sprintf(
'%s?%s', '%s?%s',
$this->branchesUrl, $this->branchesUrl,
@ -352,7 +354,7 @@ class GitBitbucketDriver extends VcsDriver
while ($hasNext) { while ($hasNext) {
$branchData = $this->fetchWithOAuthCredentials($resource)->decodeJson(); $branchData = $this->fetchWithOAuthCredentials($resource)->decodeJson();
foreach ($branchData['values'] as $data) { foreach ($branchData['values'] as $data) {
$this->branches[$data['name']] = $data['target']['hash']; $branches[$data['name']] = $data['target']['hash'];
} }
if (empty($branchData['next'])) { if (empty($branchData['next'])) {
$hasNext = false; $hasNext = false;
@ -360,6 +362,8 @@ class GitBitbucketDriver extends VcsDriver
$resource = $branchData['next']; $resource = $branchData['next'];
} }
} }
$this->branches = $branches;
} }
return $this->branches; return $this->branches;

View File

@ -322,18 +322,20 @@ class GitHubDriver extends VcsDriver
return $this->gitDriver->getTags(); return $this->gitDriver->getTags();
} }
if (null === $this->tags) { if (null === $this->tags) {
$this->tags = array(); $tags = array();
$resource = $this->getApiUrl() . '/repos/'.$this->owner.'/'.$this->repository.'/tags?per_page=100'; $resource = $this->getApiUrl() . '/repos/'.$this->owner.'/'.$this->repository.'/tags?per_page=100';
do { do {
$response = $this->getContents($resource); $response = $this->getContents($resource);
$tagsData = $response->decodeJson(); $tagsData = $response->decodeJson();
foreach ($tagsData as $tag) { foreach ($tagsData as $tag) {
$this->tags[$tag['name']] = $tag['commit']['sha']; $tags[$tag['name']] = $tag['commit']['sha'];
} }
$resource = $this->getNextPage($response); $resource = $this->getNextPage($response);
} while ($resource); } while ($resource);
$this->tags = $tags;
} }
return $this->tags; return $this->tags;
@ -348,7 +350,7 @@ class GitHubDriver extends VcsDriver
return $this->gitDriver->getBranches(); return $this->gitDriver->getBranches();
} }
if (null === $this->branches) { if (null === $this->branches) {
$this->branches = array(); $branches = array();
$resource = $this->getApiUrl() . '/repos/'.$this->owner.'/'.$this->repository.'/git/refs/heads?per_page=100'; $resource = $this->getApiUrl() . '/repos/'.$this->owner.'/'.$this->repository.'/git/refs/heads?per_page=100';
do { do {
@ -357,12 +359,14 @@ class GitHubDriver extends VcsDriver
foreach ($branchData as $branch) { foreach ($branchData as $branch) {
$name = substr($branch['ref'], 11); $name = substr($branch['ref'], 11);
if ($name !== 'gh-pages') { if ($name !== 'gh-pages') {
$this->branches[$name] = $branch['object']['sha']; $branches[$name] = $branch['object']['sha'];
} }
} }
$resource = $this->getNextPage($response); $resource = $this->getNextPage($response);
} while ($resource); } while ($resource);
$this->branches = $branches;
} }
return $this->branches; return $this->branches;

View File

@ -222,7 +222,7 @@ class SvnDriver extends VcsDriver
public function getTags() public function getTags()
{ {
if (null === $this->tags) { if (null === $this->tags) {
$this->tags = array(); $tags = array();
if ($this->tagsPath !== false) { if ($this->tagsPath !== false) {
$output = $this->execute('svn ls --verbose', $this->baseUrl . '/' . $this->tagsPath); $output = $this->execute('svn ls --verbose', $this->baseUrl . '/' . $this->tagsPath);
@ -231,7 +231,7 @@ class SvnDriver extends VcsDriver
$line = trim($line); $line = trim($line);
if ($line && preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match)) { if ($line && preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match)) {
if (isset($match[1], $match[2]) && $match[2] !== './') { if (isset($match[1], $match[2]) && $match[2] !== './') {
$this->tags[rtrim($match[2], '/')] = $this->buildIdentifier( $tags[rtrim($match[2], '/')] = $this->buildIdentifier(
'/' . $this->tagsPath . '/' . $match[2], '/' . $this->tagsPath . '/' . $match[2],
$match[1] $match[1]
); );
@ -240,6 +240,8 @@ class SvnDriver extends VcsDriver
} }
} }
} }
$this->tags = $tags;
} }
return $this->tags; return $this->tags;
@ -251,7 +253,7 @@ class SvnDriver extends VcsDriver
public function getBranches() public function getBranches()
{ {
if (null === $this->branches) { if (null === $this->branches) {
$this->branches = array(); $branches = array();
if (false === $this->trunkPath) { if (false === $this->trunkPath) {
$trunkParent = $this->baseUrl . '/'; $trunkParent = $this->baseUrl . '/';
@ -265,11 +267,11 @@ class SvnDriver extends VcsDriver
$line = trim($line); $line = trim($line);
if ($line && preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match)) { if ($line && preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match)) {
if (isset($match[1], $match[2]) && $match[2] === './') { if (isset($match[1], $match[2]) && $match[2] === './') {
$this->branches['trunk'] = $this->buildIdentifier( $branches['trunk'] = $this->buildIdentifier(
'/' . $this->trunkPath, '/' . $this->trunkPath,
$match[1] $match[1]
); );
$this->rootIdentifier = $this->branches['trunk']; $this->rootIdentifier = $branches['trunk'];
break; break;
} }
} }
@ -284,7 +286,7 @@ class SvnDriver extends VcsDriver
$line = trim($line); $line = trim($line);
if ($line && preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match)) { if ($line && preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match)) {
if (isset($match[1], $match[2]) && $match[2] !== './') { if (isset($match[1], $match[2]) && $match[2] !== './') {
$this->branches[rtrim($match[2], '/')] = $this->buildIdentifier( $branches[rtrim($match[2], '/')] = $this->buildIdentifier(
'/' . $this->branchesPath . '/' . $match[2], '/' . $this->branchesPath . '/' . $match[2],
$match[1] $match[1]
); );
@ -293,6 +295,8 @@ class SvnDriver extends VcsDriver
} }
} }
} }
$this->branches = $branches;
} }
return $this->branches; return $this->branches;

View File

@ -212,6 +212,10 @@ class VcsRepository extends ArrayRepository implements ConfigurableRepositoryInt
$this->packageName = !empty($data['name']) ? $data['name'] : null; $this->packageName = !empty($data['name']) ? $data['name'] : null;
} }
} catch (\Exception $e) { } catch (\Exception $e) {
if ($e instanceof TransportException && $this->shouldRethrowTransportException($e)) {
throw $e;
}
if ($isVeryVerbose) { if ($isVeryVerbose) {
$this->io->writeError('<error>Skipped parsing '.$driver->getRootIdentifier().', '.$e->getMessage().'</error>'); $this->io->writeError('<error>Skipped parsing '.$driver->getRootIdentifier().', '.$e->getMessage().'</error>');
} }
@ -303,7 +307,7 @@ class VcsRepository extends ArrayRepository implements ConfigurableRepositoryInt
if ($e->getCode() === 404) { if ($e->getCode() === 404) {
$this->emptyReferences[] = $identifier; $this->emptyReferences[] = $identifier;
} }
if (in_array($e->getCode(), array(401, 403, 429), true)) { if ($this->shouldRethrowTransportException($e)) {
throw $e; throw $e;
} }
} }
@ -392,7 +396,7 @@ class VcsRepository extends ArrayRepository implements ConfigurableRepositoryInt
if ($e->getCode() === 404) { if ($e->getCode() === 404) {
$this->emptyReferences[] = $identifier; $this->emptyReferences[] = $identifier;
} }
if (in_array($e->getCode(), array(401, 403, 429), true)) { if ($this->shouldRethrowTransportException($e)) {
throw $e; throw $e;
} }
if ($isVeryVerbose) { if ($isVeryVerbose) {
@ -531,4 +535,12 @@ class VcsRepository extends ArrayRepository implements ConfigurableRepositoryInt
return null; return null;
} }
/**
* @return bool
*/
private function shouldRethrowTransportException(TransportException $e)
{
return in_array($e->getCode(), array(401, 403, 429), true) || $e->getCode() >= 500;
}
} }