From aa86f1213bd9b6d2a5bdf4dcad8b2feb267c7e6b Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 2 Apr 2024 14:20:01 +0300 Subject: [PATCH] Improved filtering of repositories by pattern --- README.md | 12 +++++++++--- app/Commands/ReadCommand.php | 20 +++++++++++--------- app/Services/GitHub.php | 8 ++++---- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 09a3bda..0e76526 100644 --- a/README.md +++ b/README.md @@ -59,16 +59,22 @@ Or, if you are specifying a token: notifications read laravel --token {...} ``` -In addition to the organization, you can also specify the full or partial name of the repository. For example: +In addition, you can use any part of the organization name and/or repository name to check against the template: ```Bash notifications read laravel/framework # or -notifications read laravel/fra +notifications read lara*/fra # or -notifications read la +notifications read framework +# or +notifications read work +# or +notifications read fra*rk ``` +Pattern matching is implemented using the [`Str::is`](https://laravel.com/docs/strings#method-str-is) method. + You can also specify several names: ```Bash diff --git a/app/Commands/ReadCommand.php b/app/Commands/ReadCommand.php index 90caad7..883a90a 100644 --- a/app/Commands/ReadCommand.php +++ b/app/Commands/ReadCommand.php @@ -6,6 +6,7 @@ use DragonCode\GithubNotifications\Services\GitHub; use DragonCode\GithubNotifications\Services\Output; use Github\ResultPager; +use Illuminate\Support\Str; use LaravelZero\Framework\Commands\Command; use Symfony\Component\Console\Exception\InvalidOptionException; @@ -36,17 +37,17 @@ public function handle(): void } } - protected function welcome(array $repositories, ?array $exceptRepositories): void + protected function welcome(array $includeRepositories, ?array $exceptRepositories): void { - if ($repositories) { - $this->bulletList('You specified the following repository name masks:', $repositories); + if ($includeRepositories) { + $this->bulletList('You specified the following repository name masks:', $includeRepositories); } if ($exceptRepositories) { $this->bulletList('You specified the following masks to exclude repositories:', $exceptRepositories); } - if (! $repositories && ! $exceptRepositories) { + if (! $includeRepositories && ! $exceptRepositories) { Output::info('Mark as read all notifications except open ones'); } } @@ -95,12 +96,12 @@ protected function gitHub(): GitHub protected function repositories(): array { - return $this->argument('repository'); + return $this->resolvePattern($this->argument('repository')); } - protected function exceptRepositories(): ?array + protected function exceptRepositories(): array { - return array_filter($this->option('except-repository')) ?: null; + return $this->resolvePattern($this->option('except-repository')); } protected function exceptIssues(): bool @@ -127,14 +128,15 @@ protected function bulletList(string $title, array $values): void { Output::info($title); - $this->components->bulletList($this->sort($values)); + $this->components->bulletList($values); } - protected function sort(array $values): array + protected function resolvePattern(?array $values): array { return collect($values) ->filter() ->unique() + ->map(fn (string $value) => Str::of($value)->trim()->start('*')->finish('*')->toString()) ->sort() ->all(); } diff --git a/app/Services/GitHub.php b/app/Services/GitHub.php index ab7479c..be3570f 100644 --- a/app/Services/GitHub.php +++ b/app/Services/GitHub.php @@ -17,7 +17,7 @@ class GitHub { protected array $repositories = []; - protected ?array $exceptRepositories = null; + protected array $exceptRepositories = []; protected bool $exceptIssues = false; @@ -44,7 +44,7 @@ public function repositories(array $repositories): self return $this; } - public function exceptRepositories(?array $except): self + public function exceptRepositories(array $except): self { $this->exceptRepositories = $except; @@ -165,11 +165,11 @@ protected function pullRequest(NotificationData $notification): array protected function shouldSkip(NotificationData $notification, ItemData $item): bool { - if ($this->repositories && ! Str::startsWith($notification->fullName, $this->repositories)) { + if ($this->repositories && ! Str::is($this->repositories, $notification->fullName)) { return true; } - if ($this->exceptRepositories && Str::startsWith($notification->fullName, $this->exceptRepositories)) { + if ($this->exceptRepositories && Str::is($this->exceptRepositories, $notification->fullName)) { return true; }