github_action: Fix "notify-if-api-docs-changed" tool.

Fix `notify-if-api-docs-changed` tool to send the notification only
when "changelog.md" is changed in the PR.
This commit is contained in:
Vector73
2025-10-15 17:28:50 +00:00
committed by Tim Abbott
parent 740be1d55c
commit 2dca184fd3
2 changed files with 48 additions and 2 deletions

View File

@@ -54,14 +54,15 @@ jobs:
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.x"
- name: Add required permissions
run: chmod +x ./tools/notify-if-api-docs-changed
- name: Run tools/github-changes-contain-file
run: ./tools/github-changes-contain-file api_docs/changelog.md
- name: Run tools/notify-if-api-docs-changed
id: run_check

View File

@@ -0,0 +1,45 @@
#!/usr/bin/env python3
import argparse
import json
import os
import subprocess
import sys
from pathlib import Path
def is_file_changed(file_path: str) -> bool:
event_path = os.environ.get("GITHUB_EVENT_PATH", "")
if not event_path:
sys.exit("GITHUB_EVENT_PATH environment variable not set")
with open(Path(event_path)) as f:
event = json.load(f)
before = event.get("before")
after = event.get("after")
if not before or not after:
sys.exit("Missing 'before' or 'after' commit SHAs in event data")
try:
result = subprocess.run(
["git", "diff", "--quiet", before, after, "--", file_path],
check=False,
capture_output=True,
)
return result.returncode == 1
except subprocess.CalledProcessError:
sys.exit(1)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"file",
type=str,
)
args = parser.parse_args()
if is_file_changed(args.file):
sys.exit(0)
else:
sys.exit(1)