From 36e2a26cd9c4184f1d9a5d048ba356e2ee8d4333 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Thu, 14 Jan 2021 22:24:01 +0100 Subject: [PATCH 1/2] cleanup and replace deprecated logger Signed-off-by: Arthur Schiwon --- lib/BackgroundJobs/Launcher.php | 25 +++-- lib/Operation.php | 176 +++++++++++++++++--------------- 2 files changed, 106 insertions(+), 95 deletions(-) diff --git a/lib/BackgroundJobs/Launcher.php b/lib/BackgroundJobs/Launcher.php index 496a4e5..ff531a5 100644 --- a/lib/BackgroundJobs/Launcher.php +++ b/lib/BackgroundJobs/Launcher.php @@ -23,14 +23,16 @@ namespace OCA\WorkflowScript\BackgroundJobs; +use Exception; +use OC\BackgroundJob\QueuedJob; +use OC\Files\View; use OCP\Files\IRootFolder; -use OCP\Files\NotFoundException; -use OCP\ILogger; use OCP\ITempManager; +use Psr\Log\LoggerInterface; -class Launcher extends \OC\BackgroundJob\QueuedJob { +class Launcher extends QueuedJob { - /** @var ILogger */ + /** @var LoggerInterface */ protected $logger; /** @var ITempManager */ private $tempManager; @@ -40,9 +42,9 @@ class Launcher extends \OC\BackgroundJob\QueuedJob { /** * BackgroundJob constructor. * - * @param ILogger $logger + * @param LoggerInterface $logger */ - public function __construct(ILogger $logger, ITempManager $tempManager, IRootFolder $rootFolder) { + public function __construct(LoggerInterface $logger, ITempManager $tempManager, IRootFolder $rootFolder) { $this->logger = $logger; $this->tempManager = $tempManager; $this->rootFolder = $rootFolder; @@ -54,13 +56,16 @@ public function __construct(ILogger $logger, ITempManager $tempManager, IRootFol protected function run($argument) { $command = (string)$argument['command']; - if(strpos($command, '%f')) { + if (strpos($command, '%f')) { $path = isset($argument['path']) ? (string)$argument['path'] : ''; try { - $view = new \OC\Files\View(dirname($path)); + $view = new View(dirname($path)); $tmpFile = $view->toTmpFile(basename($path)); - } catch (\Exception $e) { - $this->logger->logException($e, ['level' => ILogger::WARN, 'app' => 'workflow_script']); + } catch (Exception $e) { + $this->logger->warning($e->getMessage(), [ + 'app' => 'workflow_script', + 'exception' => $e + ]); return; } $command = str_replace('%f', escapeshellarg($tmpFile), $command); diff --git a/lib/Operation.php b/lib/Operation.php index aa4dafa..649d21f 100644 --- a/lib/Operation.php +++ b/lib/Operation.php @@ -23,6 +23,9 @@ namespace OCA\WorkflowScript; +use Exception; +use InvalidArgumentException; +use OC; use OC\Files\Filesystem; use OC\Files\View; use OCA\WorkflowEngine\Entity\File; @@ -44,6 +47,7 @@ use OCP\WorkflowEngine\IRuleMatcher; use OCP\WorkflowEngine\ISpecificOperation; use Symfony\Component\EventDispatcher\GenericEvent as LegacyGenericEvent; +use UnexpectedValueException; class Operation implements ISpecificOperation { @@ -69,99 +73,22 @@ public function __construct(IManager $workflowEngineManager, IJobList $jobList, $this->config = $config; } - protected function buildCommand(string $template, Node $node, string $event, array $extra = []) { - $command = $template; - - if (strpos($command, '%e')) { - $command = str_replace('%e', escapeshellarg($event), $command); - } - - if (strpos($command, '%n')) { - // Nextcloud relative-path - $nodeID = -1; - try { - $nodeID = $node->getId(); - } catch (InvalidPathException $e) { - } catch (NotFoundException $e) { - } - - $base_path = $this->config->getSystemValue('datadirectory'); - - $path = Filesystem::getLocalFile(Filesystem::getPath($nodeID)); - $command = str_replace('%n', escapeshellarg(str_replace($base_path . '/', '', $path)), $command); - } - - if (strpos($command, '%f')) { - try { - $view = new View(); - if ($node instanceof Folder) { - $fullPath = $view->getLocalFolder($node->getPath()); - } else { - $fullPath = $view->getLocalFile($node->getPath()); - } - if ($fullPath === null) { - throw new \InvalidArgumentException(); - } - $command = str_replace('%f', escapeshellarg($fullPath), $command); - } catch (\Exception $e) { - throw new \InvalidArgumentException('Could not determine full path'); - } - } - - if (strpos($command, '%i')) { - $nodeID = -1; - try { - $nodeID = $node->getId(); - } catch (InvalidPathException $e) { - } catch (NotFoundException $e) { - } - $command = str_replace('%i', escapeshellarg($nodeID), $command); - } - - if (strpos($command, '%a')) { - $user = $this->session->getUser(); - $userID = ''; - if ($user instanceof IUser) { - $userID = $user->getUID(); - } - $command = str_replace('%a', escapeshellarg($userID), $command); - } - - if (strpos($command, '%o')) { - $user = $node->getOwner(); - $userID = ''; - if ($user instanceof IUser) { - $userID = $user->getUID(); - } - $command = str_replace('%o', escapeshellarg($userID), $command); - } - - if (strpos($command, '%x')) { - if (!isset($extra['oldFilePath'])) { - $extra['oldFilePath'] = ''; - } - $command = str_replace('%x', escapeshellarg($extra['oldFilePath']), $command); - } - - return $command; - } - /** - * @throws \UnexpectedValueException + * @throws UnexpectedValueException * @since 9.1 */ public function validateOperation(string $name, array $checks, string $operation): void { if (empty($operation)) { - throw new \UnexpectedValueException($this->l->t('Please provide a script name')); + throw new UnexpectedValueException($this->l->t('Please provide a script name')); } $scriptName = explode(' ', $operation, 2)[0]; if (!$this->isScriptValid($scriptName)) { - throw new \UnexpectedValueException($this->l->t('The script does not seem to be executable')); + throw new UnexpectedValueException($this->l->t('The script does not seem to be executable')); } } - protected function isScriptValid(string $scriptName) { + protected function isScriptValid(string $scriptName): bool { $which = shell_exec('command -v ' . escapeshellarg($scriptName)); if (!empty($which)) { return true; @@ -179,7 +106,7 @@ public function getDescription(): string { } public function getIcon(): string { - return \OC::$server->getURLGenerator()->imagePath('workflow_script', 'app.svg'); + return OC::$server->getURLGenerator()->imagePath('workflow_script', 'app.svg'); } public function isAvailableForScope(int $scope): bool { @@ -196,9 +123,9 @@ public function onEvent(string $eventName, Event $event, IRuleMatcher $ruleMatch $extra = []; if ($eventName === '\OCP\Files::postRename') { /** @var Node $oldNode */ - list($oldNode, $node) = $event->getSubject(); + [$oldNode, $node] = $event->getSubject(); $extra = ['oldFilePath' => $oldNode->getPath()]; - } else if ($event instanceof MapperEvent) { + } elseif ($event instanceof MapperEvent) { if ($event->getObjectType() !== 'files') { return; } @@ -214,7 +141,7 @@ public function onEvent(string $eventName, Event $event, IRuleMatcher $ruleMatch /** @var Node $node */ // '', admin, 'files', 'path/to/file.txt' - list(, , $folder,) = explode('/', $node->getPath(), 4); + [, , $folder,] = explode('/', $node->getPath(), 4); if ($folder !== 'files' || $node instanceof Folder) { return; } @@ -232,6 +159,85 @@ public function onEvent(string $eventName, Event $event, IRuleMatcher $ruleMatch } } + protected function buildCommand(string $template, Node $node, string $event, array $extra = []) { + $command = $template; + + if (strpos($command, '%e')) { + $command = str_replace('%e', escapeshellarg($event), $command); + } + + if (strpos($command, '%n')) { + // Nextcloud relative-path + $nodeID = -1; + try { + $nodeID = $node->getId(); + } catch (InvalidPathException | NotFoundException $e) { + throw new InvalidArgumentException('', 0, $e); + } + + $base_path = $this->config->getSystemValue('datadirectory'); + try { + $path = Filesystem::getLocalFile(Filesystem::getPath($nodeID)); + } catch (NotFoundException $e) { + throw new InvalidArgumentException('', 0, $e); + } + $command = str_replace('%n', escapeshellarg(str_replace($base_path . '/', '', $path)), $command); + } + + if (strpos($command, '%f')) { + try { + $view = new View(); + if ($node instanceof Folder) { + $fullPath = $view->getLocalFolder($node->getPath()); + } else { + $fullPath = $view->getLocalFile($node->getPath()); + } + if ($fullPath === null) { + throw new InvalidArgumentException(); + } + $command = str_replace('%f', escapeshellarg($fullPath), $command); + } catch (Exception $e) { + throw new InvalidArgumentException('Could not determine full path'); + } + } + + if (strpos($command, '%i')) { + $nodeID = -1; + try { + $nodeID = $node->getId(); + } catch (InvalidPathException | NotFoundException $e) { + } + $command = str_replace('%i', escapeshellarg($nodeID), $command); + } + + if (strpos($command, '%a')) { + $user = $this->session->getUser(); + $userID = ''; + if ($user instanceof IUser) { + $userID = $user->getUID(); + } + $command = str_replace('%a', escapeshellarg($userID), $command); + } + + if (strpos($command, '%o')) { + $user = $node->getOwner(); + $userID = ''; + if ($user instanceof IUser) { + $userID = $user->getUID(); + } + $command = str_replace('%o', escapeshellarg($userID), $command); + } + + if (strpos($command, '%x')) { + if (!isset($extra['oldFilePath'])) { + $extra['oldFilePath'] = ''; + } + $command = str_replace('%x', escapeshellarg($extra['oldFilePath']), $command); + } + + return $command; + } + public function getEntityId(): string { return File::class; } From 01dba070e0bbfdd70530f5de19f15206c28a05d4 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Thu, 14 Jan 2021 22:28:17 +0100 Subject: [PATCH 2/2] update CI Signed-off-by: Arthur Schiwon --- .drone.yml | 27 ---------------- .github/workflows/lint.yml | 66 ++++++++++++++++++++++++++++++++++++++ .nextcloudignore | 1 + .php_cs.dist | 18 +++++++++++ composer.json | 18 +++++++++-- 5 files changed, 100 insertions(+), 30 deletions(-) delete mode 100644 .drone.yml create mode 100644 .github/workflows/lint.yml create mode 100644 .php_cs.dist diff --git a/.drone.yml b/.drone.yml deleted file mode 100644 index 78dab25..0000000 --- a/.drone.yml +++ /dev/null @@ -1,27 +0,0 @@ -kind: pipeline -name: syntax-php7.3 - -steps: - - name: syntax-php7.3 - image: nextcloudci/php7.3:php7.3-5 - environment: - APP_NAME: workflow_script - CORE_BRANCH: master - DB: sqlite - commands: - # Pre-setup steps - - wget https://raw.githubusercontent.com/nextcloud/travis_ci/master/before_install.sh - - bash ./before_install.sh $APP_NAME $CORE_BRANCH $DB - - cd ../server/apps/$APP_NAME - - composer install - - ./vendor/bin/parallel-lint --exclude ./vendor/ . - -trigger: - branch: - - master - - stable* - event: - - pull_request - - push - -type: docker diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..c95ac01 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,66 @@ +name: Lint + +on: + pull_request: + push: + branches: + - master + - stable* + +jobs: + php: + runs-on: ubuntu-latest + + strategy: + matrix: + php-versions: ['7.3', '7.4', '8.0'] + + name: php${{ matrix.php-versions }} + steps: + - uses: actions/checkout@v2 + + - name: Set up php ${{ matrix.php-versions }} + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + coverage: none + + - name: Lint + run: composer run lint || ( echo 'Please run `composer run lint` and fix your code' && exit 1 ) + + php-cs-fixer: + runs-on: ubuntu-latest + + strategy: + matrix: + php-versions: ['7.4'] + + name: cs php${{ matrix.php-versions }} + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Set up php + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + coverage: none + + - name: Install dependencies + run: composer i + + - name: Run coding standards check + run: composer run cs:check || ( echo 'Please run `composer run cs:fix` to format your code' && exit 1 ) + + xml-linters: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@master + - name: Download schema + run: wget https://apps.nextcloud.com/schema/apps/info.xsd + - name: Lint info.xml + uses: ChristophWurst/xmllint-action@v1 + with: + xml-file: ./appinfo/info.xml + xml-schema-file: ./info.xsd diff --git a/.nextcloudignore b/.nextcloudignore index e94421c..7799f21 100644 --- a/.nextcloudignore +++ b/.nextcloudignore @@ -6,6 +6,7 @@ composer.* krankerl.toml l10n/.gitkeep .nextcloudignore +.php_cs.dist README.md screenshots tests diff --git a/.php_cs.dist b/.php_cs.dist new file mode 100644 index 0000000..f7bbdd8 --- /dev/null +++ b/.php_cs.dist @@ -0,0 +1,18 @@ +getFinder() + ->ignoreVCSIgnored(true) + ->notPath('build') + ->notPath('l10n') + ->notPath('src') + ->notPath('vendor') + ->in(__DIR__); +return $config; diff --git a/composer.json b/composer.json index 8d00bd3..cee17b2 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,17 @@ { - "require-dev": { - "php-parallel-lint/php-parallel-lint": "^1.2" - } + "autoload-dev": { + "psr-4": { + "OCP\\": "vendor/christophwurst/nextcloud/OCP", + "OCA\\WorkflowScript\\": "lib/" + } + }, + "scripts": { + "cs:check": "php-cs-fixer fix --dry-run --diff", + "cs:fix": "php-cs-fixer fix", + "lint": "find . -name \\*.php -not -path './vendor/*' -not -path './build/*' -not -path './tests/integration/vendor/*' -print0 | xargs -0 -n1 php -l" + }, + "require-dev": { + "nextcloud/coding-standard": "^0.5.0", + "christophwurst/nextcloud": "dev-master" + } }