Skip to content

Auto Proto Convert #240

Auto Proto Convert

Auto Proto Convert #240

Workflow file for this run

name: Auto Proto Convert
on:
workflow_dispatch:
inputs:
opensearch_version:
description: 'OpenSearch version. Leave empty to fetch latest.'
required: false
type: string
repository_dispatch:
types: [spec-updated]
pull_request:
paths:
- 'tools/proto-convert/**'
jobs:
auto-proto-convert:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 20
- name: Setup Java
uses: actions/setup-java@v3
with:
distribution: temurin
java-version: 17
- name: Get Latest Spec Build Run
id: get_spec_build
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const owner = "opensearch-project";
const repo = "opensearch-api-specification";
const runs = await github.rest.actions.listWorkflowRuns({
owner,
repo,
workflow_id: "build.yml",
branch: "main",
event: "push",
per_page: 20
});
const latestRun = runs.data.workflow_runs[0];
if (!latestRun) {
core.setFailed("Could not find any build.yml workflow run on main.");
return;
}
core.setOutput("run_id", String(latestRun.id));
core.setOutput("head_sha", latestRun.head_sha);
core.setOutput("conclusion", latestRun.conclusion || "unknown");
console.log("Latest build run: " + latestRun.id);
console.log("Spec commit: " + latestRun.head_sha);
console.log("Build conclusion: " + (latestRun.conclusion || "unknown"));
- name: Download Latest Spec Build Artifact
id: download_build_artifact
uses: actions/download-artifact@v5
with:
name: build
path: spec-build
github-token: ${{ secrets.API_SPEC_ACTIONS_READ_TOKEN || github.token }}
repository: opensearch-project/opensearch-api-specification
run-id: ${{ steps.get_spec_build.outputs.run_id }}
continue-on-error: true
- name: Download Latest Spec Release Asset Fallback
if: steps.download_build_artifact.outcome != 'success'
uses: robinraju/release-downloader@v1
with:
repository: 'opensearch-project/opensearch-api-specification'
latest: true
fileName: 'opensearch-openapi.yaml'
tag: 'main-latest'
preRelease: true
- name: Prepare OpenAPI Spec Source
run: |
SPEC_FILE=""
if [ -d spec-build ]; then
SPEC_FILE=$(find spec-build -maxdepth 3 -name 'opensearch-openapi.yaml' -print | head -n 1)
fi
if [ -z "$SPEC_FILE" ]; then
SPEC_FILE=$(find . -maxdepth 3 -name 'opensearch-openapi.yaml' -print | head -n 1)
fi
if [ -z "$SPEC_FILE" ]; then
echo "Could not locate opensearch-openapi.yaml from the spec source."
exit 1
fi
cp "$SPEC_FILE" opensearch-openapi.yaml
- name: Get Latest OpenSearch Core Version
id: get_opensearch_version
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
// Check if version was provided as input
const inputVersion = "${{ inputs.opensearch_version }}";
if (inputVersion && inputVersion.trim() !== "") {
core.setOutput("version", inputVersion.trim());
console.log("Using provided OpenSearch version: " + inputVersion.trim());
return;
}
// Otherwise fetch version from buildSrc/version.properties on main branch
try {
const response = await github.request(
'GET /repos/{owner}/{repo}/contents/{path}',
{
owner: 'opensearch-project',
repo: 'OpenSearch',
path: 'buildSrc/version.properties',
ref: 'main'
}
);
// Decode the file content
const content = Buffer.from(response.data.content, 'base64').toString('utf-8');
// Extract opensearch version (e.g., "opensearch = 3.4.0")
const match = content.match(/opensearch\s*=\s*([^\s]+)/);
if (match && match[1]) {
const version = match[1].trim();
core.setOutput("version", version);
console.log("Fetched OpenSearch version from source: " + version);
} else {
core.setFailed("Could not find opensearch version in gradle/libs.versions.toml");
}
} catch (error) {
core.setFailed("Could not fetch OpenSearch version: " + error.message);
}
- name: Run Proto Conversion
env:
OPENSEARCH_VERSION: ${{ steps.get_opensearch_version.outputs.version }}
run: npm ci && npm run preprocessing -- --opensearch-version "$OPENSEARCH_VERSION"
- name: Convert protobuf
env:
OPENAPI_GENERATOR_VERSION: 7.19.0
run: |
npx --yes @openapitools/openapi-generator-cli generate \
-c tools/proto-convert/src/config/protobuf-generator-config.yaml
- name: Post Process Protobuf
id: merge_report
run: |
npm run postprocessing
REPORT_PATH="/tmp/merge-report.md"
if [ -f "$REPORT_PATH" ]; then
REPORT=$(cat "$REPORT_PATH")
echo "report<<EOF" >> $GITHUB_OUTPUT
echo "$REPORT" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
else
echo "report=No changes detected." >> $GITHUB_OUTPUT
fi
- name: Configure Git User
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
- name: Check for changes
id: check_changes
run: |
if [ -z "$(git status --porcelain)" ]; then
echo "No changes detected."
echo "changed=false" >> $GITHUB_OUTPUT
else
echo "Changes detected."
echo "changed=true" >> $GITHUB_OUTPUT
fi
- name: Generate diff file for protos/schemas
if: github.event_name == 'pull_request' && steps.check_changes.outputs.changed == 'true'
run: |
git add -N .
git diff protos/schemas/ > protos-schemas.diff
- name: Upload protos/schemas diff
if: github.event_name == 'pull_request' && steps.check_changes.outputs.changed == 'true'
uses: actions/upload-artifact@v4
with:
name: protos-schemas-diff
path: protos-schemas.diff
- name: Create Pull Request if changes exist
if: github.event_name != 'pull_request' && steps.check_changes.outputs.changed == 'true'
uses: peter-evans/create-pull-request@v7
with:
token: ${{ secrets.GITHUB_TOKEN }}
branch: auto-pr-branch
commit-message: "Protobuf schema change detected"
title: "[Automated PR]: Update generated protobuf schema (OpenSearch: ${{ steps.get_opensearch_version.outputs.version }}, spec commit: ${{ steps.get_spec_build.outputs.head_sha }})"
signoff: true
base: main
delete-branch: true
labels: skip-changelog
body: |
This pull request was automatically generated by GitHub Actions.
**OpenSearch Version**: ${{ steps.get_opensearch_version.outputs.version }}
**API Spec Commit**: ${{ steps.get_spec_build.outputs.head_sha }}
---
${{ steps.merge_report.outputs.report }}