Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SDK: **[laravel/ai](https://laravel.com/docs/ai-sdk)** (`BaseAgent` + tools). We
```
GET /health
POST /api/ask Bearer KIT_PEER_TOKEN {"message":"..."}
POST /api/assign Bearer KIT_PEER_TOKEN {"issue":"...","chair":"kit","brief":"..."}
POST /api/webhooks/mattermost token=KIT_WEBHOOK_TOKEN → queues ReplyOnMattermost
php artisan queue:work --queue=kit
php artisan kit:ask "what's in the catalog?"
Expand Down
88 changes: 88 additions & 0 deletions app/Http/Controllers/AssignController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace App\Http\Controllers;

use App\Factory\Board;
use App\Mattermost\Client;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class AssignController extends Controller
{
public function __invoke(Request $request, Board $board, Client $mattermost): JsonResponse
{
$expected = (string) config('kit.peer_token');
$got = (string) $request->bearerToken();
if ($expected === '' || ! hash_equals($expected, $got)) {
return response()->json(['error' => 'unauthorized'], 401);
}

$issue = trim((string) $request->input('issue', ''));
if ($issue === '') {
return response()->json(['error' => 'issue required'], 422);
}

$chair = strtolower(trim((string) $request->input('chair', 'kit')));
if (! in_array($chair, ['kit', 'feel', 'bench'], true)) {
$chair = 'kit';
}

$number = $this->issueNumber($issue);
$id = $this->boardId($issue, $number, (string) $request->input('brief', ''));
$brief = trim((string) $request->input('brief', ''));

$item = $board->upsert($id, [
'state' => 'queued',
'lifecycle' => 'queued',
'owner' => $chair,
'issue' => $number,
'hops' => 0,
'note' => $brief !== '' ? $brief : 'assigned '.$issue,
]);

$this->hallwayAck($mattermost, $id, $issue, $chair);

return response()->json([
'ok' => true,
'board_id' => $id,
'item' => $item,
]);
}

private function issueNumber(string $issue): int
{
if (preg_match('/#(\d+)/', $issue, $m) === 1) {
return (int) $m[1];
}
if (preg_match('/\/issues\/(\d+)/', $issue, $m) === 1) {
return (int) $m[1];
}

return (int) $issue;
}

private function boardId(string $issue, int $number, string $brief): string
{
$hay = strtolower($issue.' '.$brief);
foreach (['rider', 'hero-ebike', 'ranch-7620'] as $catalog) {
if (str_contains($hay, $catalog)) {
return $catalog;
}
}

return $number > 0 ? 'issue-'.$number : 'assign';
}

private function hallwayAck(Client $mattermost, string $id, string $issue, string $chair): void
{
$hallway = (string) config('kit.mattermost.hallway_id');
$dm = (string) config('kit.mattermost.dm_id');
if ($hallway === '' || $hallway === $dm) {
return;
}

$short = $number = $this->issueNumber($issue);
$ref = $short > 0 ? '#'.$short : $issue;
$mattermost->post($hallway, 'queued '.$id.' ← '.$ref.' ('.$chair.')');
}
}
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

use App\Http\Controllers\AskController;
use App\Http\Controllers\AssignController;
use App\Http\Controllers\HealthController;
use App\Http\Controllers\MattermostWebhookController;
use Illuminate\Support\Facades\Route;

Route::get('/health', HealthController::class);
Route::post('/ask', AskController::class);
Route::post('/assign', AssignController::class);
Route::post('/webhooks/mattermost', MattermostWebhookController::class);
66 changes: 66 additions & 0 deletions tests/Feature/AssignTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

use App\Factory\Board;
use App\Mattermost\Client;

beforeEach(function () {
$path = storage_path('framework/testing/board-'.uniqid('', true).'.json');
config([
'kit.board_path' => $path,
'kit.peer_token' => 'test-peer',
'kit.mattermost.hallway_id' => 'hall-1',
'kit.mattermost.dm_id' => 'dm-1',
]);
$this->boardPath = $path;
});

afterEach(function () {
@unlink($this->boardPath);
});

test('assign refuses a bad bearer', function () {
$this->postJson('/api/assign', ['issue' => 'https://github.com/the-shit/bikes-v2/issues/12'])
->assertUnauthorized();
});

test('assign requires an issue', function () {
$this->withToken('test-peer')
->postJson('/api/assign', ['brief' => 'no issue'])
->assertStatus(422);
});

test('assign queues a board row and does not speak as the mouth', function () {
$mm = Mockery::mock(Client::class);
$mm->shouldReceive('post')->once()->with('hall-1', 'queued rider ← #12 (kit)');
$this->app->instance(Client::class, $mm);

$this->withToken('test-peer')
->postJson('/api/assign', [
'issue' => 'https://github.com/the-shit/bikes-v2/issues/12',
'chair' => 'kit',
'brief' => 'rider stills',
])
->assertOk()
->assertJsonPath('ok', true)
->assertJsonPath('board_id', 'rider');

$row = collect(app(Board::class)->read()['items'])->firstWhere('id', 'rider');
expect($row['lifecycle'])->toBe('queued')
->and($row['owner'])->toBe('kit')
->and($row['issue'])->toBe(12);
});

test('assign skips mattermost when hallway is the Jordan DM', function () {
config(['kit.mattermost.hallway_id' => 'dm-1']);
$mm = Mockery::mock(Client::class);
$mm->shouldReceive('post')->never();
$this->app->instance(Client::class, $mm);

$this->withToken('test-peer')
->postJson('/api/assign', [
'issue' => 'https://github.com/the-shit/kit/issues/5',
'chair' => 'kit',
])
->assertOk()
->assertJsonPath('board_id', 'issue-5');
});
Loading