GitHubDriver.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Repository\Vcs;
  12. use Composer\Downloader\TransportException;
  13. use Composer\Json\JsonFile;
  14. use Composer\IO\IOInterface;
  15. use Composer\Util\ProcessExecutor;
  16. use Composer\Util\RemoteFilesystem;
  17. /**
  18. * @author Jordi Boggiano <j.boggiano@seld.be>
  19. */
  20. class GitHubDriver extends VcsDriver
  21. {
  22. protected $owner;
  23. protected $repository;
  24. protected $tags;
  25. protected $branches;
  26. protected $rootIdentifier;
  27. protected $infoCache = array();
  28. protected $isPrivate = false;
  29. /**
  30. * Git Driver
  31. *
  32. * @var GitDriver
  33. */
  34. protected $gitDriver;
  35. /**
  36. * {@inheritDoc}
  37. */
  38. public function initialize()
  39. {
  40. preg_match('#^(?:https?|git)://github\.com/([^/]+)/(.+?)(?:\.git)?$#', $this->url, $match);
  41. $this->owner = $match[1];
  42. $this->repository = $match[2];
  43. $this->originUrl = 'github.com';
  44. $this->fetchRootIdentifier();
  45. }
  46. /**
  47. * {@inheritDoc}
  48. */
  49. public function getRootIdentifier()
  50. {
  51. if ($this->gitDriver) {
  52. return $this->gitDriver->getRootIdentifier();
  53. }
  54. return $this->rootIdentifier;
  55. }
  56. /**
  57. * {@inheritDoc}
  58. */
  59. public function getUrl()
  60. {
  61. if ($this->gitDriver) {
  62. return $this->gitDriver->getUrl();
  63. }
  64. return $this->url;
  65. }
  66. /**
  67. * {@inheritDoc}
  68. */
  69. public function getSource($identifier)
  70. {
  71. if ($this->gitDriver) {
  72. return $this->gitDriver->getSource($identifier);
  73. }
  74. $label = array_search($identifier, $this->getTags()) ?: $identifier;
  75. if ($this->isPrivate) {
  76. // Private GitHub repositories should be accessed using the
  77. // SSH version of the URL.
  78. $url = $this->generateSshUrl();
  79. } else {
  80. $url = $this->getUrl();
  81. }
  82. return array('type' => 'git', 'url' => $url, 'reference' => $label);
  83. }
  84. /**
  85. * {@inheritDoc}
  86. */
  87. public function getDist($identifier)
  88. {
  89. if ($this->gitDriver) {
  90. return $this->gitDriver->getDist($identifier);
  91. }
  92. $label = array_search($identifier, $this->getTags()) ?: $identifier;
  93. $url = $this->getScheme() . '://github.com/'.$this->owner.'/'.$this->repository.'/zipball/'.$label;
  94. return array('type' => 'zip', 'url' => $url, 'reference' => $label, 'shasum' => '');
  95. }
  96. /**
  97. * {@inheritDoc}
  98. */
  99. public function getComposerInformation($identifier)
  100. {
  101. if ($this->gitDriver) {
  102. return $this->gitDriver->getComposerInformation($identifier);
  103. }
  104. if (!isset($this->infoCache[$identifier])) {
  105. $composer = $this->getContents($this->getScheme() . '://raw.github.com/'.$this->owner.'/'.$this->repository.'/'.$identifier.'/composer.json');
  106. if (!$composer) {
  107. return;
  108. }
  109. $composer = JsonFile::parseJson($composer);
  110. if (!isset($composer['time'])) {
  111. $commit = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/commits/'.$identifier));
  112. $composer['time'] = $commit['commit']['committer']['date'];
  113. }
  114. $this->infoCache[$identifier] = $composer;
  115. }
  116. return $this->infoCache[$identifier];
  117. }
  118. /**
  119. * {@inheritDoc}
  120. */
  121. public function getTags()
  122. {
  123. if ($this->gitDriver) {
  124. return $this->gitDriver->getTags();
  125. }
  126. if (null === $this->tags) {
  127. $tagsData = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/tags'));
  128. $this->tags = array();
  129. foreach ($tagsData as $tag) {
  130. $this->tags[$tag['name']] = $tag['commit']['sha'];
  131. }
  132. }
  133. return $this->tags;
  134. }
  135. /**
  136. * {@inheritDoc}
  137. */
  138. public function getBranches()
  139. {
  140. if ($this->gitDriver) {
  141. return $this->gitDriver->getBranches();
  142. }
  143. if (null === $this->branches) {
  144. $branchData = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/git/refs/heads'));
  145. $this->branches = array();
  146. foreach ($branchData as $branch) {
  147. $name = substr($branch['ref'], 11);
  148. $this->branches[$name] = $branch['object']['sha'];
  149. }
  150. }
  151. return $this->branches;
  152. }
  153. /**
  154. * {@inheritDoc}
  155. */
  156. public static function supports(IOInterface $io, $url, $deep = false)
  157. {
  158. if (!preg_match('#^(?:https?|git)://github\.com/([^/]+)/(.+?)(?:\.git)?$#', $url)) {
  159. return false;
  160. }
  161. if (!extension_loaded('openssl')) {
  162. if ($io->isVerbose()) {
  163. $io->write('Skipping GitHub driver for '.$url.' because the OpenSSL PHP extension is missing.');
  164. }
  165. return false;
  166. }
  167. return true;
  168. }
  169. /**
  170. * Generate an SSH URL
  171. *
  172. * @return string
  173. */
  174. protected function generateSshUrl()
  175. {
  176. return 'git@github.com:'.$this->owner.'/'.$this->repository.'.git';
  177. }
  178. /**
  179. * Fetch root identifier from GitHub
  180. *
  181. * @throws TransportException
  182. */
  183. protected function fetchRootIdentifier()
  184. {
  185. $repoDataUrl = $this->getScheme() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository;
  186. $attemptCounter = 0;
  187. while (null === $this->rootIdentifier) {
  188. if (5 == $attemptCounter++) {
  189. throw new \RuntimeException("Either you have entered invalid credentials or this GitHub repository does not exists (404)");
  190. }
  191. try {
  192. $repoData = JsonFile::parseJson($this->getContents($repoDataUrl));
  193. if (isset($repoData['default_branch'])) {
  194. $this->rootIdentifier = $repoData['default_branch'];
  195. } elseif (isset($repoData['master_branch'])) {
  196. $this->rootIdentifier = $repoData['master_branch'];
  197. } else {
  198. $this->rootIdentifier = 'master';
  199. }
  200. } catch (TransportException $e) {
  201. switch($e->getCode()) {
  202. case 401:
  203. case 404:
  204. $this->isPrivate = true;
  205. if (!$this->io->isInteractive()) {
  206. // If this repository may be private (hard to say for sure,
  207. // GitHub returns 404 for private repositories) and we
  208. // cannot ask for authentication credentials (because we
  209. // are not interactive) then we fallback to GitDriver.
  210. $this->gitDriver = new GitDriver(
  211. $this->generateSshUrl(),
  212. $this->io,
  213. $this->config,
  214. $this->process,
  215. $this->remoteFilesystem
  216. );
  217. $this->gitDriver->initialize();
  218. return;
  219. }
  220. $this->io->write('Authentication required (<info>'.$this->url.'</info>):');
  221. $username = $this->io->ask('Username: ');
  222. $password = $this->io->askAndHideAnswer('Password: ');
  223. $this->io->setAuthorization($this->originUrl, $username, $password);
  224. break;
  225. default:
  226. throw $e;
  227. break;
  228. }
  229. }
  230. }
  231. }
  232. }