Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API specification generation #178

Merged
merged 12 commits into from
Jan 20, 2025
52 changes: 52 additions & 0 deletions .github/workflows/build-api-docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
on:
schedule:
- cron: '0 7 * * *'

env:
DOCS_REPOSITORY: romalytvynenko/cachet-docs
DOCS_BRANCH: feat/api-docs-generation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to update these to the real docs and main branch.


jbrooksuk marked this conversation as resolved.
Show resolved Hide resolved
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
uses: actions/checkout@v2
uses: actions/checkout@v4


- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.3
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo

- name: Install dependencies
run: composer install

- name: Install Scramble PRO
run: |
composer config repositories.scramble-pro '{"type": "composer", "url": "https://satis.dedoc.co"}'
composer config http-basic.satis.dedoc.co ${{ secrets.SCRAMBLE_USERNAME }} ${{ secrets.SCRAMBLE_KEY }}
composer require dedoc/scramble-pro:dev-feat/timacdonald-json-api-support --dev

- name: Checkout documentation repository
uses: actions/checkout@v2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
uses: actions/checkout@v2
uses: actions/checkout@v4

with:
repository: ${{ env.DOCS_REPOSITORY }}
ref: ${{ env.DOCS_BRANCH }}
path: 'docs-repository'

- name: Build API docs
run: php vendor/bin/testbench scramble:export --path=docs-repository/api-reference/openapi.json

- name: Setup git config
run: |
git config --global user.name "GitHub Actions"
git config --global user.email "<>"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can replace this with the setup-git-user workflow, https://github.com/marketplace/actions/setup-git-user

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, could not make it work, but found this gem used in other Cachet's workflows! I'll use it

https://github.com/stefanzweifel/git-auto-commit-action


- name: Commit and push generated template
run: |
export COMMIT_MESSAGE="Generated API specification from ${{ github.repository }}/${{ github.ref}}@${{ github.sha }}"
cd docs-repository
git add .
git diff-index --quiet HEAD || (git commit -m "$COMMIT_MESSAGE" && git push)
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"twig/twig": "^3.0"
},
"require-dev": {
"dedoc/scramble": "dev-feat/endpoint-groups-sorting",
"larastan/larastan": "^3.0",
"laravel/pail": "^1.1",
"orchestra/testbench": "^9.5.1",
Expand Down
26 changes: 26 additions & 0 deletions src/CachetCoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
namespace Cachet;

use BladeUI\Icons\Factory;
use Cachet\Documentation\AddAuthenticationToOperation;
use Cachet\Models\Incident;
use Cachet\Models\Schedule;
use Cachet\Settings\AppSettings;
use Dedoc\Scramble\Scramble;
use Dedoc\Scramble\Support\Generator\OpenApi;
use Dedoc\Scramble\Support\Generator\SecurityScheme;
use Filament\Support\Colors\Color;
use Filament\Support\Facades\FilamentColor;
use Illuminate\Cache\RateLimiting\Limit;
Expand Down Expand Up @@ -63,6 +67,8 @@ public function boot(): void
FilamentColor::register([
'cachet' => Color::rgb('rgb(4, 193, 71)'),
]);

$this->configureScramble();
}

/**
Expand Down Expand Up @@ -182,4 +188,24 @@ private function registerSchedules(): void
$schedule->command('cachet:beacon')->daily();
});
}

/**
* Scramble is installed as dev dependency hence the class existence check.
*/
private function configureScramble(): void
{
if (! class_exists(Scramble::class)) {
return;
}

Scramble::afterOpenApiGenerated(function (OpenApi $openApi) {
$openApi->info->description = 'API documentation for Cachet, the open source status page system.';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$openApi->info->description = 'API documentation for Cachet, the open source status page system.';
$openApi->info->description = 'API documentation for Cachet, the open-source, self-hosted status page system.';


$openApi->secure(
SecurityScheme::http('bearer')
);
});

Scramble::registerExtension(AddAuthenticationToOperation::class);
}
}
19 changes: 19 additions & 0 deletions src/Documentation/AddAuthenticationToOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Cachet\Documentation;

use Dedoc\Scramble\Extensions\OperationExtension;
use Dedoc\Scramble\Support\Generator\Operation;
use Dedoc\Scramble\Support\RouteInfo;

