Skip to content

Commit

Permalink
Added Automated API Update Workflow
Browse files Browse the repository at this point in the history
Signed-off-by: saimedhi <[email protected]>
  • Loading branch information
saimedhi committed Jul 12, 2024
1 parent 7b445ae commit 9ce6390
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/update_api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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')"
Expand Down
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"
]
}
}
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();

0 comments on commit 9ce6390

Please sign in to comment.