Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. GitHubDriver.php

function GitHubDriver::getContents

@inheritDoc

Overrides VcsDriver::getContents

2 calls to GitHubDriver::getContents()
GitHubDriver::getBranches in vendor/composer/composer/src/Composer/Repository/Vcs/GitHubDriver.php
@inheritDoc
GitHubDriver::getTags in vendor/composer/composer/src/Composer/Repository/Vcs/GitHubDriver.php
@inheritDoc

File

vendor/composer/composer/src/Composer/Repository/Vcs/GitHubDriver.php, line 445

Class

GitHubDriver
@author Jordi Boggiano <j.boggiano@seld.be>

Namespace

Composer\Repository\Vcs

Code

protected function getContents(string $url, bool $fetchingRepoData = false) : Response {
    try {
        return parent::getContents($url);
    } catch (TransportException $e) {
        $gitHubUtil = new GitHub($this->io, $this->config, $this->process, $this->httpDownloader);
        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()) {
                    $this->attemptCloneFallback();
                    return new Response([
                        'url' => 'dummy',
                    ], 200, [], 'null');
                }
                $scopesIssued = [];
                $scopesNeeded = [];
                if ($headers = $e->getHeaders()) {
                    if ($scopes = Response::findHeaderValue($headers, 'X-OAuth-Scopes')) {
                        $scopesIssued = explode(' ', $scopes);
                    }
                    if ($scopes = Response::findHeaderValue($headers, 'X-Accepted-OAuth-Scopes')) {
                        $scopesNeeded = explode(' ', $scopes);
                    }
                }
                $scopesFailed = array_diff($scopesNeeded, $scopesIssued);
                // non-authenticated requests get no scopesNeeded, so ask for credentials
                // authenticated requests which failed some scopes should ask for new credentials too
                if (!$headers || !count($scopesNeeded) || count($scopesFailed)) {
                    $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) {
                    $this->attemptCloneFallback();
                    return new Response([
                        'url' => 'dummy',
                    ], 200, [], 'null');
                }
                $rateLimited = $gitHubUtil->isRateLimited((array) $e->getHeaders());
                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 = $gitHubUtil->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;
        }
    }
}
RSS feed
Powered by Drupal