88 lines
2.9 KiB
PHP
88 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Api\V1;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Api\V1\RepositoryRequest;
|
|
use App\Http\Resources\RepositoryResource;
|
|
use App\Jobs\SyncRepository;
|
|
use App\Models\Repository;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
|
|
|
/**
|
|
* The collective's git repositories. Only which repository, whose project and
|
|
* whether to show it are written here; everything else is what the provider
|
|
* last reported.
|
|
*/
|
|
class RepositoryController extends Controller
|
|
{
|
|
/**
|
|
* Filters: provider, project_id, show_publicly, failing, search (full name).
|
|
*/
|
|
public function index(Request $request): AnonymousResourceCollection
|
|
{
|
|
$this->authorize('viewAny', Repository::class);
|
|
|
|
$repositories = Repository::query()
|
|
->when($request->filled('provider'), fn ($q) => $q->where('provider', $request->string('provider')))
|
|
->when($request->filled('project_id'), fn ($q) => $q->where('project_id', $request->integer('project_id')))
|
|
->when($request->has('show_publicly'), fn ($q) => $q->where('show_publicly', $request->boolean('show_publicly')))
|
|
->when($request->boolean('failing'), fn ($q) => $q->failing())
|
|
->when($request->filled('search'), fn ($q) => $q->where('full_name', 'like', '%'.$request->string('search').'%'))
|
|
->orderBy('full_name')
|
|
->paginate(perPage: min($request->integer('per_page', 25), 100))
|
|
->withQueryString();
|
|
|
|
return RepositoryResource::collection($repositories);
|
|
}
|
|
|
|
public function store(RepositoryRequest $request): JsonResponse
|
|
{
|
|
$this->authorize('create', Repository::class);
|
|
|
|
return RepositoryResource::make(Repository::create($request->validated())->refresh())->response()->setStatusCode(201);
|
|
}
|
|
|
|
public function show(Repository $repository): RepositoryResource
|
|
{
|
|
$this->authorize('view', $repository);
|
|
|
|
return RepositoryResource::make($repository);
|
|
}
|
|
|
|
public function update(RepositoryRequest $request, Repository $repository): RepositoryResource
|
|
{
|
|
$this->authorize('update', $repository);
|
|
|
|
$repository->update($request->validated());
|
|
|
|
return RepositoryResource::make($repository);
|
|
}
|
|
|
|
public function destroy(Repository $repository): JsonResponse
|
|
{
|
|
$this->authorize('delete', $repository);
|
|
|
|
$repository->delete();
|
|
|
|
return response()->json(status: 204);
|
|
}
|
|
|
|
/**
|
|
* Ask the provider again, on the queue: a sync spends a rate limit the
|
|
* whole application shares, and a caller should not wait on it.
|
|
*/
|
|
public function sync(Repository $repository): JsonResponse
|
|
{
|
|
$this->authorize('sync', $repository);
|
|
|
|
SyncRepository::dispatch($repository);
|
|
|
|
return RepositoryResource::make($repository)->response()->setStatusCode(202);
|
|
}
|
|
}
|