Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options to create a deploy-key #111

Merged
merged 23 commits into from
Oct 27, 2024
Merged
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Handle the github API error messages
mehrancodes committed Oct 27, 2024
commit e3de55596608fc080c874bc6530517d5d727021c
42 changes: 39 additions & 3 deletions app/Services/Github/GithubService.php
Original file line number Diff line number Diff line change
@@ -14,8 +14,9 @@
namespace App\Services\Github;

use App\Services\Forge\ForgeSetting;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Laravel\Forge\Exceptions\ValidationException;
use Illuminate\Validation\ValidationException;

class GithubService
{
@@ -43,7 +44,9 @@ public function putCommentOnGithubPullRequest(string $body): array
'Authorization' => sprintf('Bearer %s', $this->setting->gitToken),
])->post($uri, ['body' => $body]);

throw_if($result->failed(), ValidationException::class, [$result->body()]);
if ($result->failed()) {
$this->handleApiErrors($result, 'Comment');
}

return json_decode($result->body(), true);
}
@@ -65,8 +68,41 @@ public function createDeployKey(string $title, string $key, bool $readonly = tru
'readonly' => $readonly,
]);

throw_if($result->failed() && ! in_array('key is already in use', $result->json('errors.*.message', [])), ValidationException::class, [$result->body()]);
if ($result->failed()) {
$this->handleApiErrors($result, 'Deploy key');
}

return json_decode($result->body(), true);
}

protected function handleApiErrors(Response $response, $apiName): void
{
// Extract all error messages from the response
$errorMessages = $response->json('errors.*.message', []);

// Check if the specific error 'key is already in use' exists, used when creating a deploy key
$isDeployKeyInUse = in_array('key is already in use', $errorMessages);

if ($isDeployKeyInUse) {
throw ValidationException::withMessages([
'forbidden' => ['The deploy key is already in use.'],
]);
}

// Handle other specific status codes if needed
throw match ($response->status()) {
404 => ValidationException::withMessages([
'not_found' => ["{$apiName} could not be found."],
]),
401 => ValidationException::withMessages([
'authorization' => ['Unauthorized. Please check your GitHub token.'],
]),
403 => ValidationException::withMessages([
'forbidden' => ['Forbidden. You might not have the necessary permissions.'],
]),
default => ValidationException::withMessages([
'api_error' => ['An unexpected error occurred: ' . $response->body()],
]),
};
}
}