GitLabDriverTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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?per_page=100';
  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?per_page=100';
  201. // @link http://doc.gitlab.com/ce/api/repositories.html#list-project-repository-branches
  202. $branchData = array(
  203. array(
  204. "name" => "mymaster",
  205. "commit" => array(
  206. "id" => "97eda36b5c1dd953a3792865c222d4e85e5f302e",
  207. "committed_date" => "2013-01-03T21:04:07.000+01:00"
  208. )
  209. ),
  210. array(
  211. "name" => "staging",
  212. "commit" => array(
  213. "id" => "502cffe49f136443f2059803f2e7192d1ac066cd",
  214. "committed_date" => "2013-03-09T16:35:23.000+01:00"
  215. )
  216. ),
  217. );
  218. for ($i = 0; $i < 98; $i++) {
  219. $branchData[] = array(
  220. "name" => "stagingdupe",
  221. "commit" => array(
  222. "id" => "502cffe49f136443f2059803f2e7192d1ac066cd",
  223. "committed_date" => "2013-03-09T16:35:23.000+01:00"
  224. )
  225. );
  226. }
  227. $branchData = json_encode($branchData);
  228. $this->remoteFilesystem
  229. ->getContents('gitlab.com', $apiUrl, false, array())
  230. ->willReturn($branchData)
  231. ->shouldBeCalledTimes(1)
  232. ;
  233. $this->remoteFilesystem
  234. ->getContents('gitlab.com', "http://gitlab.com/api/v4/projects/mygroup%2Fmyproject/repository/tags?id=mygroup%2Fmyproject&page=2&per_page=20", false, array())
  235. ->willReturn($branchData)
  236. ->shouldBeCalledTimes(1)
  237. ;
  238. $this->remoteFilesystem->getLastHeaders()
  239. ->willReturn(
  240. 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"'),
  241. 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"')
  242. )
  243. ->shouldBeCalledTimes(2);
  244. $driver->setRemoteFilesystem($this->remoteFilesystem->reveal());
  245. $expected = array(
  246. 'mymaster' => '97eda36b5c1dd953a3792865c222d4e85e5f302e',
  247. 'staging' => '502cffe49f136443f2059803f2e7192d1ac066cd',
  248. 'stagingdupe' => '502cffe49f136443f2059803f2e7192d1ac066cd',
  249. );
  250. $this->assertEquals($expected, $driver->getBranches());
  251. $this->assertEquals($expected, $driver->getBranches(), 'Branches are cached');
  252. }
  253. public function testGetBranches()
  254. {
  255. $driver = $this->testInitialize('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v4/projects/mygroup%2Fmyproject');
  256. $apiUrl = 'https://gitlab.com/api/v4/projects/mygroup%2Fmyproject/repository/branches?per_page=100';
  257. // @link http://doc.gitlab.com/ce/api/repositories.html#list-project-repository-branches
  258. $branchData = <<<JSON
  259. [
  260. {
  261. "name": "mymaster",
  262. "commit": {
  263. "id": "97eda36b5c1dd953a3792865c222d4e85e5f302e",
  264. "committed_date": "2013-01-03T21:04:07.000+01:00"
  265. }
  266. },
  267. {
  268. "name": "staging",
  269. "commit": {
  270. "id": "502cffe49f136443f2059803f2e7192d1ac066cd",
  271. "committed_date": "2013-03-09T16:35:23.000+01:00"
  272. }
  273. }
  274. ]
  275. JSON;
  276. $this->remoteFilesystem
  277. ->getContents('gitlab.com', $apiUrl, false, array())
  278. ->willReturn($branchData)
  279. ->shouldBeCalledTimes(1)
  280. ;
  281. $this->remoteFilesystem->getLastHeaders()
  282. ->willReturn(array());
  283. $driver->setRemoteFilesystem($this->remoteFilesystem->reveal());
  284. $expected = array(
  285. 'mymaster' => '97eda36b5c1dd953a3792865c222d4e85e5f302e',
  286. 'staging' => '502cffe49f136443f2059803f2e7192d1ac066cd',
  287. );
  288. $this->assertEquals($expected, $driver->getBranches());
  289. $this->assertEquals($expected, $driver->getBranches(), 'Branches are cached');
  290. }
  291. /**
  292. * @dataProvider dataForTestSupports
  293. */
  294. public function testSupports($url, $expected)
  295. {
  296. $this->assertSame($expected, GitLabDriver::supports($this->io->reveal(), $this->config, $url));
  297. }
  298. public function dataForTestSupports()
  299. {
  300. return array(
  301. array('http://gitlab.com/foo/bar', true),
  302. array('http://gitlab.com/foo/bar/', true),
  303. array('http://gitlab.com/foo/bar.git', true),
  304. array('http://gitlab.com/foo/bar.baz.git', true),
  305. array('https://gitlab.com/foo/bar', extension_loaded('openssl')), // Platform requirement
  306. array('git@gitlab.com:foo/bar.git', extension_loaded('openssl')),
  307. array('git@example.com:foo/bar.git', false),
  308. array('http://example.com/foo/bar', false),
  309. array('http://mycompany.com/gitlab/mygroup/myproject', true),
  310. array('https://mycompany.com/gitlab/mygroup/myproject', extension_loaded('openssl')),
  311. array('http://othercompany.com/nested/gitlab/mygroup/myproject', true),
  312. array('https://othercompany.com/nested/gitlab/mygroup/myproject', extension_loaded('openssl')),
  313. array('http://gitlab.com/mygroup/mysubgroup/mysubsubgroup/myproject', true),
  314. array('https://gitlab.com/mygroup/mysubgroup/mysubsubgroup/myproject', extension_loaded('openssl')),
  315. );
  316. }
  317. public function testGitlabSubDirectory()
  318. {
  319. $url = 'https://mycompany.com/gitlab/mygroup/my-pro.ject';
  320. $apiUrl = 'https://mycompany.com/gitlab/api/v4/projects/mygroup%2Fmy-pro%2Eject';
  321. $projectData = <<<JSON
  322. {
  323. "id": 17,
  324. "default_branch": "mymaster",
  325. "visibility": "private",
  326. "http_url_to_repo": "https://gitlab.com/gitlab/mygroup/my-pro.ject",
  327. "ssh_url_to_repo": "git@gitlab.com:mygroup/my-pro.ject.git",
  328. "last_activity_at": "2014-12-01T09:17:51.000+01:00",
  329. "name": "My Project",
  330. "name_with_namespace": "My Group / My Project",
  331. "path": "myproject",
  332. "path_with_namespace": "mygroup/my-pro.ject",
  333. "web_url": "https://gitlab.com/gitlab/mygroup/my-pro.ject"
  334. }
  335. JSON;
  336. $this->remoteFilesystem
  337. ->getContents('mycompany.com/gitlab', $apiUrl, false, array())
  338. ->willReturn($projectData)
  339. ->shouldBeCalledTimes(1)
  340. ;
  341. $driver = new GitLabDriver(array('url' => $url), $this->io->reveal(), $this->config, $this->process->reveal(), $this->remoteFilesystem->reveal());
  342. $driver->initialize();
  343. $this->assertEquals($apiUrl, $driver->getApiUrl(), 'API URL is derived from the repository URL');
  344. }
  345. public function testGitlabSubGroup()
  346. {
  347. $url = 'https://gitlab.com/mygroup/mysubgroup/myproject';
  348. $apiUrl = 'https://gitlab.com/api/v4/projects/mygroup%2Fmysubgroup%2Fmyproject';
  349. $projectData = <<<JSON
  350. {
  351. "id": 17,
  352. "default_branch": "mymaster",
  353. "visibility": "private",
  354. "http_url_to_repo": "https://gitlab.com/mygroup/mysubgroup/my-pro.ject",
  355. "ssh_url_to_repo": "git@gitlab.com:mygroup/mysubgroup/my-pro.ject.git",
  356. "last_activity_at": "2014-12-01T09:17:51.000+01:00",
  357. "name": "My Project",
  358. "name_with_namespace": "My Group / My Project",
  359. "path": "myproject",
  360. "path_with_namespace": "mygroup/mysubgroup/my-pro.ject",
  361. "web_url": "https://gitlab.com/mygroup/mysubgroup/my-pro.ject"
  362. }
  363. JSON;
  364. $this->remoteFilesystem
  365. ->getContents('gitlab.com', $apiUrl, false, array())
  366. ->willReturn($projectData)
  367. ->shouldBeCalledTimes(1)
  368. ;
  369. $driver = new GitLabDriver(array('url' => $url), $this->io->reveal(), $this->config, $this->process->reveal(), $this->remoteFilesystem->reveal());
  370. $driver->initialize();
  371. $this->assertEquals($apiUrl, $driver->getApiUrl(), 'API URL is derived from the repository URL');
  372. }
  373. public function testGitlabSubDirectorySubGroup()
  374. {
  375. $url = 'https://mycompany.com/gitlab/mygroup/mysubgroup/myproject';
  376. $apiUrl = 'https://mycompany.com/gitlab/api/v4/projects/mygroup%2Fmysubgroup%2Fmyproject';
  377. $projectData = <<<JSON
  378. {
  379. "id": 17,
  380. "default_branch": "mymaster",
  381. "visibility": "private",
  382. "http_url_to_repo": "https://mycompany.com/gitlab/mygroup/mysubgroup/my-pro.ject",
  383. "ssh_url_to_repo": "git@mycompany.com:mygroup/mysubgroup/my-pro.ject.git",
  384. "last_activity_at": "2014-12-01T09:17:51.000+01:00",
  385. "name": "My Project",
  386. "name_with_namespace": "My Group / My Project",
  387. "path": "myproject",
  388. "path_with_namespace": "mygroup/mysubgroup/my-pro.ject",
  389. "web_url": "https://mycompany.com/gitlab/mygroup/mysubgroup/my-pro.ject"
  390. }
  391. JSON;
  392. $this->remoteFilesystem
  393. ->getContents('mycompany.com/gitlab', $apiUrl, false, array())
  394. ->willReturn($projectData)
  395. ->shouldBeCalledTimes(1)
  396. ;
  397. $driver = new GitLabDriver(array('url' => $url), $this->io->reveal(), $this->config, $this->process->reveal(), $this->remoteFilesystem->reveal());
  398. $driver->initialize();
  399. $this->assertEquals($apiUrl, $driver->getApiUrl(), 'API URL is derived from the repository URL');
  400. }
  401. public function testForwardsOptions()
  402. {
  403. $options = array(
  404. 'ssl' => array(
  405. 'verify_peer' => false,
  406. ),
  407. );
  408. $projectData = <<<JSON
  409. {
  410. "id": 17,
  411. "default_branch": "mymaster",
  412. "visibility": "private",
  413. "http_url_to_repo": "https://gitlab.mycompany.local/mygroup/myproject",
  414. "ssh_url_to_repo": "git@gitlab.mycompany.local:mygroup/myproject.git",
  415. "last_activity_at": "2014-12-01T09:17:51.000+01:00",
  416. "name": "My Project",
  417. "name_with_namespace": "My Group / My Project",
  418. "path": "myproject",
  419. "path_with_namespace": "mygroup/myproject",
  420. "web_url": "https://gitlab.mycompany.local/mygroup/myproject"
  421. }
  422. JSON;
  423. $this->remoteFilesystem
  424. ->getContents(Argument::cetera(), $options)
  425. ->willReturn($projectData)
  426. ->shouldBeCalled();
  427. $driver = new GitLabDriver(
  428. array('url' => 'https://gitlab.mycompany.local/mygroup/myproject', 'options' => $options),
  429. $this->io->reveal(),
  430. $this->config,
  431. $this->process->reveal(),
  432. $this->remoteFilesystem->reveal()
  433. );
  434. $driver->initialize();
  435. }
  436. }