Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Automated API Update Workflow #209

Merged
merged 2 commits into from
Jul 12, 2024
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
54 changes: 54 additions & 0 deletions .github/workflows/update_api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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: composer run generate-api
- 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
}
4 changes: 2 additions & 2 deletions util/ClientEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions util/NamespaceEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
80 changes: 80 additions & 0 deletions util/changelog_updater.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/**
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

require 'vendor/autoload.php';

use GuzzleHttp\Client;

function main()
{
// Update CHANGELOG.md when API generator produces new code differing from existing.

try {
$gitStatus = shell_exec("git status");
if ($gitStatus === null) {
throw new RuntimeException('Failed to execute git command.');
}

if (strpos($gitStatus, "Changes to be committed:") !== false ||
strpos($gitStatus, "Changes not staged for commit:") !== false ||
strpos($gitStatus, "Untracked files:") !== false) {
echo "Changes detected; updating changelog.\n";

$client = new Client();
$response = $client->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();
Loading