Refactor and improvements on command
Check available helpers Default permission: view only Moved validate setting from helper to command Turn more easy create a importer Docblock and improvements on interface lcfirst on system property Helper moved to ImporHelper folder Moved fixtures to ImportHelper Rename settings to config Big refactor to move import methods to service Signed-off-by: Vitor Mattos <vitor@php.rio>
This commit is contained in:
committed by
Julius Härtl
parent
eb8bf3f22b
commit
fd92fc3c4d
@@ -23,26 +23,23 @@
|
||||
|
||||
namespace OCA\Deck\Command;
|
||||
|
||||
use OCA\Deck\Command\Helper\ImportInterface;
|
||||
use OCA\Deck\Command\Helper\TrelloHelper;
|
||||
use JsonSchema\Constraints\Constraint;
|
||||
use JsonSchema\Validator;
|
||||
use OCA\Deck\Command\ImportHelper\AImport;
|
||||
use OCA\Deck\Command\ImportHelper\TrelloHelper;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Question\ChoiceQuestion;
|
||||
use Symfony\Component\Console\Question\Question;
|
||||
|
||||
class BoardImport extends Command {
|
||||
/** @var string */
|
||||
private $system;
|
||||
private $allowedSystems = ['trello'];
|
||||
private $allowedSystems;
|
||||
/** @var TrelloHelper */
|
||||
private $trelloHelper;
|
||||
/**
|
||||
* Data object created from settings JSON
|
||||
*
|
||||
* @var \StdClass
|
||||
*/
|
||||
public $settings;
|
||||
|
||||
public function __construct(
|
||||
TrelloHelper $trelloHelper
|
||||
@@ -55,6 +52,11 @@ class BoardImport extends Command {
|
||||
* @return void
|
||||
*/
|
||||
protected function configure() {
|
||||
$allowedSystems = glob(__DIR__ . '/ImportHelper/*Helper.php');
|
||||
$this->allowedSystems = array_map(function ($name) {
|
||||
preg_match('/\/(?<system>\w+)Helper\.php$/', $name, $matches);
|
||||
return lcfirst($matches['system']);
|
||||
}, $allowedSystems);
|
||||
$this
|
||||
->setName('deck:import')
|
||||
->setDescription('Import data')
|
||||
@@ -62,11 +64,11 @@ class BoardImport extends Command {
|
||||
'system',
|
||||
null,
|
||||
InputOption::VALUE_REQUIRED,
|
||||
'Source system for import. Available options: trello.',
|
||||
'Source system for import. Available options: ' . implode(', ', $this->allowedSystems) . '.',
|
||||
'trello'
|
||||
)
|
||||
->addOption(
|
||||
'setting',
|
||||
'config',
|
||||
null,
|
||||
InputOption::VALUE_REQUIRED,
|
||||
'Configuration json file.',
|
||||
@@ -89,18 +91,64 @@ class BoardImport extends Command {
|
||||
*/
|
||||
protected function interact(InputInterface $input, OutputInterface $output) {
|
||||
$this->validateSystem($input, $output);
|
||||
$this->getSystem()
|
||||
$this->validateConfig($input, $output);
|
||||
$this->getSystemHelper()
|
||||
->validate($input, $output);
|
||||
}
|
||||
|
||||
protected function validateConfig(InputInterface $input, OutputInterface $output): void {
|
||||
$configFile = $input->getOption('config');
|
||||
if (!is_file($configFile)) {
|
||||
$helper = $this->getHelper('question');
|
||||
$question = new Question(
|
||||
'Please inform a valid config json file: ',
|
||||
'config.json'
|
||||
);
|
||||
$question->setValidator(function ($answer) {
|
||||
if (!is_file($answer)) {
|
||||
throw new \RuntimeException(
|
||||
'config file not found'
|
||||
);
|
||||
}
|
||||
return $answer;
|
||||
});
|
||||
$configFile = $helper->ask($input, $output, $question);
|
||||
$input->setOption('config', $configFile);
|
||||
}
|
||||
|
||||
$config = json_decode(file_get_contents($configFile));
|
||||
$schemaPath = __DIR__ . '/ImportHelper/fixtures/config-' . $this->getSystem() . '-schema.json';
|
||||
$validator = new Validator();
|
||||
$validator->validate(
|
||||
$config,
|
||||
(object)['$ref' => 'file://' . realpath($schemaPath)],
|
||||
Constraint::CHECK_MODE_APPLY_DEFAULTS
|
||||
);
|
||||
if (!$validator->isValid()) {
|
||||
$output->writeln('<error>Invalid config file</error>');
|
||||
$output->writeln(array_map(function ($v) {
|
||||
return $v['message'];
|
||||
}, $validator->getErrors()));
|
||||
$output->writeln('Valid schema:');
|
||||
$output->writeln(print_r(file_get_contents($schemaPath), true));
|
||||
$input->setOption('config', null);
|
||||
$this->validateConfig($input, $output);
|
||||
}
|
||||
$this->getSystemHelper()->setConfigInstance($config);
|
||||
}
|
||||
|
||||
private function setSystem(string $system): void {
|
||||
$this->system = $system;
|
||||
}
|
||||
|
||||
public function getSystem() {
|
||||
return $this->system;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ImportInterface
|
||||
* @return AImport
|
||||
*/
|
||||
private function getSystem() {
|
||||
private function getSystemHelper() {
|
||||
$helper = $this->{$this->system . 'Helper'};
|
||||
$helper->setCommand($this);
|
||||
return $helper;
|
||||
@@ -134,7 +182,7 @@ class BoardImport extends Command {
|
||||
* @return int
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int {
|
||||
$this->getSystem()
|
||||
$this->getSystemHelper()
|
||||
->import($input, $output);
|
||||
$output->writeln('Done!');
|
||||
return 0;
|
||||
|
||||
@@ -1,86 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace OCA\Deck\Command\Helper;
|
||||
|
||||
use JsonSchema\Validator;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Question\Question;
|
||||
|
||||
class ImportAbstract {
|
||||
/** @var Command */
|
||||
private $command;
|
||||
/** @var \stdClass */
|
||||
private $settings;
|
||||
|
||||
public function setCommand(Command $command): void {
|
||||
$this->command = $command;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Command
|
||||
*/
|
||||
public function getCommand() {
|
||||
return $this->command;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a setting
|
||||
*
|
||||
* @param string $setting Setting name
|
||||
* @return mixed
|
||||
*/
|
||||
public function getSetting($setting) {
|
||||
return $this->settings->$setting;
|
||||
}
|
||||
|
||||
/**
|
||||
* Define a setting
|
||||
*
|
||||
* @param string $settingName
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
*/
|
||||
public function setSetting($settingName, $value) {
|
||||
$this->settings->$settingName = $value;
|
||||
}
|
||||
|
||||
protected function validateSettings(InputInterface $input, OutputInterface $output): void {
|
||||
$settingFile = $input->getOption('setting');
|
||||
if (!is_file($settingFile)) {
|
||||
$helper = $this->getCommand()->getHelper('question');
|
||||
$question = new Question(
|
||||
'Please inform a valid setting json file: ',
|
||||
'config.json'
|
||||
);
|
||||
$question->setValidator(function ($answer) {
|
||||
if (!is_file($answer)) {
|
||||
throw new \RuntimeException(
|
||||
'Setting file not found'
|
||||
);
|
||||
}
|
||||
return $answer;
|
||||
});
|
||||
$settingFile = $helper->ask($input, $output, $question);
|
||||
$input->setOption('setting', $settingFile);
|
||||
}
|
||||
|
||||
$this->settings = json_decode(file_get_contents($settingFile));
|
||||
$validator = new Validator();
|
||||
$validator->validate(
|
||||
$this->settings,
|
||||
(object)['$ref' => 'file://' . realpath(__DIR__ . '/../fixtures/setting-schema.json')]
|
||||
);
|
||||
if (!$validator->isValid()) {
|
||||
$output->writeln('<error>Invalid setting file</error>');
|
||||
$output->writeln(array_map(function ($v) {
|
||||
return $v['message'];
|
||||
}, $validator->getErrors()));
|
||||
$output->writeln('Valid schema:');
|
||||
$output->writeln(print_r(file_get_contents(__DIR__ . '/fixtures/setting-schema.json'), true));
|
||||
$input->setOption('setting', null);
|
||||
$this->validateSettings($input, $output);
|
||||
}
|
||||
}
|
||||
}
|
||||
75
lib/Command/ImportHelper/AImport.php
Normal file
75
lib/Command/ImportHelper/AImport.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace OCA\Deck\Command\ImportHelper;
|
||||
|
||||
use OCA\Deck\Command\BoardImport;
|
||||
use OCA\Deck\Service\AImportService;
|
||||
use OCA\Deck\Service\TrelloImportService;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
abstract class AImport extends AImportService {
|
||||
/** @var TrelloImportService */
|
||||
protected $trelloImportService;
|
||||
/** @var Command */
|
||||
private $command;
|
||||
/**
|
||||
* Data object created from config JSON
|
||||
*
|
||||
* @var \StdClass
|
||||
*/
|
||||
public $config;
|
||||
|
||||
public function __construct(
|
||||
TrelloImportService $trelloImportService
|
||||
) {
|
||||
$this->trelloImportService = $trelloImportService;
|
||||
}
|
||||
|
||||
abstract public function validate(InputInterface $input, OutputInterface $output): void;
|
||||
|
||||
abstract public function import(InputInterface $input, OutputInterface $output): void;
|
||||
|
||||
/**
|
||||
* Define Command instance
|
||||
*
|
||||
* @param Command $command
|
||||
* @return void
|
||||
*/
|
||||
public function setCommand(Command $command): void {
|
||||
$this->command = $command;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BoardImport
|
||||
*/
|
||||
public function getCommand() {
|
||||
return $this->command;
|
||||
}
|
||||
|
||||
public function setConfigInstance(\stdClass $config) {
|
||||
$this->trelloImportService->setConfigInstance($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Define a config
|
||||
*
|
||||
* @param string $configName
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
*/
|
||||
public function setConfig(string $configName, $value): void {
|
||||
$this->trelloImportService->setConfig($configName, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a config
|
||||
*
|
||||
* @param string $configName config name
|
||||
* @return mixed
|
||||
*/
|
||||
public function getConfig(string $configName = null) {
|
||||
return $this->trelloImportService->getConfig($configName);
|
||||
}
|
||||
}
|
||||
@@ -21,8 +21,10 @@
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Deck\Command\Helper;
|
||||
namespace OCA\Deck\Command\ImportHelper;
|
||||
|
||||
use OCA\Deck\Command\BoardImport;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
@@ -44,4 +46,34 @@ interface ImportInterface {
|
||||
* @return void
|
||||
*/
|
||||
public function import(InputInterface $input, OutputInterface $output): void;
|
||||
|
||||
/**
|
||||
* Define Command instance
|
||||
*
|
||||
* @param Command $command
|
||||
* @return void
|
||||
*/
|
||||
public function setCommand(Command $command): void;
|
||||
|
||||
/**
|
||||
* @return BoardImport
|
||||
*/
|
||||
public function getCommand();
|
||||
|
||||
/**
|
||||
* Define a config
|
||||
*
|
||||
* @param string $configName
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
*/
|
||||
public function setConfig(string $configName, $value): void;
|
||||
|
||||
/**
|
||||
* Get a config
|
||||
*
|
||||
* @param string $configName config name
|
||||
* @return mixed
|
||||
*/
|
||||
public function getConfig($configName);
|
||||
}
|
||||
76
lib/Command/ImportHelper/TrelloHelper.php
Normal file
76
lib/Command/ImportHelper/TrelloHelper.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2021 Vitor Mattos <vitor@php.rio>
|
||||
*
|
||||
* @author Vitor Mattos <vitor@php.rio>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Deck\Command\ImportHelper;
|
||||
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Question\Question;
|
||||
|
||||
class TrelloHelper extends AImport {
|
||||
public function validate(InputInterface $input, OutputInterface $output): void {
|
||||
$this->validateData($input, $output);
|
||||
$this->trelloImportService->validateOwner();
|
||||
$this->trelloImportService->validateUsers();
|
||||
}
|
||||
|
||||
public function import(InputInterface $input, OutputInterface $output): void {
|
||||
$this->trelloImportService->setUserId();
|
||||
$output->writeln('Importing board...');
|
||||
$this->trelloImportService->importBoard();
|
||||
$output->writeln('Assign users to board...');
|
||||
$this->trelloImportService->assignUsersToBoard();
|
||||
$output->writeln('Importing labels...');
|
||||
$this->trelloImportService->importLabels();
|
||||
$output->writeln('Importing stacks...');
|
||||
$this->trelloImportService->importStacks();
|
||||
$output->writeln('Importing cards...');
|
||||
$this->trelloImportService->importCards();
|
||||
}
|
||||
|
||||
private function validateData(InputInterface $input, OutputInterface $output): void {
|
||||
$filename = $input->getOption('data');
|
||||
if (!is_file($filename)) {
|
||||
$helper = $this->getCommand()->getHelper('question');
|
||||
$question = new Question(
|
||||
'Please inform a valid data json file: ',
|
||||
'data.json'
|
||||
);
|
||||
$question->setValidator(function ($answer) {
|
||||
if (!is_file($answer)) {
|
||||
throw new \RuntimeException(
|
||||
'Data file not found'
|
||||
);
|
||||
}
|
||||
return $answer;
|
||||
});
|
||||
$data = $helper->ask($input, $output, $question);
|
||||
$input->setOption('data', $data);
|
||||
}
|
||||
$this->trelloImportService->setData(json_decode(file_get_contents($filename)));
|
||||
if (!$this->trelloImportService->getData()) {
|
||||
$output->writeln('<error>Is not a json file: ' . $filename . '</error>');
|
||||
$this->validateData($input, $output);
|
||||
}
|
||||
}
|
||||
}
|
||||
24
lib/Command/ImportHelper/fixtures/config-trello-schema.json
Normal file
24
lib/Command/ImportHelper/fixtures/config-trello-schema.json
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"uidRelation": {
|
||||
"type": "object",
|
||||
"comment": "Relationship between Trello and Nextcloud usernames",
|
||||
"example": {
|
||||
"johndoe": "admin"
|
||||
}
|
||||
},
|
||||
"owner": {
|
||||
"type": "string",
|
||||
"required": true,
|
||||
"comment": "Nextcloud owner username"
|
||||
},
|
||||
"color": {
|
||||
"type": "string",
|
||||
"required": true,
|
||||
"pattern": "^[0-9a-fA-F]{6}$",
|
||||
"comment": "Default color for the board. If you don't inform, the default color will be used.",
|
||||
"default": "0800fd"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"uidRelation": {
|
||||
"type": "object"
|
||||
},
|
||||
"owner": {
|
||||
"type": "string",
|
||||
"required": true
|
||||
},
|
||||
"color": {
|
||||
"type": "string",
|
||||
"required": true,
|
||||
"pattern": "^[0-9a-fA-F]{6}$"
|
||||
}
|
||||
}
|
||||
}
|
||||
49
lib/Service/AImportService.php
Normal file
49
lib/Service/AImportService.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace OCA\Deck\Service;
|
||||
|
||||
abstract class AImportService {
|
||||
/**
|
||||
* Data object created from config JSON
|
||||
*
|
||||
* @var \stdClass
|
||||
*/
|
||||
public $config;
|
||||
|
||||
public function setConfigInstance(\stdClass $config) {
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Define a config
|
||||
*
|
||||
* @param string $configName
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
*/
|
||||
public function setConfig(string $configName, $value): void {
|
||||
if (!$this->config) {
|
||||
$this->setConfigInstance(new \stdClass);
|
||||
}
|
||||
$this->config->$configName = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a config
|
||||
*
|
||||
* @param string $configName config name
|
||||
* @return mixed
|
||||
*/
|
||||
public function getConfig(string $configName = null) {
|
||||
if (!is_object($this->config)) {
|
||||
return;
|
||||
}
|
||||
if (!$configName) {
|
||||
return $this->config;
|
||||
}
|
||||
if (!property_exists($this->config, $configName)) {
|
||||
return;
|
||||
}
|
||||
return $this->config->$configName;
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Deck\Command\Helper;
|
||||
namespace OCA\Deck\Service;
|
||||
|
||||
use OCA\Deck\Db\Acl;
|
||||
use OCA\Deck\Db\AclMapper;
|
||||
@@ -33,19 +33,16 @@ use OCA\Deck\Db\CardMapper;
|
||||
use OCA\Deck\Db\Label;
|
||||
use OCA\Deck\Db\Stack;
|
||||
use OCA\Deck\Db\StackMapper;
|
||||
use OCA\Deck\Service\BoardService;
|
||||
use OCA\Deck\Service\LabelService;
|
||||
use OCP\IDBConnection;
|
||||
use OCP\IL10N;
|
||||
use OCP\IUser;
|
||||
use OCP\IUserManager;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Question\Question;
|
||||
|
||||
class TrelloHelper extends ImportAbstract implements ImportInterface {
|
||||
class TrelloImportService extends AImportService {
|
||||
/** @var BoardService */
|
||||
private $boardService;
|
||||
/** @var LabelService */
|
||||
private $labelService;
|
||||
/** @var StackMapper */
|
||||
private $stackMapper;
|
||||
/** @var CardMapper */
|
||||
@@ -58,18 +55,10 @@ class TrelloHelper extends ImportAbstract implements ImportInterface {
|
||||
private $connection;
|
||||
/** @var IUserManager */
|
||||
private $userManager;
|
||||
/** @var Board */
|
||||
private $board;
|
||||
/** @var LabelService */
|
||||
private $labelService;
|
||||
/** @var IL10N */
|
||||
private $l10n;
|
||||
/**
|
||||
* Data object created from JSON of origin system
|
||||
*
|
||||
* @var \StdClass
|
||||
*/
|
||||
private $data;
|
||||
/** @var Board */
|
||||
private $board;
|
||||
/**
|
||||
* Array of stacks
|
||||
*
|
||||
@@ -86,6 +75,12 @@ class TrelloHelper extends ImportAbstract implements ImportInterface {
|
||||
private $cards = [];
|
||||
/** @var IUser[] */
|
||||
private $members = [];
|
||||
/**
|
||||
* Data object created from JSON of origin system
|
||||
*
|
||||
* @var \StdClass
|
||||
*/
|
||||
private $data;
|
||||
|
||||
public function __construct(
|
||||
BoardService $boardService,
|
||||
@@ -109,82 +104,30 @@ class TrelloHelper extends ImportAbstract implements ImportInterface {
|
||||
$this->l10n = $l10n;
|
||||
}
|
||||
|
||||
public function validate(InputInterface $input, OutputInterface $output): void {
|
||||
$this->validateData($input, $output);
|
||||
$this->validateSettings($input, $output);
|
||||
$this->validateUsers();
|
||||
$this->validateOwner();
|
||||
public function setData(\stdClass $data) {
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
public function import(InputInterface $input, OutputInterface $output): void {
|
||||
$this->setUserId();
|
||||
$output->writeln('Importing board...');
|
||||
$this->importBoard();
|
||||
$output->writeln('Assign users to board...');
|
||||
$this->assignUsersToBoard();
|
||||
$output->writeln('Importing labels...');
|
||||
$this->importLabels();
|
||||
$output->writeln('Importing stacks...');
|
||||
$this->importStacks();
|
||||
$output->writeln('Importing cards...');
|
||||
$this->importCards();
|
||||
public function getData() {
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
private function assignUsersToBoard(): void {
|
||||
foreach ($this->members as $member) {
|
||||
$acl = new Acl();
|
||||
$acl->setBoardId($this->board->getId());
|
||||
$acl->setType(Acl::PERMISSION_TYPE_USER);
|
||||
$acl->setParticipant($member->getUid());
|
||||
$acl->setPermissionEdit(true);
|
||||
$acl->setPermissionShare($member->getUID() === $this->getSetting('owner')->getUID());
|
||||
$acl->setPermissionManage($member->getUID() === $this->getSetting('owner')->getUID());
|
||||
$this->aclMapper->insert($acl);
|
||||
}
|
||||
}
|
||||
|
||||
private function validateData(InputInterface $input, OutputInterface $output): void {
|
||||
$filename = $input->getOption('data');
|
||||
if (!is_file($filename)) {
|
||||
$helper = $this->getCommand()->getHelper('question');
|
||||
$question = new Question(
|
||||
'Please inform a valid data json file: ',
|
||||
'data.json'
|
||||
);
|
||||
$question->setValidator(function ($answer) {
|
||||
if (!is_file($answer)) {
|
||||
throw new \RuntimeException(
|
||||
'Data file not found'
|
||||
);
|
||||
}
|
||||
return $answer;
|
||||
});
|
||||
$data = $helper->ask($input, $output, $question);
|
||||
$input->setOption('data', $data);
|
||||
}
|
||||
$this->data = json_decode(file_get_contents($filename));
|
||||
if (!$this->data) {
|
||||
$output->writeln('<error>Is not a json file: ' . $filename . '</error>');
|
||||
$this->validateData($input, $output);
|
||||
}
|
||||
}
|
||||
|
||||
private function validateOwner(): void {
|
||||
$owner = $this->userManager->get($this->getSetting('owner'));
|
||||
public function validateOwner(): void {
|
||||
$owner = $this->userManager->get($this->getConfig('owner'));
|
||||
if (!$owner) {
|
||||
throw new \LogicException('Owner "' . $this->getSetting('owner') . '" not found on Nextcloud. Check setting json.');
|
||||
throw new \LogicException('Owner "' . $this->getConfig('owner')->getUID() . '" not found on Nextcloud. Check setting json.');
|
||||
}
|
||||
$this->setSetting('owner', $owner);
|
||||
$this->setConfig('owner', $owner);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
private function validateUsers() {
|
||||
if (empty($this->getSetting('uidRelation'))) {
|
||||
public function validateUsers() {
|
||||
if (empty($this->getConfig('uidRelation'))) {
|
||||
return;
|
||||
}
|
||||
foreach ($this->getSetting('uidRelation') as $trelloUid => $nextcloudUid) {
|
||||
foreach ($this->getConfig('uidRelation') as $trelloUid => $nextcloudUid) {
|
||||
$user = array_filter($this->data->members, function ($u) use ($trelloUid) {
|
||||
return $u->username === $trelloUid;
|
||||
});
|
||||
@@ -194,12 +137,28 @@ class TrelloHelper extends ImportAbstract implements ImportInterface {
|
||||
if (!is_string($nextcloudUid)) {
|
||||
throw new \LogicException('User on setting uidRelation must be a string');
|
||||
}
|
||||
$this->getSetting('uidRelation')->$trelloUid = $this->userManager->get($nextcloudUid);
|
||||
if (!$this->getSetting('uidRelation')->$trelloUid) {
|
||||
$this->getConfig('uidRelation')->$trelloUid = $this->userManager->get($nextcloudUid);
|
||||
if (!$this->getConfig('uidRelation')->$trelloUid) {
|
||||
throw new \LogicException('User on setting uidRelation not found: ' . $nextcloudUid);
|
||||
}
|
||||
$user = current($user);
|
||||
$this->members[$user->id] = $this->getSetting('uidRelation')->$trelloUid;
|
||||
$this->members[$user->id] = $this->getConfig('uidRelation')->$trelloUid;
|
||||
}
|
||||
}
|
||||
|
||||
public function assignUsersToBoard(): void {
|
||||
foreach ($this->members as $member) {
|
||||
if ($member->getUID() === $this->getConfig('owner')->getUID()) {
|
||||
continue;
|
||||
}
|
||||
$acl = new Acl();
|
||||
$acl->setBoardId($this->board->getId());
|
||||
$acl->setType(Acl::PERMISSION_TYPE_USER);
|
||||
$acl->setParticipant($member->getUID());
|
||||
$acl->setPermissionEdit(false);
|
||||
$acl->setPermissionShare(false);
|
||||
$acl->setPermissionManage(false);
|
||||
$this->aclMapper->insert($acl);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -222,7 +181,7 @@ class TrelloHelper extends ImportAbstract implements ImportInterface {
|
||||
return $checklist_string;
|
||||
}
|
||||
|
||||
private function importCards(): void {
|
||||
public function importCards(): void {
|
||||
$checklists = [];
|
||||
foreach ($this->data->checklists as $checklist) {
|
||||
$checklists[$checklist->idCard][$checklist->id] = $this->formulateChecklistText($checklist);
|
||||
@@ -247,7 +206,7 @@ class TrelloHelper extends ImportAbstract implements ImportInterface {
|
||||
$card->setStackId($this->stacks[$trelloCard->idList]->getId());
|
||||
$card->setType('plain');
|
||||
$card->setOrder($trelloCard->idShort);
|
||||
$card->setOwner($this->getSetting('owner')->getUID());
|
||||
$card->setOwner($this->getConfig('owner')->getUID());
|
||||
$card->setDescription($trelloCard->desc);
|
||||
if ($trelloCard->due) {
|
||||
$duedate = \DateTime::createFromFormat('Y-m-d\TH:i:s.v\Z', $trelloCard->due)
|
||||
@@ -297,10 +256,10 @@ class TrelloHelper extends ImportAbstract implements ImportInterface {
|
||||
}
|
||||
);
|
||||
foreach ($comments as $trelloComment) {
|
||||
if (!empty($this->getSetting('uidRelation')->{$trelloComment->memberCreator->username})) {
|
||||
$actor = $this->getSetting('uidRelation')->{$trelloComment->memberCreator->username}->getUID();
|
||||
if (!empty($this->getConfig('uidRelation')->{$trelloComment->memberCreator->username})) {
|
||||
$actor = $this->getConfig('uidRelation')->{$trelloComment->memberCreator->username}->getUID();
|
||||
} else {
|
||||
$actor = $this->getSetting('owner')->getUID();
|
||||
$actor = $this->getConfig('owner')->getUID();
|
||||
}
|
||||
$message = $this->replaceUsernames($trelloComment->data->text);
|
||||
$qb = $this->connection->getQueryBuilder();
|
||||
@@ -329,7 +288,7 @@ class TrelloHelper extends ImportAbstract implements ImportInterface {
|
||||
}
|
||||
|
||||
private function replaceUsernames($text) {
|
||||
foreach ($this->getSetting('uidRelation') as $trello => $nextcloud) {
|
||||
foreach ($this->getConfig('uidRelation') as $trello => $nextcloud) {
|
||||
$text = str_replace($trello, $nextcloud->getUID(), $text);
|
||||
}
|
||||
return $text;
|
||||
@@ -344,7 +303,7 @@ class TrelloHelper extends ImportAbstract implements ImportInterface {
|
||||
}
|
||||
}
|
||||
|
||||
private function importStacks(): void {
|
||||
public function importStacks(): void {
|
||||
$this->stacks = [];
|
||||
foreach ($this->data->lists as $order => $list) {
|
||||
$stack = new Stack();
|
||||
@@ -386,15 +345,15 @@ class TrelloHelper extends ImportAbstract implements ImportInterface {
|
||||
}
|
||||
}
|
||||
|
||||
private function importBoard(): void {
|
||||
public function importBoard(): void {
|
||||
$this->board = $this->boardService->create(
|
||||
$this->data->name,
|
||||
$this->getSetting('owner')->getUID(),
|
||||
$this->getSetting('color')
|
||||
$this->getConfig('owner')->getUID(),
|
||||
$this->getConfig('color')
|
||||
);
|
||||
}
|
||||
|
||||
private function importLabels(): void {
|
||||
public function importLabels(): void {
|
||||
$this->labels = [];
|
||||
foreach ($this->data->labels as $label) {
|
||||
if (empty($label->name)) {
|
||||
@@ -411,7 +370,7 @@ class TrelloHelper extends ImportAbstract implements ImportInterface {
|
||||
}
|
||||
}
|
||||
|
||||
private function setUserId(): void {
|
||||
public function setUserId(): void {
|
||||
if (!property_exists($this->labelService, 'permissionService')) {
|
||||
return;
|
||||
}
|
||||
@@ -425,6 +384,6 @@ class TrelloHelper extends ImportAbstract implements ImportInterface {
|
||||
|
||||
$propertyUserId = new \ReflectionProperty($permissionService, 'userId');
|
||||
$propertyUserId->setAccessible(true);
|
||||
$propertyUserId->setValue($permissionService, $this->getSetting('owner')->getUID());
|
||||
$propertyUserId->setValue($permissionService, $this->getConfig('owner')->getUID());
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
namespace OCA\Deck\Command;
|
||||
|
||||
use OCA\Deck\Command\Helper\TrelloHelper;
|
||||
use OCA\Deck\Command\ImportHelper\TrelloHelper;
|
||||
use Symfony\Component\Console\Helper\HelperSet;
|
||||
use Symfony\Component\Console\Helper\QuestionHelper;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
@@ -55,12 +55,12 @@ class BoardImportTest extends \Test\TestCase {
|
||||
$input->method('getOption')
|
||||
->withConsecutive(
|
||||
[$this->equalTo('system')],
|
||||
[$this->equalTo('setting')],
|
||||
[$this->equalTo('config')],
|
||||
[$this->equalTo('data')]
|
||||
)
|
||||
->will($this->returnValueMap([
|
||||
['system', 'trello'],
|
||||
['setting', __DIR__ . '/fixtures/setting-trello.json'],
|
||||
['config', __DIR__ . '/fixtures/config-trello.json'],
|
||||
['data', __DIR__ . '/fixtures/data-trello.json']
|
||||
]));
|
||||
$output = $this->createMock(OutputInterface::class);
|
||||
|
||||
@@ -23,59 +23,23 @@
|
||||
|
||||
namespace OCA\Deck\Command;
|
||||
|
||||
use OCA\Deck\Command\Helper\TrelloHelper;
|
||||
use OCA\Deck\Db\AclMapper;
|
||||
use OCA\Deck\Db\AssignmentMapper;
|
||||
use OCA\Deck\Db\CardMapper;
|
||||
use OCA\Deck\Db\StackMapper;
|
||||
use OCA\Deck\Service\BoardService;
|
||||
use OCA\Deck\Service\LabelService;
|
||||
use OCP\IDBConnection;
|
||||
use OCP\IL10N;
|
||||
use OCP\IUserManager;
|
||||
use OCA\Deck\Command\ImportHelper\TrelloHelper;
|
||||
use OCA\Deck\Service\TrelloImportService;
|
||||
use Symfony\Component\Console\Helper\HelperSet;
|
||||
use Symfony\Component\Console\Helper\QuestionHelper;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class TrelloHelperTest extends \Test\TestCase {
|
||||
/** @var BoardService */
|
||||
private $boardService;
|
||||
/** @var LabelService */
|
||||
private $labelService;
|
||||
/** @var StackMapper */
|
||||
private $stackMapper;
|
||||
/** @var CardMapper */
|
||||
private $cardMapper;
|
||||
/** @var IDBConnection */
|
||||
private $connection;
|
||||
/** @var IUserManager */
|
||||
private $userManager;
|
||||
/** @var TrelloImportService */
|
||||
private $trelloImportService;
|
||||
/** @var TrelloHelper */
|
||||
private $trelloHelper;
|
||||
/** @var IL10N */
|
||||
private $l10n;
|
||||
public function setUp(): void {
|
||||
parent::setUp();
|
||||
$this->boardService = $this->createMock(BoardService::class);
|
||||
$this->labelService = $this->createMock(LabelService::class);
|
||||
$this->stackMapper = $this->createMock(StackMapper::class);
|
||||
$this->cardMapper = $this->createMock(CardMapper::class);
|
||||
$this->assignmentMapper = $this->createMock(AssignmentMapper::class);
|
||||
$this->aclMapper = $this->createMock(AclMapper::class);
|
||||
$this->connection = $this->createMock(IDBConnection::class);
|
||||
$this->userManager = $this->createMock(IUserManager::class);
|
||||
$this->l10n = $this->createMock(IL10N::class);
|
||||
$this->trelloImportService = $this->createMock(TrelloImportService::class);
|
||||
$this->trelloHelper = new TrelloHelper(
|
||||
$this->boardService,
|
||||
$this->labelService,
|
||||
$this->stackMapper,
|
||||
$this->cardMapper,
|
||||
$this->assignmentMapper,
|
||||
$this->aclMapper,
|
||||
$this->connection,
|
||||
$this->userManager,
|
||||
$this->l10n
|
||||
$this->trelloImportService
|
||||
);
|
||||
$questionHelper = new QuestionHelper();
|
||||
$command = new BoardImport($this->trelloHelper);
|
||||
@@ -92,47 +56,17 @@ class TrelloHelperTest extends \Test\TestCase {
|
||||
|
||||
$input->method('getOption')
|
||||
->withConsecutive(
|
||||
[$this->equalTo('data')],
|
||||
[$this->equalTo('setting')]
|
||||
[$this->equalTo('system')],
|
||||
[$this->equalTo('config')]
|
||||
)
|
||||
->will($this->returnValueMap([
|
||||
['data', __DIR__ . '/../fixtures/data-trello.json'],
|
||||
['setting', __DIR__ . '/../fixtures/setting-trello.json']
|
||||
['system', 'trello'],
|
||||
['config', __DIR__ . '/../fixtures/config-trello.json']
|
||||
]));
|
||||
$output = $this->createMock(OutputInterface::class);
|
||||
|
||||
$user = $this->createMock(\OCP\IUser::class);
|
||||
$user
|
||||
->method('getUID')
|
||||
->willReturn('admin');
|
||||
$this->userManager
|
||||
->method('get')
|
||||
->willReturn($user);
|
||||
$this->userManager
|
||||
->method('get')
|
||||
->willReturn($user);
|
||||
$board = $this->createMock(\OCA\Deck\Db\Board::class);
|
||||
$this->boardService
|
||||
->expects($this->once())
|
||||
->method('create')
|
||||
->willReturn($board);
|
||||
$label = $this->createMock(\OCA\Deck\Db\Label::class);
|
||||
$this->labelService
|
||||
->expects($this->once())
|
||||
->method('create')
|
||||
->willReturn($label);
|
||||
$stack = $this->createMock(\OCA\Deck\Db\Stack::class);
|
||||
$this->stackMapper
|
||||
->expects($this->once())
|
||||
->method('insert')
|
||||
->willReturn($stack);
|
||||
$card = $this->createMock(\OCA\Deck\Db\Card::class);
|
||||
$this->cardMapper
|
||||
->expects($this->once())
|
||||
->method('insert')
|
||||
->willReturn($card);
|
||||
|
||||
$this->trelloHelper->validate($input, $output);
|
||||
$this->invokePrivate($this->trelloHelper->getCommand(), 'validateSystem', [$input, $output]);
|
||||
$this->invokePrivate($this->trelloHelper->getCommand(), 'validateConfig', [$input, $output]);
|
||||
$actual = $this->trelloHelper->import($input, $output);
|
||||
$this->assertNull($actual);
|
||||
}
|
||||
|
||||
104
tests/unit/Service/TrelloImportServiceTest.php
Normal file
104
tests/unit/Service/TrelloImportServiceTest.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2021 Vitor Mattos <vitor@php.rio>
|
||||
*
|
||||
* @author Vitor Mattos <vitor@php.rio>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Deck\Service;
|
||||
|
||||
use OCA\Deck\Db\AclMapper;
|
||||
use OCA\Deck\Db\AssignmentMapper;
|
||||
use OCA\Deck\Db\CardMapper;
|
||||
use OCA\Deck\Db\StackMapper;
|
||||
use OCP\IDBConnection;
|
||||
use OCP\IL10N;
|
||||
use OCP\IUserManager;
|
||||
|
||||
class TrelloImportServiceTest extends \Test\TestCase {
|
||||
/** @var TrelloImportService */
|
||||
private $trelloImportService;
|
||||
/** @var BoardService */
|
||||
private $boardService;
|
||||
/** @var LabelService */
|
||||
private $labelService;
|
||||
/** @var StackMapper */
|
||||
private $stackMapper;
|
||||
/** @var CardMapper */
|
||||
private $cardMapper;
|
||||
/** @var AssignmentMapper */
|
||||
private $assignmentMapper;
|
||||
/** @var AclMapper */
|
||||
private $aclMapper;
|
||||
/** @var IDBConnection */
|
||||
private $connection;
|
||||
/** @var IUserManager */
|
||||
private $userManager;
|
||||
/** @var IL10N */
|
||||
private $l10n;
|
||||
public function setUp(): void {
|
||||
parent::setUp();
|
||||
$this->boardService = $this->createMock(BoardService::class);
|
||||
$this->labelService = $this->createMock(LabelService::class);
|
||||
$this->stackMapper = $this->createMock(StackMapper::class);
|
||||
$this->cardMapper = $this->createMock(CardMapper::class);
|
||||
$this->assignmentMapper = $this->createMock(AssignmentMapper::class);
|
||||
$this->aclMapper = $this->createMock(AclMapper::class);
|
||||
$this->connection = $this->createMock(IDBConnection::class);
|
||||
$this->userManager = $this->createMock(IUserManager::class);
|
||||
$this->l10n = $this->createMock(IL10N::class);
|
||||
$this->trelloImportService = new TrelloImportService(
|
||||
$this->boardService,
|
||||
$this->labelService,
|
||||
$this->stackMapper,
|
||||
$this->cardMapper,
|
||||
$this->assignmentMapper,
|
||||
$this->aclMapper,
|
||||
$this->connection,
|
||||
$this->userManager,
|
||||
$this->l10n
|
||||
);
|
||||
}
|
||||
|
||||
public function testValidateOwnerWithFaliure() {
|
||||
$owner = $this->createMock(\OCP\IUser::class);
|
||||
$owner
|
||||
->method('getUID')
|
||||
->willReturn('admin');
|
||||
$this->trelloImportService->setConfig('owner', $owner);
|
||||
$this->userManager
|
||||
->method('get')
|
||||
->willReturn(null);
|
||||
$this->expectErrorMessage('Owner "admin" not found on Nextcloud. Check setting json.');
|
||||
$this->trelloImportService->validateOwner();
|
||||
}
|
||||
|
||||
public function testValidateOwnerWithSuccess() {
|
||||
$owner = $this->createMock(\OCP\IUser::class);
|
||||
$owner
|
||||
->method('getUID')
|
||||
->willReturn('admin');
|
||||
$this->trelloImportService->setConfig('owner', $owner);
|
||||
$this->userManager
|
||||
->method('get')
|
||||
->willReturn($owner);
|
||||
$actual = $this->trelloImportService->validateOwner();
|
||||
$this->assertNull($actual);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user