|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# |
| 3 | +# The OpenSearch Contributors require contributions made to |
| 4 | +# this file be licensed under the Apache-2.0 license or a |
| 5 | +# compatible open source license. |
| 6 | +# |
| 7 | +# Modifications Copyright OpenSearch Contributors. See |
| 8 | +# GitHub history for details. |
| 9 | + |
| 10 | +import filecmp |
| 11 | +import os |
| 12 | +import shutil |
| 13 | + |
| 14 | +import requests |
| 15 | + |
| 16 | + |
| 17 | +def main() -> None: |
| 18 | + """ |
| 19 | + Update CHANGELOG.md when API generator produces new code differing from existing. |
| 20 | + """ |
| 21 | + root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 22 | + |
| 23 | + after_paths = [ |
| 24 | + os.path.join(root_dir, f"opensearchpy/{folder}") |
| 25 | + for folder in ["client", "_async/client"] |
| 26 | + ] |
| 27 | + |
| 28 | + before_paths = [ |
| 29 | + os.path.join(root_dir, f"before_generate/{folder}") |
| 30 | + for folder in ["client", "async_client"] |
| 31 | + ] |
| 32 | + |
| 33 | + # Compare only .py files and take their union for client and async_client directories |
| 34 | + before_files_client = set( |
| 35 | + file for file in os.listdir(before_paths[0]) if file.endswith(".py") |
| 36 | + ) |
| 37 | + after_files_client = set( |
| 38 | + file for file in os.listdir(after_paths[0]) if file.endswith(".py") |
| 39 | + ) |
| 40 | + |
| 41 | + before_files_async_client = set( |
| 42 | + file for file in os.listdir(before_paths[1]) if file.endswith(".py") |
| 43 | + ) |
| 44 | + after_files_async_client = set( |
| 45 | + file for file in os.listdir(after_paths[1]) if file.endswith(".py") |
| 46 | + ) |
| 47 | + |
| 48 | + all_files_union_client = before_files_client.union(after_files_client) |
| 49 | + all_files_union_async_client = before_files_async_client.union( |
| 50 | + after_files_async_client |
| 51 | + ) |
| 52 | + |
| 53 | + # Compare files and check for mismatches or errors for client and async_client directories |
| 54 | + mismatch_client, errors_client = filecmp.cmpfiles( |
| 55 | + before_paths[0], after_paths[0], all_files_union_client, shallow=True |
| 56 | + )[1:] |
| 57 | + mismatch_async_client, errors_async_client = filecmp.cmpfiles( |
| 58 | + before_paths[1], after_paths[1], all_files_union_async_client, shallow=True |
| 59 | + )[1:] |
| 60 | + |
| 61 | + if mismatch_client or errors_client or mismatch_async_client or errors_async_client: |
| 62 | + print("Changes detected") |
| 63 | + response = requests.get( |
| 64 | + "https://api.github.com/repos/opensearch-project/opensearch-api-specification/commits" |
| 65 | + ) |
| 66 | + if response.ok: |
| 67 | + commit_info = response.json()[0] |
| 68 | + commit_url = commit_info["html_url"] |
| 69 | + latest_commit_sha = commit_info.get("sha") |
| 70 | + else: |
| 71 | + raise Exception( |
| 72 | + f"Failed to fetch opensearch-api-specification commit information. Status code: {response.status_code}" |
| 73 | + ) |
| 74 | + |
| 75 | + with open("CHANGELOG.md", "r+", encoding="utf-8") as file: |
| 76 | + content = file.read() |
| 77 | + if commit_url not in content: |
| 78 | + if "### Updated APIs" in content: |
| 79 | + file_content = content.replace( |
| 80 | + "### Updated APIs", |
| 81 | + f"### Updated APIs\n- Updated opensearch-py APIs to reflect [opensearch-api-specification@{latest_commit_sha[:7]}]({commit_url})", |
| 82 | + 1, |
| 83 | + ) |
| 84 | + file.seek(0) |
| 85 | + file.write(file_content) |
| 86 | + file.truncate() |
| 87 | + else: |
| 88 | + raise Exception( |
| 89 | + "'Updated APIs' section is not present in CHANGELOG.md" |
| 90 | + ) |
| 91 | + else: |
| 92 | + print("No changes detected") |
| 93 | + |
| 94 | + # Clean up |
| 95 | + for path in before_paths: |
| 96 | + shutil.rmtree(path) |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + main() |
0 commit comments