Files
zulip/tools/notify-if-api-docs-changed
Vector73 a5d25826bd github_action: Mention PR where the endpoints were added.
Updates "API Documentation Update Check" tool to  add PR
information in the message to chat.zulip.org when the
new endpoints are added.
2025-10-09 11:39:54 -07:00

70 lines
2.2 KiB
Python
Executable File

#!/usr/bin/env python3
import json
import os
import re
import sys
from pathlib import Path
from urllib.request import Request, urlopen
TOOLS_DIR = os.path.dirname(os.path.abspath(__file__))
os.chdir(os.path.dirname(TOOLS_DIR))
sys.path.insert(0, os.path.dirname(TOOLS_DIR))
from zerver.openapi.merge_api_changelogs import get_feature_level
def get_pull_request_number_or_commit_hash() -> str:
github_token = os.environ["GITHUB_TOKEN"]
repo = os.environ["GITHUB_REPOSITORY"]
commit_hash = os.environ["GITHUB_SHA"]
url = f"https://api.github.com/repos/{repo}/commits/{commit_hash}/pulls"
headers = {
"Accept": "application/vnd.github.groot-preview+json",
"Authorization": f"token {github_token}",
}
try:
req = Request(url, headers=headers)
with urlopen(req) as response:
pull_requests = json.load(response)
if len(pull_requests) > 0:
return f"#{pull_requests[0]['number']}"
return commit_hash
except Exception:
return commit_hash
def get_changed_api_endpoints() -> list[str]:
changelog_path = Path("api_docs/changelog.md")
feature_level_pattern = re.compile(r"\*\*Feature level \d+\*\*")
link_pattern = re.compile(r"(\[[^\]]+\]\([^)]+\))")
current_feature_level_found = False
endpoints = []
with open(changelog_path) as file:
for line in file:
if re.fullmatch(feature_level_pattern, line.strip()):
if current_feature_level_found:
break
current_feature_level_found = True
continue
if current_feature_level_found and line.strip().startswith("*"):
endpoints.extend(link_pattern.findall(line.split(":", 1)[0]))
return endpoints
if __name__ == "__main__":
pull_request = get_pull_request_number_or_commit_hash()
feature_level = get_feature_level(update_feature_level=False)
endpoints = get_changed_api_endpoints()
topic = f"new feature level: {feature_level}"
endpoints_string = ", ".join(endpoints)
content = f"{pull_request} has updated the API documentation for the following endpoints: {endpoints_string}."
print(f"topic={topic}\ncontent={content}")