Skip to content

Commit

Permalink
Add workflow to regenerate connectors
Browse files Browse the repository at this point in the history
  • Loading branch information
aneeshafedo committed Aug 23, 2023
1 parent 4501d1e commit bbaa413
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 1 deletion.
Empty file added .github/reports/summary.txt
Empty file.
76 changes: 76 additions & 0 deletions .github/workflows/test-connectors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: Regenerate Connectors

on:
# # Run nightly at 2:00 AM
# schedule:
# - cron: '0 2 * * *'
# Trigger manually with specified Ballerina Docker version
workflow_dispatch:
inputs:
bal_docker_version:
description: Ballerina Docker version
required: true
default: 'nightly'
client_method:
description: Expected Client Method Type
required: true
default: 'remote'
options: ['remote', 'resource']
artifact_id:
description: Artifact Id
required: false
default: ''


jobs:
# regenerate_connectors_with_nightly:
# if: github.event_name == 'schedule'
# runs-on: ubuntu-latest
# container:
# image: ballerina/ballerina:nightly
# options: --user root
# steps:
# - name: Checkout code
# uses: actions/checkout@v2

# - name: Build with Gradle
# env:
# JAVA_OPTS: -DBALLERINA_DEV_COMPILE_BALLERINA_ORG=true
# GRADLE_USER_HOME: ~/.gradle
# run: |
# ./gradlew build -PtestConnectorGeneration=true -PclientMethod=${{ github.event.inputs.client_method }}

regenerate_connectors_with_specified_version:
if: github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
container:
image: ballerina/ballerina:${{ github.event.inputs.bal_docker_version }}
options: --user root
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Build with Gradle
env:
JAVA_OPTS: -DBALLERINA_DEV_COMPILE_BALLERINA_ORG=true
GRADLE_USER_HOME: ~/.gradle
run: |
if [[ github.event.inputs.artifact_id != '' ]]; then
wget --header="Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" -O ballerina.zip "https://api.github.com/repos/ballerina-platform/ballerina-distribution/actions/artifacts/${{ github.event.inputs.artifact_id }}/zip"
unzip -o ballerina.zip
balDistZip=$(find . -name 'ballerina-*.zip' | head -n 1)
echo "Unzipping ${balDistZip}"
unzip -o "${balDistZip}"
EXTRACTED_DISTRIBUTION_PATH="${balDistZip%.zip}"
ABSOLUTE_EXTRACTED_DISTRIBUTION_PATH=$(realpath "$EXTRACTED_DISTRIBUTION_PATH")
echo "Start generation flow..."
./gradlew build -PtestConnectorGeneration=true -PclientMethod=${{ github.event.inputs.client_method }} -PcustomDistributionPath=$ABSOLUTE_EXTRACTED_DISTRIBUTION_PATH
else
echo "Start generation flow..."
./gradlew build -PtestConnectorGeneration=true -PclientMethod=${{ github.event.inputs.client_method }}
fi
- name: Upload Summary Report
uses: actions/upload-artifact@v2
with:
name: summary-report
path: .github/reports/summary.txt
118 changes: 117 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ boolean release = false;
boolean remote = false;
boolean buildAll = false;
boolean testTool = false;
boolean testConnectorGeneration = false;
String clientMethod = "remote";
String customDistributionPath = null;

