From e06576dbb0036efba8d2b78ce6741dee4e80c469 Mon Sep 17 00:00:00 2001 From: magdev Date: Tue, 8 Feb 2022 23:27:59 +0100 Subject: [PATCH] Initial commit --- .gitignore | 6 +++ LICENSE | 21 ++++++++++ composer.json | 35 ++++++++++++++++ phpunit.xml.dist | 34 +++++++++++++++ src/DependencyInjection/Configuration.php | 42 +++++++++++++++++++ .../MagdevGiteaExtension.php | 35 ++++++++++++++++ src/MagdevGiteaBundle.php | 14 +++++++ .../DependencyInjection/ConfigurationTest.php | 41 ++++++++++++++++++ .../MagdevGiteaExtensionTest.php | 40 ++++++++++++++++++ 9 files changed, 268 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 composer.json create mode 100644 phpunit.xml.dist create mode 100644 src/DependencyInjection/Configuration.php create mode 100644 src/DependencyInjection/MagdevGiteaExtension.php create mode 100644 src/MagdevGiteaBundle.php create mode 100644 tests/DependencyInjection/ConfigurationTest.php create mode 100644 tests/DependencyInjection/MagdevGiteaExtensionTest.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..33a071b --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +/.phpunit.result.cache +/.phpunit.cache +/composer.lock +/phpunit.xml +/vendor/ +*~ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..df27e5f --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT license + +Copyright (c) 2021-2022 Marco Grätsch + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..23432c9 --- /dev/null +++ b/composer.json @@ -0,0 +1,35 @@ +{ + "name": "magdev/gitea-bundle", + "description": "Symfony bundle to integrate Gitea", + "type": "symfony-bundle", + "require": { + "owenvoke/gitea": "^0.1.6", + "guzzlehttp/guzzle": "^7.4", + "http-interop/http-factory-guzzle": "^1.2" + }, + "require-dev": { + "phpunit/phpunit": "^9.5", + "symfony/http-kernel": "^5.4", + "symfony/dependency-injection": "^5.4", + "symfony/config": "^5.4", + "symfony/phpunit-bridge": "^6.0" + }, + "license": "MIT", + "autoload": { + "psr-4": { + "Magdev\\GiteaBundle\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Magdev\\GiteaBundle\\Tests\\": "tests/" + } + }, + "authors": [ + { + "name": "magdev", + "email": "magdev3.0@gmail.com" + } + ], + "minimum-stability": "stable" +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..340d6c3 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + tests + + + + + + src + + + + + + + diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php new file mode 100644 index 0000000..a1f7264 --- /dev/null +++ b/src/DependencyInjection/Configuration.php @@ -0,0 +1,42 @@ + + */ +final class Configuration implements ConfigurationInterface +{ + public function getConfigTreeBuilder(): TreeBuilder + { + $treeBuilder = new TreeBuilder('magdev_gitea'); + $rootNode = $treeBuilder->getRootNode(); + $rootNode->fixXmlConfig('connection') + ->children() + ->arrayNode('connections') + ->useAttributeAsKey('name') + ->normalizeKeys(false) + ->arrayPrototype() + ->children() + ->scalarNode('baseurl') + ->isRequired() + ->info('The base URL of the gitea instance') + ->end() + ->scalarNode('token') + ->isRequired() + ->info('The Gitea access token') + ->end() + ->end() + ->end() + ->end() + ->scalarNode('default_connection')->end() + ; + + return $treeBuilder; + } +} diff --git a/src/DependencyInjection/MagdevGiteaExtension.php b/src/DependencyInjection/MagdevGiteaExtension.php new file mode 100644 index 0000000..861ab99 --- /dev/null +++ b/src/DependencyInjection/MagdevGiteaExtension.php @@ -0,0 +1,35 @@ + + */ +final class MagdevGiteaExtension extends Extension +{ + public function load(array $configs, ContainerBuilder $container) + { + $config = $this->processConfiguration(new Configuration(), $configs); + + if (!isset($config['default_connection'])) { + $config['default_connection'] = array_key_first($config['connections']); + } + + foreach ($config['connections'] as $name => $connection) { + $id = sprintf('magdev.gitea.%s', $name); + $container->register($id, Client::class) + ->setArguments([null, null, $connection['baseurl']]) + ->addMethodCall('authenticate', [$connection['token'], null, Client::AUTH_ACCESS_TOKEN]); + $container->registerAliasForArgument($id, Client::class, "{$name}Client"); + + if ($name === $config['default_connection']) { + $container->setAlias(Client::class, $id); + } + } + } +} diff --git a/src/MagdevGiteaBundle.php b/src/MagdevGiteaBundle.php new file mode 100644 index 0000000..5614439 --- /dev/null +++ b/src/MagdevGiteaBundle.php @@ -0,0 +1,14 @@ + + */ +final class MagdevGiteaBundle extends Bundle +{ +} diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php new file mode 100644 index 0000000..2906cac --- /dev/null +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -0,0 +1,41 @@ + + */ +class ConfigurationTest extends TestCase +{ + /** + * @dataProvider configsProvider + */ + public function testConfig(array $configs): void + { + $config = (new Processor())->processConfiguration(new Configuration(), $configs); + $this->assertSame('foo', $config['connections']['default']['token']); + $this->assertSame('bar', $config['connections']['default']['baseurl']); + } + + public function configsProvider(): iterable + { + yield [ + [ + 'magdev_gitea' => [ + 'connections' => [ + 'default' => [ + 'token' => 'foo', + 'baseurl' => 'bar' + ], + ], + ], + ], + ]; + } +} diff --git a/tests/DependencyInjection/MagdevGiteaExtensionTest.php b/tests/DependencyInjection/MagdevGiteaExtensionTest.php new file mode 100644 index 0000000..95ebb12 --- /dev/null +++ b/tests/DependencyInjection/MagdevGiteaExtensionTest.php @@ -0,0 +1,40 @@ + + */ +class MagdevGiteaExtensionTest extends TestCase +{ + public function testExtension(): void + { + $container = new ContainerBuilder(); + + (new MagdevGiteaExtension())->load([ + 'magdev_gitea' => [ + 'connections' => [ + 'primary' => ['baseurl' => 'http://foo1', 'token' => 'bar1'], + 'secondary' => ['baseurl' => 'http://foo2', 'token' => 'bar2'], + ], + ], + ], $container); + + $this->assertTrue($container->has('magdev.gitea.primary')); + $this->assertTrue($container->has('magdev.gitea.secondary')); + + $this->assertTrue($container->hasAlias(Client::class)); + $this->assertTrue($container->hasAlias(Client::class.' $primaryClient')); + $this->assertTrue($container->hasAlias(Client::class.' $secondaryClient')); + + $this->assertInstanceOf(Client::class, $container->get('magdev.gitea.primary')); + $this->assertInstanceOf(Client::class, $container->get('magdev.gitea.secondary')); + } +}