Large file handling.
Renovate / renovate (push) Successful in 2m17s

This commit is contained in:
2025-10-22 07:21:49 -04:00
parent 8a86508ebb
commit 5c183409be
2 changed files with 63 additions and 101 deletions
+25 -27
View File
@@ -1,53 +1,51 @@
name: "PR Commenter for GitHub and Gitea" name: "PR Commenter (GitHub/Gitea compatible)"
description: "Posts PR comments from large outputs like diffs, logs, or Terraform plans." description: "Posts PR comments to GitHub or Gitea, supporting diffs or large file inputs."
author: "Your Name" author: "Trez.One / AlexNorell (adapted for Gitea)"
inputs: inputs:
platform: platform:
description: "Platform type: github or gitea" description: "Target platform: github or gitea"
required: false required: false
default: "github" default: "github"
token: token:
description: "Access token for the API" description: "Auth token for GitHub or Gitea API"
required: true required: true
pr_index:
description: "Pull request number or index"
required: true
repo_owner:
description: "Repository owner (default: GITHUB_REPOSITORY_OWNER)"
required: false
repo_name: repo_name:
description: "Repository name (default: GITHUB_REPOSITORY)" description: "Repository name (defaults to GITHUB_REPOSITORY)"
required: false
repo_owner:
description: "Repository owner (defaults to GITHUB_REPOSITORY_OWNER)"
required: false required: false
api_url: api_url:
description: "API URL for Gitea (default: GITHUB_API_URL)" description: "API base URL (for Gitea)"
required: false required: false
content: pr_index:
description: "Large text content (diff, log, plan, etc.)" description: "Pull request index or number"
required: true
plan_file:
description: "Path to file containing large plan/log content"
required: false required: false
comment_template: comment_template:
description: "Template for the comment body (supports multiline and placeholders {line}, {lines})" description: "Template for comment body (supports {line} and {lines})"
required: false required: false
default: | default: |
Auto-comment: 🚀 **Automated Comment**
--- ---
{line} {lines}
runs: runs:
using: "composite" using: "composite"
steps: steps:
- name: Install Python dependencies - name: Run comment script
run: pip install requests
shell: bash
- name: Run PR Commenter
run: python3 ${{ github.action_path }}/comment_pr.py
shell: bash shell: bash
run: |
python3 $GITHUB_ACTION_PATH/commenter.py
env: env:
PLATFORM: ${{ inputs.platform }} PLATFORM: ${{ inputs.platform }}
TOKEN: ${{ inputs.token }} TOKEN: ${{ inputs.token }}
PR_INDEX: ${{ inputs.pr_index }}
REPO_OWNER: ${{ inputs.repo_owner }}
REPO_NAME: ${{ inputs.repo_name }} REPO_NAME: ${{ inputs.repo_name }}
REPO_OWNER: ${{ inputs.repo_owner }}
API_URL: ${{ inputs.api_url }} API_URL: ${{ inputs.api_url }}
CONTENT: ${{ inputs.content }} PR_INDEX: ${{ inputs.pr_index }}
PLAN_FILE: ${{ inputs.plan_file }}
COMMENT_TEMPLATE: ${{ inputs.comment_template }} COMMENT_TEMPLATE: ${{ inputs.comment_template }}
+38 -74
View File
@@ -4,92 +4,56 @@ import re
import json import json
import requests import requests
# Read inputs from environment variables # --- Read inputs from environment ---
platform = os.environ.get("PLATFORM", "github").lower() platform = os.environ.get("PLATFORM", "github").lower()
token = os.environ["TOKEN"] token = os.environ.get("TOKEN")
owner = os.environ.get("REPO_OWNER", os.environ.get("GITHUB_REPOSITORY_OWNER")) owner = os.environ.get("REPO_OWNER", os.environ.get("GITHUB_REPOSITORY_OWNER"))
repo = os.environ.get("REPO_NAME", os.environ.get("GITHUB_REPOSITORY")) repo = os.environ.get("REPO_NAME", os.environ.get("GITHUB_REPOSITORY"))
pr_index = os.environ["PR_INDEX"] pr_index = os.environ.get("PR_INDEX")
api_url = os.environ.get("API_URL", os.environ.get("GITHUB_API_URL")) api_url = os.environ.get("API_URL", os.environ.get("GITHUB_API_URL"))
content_text = os.environ.get("CONTENT") # optional large text input plan_file = os.environ.get("PLAN_FILE")
diff_text = os.environ.get("DIFF") # still supported
comment_template = os.environ.get("COMMENT_TEMPLATE", "Auto-comment: changed line -> {line}") comment_template = os.environ.get("COMMENT_TEMPLATE", "Auto-comment: changed line -> {line}")
if not token or not pr_index: if not token or not pr_index:
print("TOKEN and PR_INDEX are required.", file=sys.stderr) print("TOKEN and PR_INDEX are required.", file=sys.stderr)
sys.exit(1) sys.exit(1)
# If no content provided, just post a general PR comment # --- Load large content from file if provided ---
content_text = None
if plan_file and os.path.exists(plan_file):
with open(plan_file, "r", encoding="utf-8") as f:
content_text = f.read()
elif diff_text:
content_text = diff_text
# --- Prepare comment body ---
if not content_text: if not content_text:
body = comment_template.replace("{line}", "").replace("{lines}", "") body = comment_template.replace("{line}", "").replace("{lines}", "")
if platform == "github": else:
url = f"https://api.github.com/repos/{owner}/{repo}/issues/{pr_index}/comments" body = comment_template.replace("{lines}", content_text).replace("{line}", "")
payload = {"body": body}
elif platform == "gitea": # --- Select API endpoint ---
if not api_url: if platform == "github":
print("Gitea API URL required for Gitea platform", file=sys.stderr) url = f"https://api.github.com/repos/{owner}/{repo}/issues/{pr_index}/comments"
sys.exit(1) elif platform == "gitea":
url = f"{api_url}/repos/{owner}/{repo}/pulls/{pr_index}/comments" if not api_url:
payload = {"body": body} print("Gitea API URL required for Gitea platform", file=sys.stderr)
else:
print(f"Unsupported platform: {platform}", file=sys.stderr)
sys.exit(1) sys.exit(1)
url = f"{api_url}/repos/{owner}/{repo}/pulls/{pr_index}/comments"
else:
print(f"Unsupported platform: {platform}", file=sys.stderr)
sys.exit(1)
headers = {"Authorization": f"token {token}", "Content-Type": "application/json"} # --- Post comment ---
resp = requests.post(url, headers=headers, json=payload) headers = {"Authorization": f"token {token}", "Content-Type": "application/json"}
if resp.status_code in (200, 201): payload = {"body": body}
print("General comment posted successfully!") resp = requests.post(url, headers=headers, json=payload)
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 --- if resp.status_code in (200, 201):
diff_files = {} print("Comment posted successfully!")
current_file = None sys.exit(0)
new_line_num = None else:
print(f"Failed to post comment: {resp.status_code}", file=sys.stderr)
for line in content_text.splitlines(): print(resp.text, file=sys.stderr)
if line.startswith("+++ b/"): sys.exit(1)
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)