Skip to content

Commit 14c5ac2

Browse files
committed
begin ticketing system
1 parent eef95f9 commit 14c5ac2

4 files changed

Lines changed: 100 additions & 32 deletions

File tree

app/Console/Commands/Support/GmailAuthorizeCommand.php

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Console\Commands\Support;
44

5+
use App\Services\Support\Gmail\GmailOAuthConfig;
56
use Google\Client as GoogleClient;
67
use Google\Service\Gmail as GmailService;
78
use Illuminate\Console\Command;
@@ -18,22 +19,22 @@ class GmailAuthorizeCommand extends Command
1819

1920
public function handle(): int
2021
{
21-
$credentialsPath = (string) ($this->option('credentials') ?: config('support_gmail.credentials_json'));
22-
$tokenPath = (string) ($this->option('token') ?: config('support_gmail.token_json'));
23-
24-
if (!$credentialsPath || !is_file($credentialsPath)) {
25-
$this->error('OAuth credentials JSON not found. Set SUPPORT_GMAIL_CREDENTIALS_JSON or pass --credentials=');
26-
return self::FAILURE;
27-
}
28-
if (!$tokenPath) {
29-
$this->error('Token path not set. Set SUPPORT_GMAIL_TOKEN_JSON or pass --token=');
30-
return self::FAILURE;
31-
}
22+
$credentialsPath = $this->option('credentials');
23+
$tokenPath = $this->option('token') ?: config('support_gmail.token_json');
3224

3325
$client = new GoogleClient();
3426
$client->setApplicationName('Codeweek Internal Support Copilot');
3527
$client->setScopes([GmailService::GMAIL_READONLY]);
36-
$client->setAuthConfig($credentialsPath);
28+
if ($credentialsPath) {
29+
if (!is_file((string) $credentialsPath)) {
30+
$this->error('OAuth credentials file not found: '.$credentialsPath);
31+
32+
return self::FAILURE;
33+
}
34+
$client->setAuthConfig((string) $credentialsPath);
35+
} else {
36+
GmailOAuthConfig::applyClientSecrets($client);
37+
}
3738
$client->setAccessType('offline');
3839
$client->setPrompt('consent');
3940

@@ -62,12 +63,16 @@ public function handle(): int
6263
return self::FAILURE;
6364
}
6465

65-
// Ensure folder exists and write token json.
66-
File::ensureDirectoryExists(dirname($tokenPath));
67-
File::put($tokenPath, json_encode($token, JSON_PRETTY_PRINT));
68-
@chmod($tokenPath, 0600);
66+
if ($tokenPath) {
67+
File::ensureDirectoryExists(dirname((string) $tokenPath));
68+
File::put((string) $tokenPath, json_encode($token, JSON_PRETTY_PRINT));
69+
@chmod((string) $tokenPath, 0600);
70+
$this->info('Token saved to '.$tokenPath);
71+
} else {
72+
$this->warn('No SUPPORT_GMAIL_TOKEN_JSON / --token path — paste this JSON into Forge as SUPPORT_GMAIL_TOKEN:');
73+
$this->line(json_encode($token, JSON_PRETTY_PRINT));
74+
}
6975

70-
$this->info('Token saved to '.$tokenPath);
7176
$this->line('Next: set SUPPORT_GMAIL_ENABLED=true and run `php artisan support:gmail:poll`.');
7277

