Skip to content

Commit 45521d2

Browse files
committed
fix githook deletion
1 parent 7a94e28 commit 45521d2

3 files changed

Lines changed: 18 additions & 6 deletions

File tree

app/Http/Controllers/API/GitHookController.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
namespace App\Http\Controllers\API;
44

55
use App\Actions\Site\Deploy;
6-
use App\Exceptions\SourceControlIsNotConnected;
7-
use App\Facades\Notifier;
6+
use App\Exceptions\FailedToDestroyGitHook;
87
use App\Http\Controllers\Controller;
98
use App\Models\GitHook;
109
use App\Models\ServerLog;
1110
use App\Models\SourceControl;
12-
use App\Notifications\SourceControlDisconnected;
1311
use Illuminate\Http\JsonResponse;
1412
use Illuminate\Http\Request;
1513
use Illuminate\Support\Facades\Log;
@@ -18,6 +16,9 @@
1816

1917
class GitHookController extends Controller
2018
{
19+
/**
20+
* @throws FailedToDestroyGitHook
21+
*/
2122
#[Any('api/git-hooks', name: 'api.git-hooks')]
2223
public function __invoke(Request $request): JsonResponse
2324
{
@@ -30,15 +31,21 @@ public function __invoke(Request $request): JsonResponse
3031
->where('secret', $request->input('secret'))
3132
->firstOrFail();
3233

34+
if (! $gitHook->site) {
35+
$gitHook->destroyHook();
36+
37+
return response()->json([
38+
'success' => true,
39+
]);
40+
}
41+
3342
foreach ($gitHook->actions as $action) {
3443
/** @var SourceControl $sourceControl */
3544
$sourceControl = $gitHook->site->sourceControl;
3645
$webhookBranch = $sourceControl->provider()->getWebhookBranch($request->array());
3746
if ($action == 'deploy' && $gitHook->site->branch === $webhookBranch) {
3847
try {
3948
app(Deploy::class)->run($gitHook->site);
40-
} catch (SourceControlIsNotConnected) {
41-
Notifier::send($gitHook->sourceControl, new SourceControlDisconnected($gitHook->sourceControl));
4249
} catch (Throwable $e) {
4350
ServerLog::log(
4451
$gitHook->site->server,

app/Models/GitHook.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* @property array<string, mixed> $actions
1616
* @property string $hook_id
1717
* @property array<string, mixed> $hook_response
18-
* @property Site $site
18+
* @property ?Site $site
1919
* @property SourceControl $sourceControl
2020
*/
2121
class GitHook extends AbstractModel

app/Models/Site.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ public static function boot(): void
118118
$site->ssls()->delete();
119119
$site->deployments()->delete();
120120
$site->deploymentScript()->delete();
121+
try {
122+
$site->gitHook?->destroyHook();
123+
} catch (FailedToDestroyGitHook) {
124+
$site->refresh()->gitHook?->delete();
125+
}
121126
});
122127

123128
static::created(function (Site $site): void {

0 commit comments

Comments
 (0)