Skip to content

Commit

Permalink
fix: api doc generation
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed Sep 4, 2023
1 parent 16f70ae commit fa3cff2
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 86 deletions.
90 changes: 61 additions & 29 deletions .github/workflows/generate_api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,70 @@ on:
push:
branches:
- master
paths:
- version.txt
- fix-apidoc
jobs:
build:
runs-on: ubuntu-latest
services:
mysql:
image: mysql:5.7
env:
MYSQL_ROOT_PASSWORD: shopware
MYSQL_DATABASE: shopware
ports:
- '3306:3306'
env:
APP_ENV: dev
APP_URL: http://localhost:8000
DATABASE_URL: mysql://[email protected]/shopware
steps:
- uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
- name: Get Composer Cache Directory
id: composer-cache
run: |
echo "GH_COMPOSER_CACHE=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-6500
restore-keys: |
${{ runner.os }}-composer-
- name: Start MySQL
shell: bash
run: |
sudo mv /var/lib/mysql /var/lib/mysql-old
sudo mkdir /var/lib/mysql
sudo mount -t tmpfs tmpfs /var/lib/mysql -o size=1G
sudo -u mysql mysqld --datadir=/var/lib/mysql --default-time-zone=SYSTEM --initialize-insecure
sudo systemctl start mysql
-
name: Install Shopware CLI
uses: FriendsOfShopware/shopware-cli-action@main
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Create Project
run: shopware-cli project create shopware 6.5.0.0

- name: Clone api-doc
uses: actions/checkout@v2
- name: Install Shopware
working-directory: shopware
run: php bin/console system:install --basic-setup --create-database --force

- name: Dump Info
run: |
php generate-api-info.php
env:
FORCE_GENERATE: ${{ inputs.forceGenerate }}

- uses: EndBug/add-and-commit@v4
with:
add: '.'
author_name: Frosh Automation
author_email: [email protected]
message: 'chore: Updated Swagger definitions'
env:
GITHUB_TOKEN: ${{ secrets.FROSH_CI_TOKEN }}
- name: Start Webserver
working-directory: shopware
run: php -S localhost:8000 -t public public/index.php

- name: Clone api-doc
uses: actions/checkout@v3
with:
path: api-doc

- name: Dump Info
working-directory: api-doc
run: |
php generate-api-info.php
env:
FORCE_GENERATE: ${{ inputs.forceGenerate }}

- uses: EndBug/add-and-commit@v4
with:
add: '.'
cwd: 'api-doc'
author_name: Frosh Automation
author_email: [email protected]
message: 'chore: Updated Swagger definitions'
env:
GITHUB_TOKEN: ${{ secrets.FROSH_CI_TOKEN }}
77 changes: 20 additions & 57 deletions generate-api-info.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

include 'functions.php';

$shopwareDir = $_SERVER['GITHUB_WORKSPACE'] . '/shopware/';
$apiDir = $_SERVER['GITHUB_WORKSPACE'] . '/api-doc/';

chdir($shopwareDir);

$forceGenerate = isset($_SERVER['FORCE_GENERATE']) && $_SERVER['FORCE_GENERATE'] === '1';

echo "Force generate mode: " . var_export($forceGenerate, true);
Expand Down Expand Up @@ -42,18 +49,11 @@ function fetch($path)

function updateShopware($updateInfo)
{
$zipName = basename($updateInfo['link']);
$version = $updateInfo['name'];

echo '=> Downloading ' . $updateInfo['version'] . PHP_EOL;
exec('wget -qq ' . $updateInfo['link']);
exec('composer require shopware/core:' . $version . ' shopware/administration:' . $version . ' shopware/storefront:' . $version . ' shopware/elasticsearch:' . $version);

echo '=> Unzip ' . $updateInfo['version'] . PHP_EOL;
exec('unzip -qq -o ' . $zipName);
exec('rm -rf vendor/shopware/recovery/vendor/phpunit/php-code-coverage/tests/_files/Crash.php');

echo '=> Updating ' . $updateInfo['version'] . PHP_EOL;
exec('php public/recovery/update/index.php -n');
exec('rm -rf update-assets');
exec('php bin/console system:update:finish');
}

