Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
223 changes: 223 additions & 0 deletions .github/workflows/regenerate-connector-template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
name: Regenerate OpenAPI Connectors Template

on:
workflow_call:
inputs:
openapi-url:
description: "URL of the OpenAPI JSON"
required: false
type: string
flatten-openapi:
description: "Enable OpenAPI Flattening"
required: false
type: boolean
default: true
additional-flatten-flags:
description: "Additional flags for OpenAPI Flattening"
required: false
type: string
default: ""
align-openapi:
description: "Enable OpenAPI Alignment"
required: false
type: boolean
default: true
additional-align-flags:
description: "Additional flags for OpenAPI Alignment"
required: false
type: string
default: ""
additional-generation-flags:
description: "Additional flags for OpenAPI Generation"
required: false
type: string
default: ""
distribution-zip:
description: "Distribution of the Ballerina version to be used"
required: false
type: string
default: ""
auto-merge:
description: "Enable auto-merge of the PR"
required: false
type: boolean
default: true
ballerina-version:
description: "Ballerina Language Version"
required: false
type: string
default: ""

jobs:
build:
name: Regenerate openapi-connector
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
token: ${{ secrets.BALLERINA_BOT_TOKEN }}

- name: Set Ballerina Version
if: ${{ inputs.ballerina-version }}
run: |
FILE="gradle.properties"
sed -i "s/^ballerinaLangVersion=.*/ballerinaLangVersion=${{ inputs.ballerina-version }}/" "$FILE"

- name: Get Ballerina Version
run: |
BAL_VERSION=$(grep -w 'ballerinaLangVersion' gradle.properties | cut -d= -f2 | rev | cut --complement -d- -f1 | rev)
if [ -z "$BAL_VERSION" ]; then
BAL_VERSION="latest"
fi
echo "BAL_VERSION=$BAL_VERSION" >> $GITHUB_ENV
echo "Ballerina Version: $BAL_VERSION"
echo "BALLERINA_CMD=bal" >> $GITHUB_ENV

- name: Set Up Ballerina
if: ${{ inputs.distribution-zip == '' }}
uses: ballerina-platform/setup-ballerina@v1.1.3
with:
version: ${{ env.BAL_VERSION }}

- name: Set up JDK 21
uses: actions/setup-java@v3
with:
distribution: "temurin"
java-version: 21.0.3

- name: Set ENV Variables
run: |
echo -e '${{ toJson(secrets) }}' | jq -r 'to_entries[] | .key + "=" + .value' >> $GITHUB_ENV

- name: Remove Existing Client Files
working-directory: ballerina
run: |
rm -f client.bal utils.bal types.bal

- name: Get Ballerina Distribution
if: ${{ inputs.distribution-zip }}
run: |
pwd
echo "Downloading Ballerina distribution from provided URL..."
wget -q --no-check-certificate -O ballerina.zip "${{ inputs.distribution-zip }}"
unzip -o -q ballerina.zip -d temp_folder/
cp -r temp_folder/ballerina-*/. ballerina-dist/
rm -r temp_folder/ ballerina.zip
echo "BALLERINA_CMD=${PWD}/ballerina-dist/bin/bal" >> $GITHUB_ENV

- name: Download openapi.json
if: ${{ inputs.openapi-url }}
working-directory: docs/spec
run: |
echo "Downloading openapi.json from provided URL..."
rm -f openapi.json
wget -O openapi.json "${{ inputs.openapi-url }}"

- name: Read File Extension
run: |
file=$(echo openapi.*)
ext="${file##*.}"
echo "EXTENSION=$ext" >> "$GITHUB_ENV"

- name: Flatten OpenAPI Specification
if: ${{ inputs.flatten-openapi }}
working-directory: docs/spec
run: |
echo "Flattening OpenAPI Specification..."
${{ env.BALLERINA_CMD }} openapi flatten -i openapi.${{ env.EXTENSION}} ${{ inputs.additional-flatten-flags }}
mv flattened_openapi.${{ env.EXTENSION}} openapi.${{ env.EXTENSION}}

- name: Align OpenAPI Specification
working-directory: docs/spec
if: ${{ inputs.align-openapi }}
run: |
echo "Aligning OpenAPI Specification..."
${{ env.BALLERINA_CMD }} openapi align -i openapi.${{ env.EXTENSION}} ${{ inputs.additional-align-flags }}
mv aligned_ballerina_openapi.${{ env.EXTENSION}} openapi.${{ env.EXTENSION}}

- name: Generate Ballerina Code
working-directory: docs/spec
run: |
echo "Generating Ballerina code from OpenAPI Specification..."
${{ env.BALLERINA_CMD }} openapi -i openapi.${{ env.EXTENSION}} -o ../../ballerina/ --mode client --license ../license.txt ${{ inputs.additional-generation-flags }}

- name: Configure Git Identity
run: |
git config user.name "chathushkaayash"
git config user.email "chathushkaayash@gmail.com"

- name: Remove Downloaded Ballerina Distribution
if: ${{ inputs.distribution-zip }}
run: |
rm -r ballerina-dist/

