+10
-4
@@ -1,5 +1,5 @@
|
|||||||
name: "PR Commenter (GitHub/Gitea compatible)"
|
name: "PR Commenter (GitHub/Gitea Compatible)"
|
||||||
description: "Posts PR comments to GitHub or Gitea, supporting diffs or large file inputs."
|
description: "Posts PR comments to GitHub or Gitea, supporting diffs or large file inputs (plans, logs, etc.)."
|
||||||
author: "Trez.One / AlexNorell (adapted for Gitea)"
|
author: "Trez.One / AlexNorell (adapted for Gitea)"
|
||||||
|
|
||||||
inputs:
|
inputs:
|
||||||
@@ -17,7 +17,7 @@ inputs:
|
|||||||
description: "Repository owner (defaults to GITHUB_REPOSITORY_OWNER)"
|
description: "Repository owner (defaults to GITHUB_REPOSITORY_OWNER)"
|
||||||
required: false
|
required: false
|
||||||
api_url:
|
api_url:
|
||||||
description: "API base URL (for Gitea)"
|
description: "API base URL (required for Gitea)"
|
||||||
required: false
|
required: false
|
||||||
pr_index:
|
pr_index:
|
||||||
description: "Pull request index or number"
|
description: "Pull request index or number"
|
||||||
@@ -36,7 +36,13 @@ inputs:
|
|||||||
runs:
|
runs:
|
||||||
using: "composite"
|
using: "composite"
|
||||||
steps:
|
steps:
|
||||||
- name: Run comment script
|
- name: Install Python dependencies
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
python3 -m pip install --upgrade pip
|
||||||
|
python3 -m pip install -r $GITHUB_ACTION_PATH/requirements.txt
|
||||||
|
|
||||||
|
- name: Run git-auto-comment
|
||||||
shell: bash
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
python3 $GITHUB_ACTION_PATH/git-auto-comment.py
|
python3 $GITHUB_ACTION_PATH/git-auto-comment.py
|
||||||
|
|||||||
+44
-18
@@ -1,10 +1,26 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import re
|
import re
|
||||||
import json
|
import json
|
||||||
import requests
|
|
||||||
|
|
||||||
# --- Read inputs from environment ---
|
# --- Check for required dependency ---
|
||||||
|
try:
|
||||||
|
import requests
|
||||||
|
except ImportError:
|
||||||
|
print(
|
||||||
|
"❌ The 'requests' library is not installed.\n"
|
||||||
|
"Please ensure it's available in your runner.\n"
|
||||||
|
"If using a composite action, add:\n"
|
||||||
|
" - name: Install Python dependencies\n"
|
||||||
|
" run: |\n"
|
||||||
|
" python3 -m pip install --upgrade pip\n"
|
||||||
|
" python3 -m pip install requests",
|
||||||
|
file=sys.stderr,
|
||||||
|
)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
# --- Read environment variables ---
|
||||||
platform = os.environ.get("PLATFORM", "github").lower()
|
platform = os.environ.get("PLATFORM", "github").lower()
|
||||||
token = os.environ.get("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"))
|
||||||
@@ -12,18 +28,22 @@ repo = os.environ.get("REPO_NAME", os.environ.get("GITHUB_REPOSITORY"))
|
|||||||
pr_index = os.environ.get("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"))
|
||||||
plan_file = os.environ.get("PLAN_FILE")
|
plan_file = os.environ.get("PLAN_FILE")
|
||||||
diff_text = os.environ.get("DIFF") # still supported
|
diff_text = os.environ.get("DIFF") # still supported for legacy usage
|
||||||
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)
|
||||||
|
|
||||||
# --- Load large content from file if provided ---
|
# --- Load content from file or environment ---
|
||||||
content_text = None
|
content_text = None
|
||||||
if plan_file and os.path.exists(plan_file):
|
if plan_file and os.path.exists(plan_file):
|
||||||
with open(plan_file, "r", encoding="utf-8") as f:
|
try:
|
||||||
content_text = f.read()
|
with open(plan_file, "r", encoding="utf-8") as f:
|
||||||
|
content_text = f.read()
|
||||||
|
except Exception as e:
|
||||||
|
print(f"⚠️ Failed to read file '{plan_file}': {e}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
elif diff_text:
|
elif diff_text:
|
||||||
content_text = diff_text
|
content_text = diff_text
|
||||||
|
|
||||||
@@ -33,27 +53,33 @@ if not content_text:
|
|||||||
else:
|
else:
|
||||||
body = comment_template.replace("{lines}", content_text).replace("{line}", "")
|
body = comment_template.replace("{lines}", content_text).replace("{line}", "")
|
||||||
|
|
||||||
# --- Select API endpoint ---
|
# --- Determine API endpoint ---
|
||||||
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}/issues/{pr_index}/comments"
|
||||||
elif platform == "gitea":
|
elif platform == "gitea":
|
||||||
if not api_url:
|
if not api_url:
|
||||||
print("Gitea API URL required for Gitea platform", file=sys.stderr)
|
print("❌ Gitea API URL required for Gitea platform", 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"
|
||||||
else:
|
else:
|
||||||
print(f"Unsupported platform: {platform}", file=sys.stderr)
|
print(f"❌ Unsupported platform: {platform}", file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# --- Post comment ---
|
# --- Post the comment ---
|
||||||
headers = {"Authorization": f"token {token}", "Content-Type": "application/json"}
|
headers = {"Authorization": f"token {token}", "Content-Type": "application/json"}
|
||||||
payload = {"body": body}
|
payload = {"body": body}
|
||||||
resp = requests.post(url, headers=headers, json=payload)
|
|
||||||
|
|
||||||
if resp.status_code in (200, 201):
|
print(f"🛰️ Posting comment to {platform.upper()} PR #{pr_index}...")
|
||||||
print("Comment posted successfully!")
|
|
||||||
sys.exit(0)
|
try:
|
||||||
else:
|
resp = requests.post(url, headers=headers, json=payload)
|
||||||
print(f"Failed to post comment: {resp.status_code}", file=sys.stderr)
|
if resp.status_code in (200, 201):
|
||||||
print(resp.text, file=sys.stderr)
|
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)
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
print(f"❌ Network or API error: {e}", file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|||||||
Reference in New Issue
Block a user