Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/prefer ssh url #141

Merged
merged 3 commits into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/parameters.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ parameters:
gitlab.secret: ~
gitlab.auto_add_repo: false
gitlab.auto_add_repo_type: ~
gitlab.prefer_ssh_url_type: false
1 change: 1 addition & 0 deletions src/Playbloom/Satisfy/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ services:
$secret: "%gitlab.secret%"
$autoAdd: "%gitlab.auto_add_repo%"
$autoAddType: "%gitlab.auto_add_repo_type%"
$preferSshUrlType: "%gitlab.prefer_ssh_url_type%"

Playbloom\Satisfy\Validator\EnvValidator:
public: true
Expand Down
11 changes: 9 additions & 2 deletions src/Playbloom/Satisfy/Webhook/GitlabWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,24 @@ class GitlabWebhook extends AbstractWebhook
/** @var string */
protected $autoAddType = '';

/** @var array */
protected $urlCheckOrder = [self::BODY_HTTP_URL_KEY, self::BODY_SSH_URL_KEY];

public function __construct(
Manager $manager,
EventDispatcherInterface $dispatcher,
?string $secret = null,
?bool $autoAdd = false,
?string $autoAddType = 'gitlab'
?string $autoAddType = 'git',
?bool $preferSshUrlType = false
) {
parent::__construct($manager, $dispatcher);
$this->secret = $secret;
$this->autoAdd = $autoAdd;
$this->autoAddType = $autoAddType;
if ($preferSshUrlType) {
$this->urlCheckOrder = [self::BODY_SSH_URL_KEY, self::BODY_HTTP_URL_KEY];
}
}

public function setSecret(string $secret = null): self
Expand All @@ -55,7 +62,7 @@ protected function getRepository(Request $request): RepositoryInterface

$urls = [];
$originalUrls = [];
foreach ([self::BODY_HTTP_URL_KEY, self::BODY_SSH_URL_KEY] as $key) {
foreach ($this->urlCheckOrder as $key) {
if (empty($repositoryData[$key])) {
continue;
}
Expand Down
108 changes: 108 additions & 0 deletions tests/Playbloom/Satisfy/Webhook/GitlabWebhookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,113 @@ public function testAutoAddOnlyHttp(): void
$response = $handler->getResponse($request);
}

public function testAutoAddPreferSsh(): void
{
$httpUrl = 'https://gitlab.com/example/nonexistant.git';
$sshUrl = '[email protected]:example/nonexistant.git';
$request = $this->createRequest([
'project' => [
'git_http_url' => $httpUrl,
'git_ssh_url' => $sshUrl
],
]);

$repository = new Repository($sshUrl, 'git');

$manager = $this->getManagerMock();
$manager
->findByUrl(Argument::exact('#^git@gitlab\.com\:example/nonexistant\.git$#'))
->shouldBeCalledTimes(1);
$manager
->findByUrl(Argument::exact('#^https\://gitlab\.com/example/nonexistant\.git$#'))
->shouldBeCalledTimes(1);
$manager
->add(Argument::exact($repository))
->shouldBeCalledTimes(1);
$dispatcher = $this->getDispatcherMock();

$handler = new GitlabWebhook($manager->reveal(), $dispatcher->reveal(), null, true, 'git', true);
$response = $handler->getResponse($request);
}

public function testAutoAddPreferSshFallback(): void
{
$httpUrl = 'https://gitlab.com/example/nonexistant.git';
$request = $this->createRequest([
'project' => [
'git_http_url' => $httpUrl
],
]);

$repository = new Repository($httpUrl, 'git');

$manager = $this->getManagerMock();
$manager
->findByUrl(Argument::exact('#^https\://gitlab\.com/example/nonexistant\.git$#'))
->shouldBeCalledTimes(1);
$manager
->add(Argument::exact($repository))
->shouldBeCalledTimes(1);
$dispatcher = $this->getDispatcherMock();

$handler = new GitlabWebhook($manager->reveal(), $dispatcher->reveal(), null, true, 'git', true);
$response = $handler->getResponse($request);
}

public function testAutoAddPreferHttps(): void
{
$httpUrl = 'https://gitlab.com/example/nonexistant.git';
$sshUrl = '[email protected]:example/nonexistant.git';
$request = $this->createRequest([
'project' => [
'git_http_url' => $httpUrl,
'git_ssh_url' => $sshUrl
],
]);

$repository = new Repository($httpUrl, 'git');

$manager = $this->getManagerMock();
$manager
->findByUrl(Argument::exact('#^git@gitlab\.com\:example/nonexistant\.git$#'))
->shouldBeCalledTimes(1);
$manager
->findByUrl(Argument::exact('#^https\://gitlab\.com/example/nonexistant\.git$#'))
->shouldBeCalledTimes(1);
$manager
->add(Argument::exact($repository))
->shouldBeCalledTimes(1);
$dispatcher = $this->getDispatcherMock();

$handler = new GitlabWebhook($manager->reveal(), $dispatcher->reveal(), null, true, 'git', false);
$response = $handler->getResponse($request);
}

public function testAutoAddPreferHttpsFallback(): void
{
$sshUrl = '[email protected]:example/nonexistant.git';
$request = $this->createRequest([
'project' => [
'git_ssh_url' => $sshUrl
],
]);

$repository = new Repository($sshUrl, 'git');

$manager = $this->getManagerMock();
$manager
->findByUrl(Argument::exact('#^git@gitlab\.com\:example/nonexistant\.git$#'))
->shouldBeCalledTimes(1);
$manager
->add(Argument::exact($repository))
->shouldBeCalledTimes(1);

$dispatcher = $this->getDispatcherMock();

$handler = new GitlabWebhook($manager->reveal(), $dispatcher->reveal(), null, true, 'git', false);
$response = $handler->getResponse($request);
}

protected function createRequest($content, string $event = 'push', string $token = null): Request
{
if (!is_string($content)) {
Expand All @@ -183,4 +290,5 @@ protected function getDispatcherMock(): ObjectProphecy
{
return $this->prophesize(EventDispatcher::class);
}

}