GitLabDriver.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. /**
  31. * @var array Project data returned by GitLab API
  32. */
  33. private $project;
  34. /**
  35. * @var array Keeps commits returned by GitLab API
  36. */
  37. private $commits = array();
  38. /**
  39. * @var array List of tag => reference
  40. */
  41. private $tags;
  42. /**
  43. * @var array List of branch => reference
  44. */
  45. private $branches;
  46. /**
  47. * Git Driver
  48. *
  49. * @var GitDriver
  50. */
  51. protected $gitDriver;
  52. const URL_REGEX = '#^(?:(?P<scheme>https?)://(?P<domain>.+?)/|git@(?P<domain2>[^:]+):)(?P<owner>[^/]+)/(?P<repo>[^/]+?)(?:\.git|/)?$#';
  53. /**
  54. * Extracts information from the repository url.
  55. *
  56. * SSH urls use https by default. Set "secure-http": false on the repository config to use http instead.
  57. *
  58. * {@inheritDoc}
  59. */
  60. public function initialize()
  61. {
  62. if (!preg_match(self::URL_REGEX, $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['scheme']) ? $match['scheme'] : (isset($this->repoConfig['secure-http']) && $this->repoConfig['secure-http'] === false ? 'http' : 'https');
  66. $this->originUrl = !empty($match['domain']) ? $match['domain'] : $match['domain2'];
  67. $this->owner = $match['owner'];
  68. $this->repository = preg_replace('#(\.git)$#', '', $match['repo']);
  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. * {@inheritdoc}
  84. */
  85. public function getFileContent($file, $identifier)
  86. {
  87. // Convert the root identifier to a cachable commit id
  88. if (!preg_match('{[a-f0-9]{40}}i', $identifier)) {
  89. $branches = $this->getBranches();
  90. if (isset($branches[$identifier])) {
  91. $identifier = $branches[$identifier];
  92. }
  93. }
  94. $resource = $this->getApiUrl().'/repository/blobs/'.$identifier.'?filepath=' . $file;
  95. try {
  96. $content = $this->getContents($resource);
  97. } catch (TransportException $e) {
  98. if ($e->getCode() !== 404) {
  99. throw $e;
  100. }
  101. return null;
  102. }
  103. return $content;
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function getChangeDate($identifier)
  109. {
  110. if (isset($this->commits[$identifier])) {
  111. return new \DateTime($this->commits[$identifier]['committed_date']);
  112. }
  113. return new \DateTime();
  114. }
  115. /**
  116. * {@inheritDoc}
  117. */
  118. public function getRepositoryUrl()
  119. {
  120. return $this->project['public'] ? $this->project['http_url_to_repo'] : $this->project['ssh_url_to_repo'];
  121. }
  122. /**
  123. * {@inheritDoc}
  124. */
  125. public function getUrl()
  126. {
  127. return $this->project['web_url'];
  128. }
  129. /**
  130. * {@inheritDoc}
  131. */
  132. public function getDist($identifier)
  133. {
  134. $url = $this->getApiUrl().'/repository/archive.zip?sha='.$identifier;
  135. return array('type' => 'zip', 'url' => $url, 'reference' => $identifier, 'shasum' => '');
  136. }
  137. /**
  138. * {@inheritDoc}
  139. */
  140. public function getSource($identifier)
  141. {
  142. return array('type' => 'git', 'url' => $this->getRepositoryUrl(), 'reference' => $identifier);
  143. }
  144. /**
  145. * {@inheritDoc}
  146. */
  147. public function getRootIdentifier()
  148. {
  149. return $this->project['default_branch'];
  150. }
  151. /**
  152. * {@inheritDoc}
  153. */
  154. public function getBranches()
  155. {
  156. if (!$this->branches) {
  157. $this->branches = $this->getReferences('branches');
  158. }
  159. return $this->branches;
  160. }
  161. /**
  162. * {@inheritDoc}
  163. */
  164. public function getTags()
  165. {
  166. if (!$this->tags) {
  167. $this->tags = $this->getReferences('tags');
  168. }
  169. return $this->tags;
  170. }
  171. /**
  172. * @return string Base URL for GitLab API v3
  173. */
  174. public function getApiUrl()
  175. {
  176. return $this->scheme.'://'.$this->originUrl.'/api/v3/projects/'.$this->urlEncodeAll($this->owner).'%2F'.$this->urlEncodeAll($this->repository);
  177. }
  178. /**
  179. * Urlencode all non alphanumeric characters. rawurlencode() can not be used as it does not encode `.`
  180. *
  181. * @param string $string
  182. * @return string
  183. */
  184. private function urlEncodeAll($string)
  185. {
  186. $encoded = '';
  187. for ($i = 0; isset($string[$i]); $i++) {
  188. $character = $string[$i];
  189. if (!ctype_alnum($character) && !in_array($character, array('-', '_'), true)) {
  190. $character = '%' . sprintf('%02X', ord($character));
  191. }
  192. $encoded .= $character;
  193. }
  194. return $encoded;
  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. /**
  235. * Generate an SSH URL
  236. *
  237. * @return string
  238. */
  239. protected function generateSshUrl()
  240. {
  241. return 'git@' . $this->originUrl . ':'.$this->owner.'/'.$this->repository.'.git';
  242. }
  243. protected function setupGitDriver($url)
  244. {
  245. $this->gitDriver = new GitDriver(
  246. array('url' => $url),
  247. $this->io,
  248. $this->config,
  249. $this->process,
  250. $this->remoteFilesystem
  251. );
  252. $this->gitDriver->initialize();
  253. }
  254. /**
  255. * {@inheritDoc}
  256. */
  257. protected function getContents($url, $fetchingRepoData = false)
  258. {
  259. try {
  260. return parent::getContents($url);
  261. } catch (TransportException $e) {
  262. $gitLabUtil = new GitLab($this->io, $this->config, $this->process, $this->remoteFilesystem);
  263. switch ($e->getCode()) {
  264. case 401:
  265. case 404:
  266. // try to authorize only if we are fetching the main /repos/foo/bar data, otherwise it must be a real 404
  267. if (!$fetchingRepoData) {
  268. throw $e;
  269. }
  270. if ($gitLabUtil->authorizeOAuth($this->originUrl)) {
  271. return parent::getContents($url);
  272. }
  273. if (!$this->io->isInteractive()) {
  274. return $this->attemptCloneFallback();
  275. }
  276. $this->io->writeError('<warning>Failed to download ' . $this->owner . '/' . $this->repository . ':' . $e->getMessage() . '</warning>');
  277. $gitLabUtil->authorizeOAuthInteractively($this->scheme, $this->originUrl, 'Your credentials are required to fetch private repository metadata (<info>'.$this->url.'</info>)');
  278. return parent::getContents($url);
  279. case 403:
  280. if (!$this->io->hasAuthentication($this->originUrl) && $gitLabUtil->authorizeOAuth($this->originUrl)) {
  281. return parent::getContents($url);
  282. }
  283. if (!$this->io->isInteractive() && $fetchingRepoData) {
  284. return $this->attemptCloneFallback();
  285. }
  286. throw $e;
  287. default:
  288. throw $e;
  289. }
  290. }
  291. }
  292. /**
  293. * Uses the config `gitlab-domains` to see if the driver supports the url for the
  294. * repository given.
  295. *
  296. * {@inheritDoc}
  297. */
  298. public static function supports(IOInterface $io, Config $config, $url, $deep = false)
  299. {
  300. if (!preg_match(self::URL_REGEX, $url, $match)) {
  301. return false;
  302. }
  303. $scheme = !empty($match['scheme']) ? $match['scheme'] : null;
  304. $originUrl = !empty($match['domain']) ? $match['domain'] : $match['domain2'];
  305. if (!in_array($originUrl, (array) $config->get('gitlab-domains'))) {
  306. return false;
  307. }
  308. if ('https' === $scheme && !extension_loaded('openssl')) {
  309. $io->writeError('Skipping GitLab driver for '.$url.' because the OpenSSL PHP extension is missing.', true, IOInterface::VERBOSE);
  310. return false;
  311. }
  312. return true;
  313. }
  314. }