- name: Commit Files
id: commitFiles
run: |
git checkout -b regenerate-openapi-connector-${{ github.run_number }}
git add .
if git diff --cached --quiet;
then
echo "hasChanged=false" >> $GITHUB_OUTPUT
echo "No changes to commit."
else
git commit -m "[AUTOMATED] Regenerate the OpenAPI Connector"
echo "hasChanged=true" >> $GITHUB_OUTPUT
fi

- name: Push Results
if: ${{ steps.commitFiles.outputs.hasChanged == 'true' }}
run: git push origin regenerate-openapi-connector-${{ github.run_number }}
env:
GITHUB_TOKEN: ${{ secrets.BALLERINA_BOT_TOKEN }}

- name: Create Pull Request
id: createPR
if: ${{ steps.commitFiles.outputs.hasChanged == 'true' }}
run: |
prUrl=$(gh pr create \
--title "[Automated] Regenerate OpenAPI Connector" \
--body "Automated regenerate of the OpenAPI-based Ballerina connector." \
--base ${{ github.ref }} \
--head regenerate-openapi-connector-${{ github.run_number }})
echo "prUrl=$prUrl" >> $GITHUB_OUTPUT
env:
GITHUB_TOKEN: ${{ secrets.BALLERINA_BOT_TOKEN }}

- name: Approve PR
if: ${{ steps.commitFiles.outputs.hasChanged == 'true' }}
run: |
sleep 5
gh pr review --approve ${{ steps.createPR.outputs.prUrl }}
env:
GITHUB_TOKEN: ${{ secrets.BALLERINA_REVIEWER_BOT_TOKEN }}

- name: Merge PR
if: ${{ steps.commitFiles.outputs.hasChanged == 'true' && inputs.auto-merge == true }}
run: |
checkComplete="0"
while [ "$checkComplete" != "1" ]; do
sleep 20

allChecks=$(gh pr status --json statusCheckRollup --jq '.currentBranch.statusCheckRollup')
failedCount=$(echo "$allChecks" | jq '[.[] | select(.conclusion == "FAILURE" or .conclusion == "CANCELLED")] | length')
incompleteCount=$(echo "$allChecks" | jq '[.[] | select(.conclusion != "SUCCESS" and .conclusion != "SKIPPED" and .conclusion != "FAILURE" and .conclusion != "CANCELLED")] | length')

if [[ "$failedCount" -gt 0 ]]; then
echo "Some status checks failed or were cancelled."
exit 1
fi

if [[ "$incompleteCount" -eq 0 ]]; then
checkComplete="1"
echo "All required checks have passed."
else
echo "Waiting for checks to complete... ($incompleteCount remaining)"
fi
done
sleep 20
gh pr merge --merge --delete-branch
env:
GITHUB_TOKEN: ${{ secrets.BALLERINA_BOT_TOKEN }}
70 changes: 70 additions & 0 deletions .github/workflows/regenerate-connectors.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Regenerate OpenAPI Connectors

on:
workflow_dispatch:
inputs:
flatten-openapi:
description: "Enable OpenAPI Flattening"
required: false
type: boolean
default: true
additional-flatten-flags:
description: "Additional flags for OpenAPI Flattening"
required: false
type: string
default: ""
align-openapi:
description: "Enable OpenAPI Alignment"
required: false
type: boolean
default: true
additional-align-flags:
description: "Additional flags for OpenAPI Alignment"
required: false
type: string
default: ""
additional-generation-flags:
description: "Additional flags for OpenAPI Generation"
required: false
type: string
default: ""
distribution-zip:
description: "Distribution of the Ballerina version to be used"
required: false
type: string
default: ""
auto-merge:
description: "Enable auto-merge of the PR"
required: false
type: boolean
default: true
ballerina-version:
description: "Ballerina Language Version"
required: false
type: string
default: ""

jobs:
build:
name: Regenerate OpenAPI Connectors
runs-on: ubuntu-latest
if: github.repository_owner == 'ballerina-platform'
steps:
- uses: actions/checkout@v2

- name: Regenerate OpenAPI Connectors
uses: ballerina-platform/ballerina-action@2201.9.0
with:
args: run
env:
WORKING_DIR: ./regenerate-openapi-connectors
BALLERINA_BOT_TOKEN: ${{ secrets.BALLERINA_BOT_TOKEN }}
FLATTEN_OPENAPI: ${{ github.event.inputs.flatten-openapi }}
ADDITIONAL_FLATTEN_FLAGS: ${{ github.event.inputs.additional-flatten-flags }}
ALIGN_OPENAPI: ${{ github.event.inputs.align-openapi }}
ADDITIONAL_ALIGN_FLAGS: ${{ github.event.inputs.additional-align-flags }}
ADDITIONAL_GENERATION_FLAGS: ${{ github.event.inputs.additional-generation-flags }}
DISTRIBUTION_ZIP: ${{ github.event.inputs.distribution-zip }}
AUTO_MERGE: ${{ github.event.inputs.auto-merge }}
BALLERINA_VERSION: ${{ github.event.inputs.ballerina-version }}
BAL_CONFIG_VAR_WORKFLOW: "regenerate-connector.yml"
5 changes: 5 additions & 0 deletions regenerate-openapi-connectors/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
org = "ballerina_library"
name = "regenerate_openapi_connectors"
version = "0.1.0"
distribution = "2201.11.0"
Loading