diff --git a/action.yml b/action.yml index 24ef894..55b2499 100644 --- a/action.yml +++ b/action.yml @@ -1,5 +1,5 @@ -name: "PR Commenter (GitHub/Gitea compatible)" -description: "Posts PR comments to GitHub or Gitea, supporting diffs or large file inputs." +name: "PR Commenter (GitHub/Gitea Compatible)" +description: "Posts PR comments to GitHub or Gitea, supporting diffs or large file inputs (plans, logs, etc.)." author: "Trez.One / AlexNorell (adapted for Gitea)" inputs: @@ -17,7 +17,7 @@ inputs: description: "Repository owner (defaults to GITHUB_REPOSITORY_OWNER)" required: false api_url: - description: "API base URL (for Gitea)" + description: "API base URL (required for Gitea)" required: false pr_index: description: "Pull request index or number" @@ -36,7 +36,13 @@ inputs: runs: using: "composite" 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 run: | python3 $GITHUB_ACTION_PATH/git-auto-comment.py @@ -48,4 +54,4 @@ runs: API_URL: ${{ inputs.api_url }} PR_INDEX: ${{ inputs.pr_index }} PLAN_FILE: ${{ inputs.plan_file }} - COMMENT_TEMPLATE: ${{ inputs.comment_template }} \ No newline at end of file + COMMENT_TEMPLATE: ${{ inputs.comment_template }} diff --git a/git-auto-comment.py b/git-auto-comment.py index d921fa5..6c4b5de 100755 --- a/git-auto-comment.py +++ b/git-auto-comment.py @@ -1,10 +1,26 @@ +#!/usr/bin/env python3 import os import sys import re 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() token = os.environ.get("TOKEN") 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") api_url = os.environ.get("API_URL", os.environ.get("GITHUB_API_URL")) 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}") 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) -# --- Load large content from file if provided --- +# --- Load content from file or environment --- 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() + try: + 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: content_text = diff_text @@ -33,27 +53,33 @@ if not content_text: else: body = comment_template.replace("{lines}", content_text).replace("{line}", "") -# --- Select API endpoint --- +# --- Determine 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) + 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) + print(f"❌ Unsupported platform: {platform}", file=sys.stderr) sys.exit(1) -# --- Post comment --- +# --- Post the comment --- headers = {"Authorization": f"token {token}", "Content-Type": "application/json"} payload = {"body": body} -resp = requests.post(url, headers=headers, json=payload) -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) +print(f"🛰️ Posting comment to {platform.upper()} PR #{pr_index}...") + +try: + resp = requests.post(url, headers=headers, json=payload) + 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) +except requests.exceptions.RequestException as e: + print(f"❌ Network or API error: {e}", file=sys.stderr) sys.exit(1)