Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion app/Commands/ReadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
28 changes: 28 additions & 0 deletions app/Services/Process.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace DragonCode\GithubNotifications\Services;

use Illuminate\Support\Facades\Process as BaseProcess;

class Process
{
public static function run(string $command): ?array
{
$result = BaseProcess::run($command);

if ($result->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;
}
}
45 changes: 45 additions & 0 deletions app/Services/Token.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace DragonCode\GithubNotifications\Services;

class Token
{
public static function detect(): ?string
{
return static::fromServer() ?? static::fromComposer();
}

protected static function fromServer(): ?string
{
return $_SERVER['GITHUB_TOKEN'] ?? null;
}

protected static function fromComposer(): ?string
{
foreach (static::composerPath() as $path) {
if (! $data = Process::run('cat ' . $path)) {
continue;
}

if ($token = $data['github-oauth']['github.com'] ?? null) {
return $token;
}
}

return null;
}

protected static function composerPath(): array
{
return [
'~/.composer/auth.json',
'~/.config/.composer/auth.json',
'~/.config/composer/auth.json',
'~/AppData/Roaming/Composer/auth.json',
'~/composer/auth.json',
'%USERPROFILE%/AppData/Roaming/Composer/auth.json',
];
}
}