2024-03-04 13:24:29 -08:00
|
|
|
# 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.
|
|
|
|
|
|
2024-04-17 14:22:10 -07:00
|
|
|
import subprocess
|
2024-03-04 13:24:29 -08:00
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main() -> None:
|
|
|
|
|
"""
|
|
|
|
|
Update CHANGELOG.md when API generator produces new code differing from existing.
|
|
|
|
|
"""
|
2024-04-17 14:22:10 -07:00
|
|
|
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.")
|
2024-03-04 13:24:29 -08:00
|
|
|
|
2024-04-17 14:22:10 -07:00
|
|
|
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}"
|
|
|
|
|
)
|
2024-03-04 13:24:29 -08:00
|
|
|
|
2024-04-17 14:22:10 -07:00
|
|
|
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"
|
|
|
|
|
)
|
2024-03-04 13:24:29 -08:00
|
|
|
else:
|
2024-04-17 14:22:10 -07:00
|
|
|
print("No changes detected")
|
2024-03-04 13:24:29 -08:00
|
|
|
|
2024-04-17 14:22:10 -07:00
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
|
print(f"Error occurred while checking Git status: {e}")
|
2024-03-04 13:24:29 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|