[bugfix] Updated ```DiagnoseCommand::execute()``` and ```DiagnoseCommand::getGithubRateLimit()```

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #3859
| Doc PR        | none

Fixes #3859.
main
Javier Spagnoletti 9 years ago
parent 4d134ce8a2
commit 5b2a7e6bad

@ -51,6 +51,9 @@ EOT
; ;
} }
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
$composer = $this->getComposer(false); $composer = $this->getComposer(false);
@ -98,21 +101,27 @@ EOT
} }
} else { } else {
$this->getIO()->write('Checking github.com rate limit: ', false); $this->getIO()->write('Checking github.com rate limit: ', false);
$rate = $this->getGithubRateLimit('github.com'); try {
$rate = $this->getGithubRateLimit('github.com');
if (10 > $rate['remaining']) { $this->outputResult(true);
$this->getIO()->write('<warning>WARNING</warning>'); if (10 > $rate['remaining']) {
$this->getIO()->write(sprintf( $this->getIO()->write('<warning>WARNING</warning>');
'<comment>Github has a rate limit on their API. ' $this->getIO()->write(sprintf(
. 'You currently have <options=bold>%u</options=bold> ' '<comment>Github has a rate limit on their API. '
. 'out of <options=bold>%u</options=bold> requests left.' . PHP_EOL . 'You currently have <options=bold>%u</options=bold> '
. 'See https://developer.github.com/v3/#rate-limiting and also' . PHP_EOL . 'out of <options=bold>%u</options=bold> requests left.' . PHP_EOL
. ' https://getcomposer.org/doc/articles/troubleshooting.md#api-rate-limit-and-oauth-tokens</comment>', . 'See https://developer.github.com/v3/#rate-limiting and also' . PHP_EOL
$rate['remaining'], . ' https://getcomposer.org/doc/articles/troubleshooting.md#api-rate-limit-and-oauth-tokens</comment>',
$rate['limit'] $rate['remaining'],
)); $rate['limit']
} else { ));
$this->getIO()->write('<info>OK</info>'); }
} catch (\Exception $e) {
if ($e instanceof TransportException && $e->getCode() === 401) {
$this->outputResult('<comment>The oauth token for '.$domain.' seems invalid, run "composer config --global --unset github-oauth.'.$domain.'" to remove it</comment>');
} else {
$this->outputResult($e);
}
} }
} }
@ -263,25 +272,23 @@ EOT
} }
} }
/**
* @param string $domain
* @param string $token
* @return array
* @throws TransportException
*/
private function getGithubRateLimit($domain, $token = null) private function getGithubRateLimit($domain, $token = null)
{ {
if ($token) { if ($token) {
$this->getIO()->setAuthentication($domain, $token, 'x-oauth-basic'); $this->getIO()->setAuthentication($domain, $token, 'x-oauth-basic');
} }
try { $url = $domain === 'github.com' ? 'https://api.'.$domain.'/rate_limit' : 'https://'.$domain.'/api/rate_limit';
$url = $domain === 'github.com' ? 'https://api.'.$domain.'/rate_limit' : 'https://'.$domain.'/api/rate_limit'; $json = $this->rfs->getContents($domain, $url, false, array('retry-auth-failure' => false));
$json = $this->rfs->getContents($domain, $url, false, array('retry-auth-failure' => false)); $data = json_decode($json, true);
$data = json_decode($json, true);
return $data['resources']['core']; return $data['resources']['core'];
} catch (\Exception $e) {
if ($e instanceof TransportException && $e->getCode() === 401) {
return '<comment>The oauth token for '.$domain.' seems invalid, run "composer config --global --unset github-oauth.'.$domain.'" to remove it</comment>';
}
return $e;
}
} }
private function checkDiskSpace($config) private function checkDiskSpace($config)
@ -308,6 +315,9 @@ EOT
return true; return true;
} }
/**
* @param bool|string|\Exception $result
*/
private function outputResult($result) private function outputResult($result)
{ {
if (true === $result) { if (true === $result) {

Loading…
Cancel
Save