+25
-27
@@ -1,53 +1,51 @@
|
||||
name: "PR Commenter for GitHub and Gitea"
|
||||
description: "Posts PR comments from large outputs like diffs, logs, or Terraform plans."
|
||||
author: "Your Name"
|
||||
name: "PR Commenter (GitHub/Gitea compatible)"
|
||||
description: "Posts PR comments to GitHub or Gitea, supporting diffs or large file inputs."
|
||||
author: "Trez.One / AlexNorell (adapted for Gitea)"
|
||||
|
||||
inputs:
|
||||
platform:
|
||||
description: "Platform type: github or gitea"
|
||||
description: "Target platform: github or gitea"
|
||||
required: false
|
||||
default: "github"
|
||||
token:
|
||||
description: "Access token for the API"
|
||||
description: "Auth token for GitHub or Gitea API"
|
||||
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:
|
||||
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
|
||||
api_url:
|
||||
description: "API URL for Gitea (default: GITHUB_API_URL)"
|
||||
description: "API base URL (for Gitea)"
|
||||
required: false
|
||||
content:
|
||||
description: "Large text content (diff, log, plan, etc.)"
|
||||
pr_index:
|
||||
description: "Pull request index or number"
|
||||
required: true
|
||||
plan_file:
|
||||
description: "Path to file containing large plan/log content"
|
||||
required: false
|
||||
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
|
||||
default: |
|
||||
Auto-comment:
|
||||
🚀 **Automated Comment**
|
||||
---
|
||||
{line}
|
||||
{lines}
|
||||
|
||||
runs:
|
||||
using: "composite"
|
||||
steps:
|
||||
- name: Install Python dependencies
|
||||
run: pip install requests
|
||||
shell: bash
|
||||
|
||||
- name: Run PR Commenter
|
||||
run: python3 ${{ github.action_path }}/comment_pr.py
|
||||
- name: Run comment script
|
||||
shell: bash
|
||||
run: |
|
||||
python3 $GITHUB_ACTION_PATH/commenter.py
|
||||
env:
|
||||
PLATFORM: ${{ inputs.platform }}
|
||||
TOKEN: ${{ inputs.token }}
|
||||
PR_INDEX: ${{ inputs.pr_index }}
|
||||
REPO_OWNER: ${{ inputs.repo_owner }}
|
||||
REPO_NAME: ${{ inputs.repo_name }}
|
||||
REPO_OWNER: ${{ inputs.repo_owner }}
|
||||
API_URL: ${{ inputs.api_url }}
|
||||
CONTENT: ${{ inputs.content }}
|
||||
PR_INDEX: ${{ inputs.pr_index }}
|
||||
PLAN_FILE: ${{ inputs.plan_file }}
|
||||
COMMENT_TEMPLATE: ${{ inputs.comment_template }}
|
||||
+38
-74
@@ -4,92 +4,56 @@ import re
|
||||
import json
|
||||
import requests
|
||||
|
||||
# Read inputs from environment variables
|
||||
# --- Read inputs from environment ---
|
||||
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"))
|
||||
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"))
|
||||
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}")
|
||||
|
||||
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
|
||||
# --- 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:
|
||||
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)
|
||||
else:
|
||||
body = comment_template.replace("{lines}", content_text).replace("{line}", "")
|
||||
|
||||
# --- Select API endpoint ---
|
||||
if platform == "github":
|
||||
url = f"https://api.github.com/repos/{owner}/{repo}/issues/{pr_index}/comments"
|
||||
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"
|
||||
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)
|
||||
# --- Post comment ---
|
||||
headers = {"Authorization": f"token {token}", "Content-Type": "application/json"}
|
||||
payload = {"body": body}
|
||||
resp = requests.post(url, headers=headers, json=payload)
|
||||
|
||||
# --- 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)
|
||||
if resp.status_code in (200, 201):
|
||||
print("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)
|
||||
|
||||
Reference in New Issue
Block a user