GitLabDriverTest.php 18 KB

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