Skip to content

Commit 51cb056

Browse files
Added the ability to exclude repositories from processing
1 parent ace67a7 commit 51cb056

File tree

3 files changed

+90
-38
lines changed

3 files changed

+90
-38
lines changed

README.md

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,24 +85,25 @@ By default, only those Issues and Pull Requests that have been closed or merged
8585
But you can define the parameters yourself:
8686

8787
```Bash
88-
-i, --except-issues Exclude issues from processing
89-
-p, --except-pulls Exclude Pull Requests from processing
90-
-m, --except-mentions Exclude notifications with your mention from processing
91-
-o, --with-open Process including open Issues and Pull Requests
92-
-n, --no-interaction Do not ask any interactive question
93-
-q, --quiet Do not output any message
88+
-r, --except-repository Exclude repositories from processing
89+
-i, --except-issues Exclude issues from processing
90+
-p, --except-pulls Exclude Pull Requests from processing
91+
-m, --except-mentions Exclude notifications with your mention from processing
92+
-o, --with-open Process including open Issues and Pull Requests
93+
-n, --no-interaction Do not ask any interactive question
94+
-q, --quiet Do not output any message
9495
```
9596

9697
For example:
9798

9899
```Bash
99100
# except issues + with open
100-
notifications read qwerty -ion
101+
notifications read laravel -ion
101102
```
102103

103104
With this set of options, notifications that have:
104105

105-
- whose repository name begins with the word `qwerty`
106+
- whose repository name begins with the word `laravel`
106107
- Pull Requests only, both open and closed
107108
- will not be asked to continue in the console
108109

