44
55use App \Models \User ;
66use Illuminate \Console \Command ;
7+
78class GitHubTokenCommand extends Command
89{
910 protected $ signature = 'github:token
@@ -16,9 +17,10 @@ class GitHubTokenCommand extends Command
1617 public function handle (): int
1718 {
1819 $ user = User::first ();
19-
20- if (!$ user ) {
20+
21+ if (! $ user ) {
2122 $ this ->error ('No user found in database. Please create a user first. ' );
23+
2224 return 1 ;
2325 }
2426
@@ -43,21 +45,22 @@ private function checkToken(User $user): int
4345 // Check environment variable first (like monorepo v2 does)
4446 $ token = env ('GITHUB_TOKEN ' );
4547 $ source = 'environment ' ;
46-
48+
4749 // Fallback to user model
48- if (!$ token ) {
50+ if (! $ token ) {
4951 $ token = $ user ->github_token ;
5052 $ source = 'user model ' ;
5153 }
52-
53- if (!$ token ) {
54+
55+ if (! $ token ) {
5456 $ this ->warn ('No GitHub token found. ' );
5557 $ this ->showInstructions ();
58+
5659 return 1 ;
5760 }
5861
59- $ this ->info ("GitHub token found in {$ source }: " . substr ($ token , 0 , 10 ) . '... ' );
60-
62+ $ this ->info ("GitHub token found in {$ source }: " . substr ($ token , 0 , 10 ). '... ' );
63+
6164 // Test the token
6265 try {
6366 $ response = \Illuminate \Support \Facades \Http::withHeaders ([
@@ -68,20 +71,20 @@ private function checkToken(User $user): int
6871 if ($ response ->successful ()) {
6972 $ userData = $ response ->json ();
7073 $ this ->info ("✅ Token is valid for user: {$ userData ['login ' ]}" );
71-
74+
7275 // Check scopes
7376 $ scopes = $ response ->header ('X-OAuth-Scopes ' );
7477 $ this ->line ("📋 Available scopes: {$ scopes }" );
75-
78+
7679 $ availableScopes = explode (', ' , $ scopes ?? '' );
77-
80+
7881 // Check required scopes (with equivalent higher-level scopes)
7982 $ scopeChecks = [
8083 'repo ' => ['repo ' ],
8184 'read:org ' => ['read:org ' , 'admin:org ' ], // admin:org includes read:org
82- 'workflow ' => ['workflow ' ]
85+ 'workflow ' => ['workflow ' ],
8386 ];
84-
87+
8588 foreach ($ scopeChecks as $ required => $ acceptable ) {
8689 $ hasScope = false ;
8790 foreach ($ acceptable as $ scope ) {
@@ -90,7 +93,7 @@ private function checkToken(User $user): int
9093 break ;
9194 }
9295 }
93-
96+
9497 if ($ hasScope ) {
9598 $ this ->info ("✅ {$ required }" );
9699 } else {
@@ -102,10 +105,12 @@ private function checkToken(User $user): int
102105 } else {
103106 $ this ->error ('❌ Token is invalid or expired. ' );
104107 $ this ->showInstructions ();
108+
105109 return 1 ;
106110 }
107111 } catch (\Exception $ e ) {
108112 $ this ->error ("❌ Failed to validate token: {$ e ->getMessage ()}" );
113+
109114 return 1 ;
110115 }
111116 }
@@ -118,6 +123,7 @@ private function clearToken(User $user): int
118123 ]);
119124
120125 $ this ->info ('✅ GitHub token cleared. ' );
126+
121127 return 0 ;
122128 }
123129
@@ -126,24 +132,25 @@ private function showTokenInfo(User $user): int
126132 // Check environment variable first
127133 $ token = env ('GITHUB_TOKEN ' );
128134 $ source = 'environment ' ;
129-
135+
130136 // Fallback to user model
131- if (!$ token ) {
137+ if (! $ token ) {
132138 $ token = $ user ->github_token ;
133139 $ source = 'user model ' ;
134140 }
135-
136- if (!$ token ) {
141+
142+ if (! $ token ) {
137143 $ this ->warn ('No GitHub token found. ' );
144+
138145 return 1 ;
139146 }
140147
141148 $ this ->info ('GitHub Token Information: ' );
142149 $ this ->line ("Source: {$ source }" );
143150 $ this ->line ("User ID: {$ user ->id }" );
144151 $ this ->line ("GitHub ID: {$ user ->github_id }" );
145- $ this ->line (" Token: " . substr ($ token , 0 , 10 ) . '... ' . substr ($ token , -4 ));
146- $ this ->line (" Token Type: " . (str_starts_with ($ token , 'gho_ ' ) ? 'OAuth Token ' : 'Personal Access Token ' ));
152+ $ this ->line (' Token: ' . substr ($ token , 0 , 10 ). '... ' . substr ($ token , -4 ));
153+ $ this ->line (' Token Type: ' . (str_starts_with ($ token , 'gho_ ' ) ? 'OAuth Token ' : 'Personal Access Token ' ));
147154
148155 return 0 ;
149156 }
@@ -156,16 +163,16 @@ private function showStatus(User $user): int
156163 // Check environment variable first
157164 $ token = env ('GITHUB_TOKEN ' );
158165 $ source = 'environment ' ;
159-
166+
160167 // Fallback to user model
161- if (!$ token ) {
168+ if (! $ token ) {
162169 $ token = $ user ->github_token ;
163170 $ source = 'user model ' ;
164171 }
165-
172+
166173 if ($ token ) {
167174 $ this ->info ("✅ Token found in {$ source } for user: {$ user ->name }" );
168- $ this ->line (" Token: " . substr ($ token , 0 , 10 ) . '... ' );
175+ $ this ->line (' Token: ' . substr ($ token , 0 , 10 ). '... ' );
169176 $ this ->line ('' );
170177 $ this ->line ('Use --check to validate the token ' );
171178 } else {
@@ -196,4 +203,4 @@ private function showInstructions(): void
196203 $ this ->line (' php artisan github:token --info # Show token details ' );
197204 $ this ->line (' php artisan github:token --clear # Remove token ' );
198205 }
199- }
206+ }
0 commit comments