Skip to content

Commit

Permalink
feat(systemtags): allow setting color with occ
Browse files Browse the repository at this point in the history
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Dec 6, 2024
1 parent de0281f commit e6f983d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
6 changes: 6 additions & 0 deletions apps/dav/lib/SystemTag/SystemTagNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use OCP\SystemTag\ISystemTagObjectMapper;
use OCP\SystemTag\TagAlreadyExistsException;
use OCP\SystemTag\TagNotFoundException;
use Sabre\DAV\Exception\BadRequest;
use Sabre\DAV\Exception\Conflict;
use Sabre\DAV\Exception\Forbidden;
use Sabre\DAV\Exception\MethodNotAllowed;
Expand Down Expand Up @@ -111,6 +112,11 @@ public function update($name, $userVisible, $userAssignable, $color): void {
}
}

// Make sure color is a proper hex
if ($color !== null && (strlen($color) !== 6 || !ctype_xdigit($color))) {
throw new BadRequest('Color must be a 6-digit hexadecimal value');
}

$this->tagManager->updateTag($this->tag->getId(), $name, $userVisible, $userAssignable, $color);
} catch (TagNotFoundException $e) {
throw new NotFound('Tag with id ' . $this->tag->getId() . ' does not exist');
Expand Down
25 changes: 23 additions & 2 deletions core/Command/SystemTag/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ protected function configure() {
null,
InputOption::VALUE_OPTIONAL,
'sets the access control level (public, restricted, invisible)',
)
->addOption(
'color',
null,
InputOption::VALUE_OPTIONAL,
'set the tag color',
);
}

Expand Down Expand Up @@ -80,9 +86,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}

$color = $tag->getColor();
if ($input->hasOption('color')) {
$color = $input->getOption('color');
if (substr($color, 0, 1) === '#') {
$color = substr($color, 1);
}

if ($input->getOption('color') === '') {
$color = null;
} elseif (strlen($color) !== 6 || !ctype_xdigit($color)) {
$output->writeln('<error>Color must be a 6-digit hexadecimal value</error>');
return 2;
}
}

try {
$this->systemTagManager->updateTag($input->getArgument('id'), $name, $userVisible, $userAssignable);
$output->writeln('<info>Tag updated ("' . $name . '", ' . $userVisible . ', ' . $userAssignable . ')</info>');
$this->systemTagManager->updateTag($input->getArgument('id'), $name, $userVisible, $userAssignable, $color);
$output->writeln('<info>Tag updated ("' . $name . '", ' . json_encode($userVisible) . ', ' . json_encode($userAssignable) . ', "' . ($color ? "#$color" : '') . '")</info>');
return 0;
} catch (TagNotFoundException $e) {
$output->writeln('<error>Tag not found</error>');
Expand Down
8 changes: 5 additions & 3 deletions tests/Core/Command/SystemTag/EditTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,14 @@ public function testExecute(): void {
$tagId,
$newTagName,
$newTagUserVisible,
$newTagUserAssignable
$newTagUserAssignable,
''
);

$this->output->expects($this->once())
->method('writeln')
->with(
'<info>Tag updated ("' . $newTagName . '", ' . $newTagUserVisible . ', ' . $newTagUserAssignable . ')</info>'
'<info>Tag updated ("' . $newTagName . '", ' . json_encode($newTagUserVisible) . ', ' . json_encode($newTagUserAssignable) . ', "")</info>'
);

$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
Expand Down Expand Up @@ -145,7 +146,8 @@ public function testAlreadyExists(): void {
$tagId,
$newTagName,
$newTagUserVisible,
$newTagUserAssignable
$newTagUserAssignable,
''
);

$this->output->expects($this->once())
Expand Down

0 comments on commit e6f983d

Please sign in to comment.