Updates SDK based on api-definitions #925
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Updates SDK based on api-definitions | |
on: | |
workflow_dispatch: | |
inputs: | |
trigger-reason: | |
description: | | |
The comment that will be posted to the PR if any changes are added. | |
Used to provide context about the source of the update. | |
required: false | |
jobs: | |
generate-sdk: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout api-definitions repo | |
uses: actions/checkout@v4 | |
with: | |
path: 'api-definitions' | |
repository: 'Rebilly/api-definitions' | |
- name: Checkout PHP SDK repo | |
uses: actions/checkout@v4 | |
with: | |
path: 'sdk' | |
fetch-depth: 0 | |
- name: Keep changesets | |
working-directory: ./sdk | |
run: | | |
mkdir ../existing-changesets | |
git checkout automations/update-sdk-from-api-definitions || true | |
ls -lAh .changeset | |
cp .changeset/* ../existing-changesets/ | |
git checkout main || true | |
cp ../existing-changesets/* .changeset/ | |
- name: Checkout generator repo | |
uses: actions/checkout@v4 | |
with: | |
token: '${{ secrets.MACHINE_USER_PAT }}' | |
path: 'generator' | |
repository: 'Rebilly/rebilly' | |
sparse-checkout: | | |
backend/php-sdk-generator/remove-fqcns.php | |
backend/php-sdk-generator/remove-unused-models.php | |
sparse-checkout-cone-mode: true | |
- name: Setup PHP | |
uses: shivammathur/setup-php@v2 | |
with: | |
php-version: '8.1' | |
extensions: mbstring, intl, curl, json | |
tools: composer:v2 | |
- name: Install api-definitions dependencies | |
working-directory: ./api-definitions | |
run: npm install | |
- name: Build api-definitions | |
working-directory: ./api-definitions | |
run: npx redocly bundle all@latest -o dist/all.yaml | |
- name: Login to GitHub Registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.MACHINE_USER_PAT }} | |
- name: Build SDK | |
run: | | |
mkdir /tmp/sdk | |
docker run --rm \ | |
-u $(id -u ${USER}):$(id -g ${USER}) \ | |
-v "/tmp/sdk:/out" \ | |
-v "$(pwd)/api-definitions/dist:/api-definitions" \ | |
ghcr.io/rebilly/regenerator:latest generate \ | |
-g rebilly-php \ | |
-D /out/openapi.json \ | |
-o /out \ | |
-C '{"rootNameSpace": "Rebilly.Sdk", "customOperationGroupMapping": "/shared/custom-resource-names.json"}' \ | |
"/api-definitions/all.yaml" | |
ls -lAh /tmp/ /tmp/sdk/ | |
- name: Bundle SDK | |
run: | | |
rm -rf ./sdk/{src,composer.json,.php-cs-fixer.php,psalm.xml} && \ | |
cp -r /tmp/sdk/{src,composer.json,.php-cs-fixer.php,psalm.xml} ./sdk/ | |
cd ./sdk && node ./scripts/update-sdk-version.js | |
- name: Get composer cache directory | |
working-directory: /tmp/sdk | |
id: composer-cache | |
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT | |
- name: Cache composer dependencies | |
uses: actions/cache@v2 | |
with: | |
path: ${{ steps.composer-cache.outputs.dir }} | |
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }} | |
restore-keys: ${{ runner.os }}-composer- | |
- name: Cache npm dependencies | |
uses: actions/cache@v2 | |
with: | |
path: '~/.npm' | |
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} | |
restore-keys: ${{ runner.os }}-node- | |
- name: Install composer dependencies | |
working-directory: ./sdk | |
run: composer install --prefer-dist | |
- name: CS fix SDK | |
working-directory: ./sdk | |
run: | | |
composer cs-fix | |
rm .php-cs-fixer.cache # Remove cache for the second run | |
composer cs-fix # Run twice to ensure all large files are fixed (required due to a bug in the php-cs-fixer, remove once resolved) | |
- name: Add changeset | |
id: changeset | |
if: github.event.inputs.trigger-reason != '' | |
working-directory: ./sdk | |
run: | | |
git add . | |
git status | |
if [[ `git status --porcelain` ]]; then | |
npm ci | |
export CHANGESET_ID=$(node ./scripts/add-changeset.js patch "${{ github.event.inputs.trigger-reason }}") | |
echo "Added a patch changeset '${CHANGESET_ID}': '${{ github.event.inputs.trigger-reason }}'" | |
echo "id=${CHANGESET_ID}" >> $GITHUB_OUTPUT | |
fi | |
- name: Create PR | |
id: cpr | |
uses: peter-evans/create-pull-request@v5 | |
with: | |
# Path of the checked-out repo where we placed the generated resources | |
path: 'sdk' | |
token: ${{ secrets.MACHINE_USER_PAT }} | |
title: 'chore: update SDK from api-definitions' | |
commit-message: update SDK from api-definitions | |
branch: automations/update-sdk-from-api-definitions | |
base: main | |
# Delete the branch when closing PRs or merging | |
delete-branch: true | |
body: This PR is automatically generated and updates SDK based on a change in the API definitions | |
- name: Comment trigger reason | |
uses: marocchino/sticky-pull-request-comment@v2 | |
if: | | |
(steps.cpr.outputs.pull-request-operation == 'created' || | |
steps.cpr.outputs.pull-request-operation == 'updated') && | |
github.event.inputs.trigger-reason != '' | |
with: | |
GITHUB_TOKEN: ${{ secrets.MACHINE_USER_PAT }} | |
append: true | |
number: ${{ steps.cpr.outputs.pull-request-number }} | |
message: | | |
- ${{ github.event.inputs.trigger-reason }} [edit changeset](https://github.com/Rebilly/rebilly-php/edit/automations/update-sdk-from-api-definitions/.changeset/${{ steps.changeset.outputs.id }}.md?pr=%2FRebilly%2Frebilly-php%2Fpull%2F${{ steps.cpr.outputs.pull-request-number }}) |