Skip to content

Commit f15d2e5

Browse files
Merge pull request #101 from TheDragonCode/1.x
Added search for saved authorization token
2 parents 1b1fec3 + d694910 commit f15d2e5

File tree

4 files changed

+88
-5
lines changed

4 files changed

+88
-5
lines changed

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff 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)
3737
for 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

4454
To 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

app/Commands/ReadCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use DragonCode\GithubNotifications\Factories\ClientFactory;
66
use DragonCode\GithubNotifications\Services\GitHub;
77
use DragonCode\GithubNotifications\Services\Output;
8+
use DragonCode\GithubNotifications\Services\Token;
89
use Github\ResultPager;
910
use Illuminate\Support\Str;
1011
use 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
}

app/Services/Process.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}

app/Services/Token.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}

0 commit comments

Comments
 (0)