if (project.hasProperty("remote")){
remote = new Boolean(project.property("remote").toString())
}
Expand All @@ -36,6 +40,15 @@ if (project.hasProperty("buildAll")){
if (project.hasProperty("testTool")){
testTool = new Boolean(project.property("testTool").toString())
}
if (project.hasProperty("testConnectorGeneration")){
testConnectorGeneration = new Boolean(project.property("testConnectorGeneration").toString())
}
if (project.hasProperty("clientMethod")){
clientMethod = new String(project.property("clientMethod").toString())
}
if (project.hasProperty("customDistributionPath")){
customDistributionPath = new String(project.property("customDistributionPath").toString())
}

String toolTestsDirPath = project.projectDir.absolutePath + "/tool-tests";
List<String> toolTestPackages = new ArrayList<>();
Expand All @@ -50,7 +63,7 @@ if (buildAll){
}

task codeBuild {
if (!testTool) {
if (!testTool && !testConnectorGeneration) {
println "Task: Building connectors..."
for (String path : updatedBallerinaPackages) {
Utils.executePrechecks(path);
Expand Down Expand Up @@ -151,3 +164,106 @@ task testOpenAPITool {
}
}
}

task generateNbuildLatestOpenAPIConnectors {
if (testConnectorGeneration) {
println "Task: Re-generate and build connectors..."

def generationFailedConnectors = [] // List to store generation failed connectors
def compilationFailedConnectors = [] // List to store compilation failed connectors

if (customDistributionPath == null || customDistributionPath == "") {
println "Custom distribution path is not provided. Using the default distribution path."
} else {
println "Using the custom distribution path : " + customDistributionPath
ballerinaDistributionPath = customDistributionPath;
}

for (String path : ballerinaPackages) {
def connector = path;

// Extract the connector name from the path
def regex = /\/openapi\/(.*?)$/
def matcher = (path =~ regex)
if (matcher.find()) {
connector = matcher.group(1)
}

// Re-generate the connector
println "--- Generating the connector : " + connector
try {
exec {
workingDir(path)
commandLine 'sh', '-c', "${ballerinaDistributionPath}/bin/bal openapi -i openapi.* --mode client --client-methods ${clientMethod}"
}
println "Connector generation is successful for " + connector
} catch (Exception genEx) {
println "Failed to re-generate the connector [" + connector + "]. Error : " + genEx.toString()
generationFailedConnectors << connector // Add the failed connector to the list
continue
}

// Build the connector
try {
exec {
commandLine 'sh', '-c', "${ballerinaDistributionPath}/bin/bal pack ${path}"
}
println "bal pack success for " + connector
break;
} catch (Exception buildEx) {
println "Failed to build the re-generate the connector [" + connector + "]. Error : " + buildEx.toString()
compilationFailedConnectors << connector // Add the failed connector to the list
}
}

// Generate the summary report
def summary = constructSummaryReport(ballerinaDistributionPath, ballerinaPackages.size(), generationFailedConnectors, compilationFailedConnectors)
def reportFile = file("${project.projectDir}/.github/reports/summary.txt")
reportFile.text = summary

if (generationFailedConnectors.size() == 0 && compilationFailedConnectors.size() == 0) {
println "All connectors executed successfully."
}
}
}

def constructSummaryReport(ballerinaDistributionPath, totalConnectors, generationFailedConnectors, compilationFailedConnectors) {
def summary = "# Summary \n\n"
summary += "Date and Time: ${new Date()}\n"
summary += "Ballerina Version: ${getBallerinaVersion(ballerinaDistributionPath).toString()}\n"
summary += "Total Connectors Run: ${totalConnectors}\n\n"

if (generationFailedConnectors.size() > 0 || compilationFailedConnectors.size() > 0) {
summary += "Failed Connectors:\n\n"
if (generationFailedConnectors.size() > 0) {
summary += "Generation Failed Connectors (count: ${generationFailedConnectors.size()}) :\n"
generationFailedConnectors.each { connector ->
summary += "- ${connector}\n"
}
}
if (compilationFailedConnectors.size() > 0) {
summary += "Compilation Failed Connectors (count: ${compilationFailedConnectors.size()}) :\n"
compilationFailedConnectors.each { connector ->
summary += "- ${connector}\n"
}
}
} else {
summary += "All connectors executed successfully.\n"
}

return summary
}


def getBallerinaVersion(ballerinaDistributionPath) {
def command = ballerinaDistributionPath + "/bin/bal -v"
println "Command: " + command
def process = command.execute()
process.waitFor()

def output = process.text.trim()
def ballerinaVersion = output.split('\n').find { it.startsWith("Ballerina") }

return ballerinaVersion
}

0 comments on commit bbaa413

Please sign in to comment.