diff --git a/README.md b/README.md index 0165a77..22d1579 100644 --- a/README.md +++ b/README.md @@ -1,59 +1,123 @@ -

Laravel Logo

- -

-Build Status -Total Downloads -Latest Stable Version -License -

- -## About Laravel - -Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel takes the pain out of development by easing common tasks used in many web projects, such as: - -- [Simple, fast routing engine](https://laravel.com/docs/routing). -- [Powerful dependency injection container](https://laravel.com/docs/container). -- Multiple back-ends for [session](https://laravel.com/docs/session) and [cache](https://laravel.com/docs/cache) storage. -- Expressive, intuitive [database ORM](https://laravel.com/docs/eloquent). -- Database agnostic [schema migrations](https://laravel.com/docs/migrations). -- [Robust background job processing](https://laravel.com/docs/queues). -- [Real-time event broadcasting](https://laravel.com/docs/broadcasting). - -Laravel is accessible, powerful, and provides tools required for large, robust applications. - -## Learning Laravel - -Laravel has the most extensive and thorough [documentation](https://laravel.com/docs) and video tutorial library of all modern web application frameworks, making it a breeze to get started with the framework. You can also check out [Laravel Learn](https://laravel.com/learn), where you will be guided through building a modern Laravel application. - -If you don't feel like reading, [Laracasts](https://laracasts.com) can help. Laracasts contains thousands of video tutorials on a range of topics including Laravel, modern PHP, unit testing, and JavaScript. Boost your skills by digging into our comprehensive video library. - -## Laravel Sponsors - -We would like to extend our thanks to the following sponsors for funding Laravel development. If you are interested in becoming a sponsor, please visit the [Laravel Partners program](https://partners.laravel.com). - -### Premium Partners - -- **[Vehikl](https://vehikl.com)** -- **[Tighten Co.](https://tighten.co)** -- **[Kirschbaum Development Group](https://kirschbaumdevelopment.com)** -- **[64 Robots](https://64robots.com)** -- **[Curotec](https://www.curotec.com/services/technologies/laravel)** -- **[DevSquad](https://devsquad.com/hire-laravel-developers)** -- **[Redberry](https://redberry.international/laravel-development)** -- **[Active Logic](https://activelogic.com)** - -## Contributing - -Thank you for considering contributing to the Laravel framework! The contribution guide can be found in the [Laravel documentation](https://laravel.com/docs/contributions). - -## Code of Conduct - -In order to ensure that the Laravel community is welcoming to all, please review and abide by the [Code of Conduct](https://laravel.com/docs/contributions#code-of-conduct). - -## Security Vulnerabilities - -If you discover a security vulnerability within Laravel, please send an e-mail to Taylor Otwell via [taylor@laravel.com](mailto:taylor@laravel.com). All security vulnerabilities will be promptly addressed. +# Synapse Sentinel Core + +Event intake system for Synapse Sentinel. Receives certification results from gate workflows and stores them as events using Laravel Verbs. + +## Architecture + +``` +Gate Workflow → POST /api/webhooks/gate → Core + ↓ stores events + Laravel Verbs + ↓ projects to + CertificationState +``` + +## Installation + +```bash +composer install +cp .env.example .env +php artisan key:generate +php artisan migrate +``` + +## Webhook Endpoint + +### POST /api/webhooks/gate + +Receives certification results from gate workflows. + +**Headers:** +- `Content-Type: application/json` +- `X-Gate-Signature: ` (optional, required if `GATE_WEBHOOK_SECRET` is set) + +**Request Body:** +```json +{ + "repository": "synapse-sentinel/core", + "sha": "abc123def456", + "verdict": "approved", + "reason": "All checks passed", + "checks": { + "tests": {"status": "pass", "coverage": 100}, + "security": {"status": "pass"}, + "syntax": {"status": "pass"} + }, + "triggered_by": "pull_request", + "pr_number": 42 +} +``` + +**Response:** +```json +{"message": "ok"} +``` + +## Event Schema + +### CertificationRequested + +Fired when a certification process begins. + +| Field | Type | Required | Description | +|-------|------|----------|-------------| +| `repository` | string | yes | Full repository name (e.g., `owner/repo`) | +| `sha` | string | yes | Git commit SHA | +| `triggeredBy` | string | yes | What triggered the certification (`push`, `pull_request`, `workflow_dispatch`) | +| `prNumber` | int | no | Pull request number if triggered by PR | +| `branch` | string | no | Branch name | + +### CertificationCompleted + +Fired when certification finishes with a verdict. + +| Field | Type | Required | Description | +|-------|------|----------|-------------| +| `repository` | string | yes | Full repository name | +| `sha` | string | yes | Git commit SHA | +| `verdict` | string | yes | Result: `approved`, `rejected`, or `escalate` | +| `reason` | string | no | Human-readable explanation | +| `checks` | object | no | Detailed check results | +| `triggeredBy` | string | no | What triggered the certification | +| `prNumber` | int | no | Pull request number | + +### CertificationFailed + +Fired when certification encounters an error (distinct from rejection). + +| Field | Type | Required | Description | +|-------|------|----------|-------------| +| `repository` | string | yes | Full repository name | +| `sha` | string | yes | Git commit SHA | +| `error` | string | yes | Error message | +| `stage` | string | no | Which stage failed (`tests`, `build`, `security`) | +| `context` | object | no | Additional error context | + +## State Projection + +Events are projected to `CertificationState` which tracks: + +- `status`: `pending`, `completed`, or `failed` +- `verdict`: `approved`, `rejected`, or `escalate` (when completed) +- `repository`, `sha`, `branch`, `pr_number` +- Timestamps: `requested_at`, `completed_at`, `failed_at` + +Query state: +```php +$state = CertificationState::load($certification_id); +$state->isApproved(); // bool +$state->isRejected(); // bool +$state->isPending(); // bool +$state->isFailed(); // bool +``` + +## Testing + +```bash +php artisan test +php artisan test --coverage +``` ## License -The Laravel framework is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT). +GPL-3.0-or-later diff --git a/app/Events/CertificationCompleted.php b/app/Events/CertificationCompleted.php index 37f32dc..69bbba1 100644 --- a/app/Events/CertificationCompleted.php +++ b/app/Events/CertificationCompleted.php @@ -4,10 +4,16 @@ namespace App\Events; +use App\States\CertificationState; +use Carbon\CarbonImmutable; +use Thunk\Verbs\Attributes\Autodiscovery\StateId; use Thunk\Verbs\Event; class CertificationCompleted extends Event { + #[StateId(CertificationState::class)] + public ?int $certification_id = null; + public function __construct( public string $repository, public string $sha, @@ -17,4 +23,17 @@ public function __construct( public ?string $triggeredBy = null, public ?int $prNumber = null, ) {} + + public function apply(CertificationState $state): void + { + $state->repository = $this->repository; + $state->sha = $this->sha; + $state->status = 'completed'; + $state->verdict = $this->verdict; + $state->reason = $this->reason; + $state->checks = $this->checks; + $state->triggered_by = $this->triggeredBy; + $state->pr_number = $this->prNumber; + $state->completed_at = CarbonImmutable::now(); + } } diff --git a/app/Events/CertificationFailed.php b/app/Events/CertificationFailed.php new file mode 100644 index 0000000..ac5ac0b --- /dev/null +++ b/app/Events/CertificationFailed.php @@ -0,0 +1,31 @@ +status = 'failed'; + $state->error = $this->error; + $state->failed_at = CarbonImmutable::now(); + } +} diff --git a/app/Events/CertificationRequested.php b/app/Events/CertificationRequested.php new file mode 100644 index 0000000..eacd59c --- /dev/null +++ b/app/Events/CertificationRequested.php @@ -0,0 +1,35 @@ +repository = $this->repository; + $state->sha = $this->sha; + $state->triggered_by = $this->triggeredBy; + $state->pr_number = $this->prNumber; + $state->branch = $this->branch; + $state->status = 'pending'; + $state->requested_at = CarbonImmutable::now(); + } +} diff --git a/app/States/CertificationState.php b/app/States/CertificationState.php new file mode 100644 index 0000000..cab9cee --- /dev/null +++ b/app/States/CertificationState.php @@ -0,0 +1,62 @@ +status === 'pending'; + } + + public function isCompleted(): bool + { + return $this->status === 'completed'; + } + + public function isFailed(): bool + { + return $this->status === 'failed'; + } + + public function isApproved(): bool + { + return $this->isCompleted() && $this->verdict === 'approved'; + } + + public function isRejected(): bool + { + return $this->isCompleted() && $this->verdict === 'rejected'; + } +} diff --git a/tests/Feature/CertificationEventsTest.php b/tests/Feature/CertificationEventsTest.php new file mode 100644 index 0000000..0c8b010 --- /dev/null +++ b/tests/Feature/CertificationEventsTest.php @@ -0,0 +1,153 @@ +certification_id); + + expect($state->repository)->toBe('synapse-sentinel/core'); + expect($state->sha)->toBe('abc123'); + expect($state->triggered_by)->toBe('pull_request'); + expect($state->pr_number)->toBe(42); + expect($state->branch)->toBe('feature/test'); + expect($state->status)->toBe('pending'); + expect($state->isPending())->toBeTrue(); + expect($state->requested_at)->not->toBeNull(); + }); + + it('stores event in verb_events table', function () { + CertificationRequested::fire( + repository: 'synapse-sentinel/core', + sha: 'def456', + triggeredBy: 'push', + ); + + Verbs::commit(); + + $this->assertDatabaseHas('verb_events', [ + 'type' => CertificationRequested::class, + ]); + }); +}); + +describe('CertificationCompleted', function () { + it('updates state to completed with verdict', function () { + $requested = CertificationRequested::fire( + repository: 'synapse-sentinel/core', + sha: 'abc123', + triggeredBy: 'pull_request', + ); + + CertificationCompleted::fire( + certification_id: $requested->certification_id, + repository: 'synapse-sentinel/core', + sha: 'abc123', + verdict: 'approved', + reason: 'All checks passed', + checks: ['tests' => 'pass', 'coverage' => 100], + ); + + $state = CertificationState::load($requested->certification_id); + + expect($state->status)->toBe('completed'); + expect($state->verdict)->toBe('approved'); + expect($state->reason)->toBe('All checks passed'); + expect($state->checks)->toBe(['tests' => 'pass', 'coverage' => 100]); + expect($state->isCompleted())->toBeTrue(); + expect($state->isApproved())->toBeTrue(); + expect($state->completed_at)->not->toBeNull(); + }); + + it('can mark certification as rejected', function () { + $requested = CertificationRequested::fire( + repository: 'synapse-sentinel/core', + sha: 'abc123', + triggeredBy: 'pull_request', + ); + + CertificationCompleted::fire( + certification_id: $requested->certification_id, + repository: 'synapse-sentinel/core', + sha: 'abc123', + verdict: 'rejected', + reason: 'Coverage below threshold', + ); + + $state = CertificationState::load($requested->certification_id); + + expect($state->isRejected())->toBeTrue(); + expect($state->isApproved())->toBeFalse(); + }); + + it('can be fired without prior request for webhook intake', function () { + CertificationCompleted::fire( + repository: 'external/repo', + sha: 'xyz789', + verdict: 'approved', + ); + + Verbs::commit(); + + $this->assertDatabaseHas('verb_events', [ + 'type' => CertificationCompleted::class, + ]); + }); +}); + +describe('CertificationFailed', function () { + it('updates state to failed with error', function () { + $requested = CertificationRequested::fire( + repository: 'synapse-sentinel/core', + sha: 'abc123', + triggeredBy: 'pull_request', + ); + + CertificationFailed::fire( + certification_id: $requested->certification_id, + repository: 'synapse-sentinel/core', + sha: 'abc123', + error: 'Runner crashed unexpectedly', + stage: 'tests', + context: ['exit_code' => 137], + ); + + $state = CertificationState::load($requested->certification_id); + + expect($state->status)->toBe('failed'); + expect($state->error)->toBe('Runner crashed unexpectedly'); + expect($state->isFailed())->toBeTrue(); + expect($state->isCompleted())->toBeFalse(); + expect($state->failed_at)->not->toBeNull(); + }); + + it('stores event with context', function () { + CertificationFailed::fire( + repository: 'synapse-sentinel/core', + sha: 'abc123', + error: 'Timeout', + stage: 'build', + context: ['timeout_seconds' => 300], + ); + + Verbs::commit(); + + $this->assertDatabaseHas('verb_events', [ + 'type' => CertificationFailed::class, + ]); + }); +});