GitHubDriver.php 9.9 KB

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