@@ -112,6 +113,20 @@ With this set of options, notifications that have:
112113
> notifications read --help
113114
> ```
114115
116+
You can also exclude certain repositories:
117+
118+
```Bash
119+
notifications read laravel -ion -r laravel/framework -r laravel/breeze
120+
```
121+
122+
With this set of options, notifications that have:
123+
124+
- whose repository name begins with the word `laravel`
125+
- Pull Requests only, both open and closed
126+
- will not be asked to continue in the console
127+
- repositories `laravel/framework` and `laravel/breeze` will not be processed
128+
129+
115130
## Result
116131

117132
### Before

app/Commands/ReadCommand.php

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,41 @@
1414
class ReadCommand extends Command
1515
{
1616
protected $signature = 'read'
17-
. ' {repository?* : Full or partial repository names}'
18-
. ' {--i|except-issues : Exclude issues from processing}'
19-
. ' {--p|except-pulls : Exclude Pull Requests from processing}'
20-
. ' {--m|except-mentions : Exclude notifications with your mention from processing}'
21-
. ' {--o|with-open : Process including open Issues and Pull Requests}'
22-
. ' {--token= : Specifies the token to use}';
17+
. ' {repository?* : Full or partial repository names}'
18+
. ' {--r|except-repository=* : Exclude repositories from processing}'
19+
. ' {--i|except-issues : Exclude issues from processing}'
20+
. ' {--p|except-pulls : Exclude Pull Requests from processing}'
21+
. ' {--m|except-mentions : Exclude notifications with your mention from processing}'
22+
. ' {--o|with-open : Process including open Issues and Pull Requests}'
23+
. ' {--token= : Specifies the token to use}';
2324

2425
protected $description = 'Marks as read all notifications based on specified conditions';
2526

2627
public function handle(): void
2728
{
28-
$repositories = $this->repositories();
29+
$include = $this->repositories();
30+
$except = $this->exceptRepositories();
2931

30-
$this->welcome($repositories);
32+
$this->welcome($include, $except);
3133

3234
if ($this->hasContinue()) {
33-
$this->read($repositories);
35+
$this->read($include);
3436
}
3537
}
3638

37-
protected function welcome(array $repositories): void
39+
protected function welcome(array $repositories, ?array $exceptRepositories): void
3840
{
3941
if ($repositories) {
40-
Output::info('You specified the following repository name masks:');
41-
42-
$this->components->bulletList($repositories);
42+
$this->bulletList('You specified the following repository name masks:', $repositories);
43+
}
4344

44-
return;
45+
if ($exceptRepositories) {
46+
$this->bulletList('You specified the following masks to exclude repositories:', $exceptRepositories);
4547
}
4648

47-
Output::info('Mark as read all notifications except open ones');
49+
if (!$repositories && !$exceptRepositories) {
50+
Output::info('Mark as read all notifications except open ones');
51+
}
4852
}
4953

5054
protected function hasContinue(): bool
@@ -56,6 +60,7 @@ protected function read(array $repositories): void
5660
{
5761
$this->gitHub()
5862
->repositories($repositories)
63+
->exceptRepositories($this->exceptRepositories())
5964
->exceptIssues($this->exceptIssues())
6065
->exceptPulls($this->exceptPulls())
6166
->exceptMentions($this->exceptMentions())
@@ -70,9 +75,10 @@ protected function read(array $repositories): void
7075
protected function shouldBeAll(array $repositories): bool
7176
{
7277
return empty($repositories)
73-
&& ! $this->exceptIssues()
74-
&& ! $this->exceptPulls()
75-
&& ! $this->exceptMentions()
78+
&& !$this->exceptRepositories()
79+
&& !$this->exceptIssues()
80+
&& !$this->exceptPulls()
81+
&& !$this->exceptMentions()
7682
&& $this->withOpen();
7783
}
7884

@@ -81,19 +87,20 @@ protected function gitHub(): GitHub
8187
$client = ClientFactory::make($this->token());
8288

8389
return app(GitHub::class, [
84-
'output' => $this->components,
85-
'github' => $client,
90+
'output' => $this->components,
91+
'github' => $client,
8692
'paginator' => new ResultPager($client),
8793
]);
8894
}
8995

9096
protected function repositories(): array
9197
{
92-
return collect($this->argument('repository'))
93-
->filter()
94-
->unique()
95-
->sort()
96-
->all();
98+
return $this->argument('repository');
99+
}
100+
101+
protected function exceptRepositories(): ?array
102+
{
103+
return array_filter($this->option('except-repository')) ?: null;
97104
}
98105

99106
protected function exceptIssues(): bool
@@ -116,6 +123,22 @@ protected function withOpen(): bool
116123
return $this->option('with-open');
117124
}
118125

126+
protected function bulletList(string $title, array $values): void
127+
{
128+
Output::info($title);
129+
130+
$this->components->bulletList($this->sort($values));
131+
}
132+
133+
protected function sort(array $values): array
134+
{
135+
return collect($values)
136+
->filter()
137+
->unique()
138+
->sort()
139+
->all();
140+
}
141+
119142
protected function token(): string
120143
{
121144
if ($token = $this->detectToken()) {

app/Services/GitHub.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class GitHub
1717
{
1818
protected array $repositories = [];
1919

20+
protected ?array $exceptRepositories = null;
21+
2022
protected bool $exceptIssues = false;
2123

2224
protected bool $exceptPulls = false;
@@ -33,7 +35,8 @@ public function __construct(
3335
protected Factory $output,
3436
protected Client $github,
3537
protected ResultPager $paginator,
36-
) {}
38+
) {
39+
}
3740

3841
public function repositories(array $repositories): self
3942
{
@@ -42,6 +45,13 @@ public function repositories(array $repositories): self
4245
return $this;
4346
}
4447

48+
public function exceptRepositories(?array $except): self
49+
{
50+
$this->exceptRepositories = $except;
51+
52+
return $this;
53+
}
54+
4555
public function exceptIssues(bool $except): self
4656
{
4757
$this->exceptIssues = $except;
@@ -84,7 +94,7 @@ public function markAll(): void
8494

8595
public function mark(): void
8696
{
87-
if (! $items = $this->paginated()) {
97+
if (!$items = $this->paginated()) {
8898
Output::success('No unread notifications');
8999

90100
return;
@@ -130,9 +140,9 @@ protected function notifications(): Notification
130140
protected function requestByType(NotificationData $notification): ?array
131141
{
132142
return match ($notification->type) {
133-
'Issue' => $this->issue($notification),
143+
'Issue' => $this->issue($notification),
134144
'PullRequest' => $this->pullRequest($notification),
135-
default => null
145+
default => null
136146
};
137147
}
138148

@@ -156,7 +166,11 @@ protected function pullRequest(NotificationData $notification): array
156166

157167
protected function shouldSkip(NotificationData $notification, ItemData $item): bool
158168
{
159-
if ($this->repositories && ! Str::startsWith($notification->fullName, $this->repositories)) {
169+
if ($this->repositories && !Str::startsWith($notification->fullName, $this->repositories)) {
170+
return true;
171+
}
172+
173+
if ($this->exceptRepositories && Str::startsWith($notification->fullName, $this->exceptRepositories)) {
160174
return true;
161175
}
162176

0 commit comments

Comments
 (0)