Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
#!/usr/bin/env python3
import json
import os
import re
import subprocess
import sys
from dataclasses import dataclass
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 tools.lib.api_design_links import find_api_design_links
from zerver.openapi.merge_api_changelogs import get_feature_level
@dataclass
class PullRequestData:
commit_hash: str
number: int | None = None
body: str | None = None
def get_pull_request_data() -> PullRequestData:
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 not pull_requests:
return PullRequestData(commit_hash=commit_hash)
return PullRequestData(
commit_hash=commit_hash,
number=pull_requests[0]["number"],
body=pull_requests[0]["body"],
)
except Exception:
return PullRequestData(commit_hash=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.replace("\n ", " ").rstrip()
def get_feature_level_before_commit() -> int:
with open(os.environ["GITHUB_EVENT_PATH"]) as f:
event = json.load(f)
before_sha = event["before"]
version_file_path = "version.py"
try:
version_content_before_commit = subprocess.check_output(
["git", "show", f"{before_sha}:{version_file_path}"], text=True
)
except subprocess.CalledProcessError:
sys.exit(1)
lines = version_content_before_commit.split("\n")
for line in lines:
if line.startswith("API_FEATURE_LEVEL = "):
match = re.search(r"\d+", line)
if match:
return int(match.group())
return 0
if __name__ == "__main__":
feature_level = get_feature_level(update_feature_level=False) - 1
if feature_level <= get_feature_level_before_commit():
sys.exit(1)
topic = f"new feature level: {feature_level}"
pull_request = get_pull_request_data()
pull_request_label = (
f"#{pull_request.number}" if pull_request.number else pull_request.commit_hash
)
changelog_entries = get_changelog_entries()
discussion_links: list[str] = []
if pull_request.body:
discussion_links = find_api_design_links(pull_request.body)
content = f"{pull_request_label} has updated the API documentation for the following endpoints: \n{changelog_entries}"
if len(discussion_links) == 1:
content += f"\n\nAPI design discussion: {discussion_links[0]}"
elif discussion_links:
content += "\n\nAPI design discussions:\n" + "\n".join(
f"- {link}" for link in discussion_links
)
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")