2024-02-20 17:14:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
|
|
use App\Models\GithubApp;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\Middleware\WithoutOverlapping;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
|
2024-06-10 20:43:34 +00:00
|
|
|
class GithubAppPermissionJob implements ShouldBeEncrypted, ShouldQueue
|
2024-02-20 17:14:47 +00:00
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
|
|
public $tries = 4;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-20 17:14:47 +00:00
|
|
|
public function backoff(): int
|
|
|
|
{
|
|
|
|
return isDev() ? 1 : 3;
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-20 17:14:47 +00:00
|
|
|
public function __construct(public GithubApp $github_app)
|
|
|
|
{
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-20 17:14:47 +00:00
|
|
|
public function middleware(): array
|
|
|
|
{
|
|
|
|
return [(new WithoutOverlapping($this->github_app->uuid))];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function uniqueId(): int
|
|
|
|
{
|
|
|
|
return $this->github_app->uuid;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$github_access_token = generate_github_jwt_token($this->github_app);
|
|
|
|
$response = Http::withHeaders([
|
|
|
|
'Authorization' => "Bearer $github_access_token",
|
2024-06-10 20:43:34 +00:00
|
|
|
'Accept' => 'application/vnd.github+json',
|
2024-02-20 17:14:47 +00:00
|
|
|
])->get("{$this->github_app->api_url}/app");
|
|
|
|
$response = $response->json();
|
|
|
|
$permissions = data_get($response, 'permissions');
|
|
|
|
$this->github_app->contents = data_get($permissions, 'contents');
|
|
|
|
$this->github_app->metadata = data_get($permissions, 'metadata');
|
|
|
|
$this->github_app->pull_requests = data_get($permissions, 'pull_requests');
|
|
|
|
$this->github_app->administration = data_get($permissions, 'administration');
|
|
|
|
$this->github_app->save();
|
|
|
|
$this->github_app->makeVisible('client_secret')->makeVisible('webhook_secret');
|
|
|
|
} catch (\Throwable $e) {
|
2024-06-10 20:43:34 +00:00
|
|
|
send_internal_notification('GithubAppPermissionJob failed with: '.$e->getMessage());
|
2024-02-20 17:14:47 +00:00
|
|
|
ray($e->getMessage());
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|