GitLabDriver.php 10 KB

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