Handling empty diffs.

This commit is contained in:
2025-10-19 10:45:26 -04:00
parent 81e43bcdf4
commit 20440cb305
+41 -30
View File
@@ -4,20 +4,47 @@ 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["REPO_OWNER"]
repo = os.environ["REPO_NAME"]
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")
diff_text = os.environ.get("DIFF")
comment_template = os.environ.get("COMMENT_TEMPLATE", "")
api_url = os.environ.get("API_URL", os.environ.get("GITHUB_API_URL"))
diff_text = os.environ.get("DIFF") # optional
comment_template = os.environ.get("COMMENT_TEMPLATE", "Auto-comment: changed line -> {line}")
if not diff_text:
print("No diff provided", file=sys.stderr)
if not token or not pr_index:
print("TOKEN and PR_INDEX are required.", file=sys.stderr)
sys.exit(1)
# Parse diff
# If diff_text is empty, just post a general PR comment
if not diff_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 diff_text exists, parse diff normally ---
diff_files = {}
current_file = None
new_line_num = None
@@ -38,7 +65,7 @@ for line in diff_text.splitlines():
elif not line.startswith("-"):
new_line_num += 1
# Post comments
# Post comments for each added line
for file_path, lines in diff_files.items():
if not lines:
continue
@@ -47,37 +74,21 @@ for file_path, lines in diff_files.items():
body = comment_template.replace("{line}", line_content).replace("{lines}", all_lines_content)
if platform == "github":
url = f"https://api.github.com/repos/{owner}/{repo}/issues/{pr_index}/comments"
payload = {"body": body}
if file_path and line_number:
url = f"https://api.github.com/repos/{owner}/{repo}/pulls/{pr_index}/comments"
payload.update({
"body": body,
"path": file_path,
"position": line_number
})
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}
if file_path and line_number:
payload.update({
"path": file_path,
"position": line_number
})
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",
}
headers = {"Authorization": f"token {token}", "Content-Type": "application/json"}
resp = requests.post(url, headers=headers, json=payload)
if resp.status_code in (201, 200):
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)