fix: Add output for individual failures or skipped parts
Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
@@ -49,6 +49,7 @@ use OCP\Comments\NotFoundException as CommentNotFoundException;
|
||||
use OCP\EventDispatcher\IEventDispatcher;
|
||||
use OCP\IUserManager;
|
||||
use OCP\Server;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class BoardImportService {
|
||||
private string $system = '';
|
||||
@@ -70,6 +71,11 @@ class BoardImportService {
|
||||
private $data;
|
||||
private Board $board;
|
||||
|
||||
/** @var callable[] */
|
||||
private array $errorCollectors = [];
|
||||
/** @var callable[] */
|
||||
private array $outputCollectors = [];
|
||||
|
||||
public function __construct(
|
||||
private IUserManager $userManager,
|
||||
private BoardMapper $boardMapper,
|
||||
@@ -80,7 +86,8 @@ class BoardImportService {
|
||||
private AttachmentMapper $attachmentMapper,
|
||||
private CardMapper $cardMapper,
|
||||
private ICommentsManager $commentsManager,
|
||||
private IEventDispatcher $eventDispatcher
|
||||
private IEventDispatcher $eventDispatcher,
|
||||
private LoggerInterface $logger
|
||||
) {
|
||||
$this->board = new Board();
|
||||
$this->disableCommentsEvents();
|
||||
@@ -88,6 +95,28 @@ class BoardImportService {
|
||||
$this->config = new \stdClass();
|
||||
}
|
||||
|
||||
public function registerErrorCollector(callable $errorCollector): void {
|
||||
$this->errorCollectors[] = $errorCollector;
|
||||
}
|
||||
|
||||
public function registerOutputCollector(callable $outputCollector): void {
|
||||
$this->outputCollectors[] = $outputCollector;
|
||||
}
|
||||
|
||||
private function addError(string $message, $exception): void {
|
||||
$message .= ' (on board ' . $this->getBoard()->getTitle() . ')';
|
||||
foreach ($this->errorCollectors as $errorCollector) {
|
||||
$errorCollector($message, $exception);
|
||||
}
|
||||
$this->logger->error($message, ['exception' => $exception]);
|
||||
}
|
||||
|
||||
private function addOutput(string $message): void {
|
||||
foreach ($this->outputCollectors as $outputCollector) {
|
||||
$outputCollector($message);
|
||||
}
|
||||
}
|
||||
|
||||
private function disableCommentsEvents(): void {
|
||||
if (defined('PHPUNIT_RUN')) {
|
||||
return;
|
||||
@@ -117,6 +146,7 @@ class BoardImportService {
|
||||
$this->importComments();
|
||||
$this->importCardAssignments();
|
||||
} catch (\Throwable $th) {
|
||||
$this->logger->error('Failed to import board', ['exception' => $th]);
|
||||
throw new BadRequestException($th->getMessage());
|
||||
}
|
||||
}
|
||||
@@ -195,6 +225,10 @@ class BoardImportService {
|
||||
|
||||
public function importBoard(): void {
|
||||
$board = $this->getImportSystem()->getBoard();
|
||||
if (!$this->userManager->userExists($board->getOwner())) {
|
||||
throw new \Exception('Target owner ' . $board->getOwner() . ' not found. Please provide a mapping through the import config.');
|
||||
}
|
||||
|
||||
if ($board) {
|
||||
$this->boardMapper->insert($board);
|
||||
$this->board = $board;
|
||||
@@ -211,8 +245,13 @@ class BoardImportService {
|
||||
public function importAcl(): void {
|
||||
$aclList = $this->getImportSystem()->getAclList();
|
||||
foreach ($aclList as $code => $acl) {
|
||||
$this->aclMapper->insert($acl);
|
||||
$this->getImportSystem()->updateAcl($code, $acl);
|
||||
try {
|
||||
$this->aclMapper->insert($acl);
|
||||
$this->getImportSystem()->updateAcl($code, $acl);
|
||||
} catch (\Exception $e) {
|
||||
$this->addError('Failed to import acl rule for ' . $acl->getParticipant(), $e);
|
||||
|
||||
}
|
||||
}
|
||||
$this->getBoard()->setAcl($aclList);
|
||||
}
|
||||
@@ -220,8 +259,12 @@ class BoardImportService {
|
||||
public function importLabels(): void {
|
||||
$labels = $this->getImportSystem()->getLabels();
|
||||
foreach ($labels as $code => $label) {
|
||||
$this->labelMapper->insert($label);
|
||||
$this->getImportSystem()->updateLabel($code, $label);
|
||||
try {
|
||||
$this->labelMapper->insert($label);
|
||||
$this->getImportSystem()->updateLabel($code, $label);
|
||||
} catch (\Exception $e) {
|
||||
$this->addError('Failed to import label ' . $label->getTitle(), $e);
|
||||
}
|
||||
}
|
||||
$this->getBoard()->setLabels($labels);
|
||||
}
|
||||
@@ -229,8 +272,12 @@ class BoardImportService {
|
||||
public function importStacks(): void {
|
||||
$stacks = $this->getImportSystem()->getStacks();
|
||||
foreach ($stacks as $code => $stack) {
|
||||
$this->stackMapper->insert($stack);
|
||||
$this->getImportSystem()->updateStack($code, $stack);
|
||||
try {
|
||||
$this->stackMapper->insert($stack);
|
||||
$this->getImportSystem()->updateStack($code, $stack);
|
||||
} catch (\Exception $e) {
|
||||
$this->addError('Failed to import list ' . $stack->getTitle(), $e);
|
||||
}
|
||||
}
|
||||
$this->getBoard()->setStacks(array_values($stacks));
|
||||
}
|
||||
@@ -238,22 +285,26 @@ class BoardImportService {
|
||||
public function importCards(): void {
|
||||
$cards = $this->getImportSystem()->getCards();
|
||||
foreach ($cards as $code => $card) {
|
||||
$createdAt = $card->getCreatedAt();
|
||||
$lastModified = $card->getLastModified();
|
||||
$this->cardMapper->insert($card);
|
||||
$updateDate = false;
|
||||
if ($createdAt && $createdAt !== $card->getCreatedAt()) {
|
||||
$card->setCreatedAt($createdAt);
|
||||
$updateDate = true;
|
||||
try {
|
||||
$createdAt = $card->getCreatedAt();
|
||||
$lastModified = $card->getLastModified();
|
||||
$this->cardMapper->insert($card);
|
||||
$updateDate = false;
|
||||
if ($createdAt && $createdAt !== $card->getCreatedAt()) {
|
||||
$card->setCreatedAt($createdAt);
|
||||
$updateDate = true;
|
||||
}
|
||||
if ($lastModified && $lastModified !== $card->getLastModified()) {
|
||||
$card->setLastModified($lastModified);
|
||||
$updateDate = true;
|
||||
}
|
||||
if ($updateDate) {
|
||||
$this->cardMapper->update($card, false);
|
||||
}
|
||||
$this->getImportSystem()->updateCard($code, $card);
|
||||
} catch (\Exception $e) {
|
||||
$this->addError('Failed to import card ' . $card->getTitle(), $e);
|
||||
}
|
||||
if ($lastModified && $lastModified !== $card->getLastModified()) {
|
||||
$card->setLastModified($lastModified);
|
||||
$updateDate = true;
|
||||
}
|
||||
if ($updateDate) {
|
||||
$this->cardMapper->update($card, false);
|
||||
}
|
||||
$this->getImportSystem()->updateCard($code, $card);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -274,11 +325,15 @@ class BoardImportService {
|
||||
$data = $this->getImportSystem()->getCardLabelAssignment();
|
||||
foreach ($data as $cardId => $assignemnt) {
|
||||
foreach ($assignemnt as $assignmentId => $labelId) {
|
||||
$this->assignCardToLabel(
|
||||
$cardId,
|
||||
$labelId
|
||||
);
|
||||
$this->getImportSystem()->updateCardLabelsAssignment($cardId, $assignmentId, $labelId);
|
||||
try {
|
||||
$this->assignCardToLabel(
|
||||
$cardId,
|
||||
$labelId
|
||||
);
|
||||
$this->getImportSystem()->updateCardLabelsAssignment($cardId, $assignmentId, $labelId);
|
||||
} catch (\Exception $e) {
|
||||
$this->addError('Failed to assign label ' . $labelId . ' to ' . $cardId, $e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -320,9 +375,14 @@ class BoardImportService {
|
||||
public function importCardAssignments(): void {
|
||||
$allAssignments = $this->getImportSystem()->getCardAssignments();
|
||||
foreach ($allAssignments as $cardId => $assignments) {
|
||||
foreach ($assignments as $assignmentId => $assignment) {
|
||||
$this->assignmentMapper->insert($assignment);
|
||||
$this->getImportSystem()->updateCardAssignment($cardId, $assignmentId, $assignment);
|
||||
foreach ($assignments as $assignment) {
|
||||
try {
|
||||
$assignment = $this->assignmentMapper->insert($assignment);
|
||||
$this->getImportSystem()->updateCardAssignment($cardId, (string)$assignment->getId(), $assignment);
|
||||
$this->addOutput('Assignment ' . $assignment->getParticipant() . ' added');
|
||||
} catch (NotFoundException $e) {
|
||||
$this->addError('No origin or mapping found for card "' . $cardId . '" and ' . $assignment->getTypeString() .' assignment "' . $assignment->getParticipant(), $e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user