7378
return self::SUCCESS;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace App\Services\Support\Gmail;
4+
5+
use Google\Client as GoogleClient;
6+
7+
/**
8+
* Loads OAuth client credentials and tokens from env (inline JSON) or disk paths.
9+
* Use SUPPORT_GMAIL_CREDENTIALS + SUPPORT_GMAIL_TOKEN on Forge so deploys do not rely on gitignored files.
10+
*/
11+
final class GmailOAuthConfig
12+
{
13+
public static function applyClientSecrets(GoogleClient $client): void
14+
{
15+
$inline = config('support_gmail.credentials');
16+
if (self::nonEmptyString($inline)) {
17+
$decoded = json_decode($inline, true);
18+
if (!is_array($decoded)) {
19+
throw new \RuntimeException('SUPPORT_GMAIL_CREDENTIALS must be valid JSON (Google OAuth client secret JSON).');
20+
}
21+
$client->setAuthConfig($decoded);
22+
23+
return;
24+
}
25+
26+
$path = config('support_gmail.credentials_json');
27+
if ($path && is_file($path)) {
28+
$client->setAuthConfig($path);
29+
30+
return;
31+
}
32+
33+
if ($path) {
34+
throw new \RuntimeException(
35+
'Gmail OAuth credentials file not found: '.$path.'. Set SUPPORT_GMAIL_CREDENTIALS (paste client JSON in env) or upload the file to that path.'
36+
);
37+
}
38+
39+
throw new \RuntimeException(
40+
'Gmail OAuth credentials missing. Set SUPPORT_GMAIL_CREDENTIALS (client JSON) or SUPPORT_GMAIL_CREDENTIALS_JSON (path to client JSON).'
41+
);
42+
}
43+
44+
public static function applyAccessToken(GoogleClient $client): void
45+
{
46+
$inline = config('support_gmail.token');
47+
if (self::nonEmptyString($inline)) {
48+
$decoded = json_decode($inline, true);
49+
if (!is_array($decoded)) {
50+
throw new \RuntimeException('SUPPORT_GMAIL_TOKEN must be valid JSON.');
51+
}
52+
$client->setAccessToken($decoded);
53+
54+
return;
55+
}
56+
57+
$tokenJson = config('support_gmail.token_json');
58+
if ($tokenJson && is_file($tokenJson)) {
59+
$client->setAccessToken(json_decode((string) file_get_contents($tokenJson), true));
60+
}
61+
}
62+
63+
private static function nonEmptyString(mixed $v): bool
64+
{
65+
return is_string($v) && trim($v) !== '';
66+
}
67+
}

app/Services/Support/Gmail/GoogleGmailConnector.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,8 @@ public function __construct()
2020
$client->setScopes([GmailService::GMAIL_READONLY]);
2121
$client->setAccessType('offline');
2222

23-
$credentials = config('support_gmail.credentials_json');
24-
if (!$credentials) {
25-
throw new \RuntimeException('SUPPORT_GMAIL_CREDENTIALS_JSON not set');
26-
}
27-
28-
$client->setAuthConfig($credentials);
29-
30-
// Optional OAuth token json (installed-app flows).
31-
$tokenJson = config('support_gmail.token_json');
32-
if ($tokenJson && is_file($tokenJson)) {
33-
$client->setAccessToken(json_decode((string) file_get_contents($tokenJson), true));
34-
}
23+
GmailOAuthConfig::applyClientSecrets($client);
24+
GmailOAuthConfig::applyAccessToken($client);
3525

3626
$this->client = $client;
3727
$this->gmail = new GmailService($client);
@@ -123,7 +113,7 @@ private function ensureValidToken(): void
123113
{
124114
$token = $this->client->getAccessToken();
125115
if (empty($token)) {
126-
throw new \RuntimeException('Gmail token missing. Run support:gmail:authorize and set SUPPORT_GMAIL_TOKEN_JSON.');
116+
throw new \RuntimeException('Gmail token missing. Run support:gmail:authorize and set SUPPORT_GMAIL_TOKEN or SUPPORT_GMAIL_TOKEN_JSON.');
127117
}
128118

129119
if (!$this->client->isAccessTokenExpired()) {

config/support_gmail.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@
1919
// Example: 'is:unread newer_than:7d -category:promotions'
2020
'query' => env('SUPPORT_GMAIL_QUERY', 'newer_than:7d'),
2121

22-
// Google service account or OAuth client credentials JSON path.
22+
// Google OAuth client JSON: paste full JSON from Google Cloud (preferred on Forge; survives deploys).
23+
'credentials' => env('SUPPORT_GMAIL_CREDENTIALS'),
24+
25+
// Alternative: path to the same JSON on disk (e.g. storage/app/google/support-gmail-credentials.json).
2326
'credentials_json' => env('SUPPORT_GMAIL_CREDENTIALS_JSON', null),
2427

25-
// Token JSON path for OAuth installed-app flows (if used).
28+
// OAuth token JSON: paste token from support:gmail:authorize (preferred on Forge).
29+
'token' => env('SUPPORT_GMAIL_TOKEN'),
30+
31+
// Alternative: path to token JSON (e.g. storage/app/google/support-gmail-token.json).
2632
'token_json' => env('SUPPORT_GMAIL_TOKEN_JSON', null),
2733

2834
// When true, mark ingested messages as read and/or apply a label.

0 commit comments

Comments
 (0)