Files
zulip/tools/notify-if-api-docs-changed
Vector73 9ec4c04f2f tools: Fix notify-if-api-docs-changed tool.
Fixed feature level in topic name where notification is sent. Also,
changelog entries will be included in the notification message instead
of only changed endpoints.
2025-11-05 09:38:07 -08:00

73 lines
2.3 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_changelog_entries() -> str:
changelog_path = Path("api_docs/changelog.md")
feature_level_pattern = re.compile(r"\*\*Feature level \d+\*\*")
current_feature_level_found = False
changelog_entries = ""
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:
changelog_entries += line
return changelog_entries
if __name__ == "__main__":
pull_request = get_pull_request_number_or_commit_hash()
feature_level = get_feature_level(update_feature_level=False) - 1
changelog_entries = get_changelog_entries()
topic = f"new feature level: {feature_level}"
content = f"{pull_request} has updated the API documentation for the following endpoints: \n{changelog_entries}."
github_output = os.environ.get("GITHUB_OUTPUT")
if github_output:
with open(github_output, "a") as f:
f.write(f"topic={topic}\n")
f.write("content<<EOF\n")
f.write(f"{content.strip()}\n")
f.write("EOF\n")