Skip to content

Commit

Permalink
fix unhandled source control exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
saeedvaziry committed Nov 14, 2024
1 parent 1dfd135 commit 264913f
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 91 deletions.
4 changes: 4 additions & 0 deletions app/Models/GitHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Exceptions\FailedToDestroyGitHook;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

Expand Down Expand Up @@ -55,6 +56,9 @@ public function deployHook(): void
);
}

/**
* @throws FailedToDestroyGitHook
*/
public function destroyHook(): void
{
$this->sourceControl->provider()->destroyHook($this->site->repository, $this->hook_id);
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use App\Enums\SiteStatus;
use App\Exceptions\FailedToDestroyGitHook;
use App\Exceptions\SourceControlIsNotConnected;
use App\Exceptions\SSHError;
use App\SiteTypes\SiteType;
Expand Down Expand Up @@ -252,6 +253,7 @@ public function enableAutoDeployment(): void

/**
* @throws SourceControlIsNotConnected
* @throws FailedToDestroyGitHook
*/
public function disableAutoDeployment(): void
{
Expand Down
4 changes: 2 additions & 2 deletions app/SiteTypes/AbstractSiteType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\SiteTypes;

use App\Exceptions\SourceControlIsNotConnected;
use App\Exceptions\FailedToDeployGitKey;
use App\Models\Site;

abstract class AbstractSiteType implements SiteType
Expand All @@ -21,7 +21,7 @@ protected function progress(int $percentage): void
}

/**
* @throws SourceControlIsNotConnected
* @throws FailedToDeployGitKey
*/
protected function deployKey(): void
{
Expand Down
4 changes: 2 additions & 2 deletions app/SiteTypes/PHPSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\SiteTypes;

use App\Enums\SiteFeature;
use App\Exceptions\SourceControlIsNotConnected;
use App\Exceptions\FailedToDeployGitKey;
use App\SSH\Composer\Composer;
use App\SSH\Git\Git;
use App\SSH\Services\Webserver\Webserver;
Expand Down Expand Up @@ -72,7 +72,7 @@ public function data(array $input): array
}

/**
* @throws SourceControlIsNotConnected
* @throws FailedToDeployGitKey
*/
public function install(): void
{
Expand Down
60 changes: 38 additions & 22 deletions app/SourceControlProviders/Bitbucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ public function data(): array

public function connect(): bool
{
$res = Http::withHeaders($this->getAuthenticationHeaders())
->get($this->apiUrl.'/repositories');
try {
$res = Http::withHeaders($this->getAuthenticationHeaders())
->get($this->apiUrl.'/repositories');
} catch (Exception) {
return false;
}

return $res->successful();
}
Expand Down Expand Up @@ -68,18 +72,22 @@ public function fullRepoUrl(string $repo, string $key): string
*/
public function deployHook(string $repo, array $events, string $secret): array
{
$response = Http::withHeaders($this->getAuthenticationHeaders())
->post($this->apiUrl."/repositories/$repo/hooks", [
'description' => 'deploy',
'url' => url('/api/git-hooks?secret='.$secret),
'events' => [
'repo:'.implode(',', $events),
],
'active' => true,
]);
try {
$response = Http::withHeaders($this->getAuthenticationHeaders())
->post($this->apiUrl."/repositories/$repo/hooks", [
'description' => 'deploy',
'url' => url('/api/git-hooks?secret='.$secret),
'events' => [
'repo:'.implode(',', $events),
],
'active' => true,
]);
} catch (Exception $e) {
throw new FailedToDeployGitHook($e->getMessage());
}

if ($response->status() != 201) {
throw new FailedToDeployGitHook($response->json()['error']['message']);
throw new FailedToDeployGitHook($response->body());
}

return [
Expand All @@ -94,11 +102,15 @@ public function deployHook(string $repo, array $events, string $secret): array
public function destroyHook(string $repo, string $hookId): void
{
$hookId = urlencode($hookId);
$response = Http::withHeaders($this->getAuthenticationHeaders())
->delete($this->apiUrl."/repositories/$repo/hooks/$hookId");
try {
$response = Http::withHeaders($this->getAuthenticationHeaders())
->delete($this->apiUrl."/repositories/$repo/hooks/$hookId");
} catch (Exception $e) {
throw new FailedToDestroyGitHook($e->getMessage());
}

if ($response->status() != 204) {
throw new FailedToDestroyGitHook('Error');
throw new FailedToDestroyGitHook($response->body());
}
}

Expand Down Expand Up @@ -134,13 +146,17 @@ public function getLastCommit(string $repo, string $branch): ?array
*/
public function deployKey(string $title, string $repo, string $key): void
{
$res = Http::withHeaders($this->getAuthenticationHeaders())->post(
$this->apiUrl."/repositories/$repo/deploy-keys",
[
'label' => $title,
'key' => $key,
]
);
try {
$res = Http::withHeaders($this->getAuthenticationHeaders())->post(
$this->apiUrl."/repositories/$repo/deploy-keys",
[
'label' => $title,
'key' => $key,
]
);
} catch (Exception $e) {
throw new FailedToDeployGitKey($e->getMessage());
}

if ($res->status() != 200) {
throw new FailedToDeployGitKey($res->json()['error']['message']);
Expand Down
78 changes: 47 additions & 31 deletions app/SourceControlProviders/Github.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ class Github extends AbstractSourceControlProvider

public function connect(): bool
{
$res = Http::withHeaders([
'Accept' => 'application/vnd.github.v3+json',
'Authorization' => 'Bearer '.$this->data()['token'],
])->get($this->apiUrl.'/user/repos');
try {
$res = Http::withHeaders([
'Accept' => 'application/vnd.github.v3+json',
'Authorization' => 'Bearer '.$this->data()['token'],
])->get($this->apiUrl.'/user/repos');
} catch (Exception) {
return false;
}

return $res->successful();
}
Expand Down Expand Up @@ -52,21 +56,25 @@ public function fullRepoUrl(string $repo, string $key): string
*/
public function deployHook(string $repo, array $events, string $secret): array
{
$response = Http::withHeaders([
'Accept' => 'application/vnd.github.v3+json',
'Authorization' => 'Bearer '.$this->data()['token'],
])->post($this->apiUrl."/repos/$repo/hooks", [
'name' => 'web',
'events' => $events,
'config' => [
'url' => url('/api/git-hooks?secret='.$secret),
'content_type' => 'json',
],
'active' => true,
]);
try {
$response = Http::withHeaders([
'Accept' => 'application/vnd.github.v3+json',
'Authorization' => 'Bearer '.$this->data()['token'],
])->post($this->apiUrl."/repos/$repo/hooks", [
'name' => 'web',
'events' => $events,
'config' => [
'url' => url('/api/git-hooks?secret='.$secret),
'content_type' => 'json',
],
'active' => true,
]);
} catch (Exception $e) {
throw new FailedToDeployGitHook($e->getMessage());
}

if ($response->status() != 201) {
throw new FailedToDeployGitHook(json_decode($response->body())->message);
throw new FailedToDeployGitHook($response->body());
}

return [
Expand All @@ -80,13 +88,17 @@ public function deployHook(string $repo, array $events, string $secret): array
*/
public function destroyHook(string $repo, string $hookId): void
{
$response = Http::withHeaders([
'Accept' => 'application/vnd.github.v3+json',
'Authorization' => 'Bearer '.$this->data()['token'],
])->delete($this->apiUrl."/repos/$repo/hooks/$hookId");
try {
$response = Http::withHeaders([
'Accept' => 'application/vnd.github.v3+json',
'Authorization' => 'Bearer '.$this->data()['token'],
])->delete($this->apiUrl."/repos/$repo/hooks/$hookId");
} catch (Exception $e) {
throw new FailedToDestroyGitHook($e->getMessage());
}

if ($response->status() != 204) {
throw new FailedToDestroyGitHook(json_decode($response->body())->message);
throw new FailedToDestroyGitHook($response->body());
}
}

Expand Down Expand Up @@ -124,17 +136,21 @@ public function getLastCommit(string $repo, string $branch): ?array
*/
public function deployKey(string $title, string $repo, string $key): void
{
$response = Http::withToken($this->data()['token'])->post(
$this->apiUrl.'/repos/'.$repo.'/keys',
[
'title' => $title,
'key' => $key,
'read_only' => false,
]
);
try {
$response = Http::withToken($this->data()['token'])->post(
$this->apiUrl.'/repos/'.$repo.'/keys',
[
'title' => $title,
'key' => $key,
'read_only' => false,
]
);
} catch (Exception $e) {
throw new FailedToDeployGitKey($e->getMessage());
}

if ($response->status() != 201) {
throw new FailedToDeployGitKey(json_decode($response->body())->message);
throw new FailedToDeployGitKey($response->body());
}
}
}
84 changes: 50 additions & 34 deletions app/SourceControlProviders/Gitlab.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ public function createRules(array $input): array

public function connect(): bool
{
$res = Http::withToken($this->data()['token'])
->get($this->getApiUrl().'/projects');
try {
$res = Http::withToken($this->data()['token'])
->get($this->getApiUrl().'/projects');
} catch (Exception) {
return false;
}

return $res->successful();
}
Expand Down Expand Up @@ -61,27 +65,31 @@ public function fullRepoUrl(string $repo, string $key): string
public function deployHook(string $repo, array $events, string $secret): array
{
$repository = urlencode($repo);
$response = Http::withToken($this->data()['token'])->post(
$this->getApiUrl().'/projects/'.$repository.'/hooks',
[
'description' => 'deploy',
'url' => url('/api/git-hooks?secret='.$secret),
'push_events' => in_array('push', $events),
'issues_events' => false,
'job_events' => false,
'merge_requests_events' => false,
'note_events' => false,
'pipeline_events' => false,
'tag_push_events' => false,
'wiki_page_events' => false,
'deployment_events' => false,
'confidential_note_events' => false,
'confidential_issues_events' => false,
]
);
try {
$response = Http::withToken($this->data()['token'])->post(
$this->getApiUrl().'/projects/'.$repository.'/hooks',
[
'description' => 'deploy',
'url' => url('/api/git-hooks?secret='.$secret),
'push_events' => in_array('push', $events),
'issues_events' => false,
'job_events' => false,
'merge_requests_events' => false,
'note_events' => false,
'pipeline_events' => false,
'tag_push_events' => false,
'wiki_page_events' => false,
'deployment_events' => false,
'confidential_note_events' => false,
'confidential_issues_events' => false,
]
);
} catch (Exception $e) {
throw new FailedToDeployGitHook($e->getMessage());
}

if ($response->status() != 201) {
throw new FailedToDeployGitHook(json_decode($response->body())->message);
throw new FailedToDeployGitHook($response->body());
}

return [
Expand All @@ -96,12 +104,16 @@ public function deployHook(string $repo, array $events, string $secret): array
public function destroyHook(string $repo, string $hookId): void
{
$repository = urlencode($repo);
$response = Http::withToken($this->data()['token'])->delete(
$this->getApiUrl().'/projects/'.$repository.'/hooks/'.$hookId
);
try {
$response = Http::withToken($this->data()['token'])->delete(
$this->getApiUrl().'/projects/'.$repository.'/hooks/'.$hookId
);
} catch (Exception $e) {
throw new FailedToDestroyGitHook($e->getMessage());
}

if ($response->status() != 204) {
throw new FailedToDestroyGitHook(json_decode($response->body())->message);
throw new FailedToDestroyGitHook($response->body());
}
}

Expand Down Expand Up @@ -138,17 +150,21 @@ public function getLastCommit(string $repo, string $branch): ?array
public function deployKey(string $title, string $repo, string $key): void
{
$repository = urlencode($repo);
$response = Http::withToken($this->data()['token'])->post(
$this->getApiUrl().'/projects/'.$repository.'/deploy_keys',
[
'title' => $title,
'key' => $key,
'can_push' => true,
]
);
try {
$response = Http::withToken($this->data()['token'])->post(
$this->getApiUrl().'/projects/'.$repository.'/deploy_keys',
[
'title' => $title,
'key' => $key,
'can_push' => true,
]
);
} catch (Exception $e) {
throw new FailedToDeployGitKey($e->getMessage());
}

if ($response->status() != 201) {
throw new FailedToDeployGitKey(json_decode($response->body())->message);
throw new FailedToDeployGitKey($response->body());
}
}

Expand Down
Loading

0 comments on commit 264913f

Please sign in to comment.