GitLabDriverTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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\Test\Repository\Vcs;
  12. use Composer\Repository\Vcs\GitLabDriver;
  13. use Composer\Config;
  14. use Composer\TestCase;
  15. use Composer\Util\Filesystem;
  16. use Prophecy\Argument;
  17. /**
  18. * @author Jérôme Tamarelle <jerome@tamarelle.net>
  19. */
  20. class GitLabDriverTest extends TestCase
  21. {
  22. private $home;
  23. private $config;
  24. private $io;
  25. private $process;
  26. private $remoteFilesystem;
  27. public function setUp()
  28. {
  29. $this->home = $this->getUniqueTmpDirectory();
  30. $this->config = new Config();
  31. $this->config->merge(array(
  32. 'config' => array(
  33. 'home' => $this->home,
  34. 'gitlab-domains' => array(
  35. 'mycompany.com/gitlab',
  36. 'othercompany.com/nested/gitlab',
  37. 'gitlab.com',
  38. ),
  39. ),
  40. ));
  41. $this->io = $this->prophesize('Composer\IO\IOInterface');
  42. $this->process = $this->prophesize('Composer\Util\ProcessExecutor');
  43. $this->remoteFilesystem = $this->prophesize('Composer\Util\RemoteFilesystem');
  44. }
  45. public function tearDown()
  46. {
  47. $fs = new Filesystem();
  48. $fs->removeDirectory($this->home);
  49. }
  50. public function getInitializeUrls()
  51. {
  52. return array(
  53. array('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v4/projects/mygroup%2Fmyproject'),
  54. array('http://gitlab.com/mygroup/myproject', 'http://gitlab.com/api/v4/projects/mygroup%2Fmyproject'),
  55. array('git@gitlab.com:mygroup/myproject', 'https://gitlab.com/api/v4/projects/mygroup%2Fmyproject'),
  56. );
  57. }
  58. /**
  59. * @dataProvider getInitializeUrls
  60. */
  61. public function testInitialize($url, $apiUrl)
  62. {
  63. // @link http://doc.gitlab.com/ce/api/projects.html#get-single-project
  64. $projectData = <<<JSON
  65. {
  66. "id": 17,
  67. "default_branch": "mymaster",
  68. "visibility": "private",
  69. "http_url_to_repo": "https://gitlab.com/mygroup/myproject.git",
  70. "ssh_url_to_repo": "git@gitlab.com:mygroup/myproject.git",
  71. "last_activity_at": "2014-12-01T09:17:51.000+01:00",
  72. "name": "My Project",
  73. "name_with_namespace": "My Group / My Project",
  74. "path": "myproject",
  75. "path_with_namespace": "mygroup/myproject",
  76. "web_url": "https://gitlab.com/mygroup/myproject"
  77. }
  78. JSON;
  79. $this->remoteFilesystem
  80. ->getContents('gitlab.com', $apiUrl, false, array())
  81. ->willReturn($projectData)
  82. ->shouldBeCalledTimes(1)
  83. ;
  84. $driver = new GitLabDriver(array('url' => $url), $this->io->reveal(), $this->config, $this->process->reveal(), $this->remoteFilesystem->reveal());
  85. $driver->initialize();
  86. $this->assertEquals($apiUrl, $driver->getApiUrl(), 'API URL is derived from the repository URL');
  87. $this->assertEquals('mymaster', $driver->getRootIdentifier(), 'Root identifier is the default branch in GitLab');
  88. $this->assertEquals('git@gitlab.com:mygroup/myproject.git', $driver->getRepositoryUrl(), 'The repository URL is the SSH one by default');
  89. $this->assertEquals('https://gitlab.com/mygroup/myproject', $driver->getUrl());
  90. return $driver;
  91. }
  92. /**
  93. * @dataProvider getInitializeUrls
  94. */
  95. public function testInitializePublicProject($url, $apiUrl)
  96. {
  97. // @link http://doc.gitlab.com/ce/api/projects.html#get-single-project
  98. $projectData = <<<JSON
  99. {
  100. "id": 17,
  101. "default_branch": "mymaster",
  102. "visibility": "public",
  103. "http_url_to_repo": "https://gitlab.com/mygroup/myproject.git",
  104. "ssh_url_to_repo": "git@gitlab.com:mygroup/myproject.git",
  105. "last_activity_at": "2014-12-01T09:17:51.000+01:00",
  106. "name": "My Project",
  107. "name_with_namespace": "My Group / My Project",
  108. "path": "myproject",
  109. "path_with_namespace": "mygroup/myproject",
  110. "web_url": "https://gitlab.com/mygroup/myproject"
  111. }
  112. JSON;
  113. $this->remoteFilesystem
  114. ->getContents('gitlab.com', $apiUrl, false, array())
  115. ->willReturn($projectData)
  116. ->shouldBeCalledTimes(1)
  117. ;
  118. $driver = new GitLabDriver(array('url' => $url), $this->io->reveal(), $this->config, $this->process->reveal(), $this->remoteFilesystem->reveal());
  119. $driver->initialize();
  120. $this->assertEquals($apiUrl, $driver->getApiUrl(), 'API URL is derived from the repository URL');
  121. $this->assertEquals('mymaster', $driver->getRootIdentifier(), 'Root identifier is the default branch in GitLab');
  122. $this->assertEquals('https://gitlab.com/mygroup/myproject.git', $driver->getRepositoryUrl(), 'The repository URL is the SSH one by default');
  123. $this->assertEquals('https://gitlab.com/mygroup/myproject', $driver->getUrl());
  124. return $driver;
  125. }
  126. public function testGetDist()
  127. {
  128. $driver = $this->testInitialize('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v4/projects/mygroup%2Fmyproject');
  129. $reference = 'c3ebdbf9cceddb82cd2089aaef8c7b992e536363';
  130. $expected = array(
  131. 'type' => 'zip',
  132. 'url' => 'https://gitlab.com/api/v4/projects/mygroup%2Fmyproject/repository/archive.zip?sha='.$reference,
  133. 'reference' => $reference,
  134. 'shasum' => '',
  135. );
  136. $this->assertEquals($expected, $driver->getDist($reference));
  137. }
  138. public function testGetSource()
  139. {
  140. $driver = $this->testInitialize('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v4/projects/mygroup%2Fmyproject');
  141. $reference = 'c3ebdbf9cceddb82cd2089aaef8c7b992e536363';
  142. $expected = array(
  143. 'type' => 'git',
  144. 'url' => 'git@gitlab.com:mygroup/myproject.git',
  145. 'reference' => $reference,
  146. );
  147. $this->assertEquals($expected, $driver->getSource($reference));
  148. }
  149. public function testGetSource_GivenPublicProject()
  150. {
  151. $driver = $this->testInitializePublicProject('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v4/projects/mygroup%2Fmyproject', true);
  152. $reference = 'c3ebdbf9cceddb82cd2089aaef8c7b992e536363';
  153. $expected = array(
  154. 'type' => 'git',
  155. 'url' => 'https://gitlab.com/mygroup/myproject.git',
  156. 'reference' => $reference,
  157. );
  158. $this->assertEquals($expected, $driver->getSource($reference));
  159. }
  160. public function testGetTags()
  161. {
  162. $driver = $this->testInitialize('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v4/projects/mygroup%2Fmyproject');
  163. $apiUrl = 'https://gitlab.com/api/v4/projects/mygroup%2Fmyproject/repository/tags';
  164. // @link http://doc.gitlab.com/ce/api/repositories.html#list-project-repository-tags
  165. $tagData = <<<JSON
  166. [
  167. {
  168. "name": "v1.0.0",
  169. "commit": {
  170. "id": "092ed2c762bbae331e3f51d4a17f67310bf99a81",
  171. "committed_date": "2012-05-28T04:42:42-07:00"
  172. }
  173. },
  174. {
  175. "name": "v2.0.0",
  176. "commit": {
  177. "id": "8e8f60b3ec86d63733db3bd6371117a758027ec6",
  178. "committed_date": "2014-07-06T12:59:11.000+02:00"
  179. }
  180. }
  181. ]
  182. JSON;
  183. $this->remoteFilesystem
  184. ->getContents('gitlab.com', $apiUrl, false, array())
  185. ->willReturn($tagData)
  186. ->shouldBeCalledTimes(1)
  187. ;
  188. $this->remoteFilesystem->getLastHeaders()
  189. ->willReturn(array());
  190. $driver->setRemoteFilesystem($this->remoteFilesystem->reveal());
  191. $expected = array(
  192. 'v1.0.0' => '092ed2c762bbae331e3f51d4a17f67310bf99a81',
  193. 'v2.0.0' => '8e8f60b3ec86d63733db3bd6371117a758027ec6',
  194. );
  195. $this->assertEquals($expected, $driver->getTags());
  196. $this->assertEquals($expected, $driver->getTags(), 'Tags are cached');
  197. }
  198. public function testGetPaginatedRefs() {
  199. $driver = $this->testInitialize('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v4/projects/mygroup%2Fmyproject');
  200. $apiUrl = 'https://gitlab.com/api/v4/projects/mygroup%2Fmyproject/repository/branches';
  201. // @link http://doc.gitlab.com/ce/api/repositories.html#list-project-repository-branches
  202. $branchData = <<<JSON
  203. [
  204. {
  205. "name": "mymaster",
  206. "commit": {
  207. "id": "97eda36b5c1dd953a3792865c222d4e85e5f302e",
  208. "committed_date": "2013-01-03T21:04:07.000+01:00"
  209. }
  210. },
  211. {
  212. "name": "staging",
  213. "commit": {
  214. "id": "502cffe49f136443f2059803f2e7192d1ac066cd",
  215. "committed_date": "2013-03-09T16:35:23.000+01:00"
  216. }
  217. }
  218. ]
  219. JSON;
  220. $this->remoteFilesystem
  221. ->getContents('gitlab.com', $apiUrl, false, array())
  222. ->willReturn($branchData)
  223. ->shouldBeCalledTimes(1)
  224. ;
  225. $this->remoteFilesystem
  226. ->getContents('gitlab.com', "http://gitlab.com/api/v4/projects/mygroup%2Fmyproject/repository/tags?id=mygroup%2Fmyproject&page=2&per_page=20", false, array())
  227. ->willReturn($branchData)
  228. ->shouldBeCalledTimes(1)
  229. ;
  230. $this->remoteFilesystem->getLastHeaders()
  231. ->willReturn(array('Link: <http://gitlab.com/api/v4/projects/mygroup%2Fmyproject/repository/tags?id=mygroup%2Fmyproject&page=2&per_page=20>; rel="next", <http://gitlab.com/api/v4/projects/mygroup%2Fmyproject/repository/tags?id=mygroup%2Fmyproject&page=1&per_page=20>; rel="first", <http://gitlab.com/api/v4/projects/mygroup%2Fmyproject/repository/tags?id=mygroup%2Fmyproject&page=3&per_page=20>; rel="last"'), array('Link: <http://gitlab.com/api/v4/projects/mygroup%2Fmyproject/repository/tags?id=mygroup%2Fmyproject&page=2&per_page=20>; rel="prev", <http://gitlab.com/api/v4/projects/mygroup%2Fmyproject/repository/tags?id=mygroup%2Fmyproject&page=1&per_page=20>; rel="first", <http://gitlab.com/api/v4/projects/mygroup%2Fmyproject/repository/tags?id=mygroup%2Fmyproject&page=3&per_page=20>; rel="last"'))
  232. ->shouldBeCalledTimes(2);
  233. $driver->setRemoteFilesystem($this->remoteFilesystem->reveal());
  234. $expected = array(
  235. 'mymaster' => '97eda36b5c1dd953a3792865c222d4e85e5f302e',
  236. 'staging' => '502cffe49f136443f2059803f2e7192d1ac066cd',
  237. );
  238. $this->assertEquals($expected, $driver->getBranches());
  239. $this->assertEquals($expected, $driver->getBranches(), 'Branches are cached');
  240. }
  241. public function testGetBranches()
  242. {
  243. $driver = $this->testInitialize('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v4/projects/mygroup%2Fmyproject');
  244. $apiUrl = 'https://gitlab.com/api/v4/projects/mygroup%2Fmyproject/repository/branches';
  245. // @link http://doc.gitlab.com/ce/api/repositories.html#list-project-repository-branches
  246. $branchData = <<<JSON
  247. [
  248. {
  249. "name": "mymaster",
  250. "commit": {
  251. "id": "97eda36b5c1dd953a3792865c222d4e85e5f302e",
  252. "committed_date": "2013-01-03T21:04:07.000+01:00"
  253. }
  254. },
  255. {
  256. "name": "staging",
  257. "commit": {
  258. "id": "502cffe49f136443f2059803f2e7192d1ac066cd",
  259. "committed_date": "2013-03-09T16:35:23.000+01:00"
  260. }
  261. }
  262. ]
  263. JSON;
  264. $this->remoteFilesystem
  265. ->getContents('gitlab.com', $apiUrl, false, array())
  266. ->willReturn($branchData)
  267. ->shouldBeCalledTimes(1)
  268. ;
  269. $this->remoteFilesystem->getLastHeaders()
  270. ->willReturn([]);
  271. $driver->setRemoteFilesystem($this->remoteFilesystem->reveal());
  272. $expected = array(
  273. 'mymaster' => '97eda36b5c1dd953a3792865c222d4e85e5f302e',
  274. 'staging' => '502cffe49f136443f2059803f2e7192d1ac066cd',
  275. );
  276. $this->assertEquals($expected, $driver->getBranches());
  277. $this->assertEquals($expected, $driver->getBranches(), 'Branches are cached');
  278. }
  279. /**
  280. * @dataProvider dataForTestSupports
  281. */
  282. public function testSupports($url, $expected)
  283. {
  284. $this->assertSame($expected, GitLabDriver::supports($this->io->reveal(), $this->config, $url));
  285. }
  286. public function dataForTestSupports()
  287. {
  288. return array(
  289. array('http://gitlab.com/foo/bar', true),
  290. array('http://gitlab.com/foo/bar/', true),
  291. array('http://gitlab.com/foo/bar.git', true),
  292. array('http://gitlab.com/foo/bar.baz.git', true),
  293. array('https://gitlab.com/foo/bar', extension_loaded('openssl')), // Platform requirement
  294. array('git@gitlab.com:foo/bar.git', extension_loaded('openssl')),
  295. array('git@example.com:foo/bar.git', false),
  296. array('http://example.com/foo/bar', false),
  297. array('http://mycompany.com/gitlab/mygroup/myproject', true),
  298. array('https://mycompany.com/gitlab/mygroup/myproject', extension_loaded('openssl')),
  299. array('http://othercompany.com/nested/gitlab/mygroup/myproject', true),
  300. array('https://othercompany.com/nested/gitlab/mygroup/myproject', extension_loaded('openssl')),
  301. array('http://gitlab.com/mygroup/mysubgroup/mysubsubgroup/myproject', true),
  302. array('https://gitlab.com/mygroup/mysubgroup/mysubsubgroup/myproject', extension_loaded('openssl')),
  303. );
  304. }
  305. public function testGitlabSubDirectory()
  306. {
  307. $url = 'https://mycompany.com/gitlab/mygroup/my-pro.ject';
  308. $apiUrl = 'https://mycompany.com/gitlab/api/v4/projects/mygroup%2Fmy-pro%2Eject';
  309. $projectData = <<<JSON
  310. {
  311. "id": 17,
  312. "default_branch": "mymaster",
  313. "visibility": "private",
  314. "http_url_to_repo": "https://gitlab.com/gitlab/mygroup/my-pro.ject",
  315. "ssh_url_to_repo": "git@gitlab.com:mygroup/my-pro.ject.git",
  316. "last_activity_at": "2014-12-01T09:17:51.000+01:00",
  317. "name": "My Project",
  318. "name_with_namespace": "My Group / My Project",
  319. "path": "myproject",
  320. "path_with_namespace": "mygroup/my-pro.ject",
  321. "web_url": "https://gitlab.com/gitlab/mygroup/my-pro.ject"
  322. }
  323. JSON;
  324. $this->remoteFilesystem
  325. ->getContents('mycompany.com/gitlab', $apiUrl, false, array())
  326. ->willReturn($projectData)
  327. ->shouldBeCalledTimes(1)
  328. ;
  329. $driver = new GitLabDriver(array('url' => $url), $this->io->reveal(), $this->config, $this->process->reveal(), $this->remoteFilesystem->reveal());
  330. $driver->initialize();
  331. $this->assertEquals($apiUrl, $driver->getApiUrl(), 'API URL is derived from the repository URL');
  332. }
  333. public function testGitlabSubGroup()
  334. {
  335. $url = 'https://gitlab.com/mygroup/mysubgroup/myproject';
  336. $apiUrl = 'https://gitlab.com/api/v4/projects/mygroup%2Fmysubgroup%2Fmyproject';
  337. $projectData = <<<JSON
  338. {
  339. "id": 17,
  340. "default_branch": "mymaster",
  341. "visibility": "private",
  342. "http_url_to_repo": "https://gitlab.com/mygroup/mysubgroup/my-pro.ject",
  343. "ssh_url_to_repo": "git@gitlab.com:mygroup/mysubgroup/my-pro.ject.git",
  344. "last_activity_at": "2014-12-01T09:17:51.000+01:00",
  345. "name": "My Project",
  346. "name_with_namespace": "My Group / My Project",
  347. "path": "myproject",
  348. "path_with_namespace": "mygroup/mysubgroup/my-pro.ject",
  349. "web_url": "https://gitlab.com/mygroup/mysubgroup/my-pro.ject"
  350. }
  351. JSON;
  352. $this->remoteFilesystem
  353. ->getContents('gitlab.com', $apiUrl, false, array())
  354. ->willReturn($projectData)
  355. ->shouldBeCalledTimes(1)
  356. ;
  357. $driver = new GitLabDriver(array('url' => $url), $this->io->reveal(), $this->config, $this->process->reveal(), $this->remoteFilesystem->reveal());
  358. $driver->initialize();
  359. $this->assertEquals($apiUrl, $driver->getApiUrl(), 'API URL is derived from the repository URL');
  360. }
  361. public function testGitlabSubDirectorySubGroup()
  362. {
  363. $url = 'https://mycompany.com/gitlab/mygroup/mysubgroup/myproject';
  364. $apiUrl = 'https://mycompany.com/gitlab/api/v4/projects/mygroup%2Fmysubgroup%2Fmyproject';
  365. $projectData = <<<JSON
  366. {
  367. "id": 17,
  368. "default_branch": "mymaster",
  369. "visibility": "private",
  370. "http_url_to_repo": "https://mycompany.com/gitlab/mygroup/mysubgroup/my-pro.ject",
  371. "ssh_url_to_repo": "git@mycompany.com:mygroup/mysubgroup/my-pro.ject.git",
  372. "last_activity_at": "2014-12-01T09:17:51.000+01:00",
  373. "name": "My Project",
  374. "name_with_namespace": "My Group / My Project",
  375. "path": "myproject",
  376. "path_with_namespace": "mygroup/mysubgroup/my-pro.ject",
  377. "web_url": "https://mycompany.com/gitlab/mygroup/mysubgroup/my-pro.ject"
  378. }
  379. JSON;
  380. $this->remoteFilesystem
  381. ->getContents('mycompany.com/gitlab', $apiUrl, false, array())
  382. ->willReturn($projectData)
  383. ->shouldBeCalledTimes(1)
  384. ;
  385. $driver = new GitLabDriver(array('url' => $url), $this->io->reveal(), $this->config, $this->process->reveal(), $this->remoteFilesystem->reveal());
  386. $driver->initialize();
  387. $this->assertEquals($apiUrl, $driver->getApiUrl(), 'API URL is derived from the repository URL');
  388. }
  389. public function testForwardsOptions()
  390. {
  391. $options = array(
  392. 'ssl' => array(
  393. 'verify_peer' => false,
  394. ),
  395. );
  396. $projectData = <<<JSON
  397. {
  398. "id": 17,
  399. "default_branch": "mymaster",
  400. "visibility": "private",
  401. "http_url_to_repo": "https://gitlab.mycompany.local/mygroup/myproject",
  402. "ssh_url_to_repo": "git@gitlab.mycompany.local:mygroup/myproject.git",
  403. "last_activity_at": "2014-12-01T09:17:51.000+01:00",
  404. "name": "My Project",
  405. "name_with_namespace": "My Group / My Project",
  406. "path": "myproject",
  407. "path_with_namespace": "mygroup/myproject",
  408. "web_url": "https://gitlab.mycompany.local/mygroup/myproject"
  409. }
  410. JSON;
  411. $this->remoteFilesystem
  412. ->getContents(Argument::cetera(), $options)
  413. ->willReturn($projectData)
  414. ->shouldBeCalled();
  415. $driver = new GitLabDriver(
  416. array('url' => 'https://gitlab.mycompany.local/mygroup/myproject', 'options' => $options),
  417. $this->io->reveal(),
  418. $this->config,
  419. $this->process->reveal(),
  420. $this->remoteFilesystem->reveal()
  421. );
  422. $driver->initialize();
  423. }
  424. }