-
Notifications
You must be signed in to change notification settings - Fork 0
Spec generation task, #PG-4593 #26
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2fedcbd
Pulled out spec generation logic into common service, added new Task …
lachiebol ebb2fcd
Fixed plugin name logic and tests
lachiebol ae0260c
Fixed broken description logic from PR #23, added back in early return
lachiebol b845c1e
Add docs to public method
lachiebol 7d2acc8
Changed to daily and removed 'all' option
lachiebol e454213
Merge branch '5.x-dev' into PG-4593-spec-task
lachiebol 33a7c0c
fix tests
lachiebol adc9d93
Move blocklist validation down to generateSpec level
lachiebol 76279a4
removed config file, not needed
lachiebol b04752c
restored config.php to normal state
lachiebol 34e7db0
fix phpcs
lachiebol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Matomo - free/libre analytics platform | ||
| * | ||
| * @link https://matomo.org | ||
| * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Piwik\Plugins\OpenApiDocs\Generation; | ||
|
|
||
| use Piwik\Plugins\OpenApiDocs\Annotations\AnnotationGenerator; | ||
| use Piwik\Plugins\OpenApiDocs\OpenApiDocs; | ||
| use Piwik\Plugins\OpenApiDocs\Specs\SpecGenerator; | ||
|
|
||
| class SpecGenerationService | ||
| { | ||
| /** | ||
| * @var AnnotationGenerator | ||
| */ | ||
| private $annotationGenerator; | ||
|
|
||
| /** | ||
| * @var SpecGenerator | ||
| */ | ||
| private $specGenerator; | ||
|
|
||
| public function __construct(AnnotationGenerator $annotationGenerator, SpecGenerator $specGenerator) | ||
| { | ||
| $this->annotationGenerator = $annotationGenerator; | ||
| $this->specGenerator = $specGenerator; | ||
| } | ||
|
|
||
| /** | ||
| * Generate an OpenAPI spec for one or more comma-separated plugin names. | ||
| * | ||
| * @param string $pluginNames Comma-separated plugin names to include in the generated spec. | ||
| * @param string $format Output format for the spec, for example `json` or `yaml`. | ||
| * @param string $version Version string written into the generated OpenAPI spec. | ||
| * @param bool $writeToFile Whether the generated spec should also be written to the plugin tmp specs directory. | ||
| * @param bool $addAnnotations Whether API annotations should be regenerated before building the spec. | ||
| * @return string The generated OpenAPI spec contents. | ||
| * @throws \RuntimeException If no non-empty plugin names are provided. | ||
| */ | ||
| public function generateSpecForPlugins( | ||
| string $pluginNames, | ||
| string $format = 'json', | ||
| string $version = OpenApiDocs::DEFAULT_SPEC_VERSION, | ||
| bool $writeToFile = false, | ||
| bool $addAnnotations = false | ||
| ): string { | ||
| $parsedPluginNames = $this->getPluginNames($pluginNames); | ||
|
|
||
| if ($addAnnotations) { | ||
| $this->generateAnnotations($parsedPluginNames); | ||
| } | ||
|
|
||
| return $this->specGenerator->generateSpec($parsedPluginNames, $format, $version, $writeToFile); | ||
| } | ||
|
|
||
| /** | ||
| * @param string[] $pluginNames | ||
| */ | ||
| private function generateAnnotations(array $pluginNames): void | ||
| { | ||
| foreach ($pluginNames as $pluginName) { | ||
| $this->annotationGenerator->generatePluginApiAnnotations($pluginName, true); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @return string[] | ||
| */ | ||
| private function getPluginNames(string $pluginNames): array | ||
| { | ||
| $plugins = array_filter(array_map('trim', explode(',', $pluginNames)), static function (string $pluginName): bool { | ||
| return $pluginName !== ''; | ||
| }); | ||
|
|
||
| if (empty($plugins)) { | ||
| throw new \RuntimeException('At least one plugin name is required.'); | ||
| } | ||
|
|
||
| return array_values($plugins); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Matomo - free/libre analytics platform | ||
| * | ||
| * @link https://matomo.org | ||
| * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Piwik\Plugins\OpenApiDocs; | ||
|
|
||
| use Piwik\Config; | ||
| use Piwik\Log\LoggerInterface; | ||
| use Piwik\Plugins\OpenApiDocs\Generation\SpecGenerationService; | ||
|
|
||
| class Tasks extends \Piwik\Plugin\Tasks | ||
| { | ||
| /** | ||
| * @var SpecGenerationService | ||
| */ | ||
| private $specGenerationService; | ||
|
|
||
| /** | ||
| * @var LoggerInterface | ||
| */ | ||
| private $logger; | ||
|
|
||
| public function __construct(SpecGenerationService $specGenerationService, LoggerInterface $logger) | ||
| { | ||
| $this->specGenerationService = $specGenerationService; | ||
| $this->logger = $logger; | ||
| } | ||
|
|
||
| public function schedule() | ||
| { | ||
| if ($this->isSpecGenerationEnabled()) { | ||
| $this->daily('generateConfiguredPluginSpecs'); | ||
| } | ||
| } | ||
|
|
||
| public function generateConfiguredPluginSpecs(): void | ||
| { | ||
| $pluginNames = require __DIR__ . '/config/plugins.php'; | ||
|
|
||
| foreach ($pluginNames as $pluginName) { | ||
| try { | ||
| $this->specGenerationService->generateSpecForPlugins( | ||
| $pluginName, | ||
| 'json', | ||
| OpenApiDocs::DEFAULT_SPEC_VERSION, | ||
| true, | ||
| true | ||
| ); | ||
| } catch (\Throwable $e) { | ||
| $this->logger->error( | ||
| 'OpenApiDocs scheduled generation failed for plugin {plugin}: {error}', | ||
| [ | ||
| 'plugin' => $pluginName, | ||
| 'error' => $e->getMessage(), | ||
| ] | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private function isSpecGenerationEnabled(): bool | ||
| { | ||
| return (bool) (Config::getInstance()->OpenApiDocs['enable_spec_generation_task'] ?? 0); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,8 @@ | ||
| <?php | ||
|
|
||
| require dirname(__FILE__, 2) . '/vendor/autoload.php'; | ||
| return []; | ||
| return [ | ||
| 'OpenApiDocs' => [ | ||
|
lachiebol marked this conversation as resolved.
Outdated
|
||
| 'enable_spec_generation_task' => 0, | ||
| ], | ||
| ]; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Matomo - free/libre analytics platform | ||
| * | ||
| * @link https://matomo.org | ||
| * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Piwik\Plugins\OpenApiDocs\tests\Unit\Generation; | ||
|
|
||
| require_once PIWIK_INCLUDE_PATH . '/plugins/OpenApiDocs/vendor/autoload.php'; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use Piwik\Plugins\OpenApiDocs\Annotations\AnnotationGenerator; | ||
| use Piwik\Plugins\OpenApiDocs\Generation\SpecGenerationService; | ||
| use Piwik\Plugins\OpenApiDocs\Specs\SpecGenerator; | ||
|
|
||
| /** | ||
| * @group OpenApiDocs | ||
| * @group OpenApiDocs_Unit | ||
| * @group OpenApiDocs_SpecGenerationServiceTest | ||
| */ | ||
| class SpecGenerationServiceTest extends TestCase | ||
| { | ||
| public function testGenerateSpecForPluginsThrowsForEmptyPluginNames(): void | ||
| { | ||
| $service = new SpecGenerationService( | ||
| $this->createStub(AnnotationGenerator::class), | ||
| $this->createStub(SpecGenerator::class) | ||
| ); | ||
|
|
||
| $this->expectException(\RuntimeException::class); | ||
| $this->expectExceptionMessage('At least one plugin name is required.'); | ||
|
|
||
| $service->generateSpecForPlugins(' , ', 'json', '1.0.0', false, false); | ||
| } | ||
|
|
||
| public function testGenerateSpecForPluginsWorksForValidPluginName(): void | ||
| { | ||
| $annotationGenerator = $this->createMock(AnnotationGenerator::class); | ||
| $specGenerator = $this->getMockBuilder(SpecGenerator::class) | ||
| ->disableOriginalConstructor() | ||
| ->onlyMethods(['generateSpec']) | ||
| ->getMock(); | ||
|
|
||
| $annotationGenerator->expects($this->once()) | ||
| ->method('generatePluginApiAnnotations') | ||
| ->with('CustomAlerts', true); | ||
| $specGenerator->expects($this->once()) | ||
| ->method('generateSpec') | ||
| ->with(['CustomAlerts'], 'json', '1.0.0', true) | ||
| ->willReturn('spec body'); | ||
|
|
||
| $service = new SpecGenerationService($annotationGenerator, $specGenerator); | ||
|
|
||
| $service->generateSpecForPlugins('CustomAlerts', 'json', '1.0.0', true, true); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.