Files
git-auto-comment/git-auto-comment.py
T
Trez.One 8a86508ebb
Renovate / renovate (push) Successful in 2m15s
Large file handling.
2025-10-21 19:13:52 -04:00

96 lines
3.8 KiB
Python
Executable File

import os
import sys
import re
import json
import requests
# Read inputs from environment variables
platform = os.environ.get("PLATFORM", "github").lower()
token = os.environ["TOKEN"]
owner = os.environ.get("REPO_OWNER", os.environ.get("GITHUB_REPOSITORY_OWNER"))
repo = os.environ.get("REPO_NAME", os.environ.get("GITHUB_REPOSITORY"))
pr_index = os.environ["PR_INDEX"]
api_url = os.environ.get("API_URL", os.environ.get("GITHUB_API_URL"))
content_text = os.environ.get("CONTENT") # optional large text input
comment_template = os.environ.get("COMMENT_TEMPLATE", "Auto-comment: changed line -> {line}")
if not token or not pr_index:
print("TOKEN and PR_INDEX are required.", file=sys.stderr)
sys.exit(1)
# If no content provided, just post a general PR comment
if not content_text:
body = comment_template.replace("{line}", "").replace("{lines}", "")
if platform == "github":
url = f"https://api.github.com/repos/{owner}/{repo}/issues/{pr_index}/comments"
payload = {"body": body}
elif platform == "gitea":
if not api_url:
print("Gitea API URL required for Gitea platform", file=sys.stderr)
sys.exit(1)
url = f"{api_url}/repos/{owner}/{repo}/pulls/{pr_index}/comments"
payload = {"body": body}
else:
print(f"Unsupported platform: {platform}", file=sys.stderr)
sys.exit(1)
headers = {"Authorization": f"token {token}", "Content-Type": "application/json"}
resp = requests.post(url, headers=headers, json=payload)
if resp.status_code in (200, 201):
print("General comment posted successfully!")
sys.exit(0)
else:
print(f"Failed to post comment: {resp.status_code}", file=sys.stderr)
print(resp.text, file=sys.stderr)
sys.exit(1)
# --- If content_text exists, parse it for added lines like a diff ---
diff_files = {}
current_file = None
new_line_num = None
for line in content_text.splitlines():
if line.startswith("+++ b/"):
current_file = line[6:].strip()
diff_files[current_file] = []
new_line_num = 0
elif line.startswith("@@"):
m = re.match(r"@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@", line)
if m:
new_line_num = int(m.group(1)) - 1
elif line.startswith("+") and not line.startswith("+++"):
new_line_num += 1
content = line[1:]
diff_files[current_file].append((new_line_num, content))
elif not line.startswith("-"):
new_line_num += 1
# Post comments for each added line
for file_path, lines in diff_files.items():
if not lines:
continue
all_lines_content = "\n".join([line_content for _, line_content in lines])
for line_number, line_content in lines:
body = comment_template.replace("{line}", line_content).replace("{lines}", all_lines_content)
if platform == "github":
url = f"https://api.github.com/repos/{owner}/{repo}/pulls/{pr_index}/comments"
payload = {"body": body, "path": file_path, "position": line_number}
elif platform == "gitea":
if not api_url:
print("Gitea API URL required", file=sys.stderr)
sys.exit(1)
url = f"{api_url}/repos/{owner}/{repo}/pulls/{pr_index}/comments"
payload = {"body": body, "path": file_path, "position": line_number}
else:
print(f"Unsupported platform: {platform}", file=sys.stderr)
sys.exit(1)
headers = {"Authorization": f"token {token}", "Content-Type": "application/json"}
resp = requests.post(url, headers=headers, json=payload)
if resp.status_code in (200, 201):
print(f"Comment posted on {file_path}:{line_number}")
else:
print(f"Failed to post comment on {file_path}:{line_number} ({resp.status_code})", file=sys.stderr)
print(resp.text, file=sys.stderr)