Skip to content

Commit

Permalink
Updated code generator to use new version of OpenAPI specification (o…
Browse files Browse the repository at this point in the history
…pensearch-project#721)

* Updated code generator to use new version of OpenAPI specification

Signed-off-by: saimedhi <[email protected]>

* Updated code generator to use native OpenAPI specification

Signed-off-by: saimedhi <[email protected]>

---------

Signed-off-by: saimedhi <[email protected]>
  • Loading branch information
saimedhi authored and AbitraryYu committed Apr 22, 2024
1 parent 291bfe3 commit 3ad5b95
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 197 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/update_api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
commit-message: Updated opensearch-py to reflect the latest OpenSearch API spec (${{ steps.date.outputs.date }})
title: Updated opensearch-py to reflect the latest OpenSearch API spec
body: |
Updated [opensearch-py](https://github.com/opensearch-project/opensearch-py) to reflect the latest [OpenSearch API spec](https://github.com/opensearch-project/opensearch-api-specification/blob/main/OpenSearch.openapi.json)
Updated [opensearch-py](https://github.com/opensearch-project/opensearch-py) to reflect the latest [OpenSearch API spec](https://github.com/opensearch-project/opensearch-api-specification/releases/download/main/opensearch-openapi.yaml)
Date: ${{ steps.date.outputs.date }}
branch: automated-api-update
base: main
Expand Down
9 changes: 5 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Removed
- Removed support for Python 3.6, 3.7 ([#717](https://github.com/opensearch-project/opensearch-py/pull/717))
### Fixed
- Updated code generator to use native OpenAPI specification ([#721](https://github.com/opensearch-project/opensearch-py/pull/721))
### Updated APIs
- Updated opensearch-py APIs to reflect [opensearch-api-specification@1879553](https://github.com/opensearch-project/opensearch-api-specification/commit/18795533eac735276a141703c8f7b5de9bc0ab35)
- Updated opensearch-py APIs to reflect [opensearch-api-specification@4fe2b42](https://github.com/opensearch-project/opensearch-api-specification/commit/4fe2b42bf76a6c5550fa819e06d2fea69db5a1ff)
Expand All @@ -20,10 +21,10 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## [2.5.0]
### Added
- Added pylint `assignment-from-no-return` and `unused-variable` (([#658](https://github.com/opensearch-project/opensearch-py/pull/658))
- Added pylint `unnecessary-dunder-calls` (([#655](https://github.com/opensearch-project/opensearch-py/pull/655))
- Changed to use .pylintrc files in root and any directory with override requirements (([#654](https://github.com/opensearch-project/opensearch-py/pull/654))
- Added pylint `unspecified-encoding` and `missing-function-docstring` and ignored opensearchpy for lints (([#643](https://github.com/opensearch-project/opensearch-py/pull/643)))
- Added pylint `assignment-from-no-return` and `unused-variable` ([#658](https://github.com/opensearch-project/opensearch-py/pull/658))
- Added pylint `unnecessary-dunder-calls` ([#655](https://github.com/opensearch-project/opensearch-py/pull/655))
- Changed to use .pylintrc files in root and any directory with override requirements ([#654](https://github.com/opensearch-project/opensearch-py/pull/654))
- Added pylint `unspecified-encoding` and `missing-function-docstring` and ignored opensearchpy for lints ([#643](https://github.com/opensearch-project/opensearch-py/pull/643))
- Added pylint `line-too-long` and `invalid-name` ([#590](https://github.com/opensearch-project/opensearch-py/pull/590))
- Added pylint `pointless-statement` ([#611](https://github.com/opensearch-project/opensearch-py/pull/611))
- Added a log collection guide ([#579](https://github.com/opensearch-project/opensearch-py/pull/579))
Expand Down
2 changes: 1 addition & 1 deletion DEVELOPER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Open `docs/build/html/index.html` to see results.

## Client Code Generator

OpenSearch publishes an [OpenAPI specification](https://github.com/opensearch-project/opensearch-api-specification/blob/main/OpenSearch.openapi.json) in the [opensearch-api-specification](https://github.com/opensearch-project/opensearch-api-specification) repository, which is used to auto-generate the less interesting parts of the client.
OpenSearch publishes an [OpenAPI specification](https://github.com/opensearch-project/opensearch-api-specification/releases/download/main/opensearch-openapi.yaml) in the [opensearch-api-specification](https://github.com/opensearch-project/opensearch-api-specification) repository, which is used to auto-generate the less interesting parts of the client.

```
nox -rs generate
Expand Down
117 changes: 42 additions & 75 deletions utils/changelog_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
# Modifications Copyright OpenSearch Contributors. See
# GitHub history for details.

import filecmp
import os
import shutil
import subprocess

import requests

Expand All @@ -18,82 +16,51 @@ def main() -> None:
"""
Update CHANGELOG.md when API generator produces new code differing from existing.
"""
root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

after_paths = [
os.path.join(root_dir, f"opensearchpy/{folder}")
for folder in ["client", "_async/client"]
]

before_paths = [
os.path.join(root_dir, f"before_generate/{folder}")
for folder in ["client", "async_client"]
]

# Compare only .py files and take their union for client and async_client directories
before_files_client = set(
file for file in os.listdir(before_paths[0]) if file.endswith(".py")
)
after_files_client = set(
file for file in os.listdir(after_paths[0]) if file.endswith(".py")
)

before_files_async_client = set(
file for file in os.listdir(before_paths[1]) if file.endswith(".py")
)
after_files_async_client = set(
file for file in os.listdir(after_paths[1]) if file.endswith(".py")
)

all_files_union_client = before_files_client.union(after_files_client)
all_files_union_async_client = before_files_async_client.union(
after_files_async_client
)
git_command = "git status"
try:
git_status = subprocess.check_output(
git_command, shell=True, stderr=subprocess.STDOUT
)
if (
"Changes to be committed:" in git_status.decode()
or "Changes not staged for commit:" in git_status.decode()
or "Untracked files:" in git_status.decode()
):
print("Changes detected; updating changelog.")

# Compare files and check for mismatches or errors for client and async_client directories
mismatch_client, errors_client = filecmp.cmpfiles(
before_paths[0], after_paths[0], all_files_union_client, shallow=True
)[1:]
mismatch_async_client, errors_async_client = filecmp.cmpfiles(
before_paths[1], after_paths[1], all_files_union_async_client, shallow=True
)[1:]
base_url = "https://api.github.com/repos/opensearch-project/opensearch-api-specification/commits"
url_with_per_page = base_url + "?per_page=1"
response = requests.get(url_with_per_page)
if response.ok:
commit_info = response.json()[0]
commit_url = commit_info["html_url"]
latest_commit_sha = commit_info.get("sha")
else:
raise Exception(
f"Failed to fetch opensearch-api-specification commit information. Status code: {response.status_code}"
)

if mismatch_client or errors_client or mismatch_async_client or errors_async_client:
print("Changes detected")
response = requests.get(
"https://api.github.com/repos/opensearch-project/opensearch-api-specification/commits"
)
if response.ok:
commit_info = response.json()[0]
commit_url = commit_info["html_url"]
latest_commit_sha = commit_info.get("sha")
with open("CHANGELOG.md", "r+", encoding="utf-8") as file:
content = file.read()
if commit_url not in content:
if "### Updated APIs" in content:
file_content = content.replace(
"### Updated APIs",
f"### Updated APIs\n- Updated opensearch-py APIs to reflect [opensearch-api-specification@{latest_commit_sha[:7]}]({commit_url})",
1,
)
file.seek(0)
file.write(file_content)
file.truncate()
else:
raise Exception(
"'Updated APIs' section is not present in CHANGELOG.md"
)
else:
raise Exception(
f"Failed to fetch opensearch-api-specification commit information. Status code: {response.status_code}"
)

with open("CHANGELOG.md", "r+", encoding="utf-8") as file:
content = file.read()
if commit_url not in content:
if "### Updated APIs" in content:
file_content = content.replace(
"### Updated APIs",
f"### Updated APIs\n- Updated opensearch-py APIs to reflect [opensearch-api-specification@{latest_commit_sha[:7]}]({commit_url})",
1,
)
file.seek(0)
file.write(file_content)
file.truncate()
else:
raise Exception(
"'Updated APIs' section is not present in CHANGELOG.md"
)
else:
print("No changes detected")
print("No changes detected")

# Clean up
for path in before_paths:
shutil.rmtree(path)
except subprocess.CalledProcessError as e:
print(f"Error occurred while checking Git status: {e}")


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 3ad5b95

Please sign in to comment.