From f89c390b12045c64054bc11f5869bb4763b73db1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=BDborn=C3=BD=20Adam?= Date: Mon, 9 Jan 2023 14:59:03 +0100 Subject: [PATCH 1/4] Check if folder and files exist and return user exception if not --- src/Component.php | 4 ++++ src/Service/ArtifactsService.php | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/Component.php b/src/Component.php index 14fb65a9..519a1594 100644 --- a/src/Component.php +++ b/src/Component.php @@ -20,8 +20,10 @@ use DbtTransformation\Service\DbtYamlCreateService\DbtSourceYamlCreateService; use DbtTransformation\Service\GitRepositoryService; use Keboola\Component\BaseComponent; +use Keboola\Component\UserException; use Keboola\StorageApi\Client as StorageClient; use Psr\Log\LoggerInterface; +use Symfony\Component\Filesystem\Filesystem; class Component extends BaseComponent { @@ -186,6 +188,7 @@ protected function getSyncActions(): array /** * @return array + * @throws \Keboola\Component\UserException */ protected function actionDbtDocs(): array { @@ -194,6 +197,7 @@ protected function actionDbtDocs(): array $branchId = $this->getConfig()->getBranchId(); $this->artifacts->downloadLastRun($componentId, $configId, $branchId); + $this->artifacts->checkIfCorrectStepIsDownloaded(DbtService::COMMAND_DOCS_GENERATE); $html = $this->artifacts->readFromFile(DbtService::COMMAND_DOCS_GENERATE, 'index.html'); $manifest = $this->artifacts->readFromFile(DbtService::COMMAND_DOCS_GENERATE, 'manifest.json'); diff --git a/src/Service/ArtifactsService.php b/src/Service/ArtifactsService.php index faad9e9c..1ba3c654 100644 --- a/src/Service/ArtifactsService.php +++ b/src/Service/ArtifactsService.php @@ -48,6 +48,9 @@ public function writeResults(string $projectPath, string $step): void $this->filesystem->mirror(sprintf('%s/target/', $projectPath), $artifactsPath); } + /** + * @throws \Keboola\Component\UserException + */ public function downloadLastRun(string $componentId, string $configId, string $branchId): ?int { $query = sprintf( @@ -86,9 +89,15 @@ public function downloadLastRun(string $componentId, string $configId, string $b return $file['id']; } + /** + * @throws \Keboola\Component\UserException + */ public function readFromFile(string $step, string $filePath): string { $file = new SplFileInfo(sprintf('%s/%s/%s', $this->getDownloadDir(), $step, $filePath)); + if (!$file->isFile()) { + throw new UserException(sprintf('Missing "%s" file in downloaded artifact', $filePath)); + } return (string) file_get_contents($file->getPathname()); } @@ -111,4 +120,16 @@ private function mkdir(string $path): void $this->filesystem->mkdir($path); } } + + /** + * @throws \Keboola\Component\UserException + */ + public function checkIfCorrectStepIsDownloaded(string $step) + { + $docsPath = sprintf('%s/%s', $this->getDownloadDir(), $step); + if (!$this->filesystem->exists($docsPath)) { + throw new UserException('No artifact from previous dbt docs generate found. ' . + 'Run the component first with dbt docs generate command enabled.'); + } + } } From fbded3fdaf00357fc9ef03d5a0f0242fc3260577 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=BDborn=C3=BD=20Adam?= Date: Mon, 9 Jan 2023 14:59:40 +0100 Subject: [PATCH 2/4] Test for handling missing index.html in "dbtDocs" action --- .../run-sync-action-dbt-docs-missing-index/expected-code | 1 + .../run-sync-action-dbt-docs-missing-index/expected-stderr | 1 + .../expected/data/out/files/.gitkeep | 0 .../expected/data/out/tables/.gitkeep | 0 .../source/data/config.json | 7 +++++++ 5 files changed, 9 insertions(+) create mode 100644 tests/functionalSyncActions/run-sync-action-dbt-docs-missing-index/expected-code create mode 100644 tests/functionalSyncActions/run-sync-action-dbt-docs-missing-index/expected-stderr create mode 100644 tests/functionalSyncActions/run-sync-action-dbt-docs-missing-index/expected/data/out/files/.gitkeep create mode 100644 tests/functionalSyncActions/run-sync-action-dbt-docs-missing-index/expected/data/out/tables/.gitkeep create mode 100644 tests/functionalSyncActions/run-sync-action-dbt-docs-missing-index/source/data/config.json diff --git a/tests/functionalSyncActions/run-sync-action-dbt-docs-missing-index/expected-code b/tests/functionalSyncActions/run-sync-action-dbt-docs-missing-index/expected-code new file mode 100644 index 00000000..56a6051c --- /dev/null +++ b/tests/functionalSyncActions/run-sync-action-dbt-docs-missing-index/expected-code @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git a/tests/functionalSyncActions/run-sync-action-dbt-docs-missing-index/expected-stderr b/tests/functionalSyncActions/run-sync-action-dbt-docs-missing-index/expected-stderr new file mode 100644 index 00000000..12355437 --- /dev/null +++ b/tests/functionalSyncActions/run-sync-action-dbt-docs-missing-index/expected-stderr @@ -0,0 +1 @@ +Missing "index.html" file in downloaded artifact \ No newline at end of file diff --git a/tests/functionalSyncActions/run-sync-action-dbt-docs-missing-index/expected/data/out/files/.gitkeep b/tests/functionalSyncActions/run-sync-action-dbt-docs-missing-index/expected/data/out/files/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/tests/functionalSyncActions/run-sync-action-dbt-docs-missing-index/expected/data/out/tables/.gitkeep b/tests/functionalSyncActions/run-sync-action-dbt-docs-missing-index/expected/data/out/tables/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/tests/functionalSyncActions/run-sync-action-dbt-docs-missing-index/source/data/config.json b/tests/functionalSyncActions/run-sync-action-dbt-docs-missing-index/source/data/config.json new file mode 100644 index 00000000..caf22ec2 --- /dev/null +++ b/tests/functionalSyncActions/run-sync-action-dbt-docs-missing-index/source/data/config.json @@ -0,0 +1,7 @@ +{ + "componentId": "keboola.dbt-transformation", + "action": "dbtDocs", + "parameters": { + "configId": "12345" + } +} From a9ceb09fa5f45496a827a1217955a65b74ee6c90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=BDborn=C3=BD=20Adam?= Date: Mon, 9 Jan 2023 15:14:35 +0100 Subject: [PATCH 3/4] Add missing return type hint --- src/Service/ArtifactsService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Service/ArtifactsService.php b/src/Service/ArtifactsService.php index 1ba3c654..b2bb11a4 100644 --- a/src/Service/ArtifactsService.php +++ b/src/Service/ArtifactsService.php @@ -124,7 +124,7 @@ private function mkdir(string $path): void /** * @throws \Keboola\Component\UserException */ - public function checkIfCorrectStepIsDownloaded(string $step) + public function checkIfCorrectStepIsDownloaded(string $step): void { $docsPath = sprintf('%s/%s', $this->getDownloadDir(), $step); if (!$this->filesystem->exists($docsPath)) { From 80caac93993174f557a2b28edac5612ac009f32c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=BDborn=C3=BD=20Adam?= Date: Tue, 10 Jan 2023 10:23:29 +0100 Subject: [PATCH 4/4] Removed unused uses --- src/Component.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Component.php b/src/Component.php index 519a1594..c0cc9ea1 100644 --- a/src/Component.php +++ b/src/Component.php @@ -20,10 +20,8 @@ use DbtTransformation\Service\DbtYamlCreateService\DbtSourceYamlCreateService; use DbtTransformation\Service\GitRepositoryService; use Keboola\Component\BaseComponent; -use Keboola\Component\UserException; use Keboola\StorageApi\Client as StorageClient; use Psr\Log\LoggerInterface; -use Symfony\Component\Filesystem\Filesystem; class Component extends BaseComponent {