Skip to content

Commit

Permalink
reset form
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Jun 10, 2024
1 parent bd96d90 commit 8fab6f1
Show file tree
Hide file tree
Showing 5 changed files with 989 additions and 16 deletions.
64 changes: 48 additions & 16 deletions app/Domains/Sources/WebhookSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use LlmLaraHub\LlmDriver\LlmDriverFacade;

class WebhookSource extends BaseSource
Expand Down Expand Up @@ -79,25 +78,17 @@ public function handle(Source $source): void
->toString();

try {
try {
$results = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
} catch (\Exception $e) {
$results = Arr::wrap($content);
}

/**
* @NOTE
* hmm did not fail on server
* php version?
*/
if (is_null($results) && ! is_null($content)) {
$results = Arr::wrap($content);
}
$results = $this->checkIfJsonOrJustText($results, $content);

$page_number = 0;

foreach ($results as $index => $result) {
$id = data_get($result, 'commit_id', Str::random(12));
$result = data_get($result, 'message', $result);
if (is_array($result)) {
$result = json_encode($result);
}

$id = $this->getIdFromPayload($result);

$document = Document::updateOrCreate([
'type' => TypesEnum::WebHook,
Expand Down Expand Up @@ -156,4 +147,45 @@ public function handle(Source $source): void
}

}

protected function checkIfJsonOrJustText($results, $content): array
{

try {
$results = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
} catch (\Exception $e) {
$results = Arr::wrap($content);
}

/**
* @NOTE
* I do this extra check due to a fail on
* a PHP version
*/
if (is_null($results) && ! is_null($content)) {
$results = Arr::wrap($content);
}

return $results;
}

protected function getIdFromPayload($result): string
{
if (data_get(Arr::wrap($this->payload), 'id', false)) {
/**
* @NOTE
* You can pass in as a key in the payload
* for example
* {
* "id": "commit_id",
* "content": "Test Message"
* }
*/
$id = data_get($this->payload, 'id');
} else {
$id = md5($result);
}

return $id;
}
}
34 changes: 34 additions & 0 deletions config/cors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Cross-Origin Resource Sharing (CORS) Configuration
|--------------------------------------------------------------------------
|
| Here you may configure your settings for cross-origin resource sharing
| or "CORS". This determines what cross-origin operations may execute
| in web browsers. You are free to adjust these settings as needed.
|
| To learn more: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
|
*/

'paths' => ['api/*', 'sanctum/csrf-cookie'],

'allowed_methods' => ['*'],

'allowed_origins' => ['*'],

'allowed_origins_patterns' => [],

'allowed_headers' => ['*'],

'exposed_headers' => [],

'max_age' => 0,

'supports_credentials' => false,

];
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ const submit = () => {
collection: props.collection.id
}), {
onSuccess: () => {
form.reset();
closeSlideOut()
}
})
Expand Down
69 changes: 69 additions & 0 deletions tests/Feature/WebhookSourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,73 @@ public function test_non_json()
Bus::assertBatchCount(1);

}

public function test_prevent_duplicates_github()
{
Bus::fake();

$payload = get_fixture('example_github.json');

LlmDriverFacade::shouldReceive('driver->onQueue')
->twice()->andReturn('default');

LlmDriverFacade::shouldReceive('driver->completion')
->once()->andReturn(
CompletionResponse::from([
'content' => get_fixture('github_transformed.json', false),
])
);

$source = Source::factory()->create();

(new \App\Domains\Sources\WebhookSource())
->payload($payload)
->handle($source);

(new \App\Domains\Sources\WebhookSource())
->payload($payload)
->handle($source);

$this->assertDatabaseCount('documents', 2);
$this->assertDatabaseCount('document_chunks', 2);

Bus::assertBatchCount(4);

}

public function test_prevent_duplicates_statamic()
{
Bus::fake();

$payload = get_fixture('statamic.json');

$payload['id'] = 'fake_id';
$payload['content'] = $payload;

LlmDriverFacade::shouldReceive('driver->onQueue')
->times(2)->andReturn('default');

LlmDriverFacade::shouldReceive('driver->completion')
->times(2)->andReturn(
CompletionResponse::from([
'content' => 'Foo Bar',
])
);

$source = Source::factory()->create();

(new \App\Domains\Sources\WebhookSource())
->payload($payload)
->handle($source);

(new \App\Domains\Sources\WebhookSource())
->payload($payload)
->handle($source);

$this->assertDatabaseCount('documents', 1);
$this->assertDatabaseCount('document_chunks', 1);

Bus::assertBatchCount(2);

}
}
Loading

0 comments on commit 8fab6f1

Please sign in to comment.