From 51cb05634d9e991a94f155d366257b502a4f68dc Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 2 Apr 2024 13:58:54 +0300 Subject: [PATCH 1/2] Added the ability to exclude repositories from processing --- README.md | 31 +++++++++++---- app/Commands/ReadCommand.php | 73 ++++++++++++++++++++++++------------ app/Services/GitHub.php | 24 +++++++++--- 3 files changed, 90 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 1a52b5f..09a3bda 100644 --- a/README.md +++ b/README.md @@ -85,24 +85,25 @@ By default, only those Issues and Pull Requests that have been closed or merged But you can define the parameters yourself: ```Bash --i, --except-issues Exclude issues from processing --p, --except-pulls Exclude Pull Requests from processing --m, --except-mentions Exclude notifications with your mention from processing --o, --with-open Process including open Issues and Pull Requests --n, --no-interaction Do not ask any interactive question --q, --quiet Do not output any message +-r, --except-repository Exclude repositories from processing +-i, --except-issues Exclude issues from processing +-p, --except-pulls Exclude Pull Requests from processing +-m, --except-mentions Exclude notifications with your mention from processing +-o, --with-open Process including open Issues and Pull Requests +-n, --no-interaction Do not ask any interactive question +-q, --quiet Do not output any message ``` For example: ```Bash # except issues + with open -notifications read qwerty -ion +notifications read laravel -ion ``` With this set of options, notifications that have: -- whose repository name begins with the word `qwerty` +- whose repository name begins with the word `laravel` - Pull Requests only, both open and closed - will not be asked to continue in the console @@ -112,6 +113,20 @@ With this set of options, notifications that have: > notifications read --help > ``` +You can also exclude certain repositories: + +```Bash +notifications read laravel -ion -r laravel/framework -r laravel/breeze +``` + +With this set of options, notifications that have: + +- whose repository name begins with the word `laravel` +- Pull Requests only, both open and closed +- will not be asked to continue in the console +- repositories `laravel/framework` and `laravel/breeze` will not be processed + + ## Result ### Before diff --git a/app/Commands/ReadCommand.php b/app/Commands/ReadCommand.php index c2772e1..9020d42 100644 --- a/app/Commands/ReadCommand.php +++ b/app/Commands/ReadCommand.php @@ -14,37 +14,41 @@ class ReadCommand extends Command { protected $signature = 'read' - . ' {repository?* : Full or partial repository names}' - . ' {--i|except-issues : Exclude issues from processing}' - . ' {--p|except-pulls : Exclude Pull Requests from processing}' - . ' {--m|except-mentions : Exclude notifications with your mention from processing}' - . ' {--o|with-open : Process including open Issues and Pull Requests}' - . ' {--token= : Specifies the token to use}'; + . ' {repository?* : Full or partial repository names}' + . ' {--r|except-repository=* : Exclude repositories from processing}' + . ' {--i|except-issues : Exclude issues from processing}' + . ' {--p|except-pulls : Exclude Pull Requests from processing}' + . ' {--m|except-mentions : Exclude notifications with your mention from processing}' + . ' {--o|with-open : Process including open Issues and Pull Requests}' + . ' {--token= : Specifies the token to use}'; protected $description = 'Marks as read all notifications based on specified conditions'; public function handle(): void { - $repositories = $this->repositories(); + $include = $this->repositories(); + $except = $this->exceptRepositories(); - $this->welcome($repositories); + $this->welcome($include, $except); if ($this->hasContinue()) { - $this->read($repositories); + $this->read($include); } } - protected function welcome(array $repositories): void + protected function welcome(array $repositories, ?array $exceptRepositories): void { if ($repositories) { - Output::info('You specified the following repository name masks:'); - - $this->components->bulletList($repositories); + $this->bulletList('You specified the following repository name masks:', $repositories); + } - return; + if ($exceptRepositories) { + $this->bulletList('You specified the following masks to exclude repositories:', $exceptRepositories); } - Output::info('Mark as read all notifications except open ones'); + if (!$repositories && !$exceptRepositories) { + Output::info('Mark as read all notifications except open ones'); + } } protected function hasContinue(): bool @@ -56,6 +60,7 @@ protected function read(array $repositories): void { $this->gitHub() ->repositories($repositories) + ->exceptRepositories($this->exceptRepositories()) ->exceptIssues($this->exceptIssues()) ->exceptPulls($this->exceptPulls()) ->exceptMentions($this->exceptMentions()) @@ -70,9 +75,10 @@ protected function read(array $repositories): void protected function shouldBeAll(array $repositories): bool { return empty($repositories) - && ! $this->exceptIssues() - && ! $this->exceptPulls() - && ! $this->exceptMentions() + && !$this->exceptRepositories() + && !$this->exceptIssues() + && !$this->exceptPulls() + && !$this->exceptMentions() && $this->withOpen(); } @@ -81,19 +87,20 @@ protected function gitHub(): GitHub $client = ClientFactory::make($this->token()); return app(GitHub::class, [ - 'output' => $this->components, - 'github' => $client, + 'output' => $this->components, + 'github' => $client, 'paginator' => new ResultPager($client), ]); } protected function repositories(): array { - return collect($this->argument('repository')) - ->filter() - ->unique() - ->sort() - ->all(); + return $this->argument('repository'); + } + + protected function exceptRepositories(): ?array + { + return array_filter($this->option('except-repository')) ?: null; } protected function exceptIssues(): bool @@ -116,6 +123,22 @@ protected function withOpen(): bool return $this->option('with-open'); } + protected function bulletList(string $title, array $values): void + { + Output::info($title); + + $this->components->bulletList($this->sort($values)); + } + + protected function sort(array $values): array + { + return collect($values) + ->filter() + ->unique() + ->sort() + ->all(); + } + protected function token(): string { if ($token = $this->detectToken()) { diff --git a/app/Services/GitHub.php b/app/Services/GitHub.php index a80279d..3a97f5c 100644 --- a/app/Services/GitHub.php +++ b/app/Services/GitHub.php @@ -17,6 +17,8 @@ class GitHub { protected array $repositories = []; + protected ?array $exceptRepositories = null; + protected bool $exceptIssues = false; protected bool $exceptPulls = false; @@ -33,7 +35,8 @@ public function __construct( protected Factory $output, protected Client $github, protected ResultPager $paginator, - ) {} + ) { + } public function repositories(array $repositories): self { @@ -42,6 +45,13 @@ public function repositories(array $repositories): self return $this; } + public function exceptRepositories(?array $except): self + { + $this->exceptRepositories = $except; + + return $this; + } + public function exceptIssues(bool $except): self { $this->exceptIssues = $except; @@ -84,7 +94,7 @@ public function markAll(): void public function mark(): void { - if (! $items = $this->paginated()) { + if (!$items = $this->paginated()) { Output::success('No unread notifications'); return; @@ -130,9 +140,9 @@ protected function notifications(): Notification protected function requestByType(NotificationData $notification): ?array { return match ($notification->type) { - 'Issue' => $this->issue($notification), + 'Issue' => $this->issue($notification), 'PullRequest' => $this->pullRequest($notification), - default => null + default => null }; } @@ -156,7 +166,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::startsWith($notification->fullName, $this->repositories)) { + return true; + } + + if ($this->exceptRepositories && Str::startsWith($notification->fullName, $this->exceptRepositories)) { return true; } From 3df85349a0995a493afadcedd92eff01d74ce6b5 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 2 Apr 2024 13:59:34 +0300 Subject: [PATCH 2/2] Fixed code-style --- app/Commands/ReadCommand.php | 30 +++++++++++++++--------------- app/Services/GitHub.php | 11 +++++------ 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/app/Commands/ReadCommand.php b/app/Commands/ReadCommand.php index 9020d42..90caad7 100644 --- a/app/Commands/ReadCommand.php +++ b/app/Commands/ReadCommand.php @@ -14,20 +14,20 @@ class ReadCommand extends Command { protected $signature = 'read' - . ' {repository?* : Full or partial repository names}' - . ' {--r|except-repository=* : Exclude repositories from processing}' - . ' {--i|except-issues : Exclude issues from processing}' - . ' {--p|except-pulls : Exclude Pull Requests from processing}' - . ' {--m|except-mentions : Exclude notifications with your mention from processing}' - . ' {--o|with-open : Process including open Issues and Pull Requests}' - . ' {--token= : Specifies the token to use}'; + . ' {repository?* : Full or partial repository names}' + . ' {--r|except-repository=* : Exclude repositories from processing}' + . ' {--i|except-issues : Exclude issues from processing}' + . ' {--p|except-pulls : Exclude Pull Requests from processing}' + . ' {--m|except-mentions : Exclude notifications with your mention from processing}' + . ' {--o|with-open : Process including open Issues and Pull Requests}' + . ' {--token= : Specifies the token to use}'; protected $description = 'Marks as read all notifications based on specified conditions'; public function handle(): void { $include = $this->repositories(); - $except = $this->exceptRepositories(); + $except = $this->exceptRepositories(); $this->welcome($include, $except); @@ -46,7 +46,7 @@ protected function welcome(array $repositories, ?array $exceptRepositories): voi $this->bulletList('You specified the following masks to exclude repositories:', $exceptRepositories); } - if (!$repositories && !$exceptRepositories) { + if (! $repositories && ! $exceptRepositories) { Output::info('Mark as read all notifications except open ones'); } } @@ -75,10 +75,10 @@ protected function read(array $repositories): void protected function shouldBeAll(array $repositories): bool { return empty($repositories) - && !$this->exceptRepositories() - && !$this->exceptIssues() - && !$this->exceptPulls() - && !$this->exceptMentions() + && ! $this->exceptRepositories() + && ! $this->exceptIssues() + && ! $this->exceptPulls() + && ! $this->exceptMentions() && $this->withOpen(); } @@ -87,8 +87,8 @@ protected function gitHub(): GitHub $client = ClientFactory::make($this->token()); return app(GitHub::class, [ - 'output' => $this->components, - 'github' => $client, + 'output' => $this->components, + 'github' => $client, 'paginator' => new ResultPager($client), ]); } diff --git a/app/Services/GitHub.php b/app/Services/GitHub.php index 3a97f5c..ab7479c 100644 --- a/app/Services/GitHub.php +++ b/app/Services/GitHub.php @@ -35,8 +35,7 @@ public function __construct( protected Factory $output, protected Client $github, protected ResultPager $paginator, - ) { - } + ) {} public function repositories(array $repositories): self { @@ -94,7 +93,7 @@ public function markAll(): void public function mark(): void { - if (!$items = $this->paginated()) { + if (! $items = $this->paginated()) { Output::success('No unread notifications'); return; @@ -140,9 +139,9 @@ protected function notifications(): Notification protected function requestByType(NotificationData $notification): ?array { return match ($notification->type) { - 'Issue' => $this->issue($notification), + 'Issue' => $this->issue($notification), 'PullRequest' => $this->pullRequest($notification), - default => null + default => null }; } @@ -166,7 +165,7 @@ 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::startsWith($notification->fullName, $this->repositories)) { return true; }