2023-05-24 12:26:50 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use App\Models\GithubApp;
|
2023-05-30 13:52:17 +00:00
|
|
|
use App\Models\GitlabApp;
|
2023-06-13 13:01:11 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2023-05-24 12:26:50 +00:00
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
use Lcobucci\JWT\Encoding\ChainedFormatter;
|
|
|
|
use Lcobucci\JWT\Encoding\JoseEncoder;
|
|
|
|
use Lcobucci\JWT\Signer\Key\InMemory;
|
|
|
|
use Lcobucci\JWT\Signer\Rsa\Sha256;
|
|
|
|
use Lcobucci\JWT\Token\Builder;
|
|
|
|
|
|
|
|
function generate_github_installation_token(GithubApp $source)
|
|
|
|
{
|
|
|
|
$signingKey = InMemory::plainText($source->privateKey->private_key);
|
|
|
|
$algorithm = new Sha256();
|
|
|
|
$tokenBuilder = (new Builder(new JoseEncoder(), ChainedFormatter::default()));
|
|
|
|
$now = new DateTimeImmutable();
|
|
|
|
$now = $now->setTime($now->format('H'), $now->format('i'));
|
|
|
|
$issuedToken = $tokenBuilder
|
|
|
|
->issuedBy($source->app_id)
|
|
|
|
->issuedAt($now)
|
|
|
|
->expiresAt($now->modify('+10 minutes'))
|
|
|
|
->getToken($algorithm, $signingKey)
|
|
|
|
->toString();
|
|
|
|
$token = Http::withHeaders([
|
|
|
|
'Authorization' => "Bearer $issuedToken",
|
|
|
|
'Accept' => 'application/vnd.github.machine-man-preview+json'
|
|
|
|
])->post("{$source->api_url}/app/installations/{$source->installation_id}/access_tokens");
|
|
|
|
if ($token->failed()) {
|
|
|
|
throw new \Exception("Failed to get access token for " . $source->name . " with error: " . $token->json()['message']);
|
|
|
|
}
|
|
|
|
return $token->json()['token'];
|
|
|
|
}
|
|
|
|
function generate_github_jwt_token(GithubApp $source)
|
|
|
|
{
|
|
|
|
$signingKey = InMemory::plainText($source->privateKey->private_key);
|
|
|
|
$algorithm = new Sha256();
|
|
|
|
$tokenBuilder = (new Builder(new JoseEncoder(), ChainedFormatter::default()));
|
|
|
|
$now = new DateTimeImmutable();
|
|
|
|
$now = $now->setTime($now->format('H'), $now->format('i'));
|
|
|
|
$issuedToken = $tokenBuilder
|
|
|
|
->issuedBy($source->app_id)
|
|
|
|
->issuedAt($now->modify('-1 minute'))
|
|
|
|
->expiresAt($now->modify('+10 minutes'))
|
|
|
|
->getToken($algorithm, $signingKey)
|
|
|
|
->toString();
|
|
|
|
return $issuedToken;
|
|
|
|
}
|
2023-05-30 13:52:17 +00:00
|
|
|
|
2023-06-13 13:01:11 +00:00
|
|
|
function git_api(GithubApp|GitlabApp $source, string $endpoint, string $method = 'get', array|null $data = null, bool $throwError = true)
|
2023-05-30 13:52:17 +00:00
|
|
|
{
|
|
|
|
if ($source->getMorphClass() == 'App\Models\GithubApp') {
|
|
|
|
if ($source->is_public) {
|
2023-06-13 13:01:11 +00:00
|
|
|
$response = Http::github($source->api_url)->$method($endpoint);
|
|
|
|
} else {
|
|
|
|
$github_access_token = generate_github_installation_token($source);
|
|
|
|
if ($data && ($method === 'post' || $method === 'patch' || $method === 'put')) {
|
|
|
|
$response = Http::github($source->api_url, $github_access_token)->$method($endpoint, $data);
|
|
|
|
} else {
|
|
|
|
$response = Http::github($source->api_url, $github_access_token)->$method($endpoint);
|
|
|
|
}
|
2023-05-30 13:52:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$json = $response->json();
|
2023-06-13 13:01:11 +00:00
|
|
|
if ($response->failed() && $throwError) {
|
2023-05-30 13:52:17 +00:00
|
|
|
throw new \Exception("Failed to get data from {$source->name} with error: " . $json['message']);
|
|
|
|
}
|
|
|
|
return [
|
|
|
|
'rate_limit_remaining' => $response->header('X-RateLimit-Remaining'),
|
|
|
|
'data' => collect($json)
|
|
|
|
];
|
|
|
|
}
|