Auto Proto Convert #242
This file contains hidden or 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: Auto Proto Convert | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| artifact_download_url: | |
| description: 'Download URL for the api-spec build artifact zip.' | |
| required: true | |
| type: string | |
| api_spec_commit: | |
| description: 'Optional api-spec commit SHA for PR metadata.' | |
| required: false | |
| type: string | |
| api_spec_run_id: | |
| description: 'Optional api-spec workflow run ID for PR metadata.' | |
| required: false | |
| type: string | |
| 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 | |
| if: github.repository == 'opensearch-project/opensearch-protobufs' | |
| steps: | |
| - name: Checkout Repository | |
| uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@3235b876344d2a9aa001b8d1453c930bba69e610 # v3 | |
| with: | |
| node-version: 20 | |
| - name: Setup Java | |
| uses: actions/setup-java@17f84c3641ba7b8f6deff6309fc4c864478f5d62 # v3 | |
| with: | |
| distribution: temurin | |
| java-version: 17 | |
| - name: Resolve provided artifact metadata | |
| id: api_spec_artifact | |
| env: | |
| ARTIFACT_DOWNLOAD_URL: ${{ inputs.artifact_download_url || github.event.client_payload.artifact_download_url || '' }} | |
| API_SPEC_COMMIT: ${{ inputs.api_spec_commit || github.event.client_payload.api_spec_commit || '' }} | |
| API_SPEC_RUN_ID: ${{ inputs.api_spec_run_id || github.event.client_payload.api_spec_run_id || '' }} | |
| run: | | |
| if [ -z "$ARTIFACT_DOWNLOAD_URL" ]; then | |
| echo "artifact_download_url is required for both workflow_dispatch and repository_dispatch." | |
| exit 1 | |
| fi | |
| echo "artifact_download_url=$ARTIFACT_DOWNLOAD_URL" >> "$GITHUB_OUTPUT" | |
| echo "source_commit=${API_SPEC_COMMIT:-unknown}" >> "$GITHUB_OUTPUT" | |
| echo "run_id=${API_SPEC_RUN_ID:-unknown}" >> "$GITHUB_OUTPUT" | |
| - name: Download API spec build artifact | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| curl -L \ | |
| -H "Authorization: Bearer $GH_TOKEN" \ | |
| -H "Accept: application/vnd.github+json" \ | |
| -o api-spec-build.zip \ | |
| "${{ steps.api_spec_artifact.outputs.artifact_download_url }}" | |
| rm -rf api-spec-build | |
| unzip -q api-spec-build.zip -d api-spec-build | |
| SPEC_PATH=$(find api-spec-build -name opensearch-openapi.yaml -print -quit) | |
| if [ -z "$SPEC_PATH" ]; then | |
| echo "Could not find opensearch-openapi.yaml in downloaded artifact" | |
| exit 1 | |
| fi | |
| cp "$SPEC_PATH" opensearch-openapi.yaml | |
| - name: Get Latest OpenSearch Core Version | |
| id: get_opensearch_version | |
| uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # 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@ea165f8d65b6e75b540449e92b4886f43607fa02 # 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' && github.repository == 'opensearch-project/opensearch-protobufs' | |
| uses: peter-evans/create-pull-request@22a9089034f40e5a961c8808d113e2c98fb63676 # 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.api_spec_artifact.outputs.source_commit }})" | |
| 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.api_spec_artifact.outputs.source_commit }} | |
| **API Spec Build Run**: ${{ steps.api_spec_artifact.outputs.run_id }} | |
| --- | |
| ${{ steps.merge_report.outputs.report }} |