class AddAuthenticationToOperation extends OperationExtension
{
public function handle(Operation $operation, RouteInfo $routeInfo)
{
jbrooksuk marked this conversation as resolved.
Show resolved Hide resolved
// @todo find out if a route is protected by auth by looking at route's middleware to avoid manually annotating methods

if (in_array('GET', $routeInfo->route->methods())) {
$operation->addSecurity([]);
}
}
}
29 changes: 2 additions & 27 deletions src/Http/Controllers/Api/ComponentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
use Cachet\Data\Requests\Component\UpdateComponentRequestData;
use Cachet\Http\Resources\Component as ComponentResource;
use Cachet\Models\Component;
use Dedoc\Scramble\Attributes\Group;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Spatie\QueryBuilder\QueryBuilder;

/**
* @group Components
*/
#[Group('Components', weight: 1)]
class ComponentController extends Controller
{
/**
Expand All @@ -29,10 +28,6 @@ class ComponentController extends Controller
/**
* List Components
*
* @apiResourceCollection \Cachet\Http\Resources\Component
*
* @apiResourceModel \Cachet\Models\Component
*
* @queryParam per_page int How many items to show per page. Example: 20
* @queryParam page int Which page to show. Example: 2
* @queryParam sort Field to sort by. Enum: name, status, enabled. Example: name
Expand All @@ -54,12 +49,6 @@ public function index()

/**
* Create Component
*
* @apiResource \Cachet\Http\Resources\Component
*
* @apiResourceModel \Cachet\Models\Component
*
* @authenticated
*/
public function store(CreateComponentRequestData $data, CreateComponent $createComponentAction)
{
Expand All @@ -73,10 +62,6 @@ public function store(CreateComponentRequestData $data, CreateComponent $createC
/**
* Get Component
*
* @apiResource \Cachet\Http\Resources\Component
*
* @apiResourceModel \Cachet\Models\Component
*
* @queryParam include Include related resources. Enum: group, incidents. Example: group,incidents
*/
public function show(Component $component)
Expand All @@ -92,12 +77,6 @@ public function show(Component $component)

/**
* Update Component
*
* @apiResource \Cachet\Http\Resources\Component
*
* @apiResourceModel \Cachet\Models\Component
*
* @authenticated
*/
public function update(UpdateComponentRequestData $data, Component $component, UpdateComponent $updateComponentAction)
{
Expand All @@ -108,10 +87,6 @@ public function update(UpdateComponentRequestData $data, Component $component, U

/**
* Delete Component
*
* @response 204
*
* @authenticated
*/
public function destroy(Component $component, DeleteComponent $deleteComponentAction)
{
Expand Down
31 changes: 2 additions & 29 deletions src/Http/Controllers/Api/ComponentGroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,17 @@
use Cachet\Data\Requests\ComponentGroup\UpdateComponentGroupRequestData;
use Cachet\Http\Resources\ComponentGroup as ComponentGroupResource;
use Cachet\Models\ComponentGroup;
use Dedoc\Scramble\Attributes\Group;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Spatie\QueryBuilder\QueryBuilder;

/**
* @group Component Groups
*/
#[Group('Component Groups', weight: 2)]
class ComponentGroupController extends Controller
{
/**
* List Component Groups
*
* @apiResource \Cachet\Http\Resources\ComponentGroup
*
* @apiResourceModel \Cachet\Models\ComponentGroup
*
* @queryParam per_page int How many items to show per page. Example: 20
* @queryParam page int Which page to show. Example: 2
* @queryParam sort Field to sort by. Enum: name, id. Example: name
Expand All @@ -42,12 +37,6 @@ public function index()

/**
* Create Component Group
*
* @apiResource \Cachet\Http\Resources\ComponentGroup
*
* @apiResourceModel \Cachet\Models\ComponentGroup
*
* @authenticated
*/
public function store(CreateComponentGroupRequestData $data, CreateComponentGroup $createComponentGroupAction)
{
Expand All @@ -59,10 +48,6 @@ public function store(CreateComponentGroupRequestData $data, CreateComponentGrou
/**
* Get Component Group
*
* @apiResource \Cachet\Http\Resources\ComponentGroup
*
* @apiResourceModel \Cachet\Models\ComponentGroup
*
* @queryParam include Include related resources. Enum: components. Example: components
*/
public function show(ComponentGroup $componentGroup)
Expand All @@ -78,12 +63,6 @@ public function show(ComponentGroup $componentGroup)

/**
* Update Component Group
*
* @apiResource \Cachet\Http\Resources\ComponentGroup
*
* @apiResourceModel \Cachet\Models\ComponentGroup
*
* @authenticated
*/
public function update(UpdateComponentGroupRequestData $data, ComponentGroup $componentGroup, UpdateComponentGroup $updateComponentGroupAction)
{
Expand All @@ -94,12 +73,6 @@ public function update(UpdateComponentGroupRequestData $data, ComponentGroup $co

/**
* Delete Component Group
*
* @apiResource \Cachet\Http\Resources\ComponentGroup
*
* @apiResourceModel \Cachet\Models\ComponentGroup
*
* @authenticated
*/
public function destroy(ComponentGroup $componentGroup, DeleteComponentGroup $deleteComponentGroupAction)
{
Expand Down
16 changes: 3 additions & 13 deletions src/Http/Controllers/Api/GeneralController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,15 @@
namespace Cachet\Http\Controllers\Api;

use Cachet\Cachet;
use Dedoc\Scramble\Attributes\Group;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Controller;

/**
* @group Cachet
*/
#[Group('Cachet', weight: 0)]
class GeneralController extends Controller
{
/**
* Test the API
*
* @response {
* "data": "Pong!"
* }
*/
public function ping(): JsonResponse
{
Expand All @@ -25,17 +20,12 @@ public function ping(): JsonResponse

/**
* Get Version
*
* @response {
* "data": {
* "version": "3.x-dev"
* }
* }
*/
public function version(): JsonResponse
{
return response()->json([
'data' => [
/** @var "'3.x-dev'" */
'version' => Cachet::version(),
],
]);
Expand Down
29 changes: 2 additions & 27 deletions src/Http/Controllers/Api/IncidentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
use Cachet\Data\Requests\Incident\UpdateIncidentRequestData;
use Cachet\Http\Resources\Incident as IncidentResource;
use Cachet\Models\Incident;
use Dedoc\Scramble\Attributes\Group;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Spatie\QueryBuilder\QueryBuilder;

/**
* @group Incidents
*/
#[Group('Incidents', weight: 3)]
class IncidentController extends Controller
{
/**
Expand All @@ -31,10 +30,6 @@ class IncidentController extends Controller
/**
* List Incidents
*
* @apiResourceCollection \Cachet\Http\Resources\Incident
*
* @apiResourceModel \Cachet\Models\Incident
*
* @queryParam per_page int How many items to show per page. Example: 20
* @queryParam page int Which page to show. Example: 2
* @queryParam sort Field to sort by. Enum: name, id, status. Example: status
Expand All @@ -58,12 +53,6 @@ public function index()

/**
* Create Incident
*
* @apiResource \Cachet\Http\Resources\Incident
*
* @apiResourceModel \Cachet\Models\Incident
*
* @authenticated
*/
public function store(CreateIncidentRequestData $data, CreateIncident $createIncidentAction)
{
Expand All @@ -75,10 +64,6 @@ public function store(CreateIncidentRequestData $data, CreateIncident $createInc
/**
* Get Incident
*
* @apiResource \Cachet\Http\Resources\Incident
*
* @apiResourceModel \Cachet\Models\Incident
*
* @queryParam include Include related resources. Enum: components, updates, user. Example: updates
*/
public function show(Incident $incident)
Expand All @@ -94,12 +79,6 @@ public function show(Incident $incident)

/**
* Update Incident
*
* @apiResource \Cachet\Http\Resources\Incident
*
* @apiResourceModel \Cachet\Models\Incident
*
* @authenticated
*/
public function update(UpdateIncidentRequestData $data, Incident $incident, UpdateIncident $updateIncidentAction)
{
Expand All @@ -110,10 +89,6 @@ public function update(UpdateIncidentRequestData $data, Incident $incident, Upda

/**
* Delete Incident
*
* @response 204
*
* @authenticated
*/
public function destroy(Incident $incident, DeleteIncident $deleteIncidentAction)
{
Expand Down
Loading
Loading