File tree Expand file tree Collapse file tree 4 files changed +88
-5
lines changed
Expand file tree Collapse file tree 4 files changed +88
-5
lines changed Original file line number Diff line number Diff line change @@ -36,10 +36,20 @@ You'll also need to create yourself a
3636[ personal access token] ( https://github.com/settings/tokens/new?description=Notifications%20Reader )
3737for GitHub's API with access to the ` notifications ` scope.
3838
39- ## Usage
39+ By default, we check several places for the presence of a token in the following order:
40+
41+ 1 . The ` token ` parameter passed when calling the console command
42+ 2 . The ` GITHUB_TOKEN ` environment variable
43+ 3 . ` ~/.composer/auth.json ` file
44+ 4 . ` ~/.config/.composer/auth.json ` file
45+ 5 . ` ~/.config/composer/auth.json ` file
46+ 6 . ` ~/AppData/Roaming/Composer/auth.json ` file
47+ 7 . ` ~/composer/auth.json ` file
48+ 8 . ` %USERPROFILE%/AppData/Roaming/Composer/auth.json ` file
4049
41- By default, we'll try and read your personal access token for GitHub from the ` GITHUB_TOKEN ` environment variable,
42- however you can also specify a token with the ` --token ` command-line flag.
50+ If the token is not found, you will receive a message about this.
51+
52+ ## Usage
4353
4454To read all issue notifications:
4555
@@ -132,7 +142,6 @@ With this set of options, notifications that have:
132142- will not be asked to continue in the console
133143- repositories ` laravel/framework ` and ` laravel/breeze ` will not be processed
134144
135-
136145## Result
137146
138147### Before
Original file line number Diff line number Diff line change 55use DragonCode \GithubNotifications \Factories \ClientFactory ;
66use DragonCode \GithubNotifications \Services \GitHub ;
77use DragonCode \GithubNotifications \Services \Output ;
8+ use DragonCode \GithubNotifications \Services \Token ;
89use Github \ResultPager ;
910use Illuminate \Support \Str ;
1011use LaravelZero \Framework \Commands \Command ;
@@ -152,6 +153,6 @@ protected function token(): string
152153
153154 protected function detectToken (): ?string
154155 {
155- return $ this ->option ('token ' ) ?: ( $ _SERVER [ ' GITHUB_TOKEN ' ] ?? null );
156+ return $ this ->option ('token ' ) ?: Token:: detect ( );
156157 }
157158}
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace DragonCode \GithubNotifications \Services ;
6+
7+ use Illuminate \Support \Facades \Process as BaseProcess ;
8+
9+ class Process
10+ {
11+ public static function run (string $ command ): ?array
12+ {
13+ $ result = BaseProcess::run ($ command );
14+
15+ if ($ result ->failed () || ! static ::validateJson ($ result ->output ())) {
16+ return null ;
17+ }
18+
19+ return json_decode ($ result ->output (), true );
20+ }
21+
22+ protected static function validateJson (string $ json ): bool
23+ {
24+ json_decode ($ json );
25+
26+ return json_last_error () === JSON_ERROR_NONE ;
27+ }
28+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace DragonCode \GithubNotifications \Services ;
6+
7+ class Token
8+ {
9+ public static function detect (): ?string
10+ {
11+ return static ::fromServer () ?? static ::fromComposer ();
12+ }
13+
14+ protected static function fromServer (): ?string
15+ {
16+ return $ _SERVER ['GITHUB_TOKEN ' ] ?? null ;
17+ }
18+
19+ protected static function fromComposer (): ?string
20+ {
21+ foreach (static ::composerPath () as $ path ) {
22+ if (! $ data = Process::run ('cat ' . $ path )) {
23+ continue ;
24+ }
25+
26+ if ($ token = $ data ['github-oauth ' ]['github.com ' ] ?? null ) {
27+ return $ token ;
28+ }
29+ }
30+
31+ return null ;
32+ }
33+
34+ protected static function composerPath (): array
35+ {
36+ return [
37+ '~/.composer/auth.json ' ,
38+ '~/.config/.composer/auth.json ' ,
39+ '~/.config/composer/auth.json ' ,
40+ '~/AppData/Roaming/Composer/auth.json ' ,
41+ '~/composer/auth.json ' ,
42+ '%USERPROFILE%/AppData/Roaming/Composer/auth.json ' ,
43+ ];
44+ }
45+ }
You can’t perform that action at this time.
0 commit comments