Handling empty diffs.
This commit is contained in:
+41
-30
@@ -4,20 +4,47 @@ import re
|
|||||||
import json
|
import json
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
# Read inputs from environment variables
|
||||||
platform = os.environ.get("PLATFORM", "github").lower()
|
platform = os.environ.get("PLATFORM", "github").lower()
|
||||||
token = os.environ["TOKEN"]
|
token = os.environ["TOKEN"]
|
||||||
owner = os.environ["REPO_OWNER"]
|
owner = os.environ.get("REPO_OWNER", os.environ.get("GITHUB_REPOSITORY_OWNER"))
|
||||||
repo = os.environ["REPO_NAME"]
|
repo = os.environ.get("REPO_NAME", os.environ.get("GITHUB_REPOSITORY"))
|
||||||
pr_index = os.environ["PR_INDEX"]
|
pr_index = os.environ["PR_INDEX"]
|
||||||
api_url = os.environ.get("API_URL")
|
api_url = os.environ.get("API_URL", os.environ.get("GITHUB_API_URL"))
|
||||||
diff_text = os.environ.get("DIFF")
|
diff_text = os.environ.get("DIFF") # optional
|
||||||
comment_template = os.environ.get("COMMENT_TEMPLATE", "")
|
comment_template = os.environ.get("COMMENT_TEMPLATE", "Auto-comment: changed line -> {line}")
|
||||||
|
|
||||||
if not diff_text:
|
if not token or not pr_index:
|
||||||
print("No diff provided", file=sys.stderr)
|
print("TOKEN and PR_INDEX are required.", file=sys.stderr)
|
||||||
sys.exit(1)
|
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 = {}
|
diff_files = {}
|
||||||
current_file = None
|
current_file = None
|
||||||
new_line_num = None
|
new_line_num = None
|
||||||
@@ -38,7 +65,7 @@ for line in diff_text.splitlines():
|
|||||||
elif not line.startswith("-"):
|
elif not line.startswith("-"):
|
||||||
new_line_num += 1
|
new_line_num += 1
|
||||||
|
|
||||||
# Post comments
|
# Post comments for each added line
|
||||||
for file_path, lines in diff_files.items():
|
for file_path, lines in diff_files.items():
|
||||||
if not lines:
|
if not lines:
|
||||||
continue
|
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)
|
body = comment_template.replace("{line}", line_content).replace("{lines}", all_lines_content)
|
||||||
|
|
||||||
if platform == "github":
|
if platform == "github":
|
||||||
url = f"https://api.github.com/repos/{owner}/{repo}/issues/{pr_index}/comments"
|
url = f"https://api.github.com/repos/{owner}/{repo}/pulls/{pr_index}/comments"
|
||||||
payload = {"body": body}
|
payload = {"body": body, "path": file_path, "position": line_number}
|
||||||
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
|
|
||||||
})
|
|
||||||
elif platform == "gitea":
|
elif platform == "gitea":
|
||||||
if not api_url:
|
if not api_url:
|
||||||
print("Gitea API URL required", file=sys.stderr)
|
print("Gitea API URL required", file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
url = f"{api_url}/repos/{owner}/{repo}/pulls/{pr_index}/comments"
|
url = f"{api_url}/repos/{owner}/{repo}/pulls/{pr_index}/comments"
|
||||||
payload = {"body": body}
|
payload = {"body": body, "path": file_path, "position": line_number}
|
||||||
if file_path and line_number:
|
|
||||||
payload.update({
|
|
||||||
"path": file_path,
|
|
||||||
"position": line_number
|
|
||||||
})
|
|
||||||
else:
|
else:
|
||||||
print(f"Unsupported platform: {platform}", file=sys.stderr)
|
print(f"Unsupported platform: {platform}", file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
headers = {
|
headers = {"Authorization": f"token {token}", "Content-Type": "application/json"}
|
||||||
"Authorization": f"token {token}",
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
}
|
|
||||||
|
|
||||||
resp = requests.post(url, headers=headers, json=payload)
|
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}")
|
print(f"Comment posted on {file_path}:{line_number}")
|
||||||
else:
|
else:
|
||||||
print(f"Failed to post comment on {file_path}:{line_number} ({resp.status_code})", file=sys.stderr)
|
print(f"Failed to post comment on {file_path}:{line_number} ({resp.status_code})", file=sys.stderr)
|
||||||
|
|||||||
Reference in New Issue
Block a user