GitLabDriver.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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\Config;
  13. use Composer\Cache;
  14. use Composer\IO\IOInterface;
  15. use Composer\Json\JsonFile;
  16. use Composer\Downloader\TransportException;
  17. use Composer\Util\RemoteFilesystem;
  18. use Composer\Util\GitLab;
  19. /**
  20. * Driver for GitLab API, use the Git driver for local checkouts.
  21. *
  22. * @author Henrik Bjørnskov <henrik@bjrnskov.dk>
  23. * @author Jérôme Tamarelle <jerome@tamarelle.net>
  24. */
  25. class GitLabDriver extends VcsDriver
  26. {
  27. private $scheme;
  28. private $owner;
  29. private $repository;
  30. private $cache;
  31. private $infoCache = array();
  32. /**
  33. * @var array Project data returned by GitLab API
  34. */
  35. private $project;
  36. /**
  37. * @var array Keeps commits returned by GitLab API
  38. */
  39. private $commits = array();
  40. /**
  41. * @var array List of tag => reference
  42. */
  43. private $tags;
  44. /**
  45. * @var array List of branch => reference
  46. */
  47. private $branches;
  48. /**
  49. * Git Driver
  50. *
  51. * @var GitDriver
  52. */
  53. protected $gitDriver;
  54. /**
  55. * Extracts information from the repository url.
  56. * SSH urls uses https by default.
  57. *
  58. * {@inheritDoc}
  59. */
  60. public function initialize()
  61. {
  62. if (!preg_match('#^((https?)://([0-9a-zA-Z\./]+)/|git@([^:]+):)([^/]+)/(.+?)(?:\.git|/)?$#', $this->url, $match)) {
  63. throw new \InvalidArgumentException('The URL provided is invalid. It must be the HTTP URL of a GitLab project.');
  64. }
  65. $this->scheme = !empty($match[2]) ? $match[2] : 'https';
  66. $this->originUrl = !empty($match[3]) ? $match[3] : $match[4];
  67. $this->owner = $match[5];
  68. $this->repository = preg_replace('#(\.git)$#', '', $match[6]);
  69. $this->cache = new Cache($this->io, $this->config->get('cache-repo-dir').'/'.$this->originUrl.'/'.$this->owner.'/'.$this->repository);
  70. $this->fetchProject();
  71. }
  72. /**
  73. * Updates the RemoteFilesystem instance.
  74. * Mainly useful for tests.
  75. *
  76. * @internal
  77. */
  78. public function setRemoteFilesystem(RemoteFilesystem $remoteFilesystem)
  79. {
  80. $this->remoteFilesystem = $remoteFilesystem;
  81. }
  82. /**
  83. * Fetches the composer.json file from the project by a identifier.
  84. *
  85. * if specific keys arent present it will try and infer them by default values.
  86. *
  87. * {@inheritDoc}
  88. */
  89. public function getComposerInformation($identifier)
  90. {
  91. // Convert the root identifier to a cachable commit id
  92. if (!preg_match('{[a-f0-9]{40}}i', $identifier)) {
  93. $branches = $this->getBranches();
  94. if (isset($branches[$identifier])) {
  95. $identifier = $branches[$identifier];
  96. }
  97. }
  98. if (isset($this->infoCache[$identifier])) {
  99. return $this->infoCache[$identifier];
  100. }
  101. if (preg_match('{[a-f0-9]{40}}i', $identifier) && $res = $this->cache->read($identifier)) {
  102. return $this->infoCache[$identifier] = JsonFile::parseJson($res, $res);
  103. }
  104. try {
  105. $composer = $this->fetchComposerFile($identifier);
  106. } catch (TransportException $e) {
  107. if ($e->getCode() !== 404) {
  108. throw $e;
  109. }
  110. $composer = false;
  111. }
  112. if ($composer && !isset($composer['time']) && isset($this->commits[$identifier])) {
  113. $composer['time'] = $this->commits[$identifier]['committed_date'];
  114. }
  115. if (preg_match('{[a-f0-9]{40}}i', $identifier)) {
  116. $this->cache->write($identifier, json_encode($composer));
  117. }
  118. return $this->infoCache[$identifier] = $composer;
  119. }
  120. /**
  121. * {@inheritDoc}
  122. */
  123. public function getRepositoryUrl()
  124. {
  125. return $this->project['ssh_url_to_repo'];
  126. }
  127. /**
  128. * {@inheritDoc}
  129. */
  130. public function getUrl()
  131. {
  132. return $this->project['web_url'];
  133. }
  134. /**
  135. * {@inheritDoc}
  136. */
  137. public function getDist($identifier)
  138. {
  139. $url = $this->getApiUrl().'/repository/archive.zip?sha='.$identifier;
  140. return array('type' => 'zip', 'url' => $url, 'reference' => $identifier, 'shasum' => '');
  141. }
  142. /**
  143. * {@inheritDoc}
  144. */
  145. public function getSource($identifier)
  146. {
  147. return array('type' => 'git', 'url' => $this->getRepositoryUrl(), 'reference' => $identifier);
  148. }
  149. /**
  150. * {@inheritDoc}
  151. */
  152. public function getRootIdentifier()
  153. {
  154. return $this->project['default_branch'];
  155. }
  156. /**
  157. * {@inheritDoc}
  158. */
  159. public function getBranches()
  160. {
  161. if (!$this->branches) {
  162. $this->branches = $this->getReferences('branches');
  163. }
  164. return $this->branches;
  165. }
  166. /**
  167. * {@inheritDoc}
  168. */
  169. public function getTags()
  170. {
  171. if (!$this->tags) {
  172. $this->tags = $this->getReferences('tags');
  173. }
  174. return $this->tags;
  175. }
  176. /**
  177. * Fetches composer.json file from the repository through api.
  178. *
  179. * @param string $identifier
  180. *
  181. * @return array
  182. */
  183. protected function fetchComposerFile($identifier)
  184. {
  185. $resource = $this->getApiUrl().'/repository/blobs/'.$identifier.'?filepath=composer.json';
  186. return JsonFile::parseJson($this->getContents($resource), $resource);
  187. }
  188. /**
  189. * @return string Base URL for GitLab API v3
  190. */
  191. public function getApiUrl()
  192. {
  193. return $this->scheme.'://'.$this->originUrl.'/api/v3/projects/'.$this->owner.'%2F'.$this->repository;
  194. }
  195. /**
  196. * @param string $type
  197. *
  198. * @return string[] where keys are named references like tags or branches and the value a sha
  199. */
  200. protected function getReferences($type)
  201. {
  202. $resource = $this->getApiUrl().'/repository/'.$type;
  203. $data = JsonFile::parseJson($this->getContents($resource), $resource);
  204. $references = array();
  205. foreach ($data as $datum) {
  206. $references[$datum['name']] = $datum['commit']['id'];
  207. // Keep the last commit date of a reference to avoid
  208. // unnecessary API call when retrieving the composer file.
  209. $this->commits[$datum['commit']['id']] = $datum['commit'];
  210. }
  211. return $references;
  212. }
  213. protected function fetchProject()
  214. {
  215. // we need to fetch the default branch from the api
  216. $resource = $this->getApiUrl();
  217. $this->project = JsonFile::parseJson($this->getContents($resource, true), $resource);
  218. }
  219. protected function attemptCloneFallback()
  220. {
  221. try {
  222. // If this repository may be private and we
  223. // cannot ask for authentication credentials (because we
  224. // are not interactive) then we fallback to GitDriver.
  225. $this->setupGitDriver($this->generateSshUrl());
  226. return;
  227. } catch (\RuntimeException $e) {
  228. $this->gitDriver = null;
  229. $this->io->writeError('<error>Failed to clone the '.$this->generateSshUrl().' repository, try running in interactive mode so that you can enter your credentials</error>');
  230. throw $e;
  231. }
  232. }
  233. protected function setupGitDriver($url)
  234. {
  235. $this->gitDriver = new GitDriver(
  236. array('url' => $url),
  237. $this->io,
  238. $this->config,
  239. $this->process,
  240. $this->remoteFilesystem
  241. );
  242. $this->gitDriver->initialize();
  243. }
  244. /**
  245. * {@inheritDoc}
  246. */
  247. protected function getContents($url, $fetchingRepoData = false)
  248. {
  249. try {
  250. return parent::getContents($url);
  251. } catch (TransportException $e) {
  252. $gitLabUtil = new GitLab($this->io, $this->config, $this->process, $this->remoteFilesystem);
  253. switch ($e->getCode()) {
  254. case 401:
  255. case 404:
  256. // try to authorize only if we are fetching the main /repos/foo/bar data, otherwise it must be a real 404
  257. if (!$fetchingRepoData) {
  258. throw $e;
  259. }
  260. if ($gitLabUtil->authorizeOAuth($this->originUrl)) {
  261. return parent::getContents($url);
  262. }
  263. if (!$this->io->isInteractive()) {
  264. return $this->attemptCloneFallback();
  265. }
  266. $this->io->writeError('<warning>Failed to download ' . $this->owner . '/' . $this->repository . ':' . $e->getMessage() . '</warning>');
  267. $gitLabUtil->authorizeOAuthInteractively($this->originUrl, 'Your credentials are required to fetch private repository metadata (<info>'.$this->url.'</info>)');
  268. return parent::getContents($url);
  269. case 403:
  270. if (!$this->io->hasAuthentication($this->originUrl) && $gitLabUtil->authorizeOAuth($this->originUrl)) {
  271. return parent::getContents($url);
  272. }
  273. if (!$this->io->isInteractive() && $fetchingRepoData) {
  274. return $this->attemptCloneFallback();
  275. }
  276. throw $e;
  277. default:
  278. throw $e;
  279. }
  280. }
  281. }
  282. /**
  283. * Uses the config `gitlab-domains` to see if the driver supports the url for the
  284. * repository given.
  285. *
  286. * {@inheritDoc}
  287. */
  288. public static function supports(IOInterface $io, Config $config, $url, $deep = false)
  289. {
  290. if (!preg_match('#^((https?)://([^/]+)/|git@([^:]+):)([^/]+)/(.+?)(?:\.git|/)?$#', $url, $match)) {
  291. return false;
  292. }
  293. $scheme = !empty($match[2]) ? $match[2] : 'https';
  294. $originUrl = !empty($match[3]) ? $match[3] : $match[4];
  295. if (!in_array($originUrl, (array) $config->get('gitlab-domains'))) {
  296. return false;
  297. }
  298. if ('https' === $scheme && !extension_loaded('openssl')) {
  299. if ($io->isVerbose()) {
  300. $io->write('Skipping GitLab driver for '.$url.' because the OpenSSL PHP extension is missing.');
  301. }
  302. return false;
  303. }
  304. return true;
  305. }
  306. }