From 21bae5d7e39cefab851c2df1a8660ab1b9bd6823 Mon Sep 17 00:00:00 2001
From: 0ry5 <oscar.bloch@posteo.de>
Date: Sat, 12 Apr 2025 00:15:24 +0200
Subject: [PATCH] feat(github_connector): use PyGithub

---
 github_connector.py | 86 +++++++++++++++++++--------------------------
 1 file changed, 36 insertions(+), 50 deletions(-)

diff --git a/github_connector.py b/github_connector.py
index 9160a4b..51a6529 100644
--- a/github_connector.py
+++ b/github_connector.py
@@ -1,8 +1,7 @@
-import requests
-import json
 from dotenv import load_dotenv
 from os import getenv
 import base64
+from github import Github
 
 load_dotenv()
 
@@ -12,60 +11,47 @@ REPO_NAME = getenv("REPO_NAME")
 BASE_BRANCH = getenv("BASE_BRANCH")
 GITHUB_TOKEN = getenv("GITHUB_TOKEN")
 
-# GitHub API URL
-API_URL = f"https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}"
-
-# Headers for authentication
-HEADERS = {
-    "Authorization": f"token {GITHUB_TOKEN}",
-    "Accept": "application/vnd.github.v3+json",
-}
+g = Github(GITHUB_TOKEN)
+repo = g.get_repo(f"{REPO_OWNER}/{REPO_NAME}")
 
 
-def get_file_sha(file_path):
-    url = f"{API_URL}/contents/{file_path}?ref={BASE_BRANCH}"
-    response = requests.get(url, headers=HEADERS)
+def similar_exists(search_text):
+    contents = repo.get_contents("_events")
 
-    if response.status_code == 200:
-        return response.json()["sha"]
-    else:
-        raise Exception(f"Error getting file SHA: {response.json()}")
+    for content in contents:
+        if content.type == "dir":
+            return similar_exists(repo, content.path, search_text)
+        elif content.type == "file":
+            file_content = repo.get_contents(content.path)
+            decoded_content = base64.b64decode(file_content.content).decode(
+                "utf-8", errors="ignore"
+            )
+            if search_text in decoded_content:
+                return True
+    return False
+
+
+def get_file(file_path):
+    return repo.get_contents(file_path)
 
 
 def delete_file(file_path):
-    url = f"{API_URL}/contents/{file_path}"
-    data = {
-        "message": f"Deleting file: {file_path}",
-        "sha": get_file_sha(file_path),
-        "branch": BASE_BRANCH,
-    }
-    response = requests.delete(url, headers=HEADERS, json=data)
-
-    if not response.status_code == 200 and not response.status_code == 404:
-        raise Exception(f"Error deleting file: {response.json()}")
+    try:
+        repo.delete_file(
+            file_path,
+            f"Deleting file: {file_path}",
+            get_file(file_path).sha,
+            BASE_BRANCH,
+        )
+    except Exception as e:
+        if "404" in str(e):
+            return
 
 
 def create_or_update_file(file_path, content, commit_message):
-    url = f"{API_URL}/contents/{file_path}"
-    b = base64.b64encode(bytes(content, "utf-8"))
-    base64_str = b.decode("utf-8")
-
-    # Check if file exists
-    response = requests.get(url, headers=HEADERS)
-    if response.status_code == 200:
-        sha = response.json()["sha"]
-    else:
-        sha = None
-
-    data = {
-        "message": commit_message,
-        "content": base64_str,
-        "branch": BASE_BRANCH,
-    }
-
-    if sha:
-        data["sha"] = sha  # If file exists, update it
-
-    response = requests.put(url, headers=HEADERS, json=data)
-    if not response.status_code in [200, 201]:
-        raise Exception(f"Error committing file: {response.json()}")
+    try:
+        existing = get_file(file_path)
+        repo.update_file(file_path, commit_message, content, existing.sha, BASE_BRANCH)
+    except Exception as e:
+        if "404" in str(e):
+            repo.create_file(file_path, commit_message, content, BASE_BRANCH)