-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathPluginWebhookController.php
More file actions
72 lines (55 loc) · 2.11 KB
/
PluginWebhookController.php
File metadata and controls
72 lines (55 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
namespace App\Http\Controllers;
use App\Jobs\SyncPluginReleases;
use App\Models\Plugin;
use App\Services\PluginSyncService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class PluginWebhookController extends Controller
{
public function __invoke(Request $request, string $secret, PluginSyncService $syncService): JsonResponse
{
$plugin = Plugin::where('webhook_secret', $secret)->first();
if (! $plugin) {
return response()->json(['error' => 'Invalid webhook secret'], 404);
}
$event = $request->header('X-GitHub-Event');
if ($event === 'ping') {
return response()->json(['success' => true, 'message' => 'pong']);
}
if (! $plugin->isApproved()) {
return response()->json(['error' => 'Plugin is not approved'], 403);
}
if ($event === 'release') {
// Sync plugin metadata to update latest_version
$syncService->sync($plugin);
// Queue release sync for version records
dispatch(new SyncPluginReleases($plugin));
return response()->json([
'success' => true,
'message' => 'Release sync queued',
'synced_at' => $plugin->fresh()->last_synced_at->toIso8601String(),
]);
}
if ($event === 'push') {
$synced = $syncService->sync($plugin);
if (! $synced) {
return response()->json(['error' => 'Failed to sync plugin'], 500);
}
return response()->json([
'success' => true,
'synced_at' => $plugin->fresh()->last_synced_at->toIso8601String(),
]);
}
$synced = $syncService->sync($plugin);
if (! $synced) {
return response()->json(['error' => 'Failed to sync plugin'], 500);
}
dispatch(new SyncPluginReleases($plugin));
return response()->json([
'success' => true,
'synced_at' => $plugin->fresh()->last_synced_at->toIso8601String(),
'releases_sync' => 'queued',
]);
}
}