From 676beab6b3471b5cfd3a9552fe70ca1497e24b21 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 22 Feb 2024 00:10:58 +0000 Subject: [PATCH] Gitpush needs auth --- requirements.txt | 4 +++- src/gitea_connector.py | 36 +++++++++++------------------------- src/local_mode.py | 5 ++--- 3 files changed, 16 insertions(+), 29 deletions(-) diff --git a/requirements.txt b/requirements.txt index c1da158..a8979de 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,11 +1,13 @@ certifi==2024.2.2 cffi==1.16.0 charset-normalizer==3.3.2 +gitdb==4.0.11 +GitPython==3.1.42 idna==3.6 lxml==5.1.0 pycparser==2.21 -pygit2==1.14.1 python-docx-2023==0.2.17 PyYAML==6.0.1 requests==2.31.0 +smmap==5.0.1 urllib3==2.2.1 diff --git a/src/gitea_connector.py b/src/gitea_connector.py index 9d76610..eec2214 100644 --- a/src/gitea_connector.py +++ b/src/gitea_connector.py @@ -1,4 +1,4 @@ -import requests, os, pygit2 +import requests, os, git from datetime import datetime BASE_URL = "https://git.fjla.uk/" @@ -10,6 +10,8 @@ HEADERS = { 'Content-Type': 'application/json', 'accept': 'application/json', } +BRANCH_NAME = datetime.now().strftime("%Y%m%d-%H%M%S") + ''' I need a way here to get the original file from the 'main' branch and @@ -24,30 +26,14 @@ None of this currently works... def clone_repository(): - # Clone the repository - repo = pygit2.clone_repository(REPO_URL, REPO_PATH, bare=False, depth=1) - new_branch_name = "bot" + datetime.now().strftime("%Y%m%d-%H%M%S") - repo.create_branch(new_branch_name) - repo.checkout(new_branch_name) - print("Repository cloned successfully!") - return repo, branch + git.Repo.clone_from(REPO_URL, REPO_PATH) -def commit_and_push(repo, branch, commit_msg): - index = repo.index - index.add(REPO_PATH + "/pis/gw.yaml") - tree = index.write_tree() - author = pygit2.Signature("OwlBot", "bot@owlboard.info") - committer = author - new_commit = repo.create_commit(branch, author, committer, commit_message, tree, [repo.head.target]) - remote = repo.remotes["origin"] - remote.push(["new-branch"]) - -def append_to_pis_file(text_to_append): +def commit_and_push_changes(text_to_append, commit_message): + repo = git.Repo(REPO_PATH) + repo.git.checkout("-b", BRANCH_NAME) with open(REPO_PATH + "/pis/gw.yaml", 'a') as file: file.write(text_to_append) - - - - -if __name__ == "__main__": - clone_repository() + repo.index.add(["pis/gw.yaml"]) + repo.index.commit(commit_message) + origin = repo.remote(name='origin') + origin.push(refspec=BRANCH_NAME) diff --git a/src/local_mode.py b/src/local_mode.py index 5c6c99e..43668a1 100644 --- a/src/local_mode.py +++ b/src/local_mode.py @@ -55,9 +55,8 @@ def start(): formatted_additions = formatter.humanYaml(details) print(formatted_additions) - repo, branch = gitea_connector.clone_repository() - gitea_connector.append_to_pis_file(formatted_additions) - gitea_connector.commit_and_push(repo, branch) + gitea_connector.clone_repository() + gitea_connector.commit_and_push_changes(formatted_additions,"From owlbot diagram-parser")