From d694910822fe9699b47071f28a59d4c510b4170a Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 23 May 2024 00:16:32 +0300 Subject: [PATCH] Added search for saved authorization token --- README.md | 17 ++++++++++---- app/Commands/ReadCommand.php | 3 ++- app/Services/Process.php | 28 ++++++++++++++++++++++ app/Services/Token.php | 45 ++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 app/Services/Process.php create mode 100644 app/Services/Token.php diff --git a/README.md b/README.md index 4ea9b9e..2f30e95 100644 --- a/README.md +++ b/README.md @@ -36,10 +36,20 @@ You'll also need to create yourself a [personal access token](https://github.com/settings/tokens/new?description=Notifications%20Reader) for GitHub's API with access to the `notifications` scope. -## Usage +By default, we check several places for the presence of a token in the following order: + +1. The `token` parameter passed when calling the console command +2. The `GITHUB_TOKEN` environment variable +3. `~/.composer/auth.json` file +4. `~/.config/.composer/auth.json` file +5. `~/.config/composer/auth.json` file +6. `~/AppData/Roaming/Composer/auth.json` file +7. `~/composer/auth.json` file +8. `%USERPROFILE%/AppData/Roaming/Composer/auth.json` file -By default, we'll try and read your personal access token for GitHub from the `GITHUB_TOKEN` environment variable, -however you can also specify a token with the `--token` command-line flag. +If the token is not found, you will receive a message about this. + +## Usage To read all issue notifications: @@ -132,7 +142,6 @@ With this set of options, notifications that have: - 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 883a90a..ec48d36 100644 --- a/app/Commands/ReadCommand.php +++ b/app/Commands/ReadCommand.php @@ -5,6 +5,7 @@ use DragonCode\GithubNotifications\Factories\ClientFactory; use DragonCode\GithubNotifications\Services\GitHub; use DragonCode\GithubNotifications\Services\Output; +use DragonCode\GithubNotifications\Services\Token; use Github\ResultPager; use Illuminate\Support\Str; use LaravelZero\Framework\Commands\Command; @@ -152,6 +153,6 @@ protected function token(): string protected function detectToken(): ?string { - return $this->option('token') ?: ($_SERVER['GITHUB_TOKEN'] ?? null); + return $this->option('token') ?: Token::detect(); } } diff --git a/app/Services/Process.php b/app/Services/Process.php new file mode 100644 index 0000000..9253eea --- /dev/null +++ b/app/Services/Process.php @@ -0,0 +1,28 @@ +failed() || ! static::validateJson($result->output())) { + return null; + } + + return json_decode($result->output(), true); + } + + protected static function validateJson(string $json): bool + { + json_decode($json); + + return json_last_error() === JSON_ERROR_NONE; + } +} diff --git a/app/Services/Token.php b/app/Services/Token.php new file mode 100644 index 0000000..5e46c8f --- /dev/null +++ b/app/Services/Token.php @@ -0,0 +1,45 @@ +