Skip to content

Commit 8ba352a

Browse files
authored
Fix & Enhance API refs tool (#3230)
* Fix Composer authentication * Add `base_branch`, `work_branch`, and `force` inputs
1 parent a8633fd commit 8ba352a

3 files changed

Lines changed: 130 additions & 31 deletions

File tree

.github/workflows/api_refs.yaml

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ on:
1717
required: false
1818
type: boolean
1919
default: false
20+
base_branch:
21+
description: 'Start from this branch of the doc to build the refs (default: Maj.min from version)'
22+
required: false
23+
type: string
24+
work_branch:
25+
description: 'Commit builds to this branch of the doc (default: api_refs_<version>)'
26+
required: false
27+
type: string
28+
force:
29+
description: 'Push to work_branch even if it exists (default: false)'
30+
required: false
31+
type: boolean
32+
default: false
2033

2134
jobs:
2235
open_php_api_ref_pr:
@@ -26,17 +39,28 @@ jobs:
2639
steps:
2740
- name: Set version and branches
2841
id: version_and_branches
42+
env:
43+
INPUT_VERSION: ${{ inputs.version }}
44+
INPUT_BASE_BRANCH: ${{ inputs.base_branch }}
45+
INPUT_WORK_BRANCH: ${{ inputs.work_branch }}
46+
INPUT_USE_DEV_VERSION: ${{ inputs.use_dev_version }}
2947
run: |
3048
# Strip leading 'v' to get a plain version number (e.g. 5.0.9)
31-
version="${{ inputs.version }}"
49+
version="$INPUT_VERSION"
3250
version="${version#v}"
33-
base_branch="$(echo $version | sed 's/\(.*\..*\)\..*/\1/')"
34-
work_branch="api_refs_v${version}"
51+
base_branch="$INPUT_BASE_BRANCH"
52+
if [ -z "$base_branch" ]; then
53+
base_branch="$(echo "$version" | sed 's/\(.*\..*\)\..*/\1/')"
54+
fi
55+
work_branch="$INPUT_WORK_BRANCH"
56+
if [ -z "$work_branch" ]; then
57+
work_branch="api_refs_v${version}"
58+
fi
3559
36-
if [[ "${{ inputs.use_dev_version }}" == "true" ]]; then
60+
if [[ "$INPUT_USE_DEV_VERSION" == "true" ]]; then
3761
# Dev build: install from the x-dev branch and label output with the target version
38-
dxp_version="v${base_branch}.x-dev"
39-
base_dxp_branch="${base_branch}"
62+
base_dxp_branch="$(echo "$version" | sed 's/\(.*\..*\)\..*/\1/')"
63+
dxp_version="v${base_dxp_branch}.x-dev"
4064
virtual_dxp_version="${version}"
4165
else
4266
# Stable build: install from the released tag
@@ -57,6 +81,15 @@ jobs:
5781
with:
5882
ref: ${{ steps.version_and_branches.outputs.base_branch }}
5983

84+
- name: Check if work branch exists
85+
id: check_work_branch
86+
if: inputs.force == false
87+
run: |
88+
if git ls-remote --exit-code --heads origin "${{ steps.version_and_branches.outputs.work_branch }}"; then
89+
echo "::error title=Branch exists::The branch ${{ steps.version_and_branches.outputs.work_branch }} already exists. You can use the 'force' option to overwrite it."
90+
exit 1
91+
fi
92+
6093
- name: Disable PHP coverage
6194
uses: shivammathur/setup-php@v2
6295
with:

tools/api_refs/README.md

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
<!-- vale off -->
2+
13
# PHP API Ref
24

35
## Install/Dependencies
@@ -6,16 +8,20 @@ Requires [`jq`](https://stedolan.github.io/jq/download/)
68

79
## Basic usage
810

9-
`tools/api_refs/api_refs.sh` is a script generating PHP API Reference, by default, under `docs/api/php_api/php_api_reference/`.
11+
`tools/api_refs/api_refs.sh` is a script generating PHP & REST API References, by default, under `docs/api/php_api/php_api_reference/` and `docs/api/rest_api/rest_api_reference/`.
1012

11-
- For Composer, if you do not use a global authentication to retrieve _Commerce_ edition, a path to an auth.json file can be given as first argument. For example:
13+
- For Composer, if you do not use a global authentication to retrieve _Commerce_ edition, a path to an auth.json file can be given as first optional argument. For example:
1214
```
1315
tools/api_refs/api_refs.sh ~/www/ibexa-dxp-commerce/auth.json
1416
```
15-
- The second argument can be a path to an output directory to use instead of the default one. For example, using the Composer global authentication file as first argument and the path to directory (which is created if it doesn't exist yet):
17+
- The second optional argument can be a path to an output directory to use instead of the default one. For example, using the Composer global authentication file as first argument and the path to directory (which is created if it doesn't exist yet):
1618
```
1719
tools/api_refs/api_refs.sh ~/.composer/auth.json ./docs/api/php_api/php_api_reference-TMP
1820
```
21+
- The next three optional arguments are the REST API files
22+
- 3rd argument is the reference HTML file path
23+
- 4th argument is the file path for the OpenAPI specification in YAML format
24+
- 5th argument is the file path for the OpenAPI specification in JSON format
1925

2026
## Rebuild example
2127

@@ -32,7 +38,7 @@ In `tools/api_refs/api_refs.sh`:
3238

3339
`PHPDOC_VERSION` should always target the last version of phpDocumentor.
3440

35-
`DXP_VERSION` should target the version of Ibexa DXP Commerce corresponding to the main doc's branch.
41+
`DXP_VERSION` should target the version of Ibexa DXP Commerce corresponding to the doc's branch.
3642

3743
### Templates
3844

@@ -51,17 +57,35 @@ For example, when working on the design, the set of parsed files can be reduced
5157

5258
`PHP_BINARY` can be edited, for example, to use a different PHP version than the default, to change verbosity, or to add `-d memory_limit=-1`.
5359

60+
```bash
61+
PHP_BINARY="/opt/homebrew/opt/php@8.4/bin/php -d memory_limit=-1 -d error_reporting=`php -r 'echo E_ALL & ~E_DEPRECATED;'`";
62+
```
63+
64+
`COMPOSER_BINARY` can be edited, for example, to add options to `composer`, or change the PHP used with it.
65+
66+
```bash
67+
COMPOSER_BINARY="$PHP_BINARY $(which composer)";
68+
```
69+
5470
`FORCE_DXP_INSTALL` can be changed to `0` (zero) to have a persistent `TMP_DXP_DIR`.
5571
After a first run to create it, the Ibexa DXP won't be rebuilt by Composer by next runs.
5672
Time is saved. The DXP's code could even be modified for test purpose.
73+
`TMP_DXP_DIR` can be set to the path of a running installation.
74+
75+
```bash
76+
PHP_BINARY='ddev php';
77+
COMPOSER_BINARY='ddev composer';
78+
TMP_DXP_DIR=~/my-ddev-project/;
79+
FORCE_DXP_INSTALL=0;
80+
```
5781

5882
If you change some of those values, please do not commit those changes, and don't commit their output.
5983
To prevent that, you can make a local copy, and use this copy to generate in a temporary output directory:
6084
```bash
61-
cp tools/api_refs/api_refs.sh tools/api_refs/phpdoc.dev.sh
62-
nano phpdoc.dev.sh # Edit and make your changes. For example, change PHPDOC_CONF to use phpdoc.dev.xml.
63-
nano phpdoc.dev.xml # Edit and make your changes. For example, target only your package.
64-
tools/api_refs/api_refs.sh ~/.composer/auth.json ./docs/api/php_api/php_api_reference-TMP
85+
cp tools/api_refs/api_refs.sh tools/api_refs/api_refs.dev.sh
86+
nano tools/api_refs/api_refs.dev.sh # Edit and make your changes. For example, change PHPDOC_CONF to use phpdoc.dev.xml.
87+
nano tools/api_refs/phpdoc.dev.xml # Edit and make your changes. For example, target only your package.
88+
tools/api_refs/api_refs.dev.sh ~/.composer/auth.json ./docs/api/php_api/php_api_reference-TMP
6589
```
6690

6791
### Creating a build of dev version
@@ -100,3 +124,39 @@ if [ 0 -eq $DXP_ALREADY_EXISTS ]; then
100124
composer require --no-interaction --ignore-platform-reqs --no-scripts ibexa/$MY_PACKAGE "$MY_BRANCH as $DXP_VERSION";
101125
fi;
102126
```
127+
128+
### Run as GitHub Action
129+
130+
#### By using `gh`
131+
132+
With [GitHub CLI `gh`](https://cli.github.com/), you can trigger a GitHub Action workflow to build the API References.
133+
134+
```bash
135+
gh workflow run api_refs.yaml -f version=<tag> -f use_dev_version=<false|true> --ref <branch> -f base_branch=<branch> -f work_branch=<branch> -f force=<false|true>
136+
```
137+
138+
`-f version=<tag>` to pass the Ibexa DXP version tag for which the API References are built.
139+
`-f use_dev_version=<false|true>` to use the released version designed by the tag, or to use the development version (`v5.0.x-dev`) for an incoming tag.
140+
`--ref <branch>` to use the `api_refs.yaml` workflow from a given branch instead of the default branch (`5.0`).
141+
`-f base_branch=<branch>` to use the `api_refs.sh` from a given branch and make a PR to that branch.
142+
`-f work_branch=<branch>` to use a given target branch to commit the build and make a PR from that branch.
143+
`-f force=<false|true>` to force the commit on the target branch even if it already exists.
144+
145+
Examples:
146+
147+
Build from the dev branch `5.0.x-dev` API references for `v5.0.999`:
148+
149+
```bash
150+
gh workflow run api_refs.yaml -f version=v5.0.999 -f use_dev_version=true
151+
```
152+
153+
Rebuild references for the released version `v5.0.10` from `my-tools`'s `api_refs.yaml` with `my-tools`'s tools and commit the result into `my-api-refs` even if it already exists:
154+
155+
```bash
156+
gh workflow run api_refs.yaml -f version=v5.0.10 --ref my-builder -f base_branch=my-builder -f work_branch=my-api-refs -f force=true
157+
```
158+
159+
#### By using web UI
160+
161+
On the GitHub repository page, go to the "Actions" tab and, in the workflow list, select ["Build API Refs"](https://github.com/ibexa/documentation-developer/actions/workflows/api_refs.yaml).
162+
On top right of the past workflow table, unfold "Run workflow" menu, set the fields, then click "Run workflow" button.

tools/api_refs/api_refs.sh

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env bash
22

3-
set -x;
3+
set +x;
44

5-
AUTH_JSON=${1:-~/.composer/auth.json}; # Path to an auth.json file allowing to install the targeted edition and version
5+
AUTH_JSON=$(realpath ${1:-~/.composer/auth.json}); # Path to an auth.json file allowing to install the targeted edition and version
66
PHP_API_OUTPUT_DIR=${2:-./docs/api/php_api/php_api_reference}; # Path to the directory where the built PHP API Reference is hosted
77
REST_API_OUTPUT_FILE=${3:-./docs/api/rest_api/rest_api_reference/rest_api_reference.html}; # Path to the REST API Reference file
88
REST_API_OPENAPI_FILE_YAML=${4:-./docs/api/rest_api/rest_api_reference/openapi.yaml}; # Path to the REST API OpenAPI spec file
@@ -24,6 +24,7 @@ REDOCLY_TEMPLATE="$(pwd)/tools/api_refs/redocly.hbs"; # Absolute path to Redocly
2424
OPENAPI_FIX="$(pwd)/tools/api_refs/openapi.php"; # A script editing and fixing few things on the dumped schema (should be temporary and fixes reported to source)
2525

2626
PHP_BINARY="php -d error_reporting=`php -r 'echo E_ALL & ~E_DEPRECATED;'`"; # Avoid depreciation messages from phpDocumentor/Reflection/issues/529 when using PHP 8.2 or higher
27+
COMPOSER_BINARY='composer';
2728
TMP_DXP_DIR=/tmp/ibexa-dxp-phpdoc; # Absolute path of the temporary directory in which Ibexa DXP will be installed and the PHP API Reference built
2829
FORCE_DXP_INSTALL=1; # If 1, empty the temporary directory, install DXP from scratch, build, remove temporary directory; if 0, potentially reuse the DXP already installed in temporary directory, keep temporary directory for future uses.
2930
BASE_DXP_BRANCH="${BASE_DXP_BRANCH:-}"; # Branch from and for which the Reference is built when using a dev branch as version; can be overridden by the BASE_DXP_BRANCH env var
@@ -61,42 +62,47 @@ fi;
6162
cd $TMP_DXP_DIR; # /!\ Change working directory (reason why all paths must be absolute)
6263

6364
if [ 0 -eq $DXP_ALREADY_EXISTS ]; then
65+
if [ ! -f $AUTH_JSON ]; then
66+
echo "Credentials file ${AUTH_JSON} doesn't exist.";
67+
exit 3;
68+
fi;
69+
6470
echo "Creating ibexa/$DXP_EDITION-skeleton:$DXP_VERSION project in ${TMP_DXP_DIR}";
6571
if [[ "$DXP_VERSION" == *".x-dev" ]]; then
66-
composer create-project ibexa/website-skeleton:$DXP_VERSION . --no-interaction --ignore-platform-reqs --no-scripts --stability=dev;
72+
COMPOSER_AUTH="$(tr -d '\n' < $AUTH_JSON)" $COMPOSER_BINARY create-project ibexa/website-skeleton:$DXP_VERSION . --no-interaction --ignore-platform-reqs --no-scripts --stability=dev;
6773
if [ -n "$AUTH_JSON" ]; then
6874
cp $AUTH_JSON ./;
6975
fi;
70-
composer config repositories.ibexa composer https://updates.ibexa.co;
71-
composer config extra.symfony.endpoint "https://api.github.com/repos/ibexa/recipes-dev/contents/index.json?ref=flex/main";
72-
composer require ibexa/$DXP_EDITION:$DXP_VERSION --no-interaction --update-with-all-dependencies --no-install --ignore-platform-reqs --no-scripts;
76+
$COMPOSER_BINARY config repositories.ibexa composer https://updates.ibexa.co;
77+
$COMPOSER_BINARY config extra.symfony.endpoint "https://api.github.com/repos/ibexa/recipes-dev/contents/index.json?ref=flex/main";
78+
$COMPOSER_BINARY require ibexa/$DXP_EDITION:$DXP_VERSION --no-interaction --update-with-all-dependencies --no-install --ignore-platform-reqs --no-scripts;
7379
elif [[ "$DXP_VERSION" == *"-rc"* ]]; then
74-
composer create-project ibexa/website-skeleton:$DXP_VERSION . --no-interaction --ignore-platform-reqs --no-scripts --stability=rc;
80+
COMPOSER_AUTH="$(tr -d '\n' < $AUTH_JSON)" $COMPOSER_BINARY create-project ibexa/website-skeleton:$DXP_VERSION . --no-interaction --ignore-platform-reqs --no-scripts --stability=rc;
7581
if [ -n "$AUTH_JSON" ]; then
7682
cp $AUTH_JSON ./;
7783
fi;
78-
composer config repositories.ibexa composer https://updates.ibexa.co;
79-
composer require ibexa/$DXP_EDITION:$DXP_VERSION --no-interaction --update-with-all-dependencies --no-install --ignore-platform-reqs --no-scripts;
84+
$COMPOSER_BINARY config repositories.ibexa composer https://updates.ibexa.co;
85+
$COMPOSER_BINARY require ibexa/$DXP_EDITION:$DXP_VERSION --no-interaction --update-with-all-dependencies --no-install --ignore-platform-reqs --no-scripts;
8086
else
81-
composer create-project ibexa/$DXP_EDITION-skeleton:$DXP_VERSION . --no-interaction --no-install --ignore-platform-reqs --no-scripts;
87+
COMPOSER_AUTH="$(tr -d '\n' < $AUTH_JSON)" $COMPOSER_BINARY create-project ibexa/$DXP_EDITION-skeleton:$DXP_VERSION . --no-interaction --no-install --ignore-platform-reqs --no-scripts;
8288
if [ -n "$AUTH_JSON" ]; then
8389
cp $AUTH_JSON ./;
8490
fi;
8591
fi;
86-
composer install --no-interaction --ignore-platform-reqs --no-scripts;
92+
$COMPOSER_BINARY install --no-interaction --ignore-platform-reqs --no-scripts;
8793
fi;
8894

8995
if [[ "$DXP_VERSION" == *".*"* ]]; then
9096
export COMPOSER_ROOT_VERSION=0.0.0;
91-
DXP_VERSION=$(composer -n show ibexa/$DXP_EDITION | grep -E "^version" | cut -d 'v' -f 3);
97+
DXP_VERSION=$($COMPOSER_BINARY -n show ibexa/$DXP_EDITION | grep -E "^version" | cut -d 'v' -f 3);
9298
echo "Obtained version: $DXP_VERSION";
9399
fi;
94100

95101
export COMPOSER_ROOT_VERSION=$DXP_VERSION;
96102

97103
if [ 0 -eq $DXP_ALREADY_EXISTS ]; then
98104
for additional_package in "${DXP_ADD_ONS[@]}"; do
99-
composer require --no-interaction --ignore-platform-reqs --no-scripts --with-all-dependencies ibexa/$additional_package:$DXP_VERSION;
105+
$COMPOSER_BINARY require --no-interaction --ignore-platform-reqs --no-scripts --with-all-dependencies ibexa/$additional_package:$DXP_VERSION;
100106
done;
101107
fi;
102108

@@ -118,7 +124,7 @@ if [ 0 -eq $DXP_ALREADY_EXISTS ]; then
118124
package=$(echo $line | cut -d '"' -f 2);
119125
if [[ ! "${DXP_EDITIONS[*]}" =~ "${package/ibexa\//}" ]]; then
120126
PACKAGE_MAP="$PACKAGE_MAP\n'$package': '$edition',"
121-
NAMESPACES=$(composer show "$package" --available --format=json | \
127+
NAMESPACES=$($COMPOSER_BINARY show "$package" --available --format=json | \
122128
jq -r --arg PACKAGE "$package" '"'\''\(.autoload | ."psr-4" | try to_entries[] catch empty | .key[:-1] | sub("\\\\";"\\\\\\";"g"))'\'': '\''\($PACKAGE)'\'',"')
123129
NAMESPACE_MAP="$NAMESPACE_MAP\n$NAMESPACES"
124130
fi;
@@ -131,7 +137,7 @@ if [ 0 -eq $DXP_ALREADY_EXISTS ]; then
131137

132138
echo -n 'Building namespace→edition map… ';
133139
for package in "${DXP_ADD_ONS[@]}"; do
134-
NAMESPACES=$(composer show "ibexa/$package" --available --format=json | \
140+
NAMESPACES=$($COMPOSER_BINARY show "ibexa/$package" --available --format=json | \
135141
jq -r --arg PACKAGE "ibexa/$package" '"'\''\(.autoload | ."psr-4" | try to_entries[] catch empty | .key[:-1] | sub("\\\\";"\\\\\\";"g"))'\'': '\''\($PACKAGE)'\'',"')
136142
NAMESPACE_MAP="$NAMESPACE_MAP\n$NAMESPACES"
137143
PACKAGE_MAP="$PACKAGE_MAP\n'ibexa/$package': 'optional',"
@@ -217,8 +223,8 @@ fi;
217223

218224
if [ 0 -eq $DXP_ALREADY_EXISTS ]; then
219225
echo 'Set up DXP recipes…';
220-
git init && git add . && git commit -m "Installed Ibexa Commerce" > /dev/null;
221-
composer recipes:install ibexa/$DXP_EDITION --force --reset --no-interaction;
226+
git init -b main && git add . && git commit -m "Installed Ibexa Commerce" > /dev/null;
227+
$COMPOSER_BINARY recipes:install ibexa/$DXP_EDITION --force --reset --no-interaction;
222228
fi;
223229

224230
echo 'Dump REST OpenAPI schema… ';

0 commit comments

Comments
 (0)