From ba1a8f75c7493396b2075b2db6a696adfc65a348 Mon Sep 17 00:00:00 2001 From: Fundor333 Date: Wed, 22 Oct 2025 19:35:32 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=BEAdd=20GitHub=20issue=20search=20act?= =?UTF-8?q?ion=20for=20my=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/cron_github_issiue.yml | 44 ++++++++++ action_script/github_issue.py | 102 +++++++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 .github/workflows/cron_github_issiue.yml create mode 100644 action_script/github_issue.py diff --git a/.github/workflows/cron_github_issiue.yml b/.github/workflows/cron_github_issiue.yml new file mode 100644 index 00000000..412428f5 --- /dev/null +++ b/.github/workflows/cron_github_issiue.yml @@ -0,0 +1,44 @@ +name: Cron Github issue + +on: + schedule: + - cron: "0 0 * * *" + push: + branches: + - main + pull_request: + branches: + - main + workflow_dispatch: +jobs: + webmentions: + runs-on: ubuntu-latest + steps: + - name: Check out repository + uses: actions/checkout@master + + - name: Set up Python + uses: actions/setup-python@v6 + with: + python-version: "3.12" + cache: "pip" # caching pip dependencies + + - name: Install Pip dependencies + run: pip install -r requirements.txt + + - name: Fetch Github Issiue + run: python ./action_script/github_issue.py + + - name: Commit to repository + env: + GITHUB_TOKEN: ${{ secrets.TOKEN }} + COMMIT_MSG: | + 👾Fetch webmentions + skip-checks: true + run: | + git config user.email "git@fundor333.com" + git config user.name "fundor333" + git remote set-url origin https://x-access-token:${GITHUB_TOKEN}@github.com/fundor333/fundor333.github.io.git + git checkout main + git add . + git diff --quiet && git diff --staged --quiet || (git commit -m "${COMMIT_MSG}"; git push origin main) diff --git a/action_script/github_issue.py b/action_script/github_issue.py new file mode 100644 index 00000000..e70695f6 --- /dev/null +++ b/action_script/github_issue.py @@ -0,0 +1,102 @@ +import requests +import json +import os +import logging +from typing import Any + + +logging.basicConfig( + level=logging.INFO, + format="%(asctime)s - %(levelname)s - %(message)s", + handlers=[logging.StreamHandler()], +) +logger = logging.getLogger(__name__) + + +def search_issues_github(website: str) -> dict[str, Any]: + query = '"' + website + '" in:body,title type:issue, -author:fundor333 ' + logger.info(f"Query: {query}") + api_url = "https://api.github.com/search/issues" + + params: dict = { + "q": query, + "s": "created", + "order": "desc", + } + + found_issues = [] + page = 1 + max_pages = 10 + + os.makedirs(os.path.join("data", "github"), exist_ok=True) + + while page <= max_pages: + params["page"] = page + logger.debug(f"Fetching page {page}...") + try: + response = requests.get(api_url, params=params, timeout=15) + # Raises an exception for HTTP errors (4xx or 5xx) + response.raise_for_status() + + data = response.json() + items = data.get("items", []) + logger.debug(f"Page {page} returned {len(items)} items.") + + if not items: + logger.info( + f"No more results found after page {page - 1}. Stopping search." + ) + break + + for issue in items: + reduced_issue = { + "id": issue.get("id"), + "number": issue.get("number"), + "title": issue.get("title"), + "url": issue.get("html_url"), + "repository": issue.get("repository_url", "").replace( + "https://api.github.com/repos/", "" + ), + "created_at": issue.get("created_at"), + "state": issue.get("state"), + "author": issue.get("user", {}).get("login"), + "body": issue.get("body"), + } + found_issues.append(reduced_issue) + + file_path = os.path.join( + "data", "github", str(issue.get("id")) + ".json" + ) + try: + with open(file_path, "w", encoding="utf-8") as fp: + json.dump(reduced_issue, fp, ensure_ascii=False, indent=4) + logger.debug(f"Saved issue {issue.get('id')} to {file_path}") + except OSError as file_err: + logger.error( + f"❌ Error writing file for issue {issue.get('id')}: {file_err}" + ) + + page += 1 + + except requests.exceptions.RequestException as e: + error_message = f"🛑 API Request Error: {e}" + logger.error(error_message) + return { + "status": "error", + "details": error_message, + "issues_found": len(found_issues), + } + + result_summary = f"✅ Found {len(found_issues)} GitHub issues." + logger.info(result_summary) + return { + "status": "success", + "result_summary": result_summary, + "issues_found": len(found_issues), + "website": website, + } + + +WEBSITE_TO_SEARCH = "fundor333.com" +search_result = search_issues_github(website=WEBSITE_TO_SEARCH) +logger.info(search_result)