function dumpApiInfo(string $currentVersion)
Expand All @@ -65,10 +65,9 @@ function dumpApiInfo(string $currentVersion)
$apiPath = '';
}

$api = fetch('/api' . $apiPath . '/_info/openapi3.json');
$scApi = fetch('/sales-channel-api' . $apiPath . '/_info/openapi3.json');
$stApi = fetch('/store-api' . $apiPath . '/_info/openapi3.json');
$entitySchema = fetch('/api' . $apiPath . '/_info/entity-schema.json');
$api = fetch('/api/_info/openapi3.json');
$stApi = fetch('/store-api/_info/openapi3.json');
$entitySchema = fetch('/api/_info/entity-schema.json');

$outputDir = dirname(__DIR__) . '/api-doc/version/' . $currentVersion . '/';
if (!file_exists($outputDir)) {
Expand All @@ -79,10 +78,6 @@ function dumpApiInfo(string $currentVersion)
file_put_contents($outputDir . '/api.json', json_encode($api, JSON_UNESCAPED_SLASHES));
}

if ($scApi) {
file_put_contents($outputDir . '/sales-channel-api.json', json_encode($scApi, JSON_UNESCAPED_SLASHES));
}

if ($stApi) {
file_put_contents($outputDir . '/store-api.json', json_encode($stApi, JSON_UNESCAPED_SLASHES));
}
Expand All @@ -100,7 +95,11 @@ function getMissingVersions(array $releases): array
$missing = [];

foreach ($releases as $release) {
if (is_dir(dirname(__DIR__) . '/api-doc/version/' . $release['version']) && !$forceGenerate) {
if (version_compare(ltrim($release['name'], 'v'), '6.5.0.0', '<')) {
continue;
}

if (is_file(dirname(__DIR__) . '/api-doc/version/' . ltrim($release['name'], 'v') . '/api.json') && !$forceGenerate) {
continue;
}

Expand All @@ -110,38 +109,6 @@ function getMissingVersions(array $releases): array
return $missing;
}

function setUpShopware(array $release)
{
$installFolder = sys_get_temp_dir() . '/' . uniqid('sw', true);
if (!mkdir($installFolder) && !is_dir($installFolder)) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $installFolder));
}

chdir($installFolder);

printf('> Installing Shopware with Version: %s in %s' . PHP_EOL, $release['version'], $installFolder);

$descriptorspec = [
0 => ['pipe', 'r'],
1 => ['pipe', 'w'],
2 => ['pipe', '/w'],
];


exec('wget -O install.zip -qq ' . $release['download']);
exec('unzip -q install.zip');

$webServer = proc_open('php -S localhost:8000 -t public public/index.php', $descriptorspec, $pipes);
register_shutdown_function(static function () use($webServer, $installFolder) {
proc_terminate($webServer, 9);
proc_close($webServer);
exec('rm -rf ' . escapeshellarg($installFolder));
});

exec('php public/recovery/install/index.php --shop-host localhost --db-host 127.0.0.1 --db-user root --db-password shopware --db-name shopware --shop-locale en-GB --shop-currency EUR --admin-username demo --admin-password demo --admin-email [email protected] --admin-locale en-GB --admin-firstname demo --admin-lastname demo -n');
exec('echo \'APP_ENV=dev\' >> .env');
}

function updateSwaggerIndex()
{
$folders = scandir(__DIR__ . '/version/', SCANDIR_SORT_ASCENDING);
Expand Down Expand Up @@ -186,7 +153,7 @@ function updateSwaggerIndex()
file_put_contents(__DIR__ . '/data.json', json_encode($listing, JSON_PRETTY_PRINT));
}

$releases = array_reverse(json_decode(file_get_contents('https://n0g72msg55.execute-api.eu-central-1.amazonaws.com/'), true));
$releases = fetch_tags();
$missingVersions = getMissingVersions($releases);

if ($missingVersions === []) {
Expand All @@ -195,11 +162,7 @@ function updateSwaggerIndex()
}

foreach ($missingVersions as $i => $missingVersion) {
if ($i === 0) {
setUpShopware($missingVersion);
} else {
updateShopware($missingVersion);
}
updateShopware($missingVersion);

dumpApiInfo($missingVersion['version']);
}
Expand Down

0 comments on commit fa3cff2

Please sign in to comment.