Пример #1
0
 /**
  * {@inheritDoc}
  */
 protected function getContents($url, $fetchingRepoData = false)
 {
     try {
         return parent::getContents($url);
     } catch (TransportException $e) {
         $gitLabUtil = new GitLab($this->io, $this->config, $this->process, $this->remoteFilesystem);
         switch ($e->getCode()) {
             case 401:
             case 404:
                 // try to authorize only if we are fetching the main /repos/foo/bar data, otherwise it must be a real 404
                 if (!$fetchingRepoData) {
                     throw $e;
                 }
                 if ($gitLabUtil->authorizeOAuth($this->originUrl)) {
                     return parent::getContents($url);
                 }
                 if (!$this->io->isInteractive()) {
                     return $this->attemptCloneFallback();
                 }
                 $this->io->writeError('<warning>Failed to download ' . $this->owner . '/' . $this->repository . ':' . $e->getMessage() . '</warning>');
                 $gitLabUtil->authorizeOAuthInteractively($this->scheme, $this->originUrl, 'Your credentials are required to fetch private repository metadata (<info>' . $this->url . '</info>)');
                 return parent::getContents($url);
             case 403:
                 if (!$this->io->hasAuthentication($this->originUrl) && $gitLabUtil->authorizeOAuth($this->originUrl)) {
                     return parent::getContents($url);
                 }
                 if (!$this->io->isInteractive() && $fetchingRepoData) {
                     return $this->attemptCloneFallback();
                 }
                 throw $e;
             default:
                 throw $e;
         }
     }
 }
Пример #2
0
 /**
  * {@inheritDoc}
  */
 protected function getContents($url, $fetchingRepoData = false)
 {
     try {
         return parent::getContents($url);
     } catch (TransportException $e) {
         $gitHubUtil = new GitHub($this->io, $this->config, $this->process, $this->remoteFilesystem);
         switch ($e->getCode()) {
             case 401:
             case 404:
                 // try to authorize only if we are fetching the main /repos/foo/bar data, otherwise it must be a real 404
                 if (!$fetchingRepoData) {
                     throw $e;
                 }
                 if ($gitHubUtil->authorizeOAuth($this->originUrl)) {
                     return parent::getContents($url);
                 }
                 if (!$this->io->isInteractive()) {
                     return $this->attemptCloneFallback();
                 }
                 $gitHubUtil->authorizeOAuthInteractively($this->originUrl, 'Your GitHub credentials are required to fetch private repository metadata (<info>' . $this->url . '</info>)');
                 return parent::getContents($url);
             case 403:
                 if (!$this->io->hasAuthentication($this->originUrl) && $gitHubUtil->authorizeOAuth($this->originUrl)) {
                     return parent::getContents($url);
                 }
                 if (!$this->io->isInteractive() && $fetchingRepoData) {
                     return $this->attemptCloneFallback();
                 }
                 $rateLimited = false;
                 foreach ($e->getHeaders() as $header) {
                     if (preg_match('{^X-RateLimit-Remaining: *0$}i', trim($header))) {
                         $rateLimited = true;
                     }
                 }
                 if (!$this->io->hasAuthentication($this->originUrl)) {
                     if (!$this->io->isInteractive()) {
                         $this->io->writeError('<error>GitHub API limit exhausted. Failed to get metadata for the ' . $this->url . ' repository, try running in interactive mode so that you can enter your GitHub credentials to increase the API limit</error>');
                         throw $e;
                     }
                     $gitHubUtil->authorizeOAuthInteractively($this->originUrl, 'API limit exhausted. Enter your GitHub credentials to get a larger API limit (<info>' . $this->url . '</info>)');
                     return parent::getContents($url);
                 }
                 if ($rateLimited) {
                     $rateLimit = $this->getRateLimit($e->getHeaders());
                     $this->io->writeError(sprintf('<error>GitHub API limit (%d calls/hr) is exhausted. You are already authorized so you have to wait until %s before doing more requests</error>', $rateLimit['limit'], $rateLimit['reset']));
                 }
                 throw $e;
             default:
                 throw $e;
         }
     }
 }
Пример #3
0
 /**
  * Get the remote content.
  *
  * @param string $url The URL of content
  * @param bool $fetchingRepoData
  *
  * @return mixed The result
  */
 protected function getContentsWithOAuthCredentials($url, $fetchingRepoData = false)
 {
     try {
         return parent::getContents($url);
     } catch (TransportException $e) {
         $bitbucketUtil = new Bitbucket($this->io, $this->config, $this->process, $this->remoteFilesystem);
         if (403 === $e->getCode()) {
             if (!$this->io->hasAuthentication($this->originUrl) && $bitbucketUtil->authorizeOAuth($this->originUrl)) {
                 return parent::getContents($url);
             }
             if (!$this->io->isInteractive() && $fetchingRepoData) {
                 return $this->attemptCloneFallback();
             }
         }
         throw $e;
     }
 }