authorize('viewAny', SourceControl::class); $sourceControls = SourceControl::getByProjectId($project->id)->simplePaginate(25); return SourceControlResource::collection($sourceControls); } #[Post('/', name: 'api.projects.source-controls.create', middleware: 'ability:write')] #[Endpoint(title: 'create')] #[BodyParam(name: 'provider', description: 'The provider', required: true, enum: [\App\Enums\SourceControl::GITLAB, \App\Enums\SourceControl::GITHUB, \App\Enums\SourceControl::BITBUCKET])] #[BodyParam(name: 'name', description: 'The name of the storage provider.', required: true)] #[BodyParam(name: 'token', description: 'The token if provider requires api token')] #[BodyParam(name: 'url', description: 'The URL if the provider is Gitlab and it is self-hosted')] #[BodyParam(name: 'username', description: 'The username if the provider is Bitbucket')] #[BodyParam(name: 'password', description: 'The password if the provider is Bitbucket')] #[ResponseFromApiResource(SourceControlResource::class, SourceControl::class)] public function create(Request $request, Project $project): SourceControlResource { $this->authorize('create', SourceControl::class); $this->validate($request, ConnectSourceControl::rules($request->all())); $sourceControl = app(ConnectSourceControl::class)->connect(auth()->user(), $project, $request->all()); return new SourceControlResource($sourceControl); } #[Get('{sourceControl}', name: 'api.projects.source-controls.show', middleware: 'ability:read')] #[Endpoint(title: 'show')] #[ResponseFromApiResource(SourceControlResource::class, SourceControl::class)] public function show(Project $project, SourceControl $sourceControl) { $this->authorize('view', $sourceControl); $this->validateRoute($project, $sourceControl); return new SourceControlResource($sourceControl); } #[Put('{sourceControl}', name: 'api.projects.source-controls.update', middleware: 'ability:write')] #[Endpoint(title: 'update')] #[BodyParam(name: 'name', description: 'The name of the storage provider.', required: true)] #[BodyParam(name: 'token', description: 'The token if provider requires api token')] #[BodyParam(name: 'url', description: 'The URL if the provider is Gitlab and it is self-hosted')] #[BodyParam(name: 'username', description: 'The username if the provider is Bitbucket')] #[BodyParam(name: 'password', description: 'The password if the provider is Bitbucket')] #[BodyParam(name: 'global', description: 'Accessible in all projects', enum: [true, false])] #[ResponseFromApiResource(SourceControlResource::class, SourceControl::class)] public function update(Request $request, Project $project, SourceControl $sourceControl) { $this->authorize('update', $sourceControl); $this->validateRoute($project, $sourceControl); $this->validate($request, EditSourceControl::rules($sourceControl, $request->all())); $sourceControl = app(EditSourceControl::class)->edit($sourceControl, $project, $request->all()); return new SourceControlResource($sourceControl); } #[Delete('{sourceControl}', name: 'api.projects.source-controls.delete', middleware: 'ability:write')] #[Endpoint(title: 'delete')] #[Response(status: 204)] public function delete(Project $project, SourceControl $sourceControl) { $this->authorize('delete', $sourceControl); $this->validateRoute($project, $sourceControl); app(DeleteSourceControl::class)->delete($sourceControl); return response()->noContent(); } private function validateRoute(Project $project, SourceControl $sourceControl): void { if (! $sourceControl->project_id) { return; } if ($project->id !== $sourceControl->project_id) { abort(404, 'Source Control not found in project'); } } }