Skip to content

Commit 0fe0249

Browse files
committed
Improved Github and Gitlab projects. Issue php-censor#163.
1 parent 347d05a commit 0fe0249

4 files changed

Lines changed: 227 additions & 50 deletions

File tree

src/Controller/ProjectController.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,22 @@ public function edit($projectId)
374374
$values['pubkey'] = $values['ssh_public_key'];
375375
$values['environments'] = $project->getEnvironments();
376376

377-
if (Project::TYPE_GITLAB === $values['type']) {
378-
$accessInfo = $project->getAccessInformation();
379-
$reference = $accessInfo["user"] . '@' . $accessInfo["domain"] . ':' . $accessInfo["port"] . '/' . ltrim($project->getReference(), '/') . ".git";
380-
$values['reference'] = $reference;
377+
if (in_array($values['type'], [
378+
Project::TYPE_GITHUB,
379+
Project::TYPE_GITLAB
380+
], true)) {
381+
$originReference = $project->getAccessInformation('origin');
382+
if ($originReference) {
383+
$values['reference'] = $originReference;
384+
} else {
385+
$accessInfo = $project->getAccessInformation();
386+
$reference = $accessInfo['user'] . '@' . $accessInfo['domain'] . ':' . ltrim($project->getReference(), '/') . '.git';
387+
if (isset($accessInfo['port']) && $accessInfo['port']) {
388+
$reference = $accessInfo['user'] . '@' . $accessInfo['domain'] . ':' . $accessInfo['port'] . '/' . ltrim($project->getReference(), '/') . '.git';
389+
}
390+
391+
$values['reference'] = $reference;
392+
}
381393
}
382394

383395
if ($method == 'POST') {
@@ -535,27 +547,28 @@ protected function projectForm($values, $type = 'add')
535547
protected function getReferenceValidator($values)
536548
{
537549
return function ($val) use ($values) {
538-
$type = $values['type'];
550+
$type = $values['type'];
551+
$gitRegex = '#^((https|http|ssh)://)?((.+)@)?(([^/:]+):?)(:?([0-9]*)/?)(.+)\.git#';
539552

540553
$validators = [
541554
Project::TYPE_HG => [
542555
'regex' => '/^(ssh|https?):\/\//',
543556
'message' => Lang::get('error_hg')
544557
],
545558
Project::TYPE_GIT => [
546-
'regex' => '/^(git|https?):\/\//',
559+
'regex' => $gitRegex,
547560
'message' => Lang::get('error_git')
548561
],
549562
Project::TYPE_GITLAB => [
550-
'regex' => '/^(git|https?):\/\//',
563+
'regex' => $gitRegex,
551564
'message' => Lang::get('error_gitlab')
552565
],
553566
Project::TYPE_GITHUB => [
554-
'regex' => '/^(git|https?):\/\//',
567+
'regex' => $gitRegex,
555568
'message' => Lang::get('error_github')
556569
],
557570
Project::TYPE_BITBUCKET => [
558-
'regex' => '/^[a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-\.]+$/',
571+
'regex' => $gitRegex,
559572
'message' => Lang::get('error_bitbucket')
560573
],
561574
Project::TYPE_BITBUCKET_HG => [

src/Model/Build/GitlabBuild.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ protected function getCloneUrl()
4949
$key = trim($this->getProject()->getSshPrivateKey());
5050

5151
if (!empty($key)) {
52-
$user = $this->getProject()->getAccessInformation("user");
53-
$domain = $this->getProject()->getAccessInformation("domain");
52+
$user = $this->getProject()->getAccessInformation('user');
53+
$domain = $this->getProject()->getAccessInformation('domain');
5454
$port = $this->getProject()->getAccessInformation('port');
5555

5656
$url = $user . '@' . $domain . ':';

src/Service/ProjectService.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ public function deleteProject(Project $project)
143143
*/
144144
protected function processAccessInformation(Project $project)
145145
{
146-
$matches = [];
147146
$reference = $project->getReference();
148147

149148
if (in_array($project->getType(), [
@@ -152,12 +151,30 @@ protected function processAccessInformation(Project $project)
152151
], true)) {
153152
$info = [];
154153

155-
if (preg_match('`^(.+)@(.+):([0-9]*)\/?(.+)\.git`', $reference, $matches)) {
156-
$info['user'] = $matches[1];
157-
$info['domain'] = $matches[2];
158-
$info['port'] = $matches[3];
154+
if (preg_match(
155+
'#^((https|http|ssh)://)?((.+)@)?(([^/:]+):?)(:?([0-9]*)/?)(.+)\.git#',
156+
$reference,
157+
$matches
158+
)) {
159+
if (isset($matches[4]) && $matches[4]) {
160+
$info['user'] = $matches[4];
161+
}
159162

160-
$project->setReference($matches[4]);
163+
if (isset($matches[6]) && $matches[6]) {
164+
$info['domain'] = $matches[6];
165+
}
166+
167+
if (isset($matches[8]) && $matches[8]) {
168+
$info['port'] = $matches[8];
169+
}
170+
171+
if (isset($matches[9]) && $matches[9]) {
172+
$info['reference'] = $matches[9];
173+
174+
$project->setReference($matches[9]);
175+
}
176+
177+
$info['origin'] = $reference;
161178
}
162179

163180
$project->setAccessInformation($info);

tests/src/Service/ProjectServiceTest.php

Lines changed: 180 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,184 @@ class ProjectServiceTest extends \PHPUnit\Framework\TestCase
2525

2626
public function setUp()
2727
{
28-
$this->mockProjectStore = $this->getMockBuilder('PHPCensor\Store\ProjectStore')->getMock();
29-
$this->mockProjectStore->expects($this->any())
30-
->method('save')
31-
->will($this->returnArgument(0));
28+
$this->mockProjectStore = $this
29+
->getMockBuilder('PHPCensor\Store\ProjectStore')
30+
->getMock();
31+
32+
$this->mockProjectStore
33+
->expects($this->any())
34+
->method('save')
35+
->will(
36+
$this->returnArgument(0)
37+
);
3238

3339
$this->testedService = new ProjectService($this->mockProjectStore);
3440
}
3541

36-
public function testExecute_CreateBasicProject()
42+
public function testExecuteCreateGithubProject()
43+
{
44+
$project = $this->testedService->createProject(
45+
'Test Project',
46+
'github',
47+
'php-censor/php-censor1',
48+
0
49+
);
50+
51+
self::assertEquals('Test Project', $project->getTitle());
52+
self::assertEquals('github', $project->getType());
53+
self::assertEquals('php-censor/php-censor1', $project->getReference());
54+
self::assertEquals('master', $project->getBranch());
55+
self::assertEquals([], $project->getAccessInformation());
56+
}
57+
58+
/**
59+
* @return array
60+
*/
61+
public function getExecuteCreateGithubProjectAccessInformationData()
62+
{
63+
return [
64+
[
65+
'git@github.com:php-censor/php-censor.git', [
66+
'user' => 'git',
67+
'domain' => 'github.com',
68+
'reference' => 'php-censor/php-censor',
69+
'origin' => 'git@github.com:php-censor/php-censor.git',
70+
],
71+
], [
72+
'git@sss.github.com:php-censor/php-censor.git', [
73+
'user' => 'git',
74+
'domain' => 'sss.github.com',
75+
'reference' => 'php-censor/php-censor',
76+
'origin' => 'git@sss.github.com:php-censor/php-censor.git',
77+
],
78+
], [
79+
'ssh://git@github.com/php-censor/php-censor.git', [
80+
'user' => 'git',
81+
'domain' => 'github.com',
82+
'reference' => 'php-censor/php-censor',
83+
'origin' => 'ssh://git@github.com/php-censor/php-censor.git',
84+
],
85+
], [
86+
'https://github.com/php-censor/php-censor.git', [
87+
'domain' => 'github.com',
88+
'reference' => 'php-censor/php-censor',
89+
'origin' => 'https://github.com/php-censor/php-censor.git',
90+
],
91+
], [
92+
'http://github.com/php-censor/php-censor.git', [
93+
'domain' => 'github.com',
94+
'reference' => 'php-censor/php-censor',
95+
'origin' => 'http://github.com/php-censor/php-censor.git',
96+
],
97+
], [
98+
'git@github.com:443/php-censor/php-censor.git', [
99+
'user' => 'git',
100+
'domain' => 'github.com',
101+
'port' => '443',
102+
'reference' => 'php-censor/php-censor',
103+
'origin' => 'git@github.com:443/php-censor/php-censor.git',
104+
],
105+
], [
106+
'ssh://git@github.com:443/php-censor/php-censor.git', [
107+
'user' => 'git',
108+
'domain' => 'github.com',
109+
'port' => '443',
110+
'reference' => 'php-censor/php-censor',
111+
'origin' => 'ssh://git@github.com:443/php-censor/php-censor.git',
112+
],
113+
], [
114+
'https://github.com:443/php-censor/php-censor.git', [
115+
'domain' => 'github.com',
116+
'port' => '443',
117+
'reference' => 'php-censor/php-censor',
118+
'origin' => 'https://github.com:443/php-censor/php-censor.git',
119+
],
120+
], [
121+
'http://github.com:443/php-censor/php-censor.git', [
122+
'domain' => 'github.com',
123+
'port' => '443',
124+
'reference' => 'php-censor/php-censor',
125+
'origin' => 'http://github.com:443/php-censor/php-censor.git',
126+
],
127+
], [
128+
'git@github:php-censor.git', [
129+
'user' => 'git',
130+
'domain' => 'github',
131+
'reference' => 'php-censor',
132+
'origin' => 'git@github:php-censor.git',
133+
],
134+
], [
135+
'ssh://git@github/php-censor.git', [
136+
'user' => 'git',
137+
'domain' => 'github',
138+
'reference' => 'php-censor',
139+
'origin' => 'ssh://git@github/php-censor.git',
140+
],
141+
], [
142+
'https://github/php-censor.git', [
143+
'domain' => 'github',
144+
'reference' => 'php-censor',
145+
'origin' => 'https://github/php-censor.git',
146+
],
147+
], [
148+
'http://github/php-censor.git', [
149+
'domain' => 'github',
150+
'reference' => 'php-censor',
151+
'origin' => 'http://github/php-censor.git',
152+
],
153+
], [
154+
'git@github:443/php-censor.git', [
155+
'user' => 'git',
156+
'domain' => 'github',
157+
'port' => '443',
158+
'reference' => 'php-censor',
159+
'origin' => 'git@github:443/php-censor.git',
160+
],
161+
], [
162+
'ssh://git@github:443/php-censor.git', [
163+
'user' => 'git',
164+
'domain' => 'github',
165+
'port' => '443',
166+
'reference' => 'php-censor',
167+
'origin' => 'ssh://git@github:443/php-censor.git',
168+
],
169+
], [
170+
'https://github:443/php-censor.git', [
171+
'domain' => 'github',
172+
'port' => '443',
173+
'reference' => 'php-censor',
174+
'origin' => 'https://github:443/php-censor.git',
175+
],
176+
], [
177+
'http://github:443/php-censor.git', [
178+
'domain' => 'github',
179+
'port' => '443',
180+
'reference' => 'php-censor',
181+
'origin' => 'http://github:443/php-censor.git',
182+
],
183+
],
184+
];
185+
}
186+
187+
/**
188+
* @param string $reference
189+
* @param array $accessInformation
190+
*
191+
* @dataProvider getExecuteCreateGithubProjectAccessInformationData
192+
*/
193+
public function testExecuteCreateGithubProjectAccessInformation($reference, array $accessInformation)
37194
{
38-
$returnValue = $this->testedService->createProject('Test Project', 'github', 'block8/phpci', 0);
195+
$project = $this->testedService->createProject(
196+
'Test Project',
197+
'github',
198+
$reference,
199+
0
200+
);
39201

40-
self::assertEquals('Test Project', $returnValue->getTitle());
41-
self::assertEquals('github', $returnValue->getType());
42-
self::assertEquals('block8/phpci', $returnValue->getReference());
43-
self::assertEquals('master', $returnValue->getBranch());
202+
self::assertEquals($accessInformation, $project->getAccessInformation());
44203
}
45204

46-
public function testExecute_CreateProjectWithOptions()
205+
public function testExecuteCreateProjectWithOptions()
47206
{
48207
$options = [
49208
'ssh_private_key' => 'private',
@@ -53,7 +212,13 @@ public function testExecute_CreateProjectWithOptions()
53212
'branch' => 'testbranch',
54213
];
55214

56-
$returnValue = $this->testedService->createProject('Test Project', 'github', 'block8/phpci', 0, $options);
215+
$returnValue = $this->testedService->createProject(
216+
'Test Project',
217+
'github',
218+
'block8/phpci',
219+
0,
220+
$options
221+
);
57222

58223
self::assertEquals('private', $returnValue->getSshPrivateKey());
59224
self::assertEquals('public', $returnValue->getSshPublicKey());
@@ -62,25 +227,7 @@ public function testExecute_CreateProjectWithOptions()
62227
self::assertEquals(true, $returnValue->getAllowPublicStatus());
63228
}
64229

65-
/**
66-
* @link https://github.com/Block8/PHPCI/issues/484
67-
*/
68-
public function testExecute_CreateGitlabProjectWithoutPort()
69-
{
70-
$reference = 'git@gitlab.block8.net:block8/phpci.git';
71-
$returnValue = $this->testedService->createProject(
72-
'Gitlab',
73-
Project::TYPE_GITLAB,
74-
$reference,
75-
0
76-
);
77-
78-
self::assertEquals('git', $returnValue->getAccessInformation('user'));
79-
self::assertEquals('gitlab.block8.net', $returnValue->getAccessInformation('domain'));
80-
self::assertEquals('block8/phpci', $returnValue->getReference());
81-
}
82-
83-
public function testExecute_UpdateExistingProject()
230+
public function testExecuteUpdateExistingProject()
84231
{
85232
$project = new Project();
86233
$project->setTitle('Before Title');
@@ -94,7 +241,7 @@ public function testExecute_UpdateExistingProject()
94241
self::assertEquals('bitbucket', $returnValue->getType());
95242
}
96243

97-
public function testExecute_EmptyPublicStatus()
244+
public function testExecuteEmptyPublicStatus()
98245
{
99246
$project = new Project();
100247
$project->setAllowPublicStatus(true);
@@ -110,7 +257,7 @@ public function testExecute_EmptyPublicStatus()
110257
self::assertEquals(false, $returnValue->getAllowPublicStatus());
111258
}
112259

113-
public function testExecute_DeleteProject()
260+
public function testExecuteDeleteProject()
114261
{
115262
$store = $this->getMockBuilder('PHPCensor\Store\ProjectStore')->getMock();
116263
$store->expects($this->once())

0 commit comments

Comments
 (0)