From 7b445ae3cee8b9ded08983b9df586373f76da29f Mon Sep 17 00:00:00 2001 From: saimedhi Date: Wed, 10 Jul 2024 03:00:35 -0700 Subject: [PATCH 1/2] Add Automated API Update Workflow Signed-off-by: saimedhi --- .github/workflows/update_api.yml | 56 ++++++++++++++++++++++++++++++++ util/ClientEndpoint.php | 4 +-- util/NamespaceEndpoint.php | 4 +-- 3 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/update_api.yml diff --git a/.github/workflows/update_api.yml b/.github/workflows/update_api.yml new file mode 100644 index 00000000..a12e22f9 --- /dev/null +++ b/.github/workflows/update_api.yml @@ -0,0 +1,56 @@ +name: Update API +on: + workflow_dispatch: + schedule: + - cron: "30 3 * * *" +jobs: + update-api: + if: ${{ github.repository == 'opensearch-project/opensearch-php' }} + runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: write + steps: + - uses: actions/checkout@v4 + with: + submodules: recursive + fetch-depth: 0 + - name: Config git to rebase + run: git config --global pull.rebase true + - name: Set up PHP 8.3 + uses: shivammathur/setup-php@v2 + with: + php-version: 8.3 + extensions: yaml, zip, curl + tools: composer:v2.7.2 + - name: Install dependencies + run: composer install --prefer-dist + - name: Generate API + run: php util/GenerateEndpoints.php + - name: Format generated code + run: composer run php-cs + - name: Get current date + id: date + run: echo "::set-output name=date::$(date +'%Y-%m-%d')" + - name: GitHub App token + id: github_app_token + uses: tibdex/github-app-token@v1.5.0 + with: + app_id: ${{ secrets.APP_ID }} + private_key: ${{ secrets.APP_PRIVATE_KEY }} + installation_id: 22958780 + - name: Create pull request + id: cpr + uses: peter-evans/create-pull-request@v5 + with: + token: ${{ steps.github_app_token.outputs.token }} + commit-message: Updated opensearch-php to reflect the latest OpenSearch API spec (${{ steps.date.outputs.date }}) + title: Updated opensearch-php to reflect the latest OpenSearch API spec + body: | + Updated [opensearch-php](https://github.com/opensearch-project/opensearch-php) to reflect the latest [OpenSearch API spec](https://github.com/opensearch-project/opensearch-api-specification/releases/download/main-latest/opensearch-openapi.yaml) + Date: ${{ steps.date.outputs.date }} + branch: automated-api-update + base: main + signoff: true + labels: | + autocut diff --git a/util/ClientEndpoint.php b/util/ClientEndpoint.php index 0eeffbbf..1acec505 100644 --- a/util/ClientEndpoint.php +++ b/util/ClientEndpoint.php @@ -94,12 +94,12 @@ public function renderClass(): string $endpoints = ''; foreach ($this->endpoints as $endpoint) { $endpointName = $this->getEndpointName($endpoint->name); - $proxyFilePath = 'util/endpointproxies/' . $this->name . '/' . $endpointName . 'Proxy.php'; + $proxyFilePath = __DIR__ . '/EndpointProxies/' . $this->name . '/' . $endpointName . 'Proxy.php'; if (!file_exists($proxyFilePath)) { $endpoints .= $this->renderEndpoint($endpoint); } } - $proxyFolder = 'util/endpointproxies/'; + $proxyFolder = __DIR__. '/EndpointProxies/'; if (is_dir($proxyFolder)) { $proxyFiles = glob($proxyFolder . '/*.php'); foreach ($proxyFiles as $file) { diff --git a/util/NamespaceEndpoint.php b/util/NamespaceEndpoint.php index 976f42e5..b8c7dfb4 100644 --- a/util/NamespaceEndpoint.php +++ b/util/NamespaceEndpoint.php @@ -69,13 +69,13 @@ public function renderClass(): string $endpoints = ''; foreach ($this->endpoints as $endpoint) { $endpointName = $this->getEndpointName($endpoint->name); - $proxyFilePath = 'util/endpointproxies/' . $this->name . '/' . $endpointName . 'Proxy.php'; + $proxyFilePath = __DIR__ . '/EndpointProxies/' . $this->name . '/' . $endpointName . 'Proxy.php'; if (!file_exists($proxyFilePath)) { $endpoints .= $this->renderEndpoint($endpoint); } } - $proxyFolder = 'util/endpointproxies/' . $this->name; + $proxyFolder = __DIR__ . '/EndpointProxies/' . $this->name; if (is_dir($proxyFolder)) { foreach (glob($proxyFolder . '/*Proxy.php') as $file) { $endpoints .= require $file; From 9ce6390a377e7ea34bf29662475e2d6434a289d7 Mon Sep 17 00:00:00 2001 From: saimedhi Date: Thu, 11 Jul 2024 21:22:23 -0700 Subject: [PATCH 2/2] Added Automated API Update Workflow Signed-off-by: saimedhi --- .github/workflows/update_api.yml | 4 +- CHANGELOG.md | 1 + composer.json | 5 ++ util/changelog_updater.php | 80 ++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 util/changelog_updater.php diff --git a/.github/workflows/update_api.yml b/.github/workflows/update_api.yml index a12e22f9..508f737c 100644 --- a/.github/workflows/update_api.yml +++ b/.github/workflows/update_api.yml @@ -26,9 +26,7 @@ jobs: - name: Install dependencies run: composer install --prefer-dist - name: Generate API - run: php util/GenerateEndpoints.php - - name: Format generated code - run: composer run php-cs + run: composer run generate-api - name: Get current date id: date run: echo "::set-output name=date::$(date +'%Y-%m-%d')" diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c6d28b7..3cb14f22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ## [Unreleased] ### Added - Generate endpoints from OpenSearch API Specification ([#194](https://github.com/opensearch-project/opensearch-php/pull/194)) +- Added workflow for automated API update using OpenSearch API specification ([#209](https://github.com/opensearch-project/opensearch-php/pull/209)) ### Changed ### Deprecated ### Removed diff --git a/composer.json b/composer.json index 83ed1326..6b5af53b 100644 --- a/composer.json +++ b/composer.json @@ -70,6 +70,11 @@ "chmod +x apigen", "./apigen src --output docs", "rm -f apigen" + ], + "generate-api": [ + "php util/GenerateEndpoints.php", + "composer run php-cs", + "php util/changelog_updater.php" ] } } diff --git a/util/changelog_updater.php b/util/changelog_updater.php new file mode 100644 index 00000000..8d8bd04d --- /dev/null +++ b/util/changelog_updater.php @@ -0,0 +1,80 @@ +get('https://api.github.com/repos/opensearch-project/opensearch-api-specification/commits', [ + 'query' => ['per_page' => 1], + 'headers' => ['User-Agent' => 'PHP'] + ]); + + if ($response->getStatusCode() !== 200) { + throw new RuntimeException( + 'Failed to fetch opensearch-api-specification commit information. Status code: ' . $response->getStatusCode() + ); + } + + $commitInfo = json_decode($response->getBody(), true)[0]; + $commitUrl = $commitInfo["html_url"]; + $latestCommitSha = $commitInfo["sha"]; + + $changelogPath = "CHANGELOG.md"; + $content = file_get_contents($changelogPath); + if ($content === false) { + throw new RuntimeException('Failed to read CHANGELOG.md'); + } + + if (strpos($content, $commitUrl) === false) { + if (strpos($content, "### Updated APIs") !== false) { + $fileContent = str_replace( + "### Updated APIs", + "### Updated APIs\n- Updated opensearch-php APIs to reflect [opensearch-api-specification@" . substr($latestCommitSha, 0, 7) . "]($commitUrl)", + $content + ); + + $result = file_put_contents($changelogPath, $fileContent); + if ($result === false) { + throw new RuntimeException('Failed to write to CHANGELOG.md'); + } + } else { + throw new RuntimeException("'Updated APIs' section is not present in CHANGELOG.md"); + } + } + } else { + echo "No changes detected\n"; + } + + } catch (Exception $e) { + echo "Error occurred: " . $e->getMessage() . "\n"; + } +} + +main();