git lib added

This commit is contained in:
pseusys
2023-02-27 22:18:29 +01:00
parent e1576edea1
commit 97b38f57b0
3 changed files with 18 additions and 9 deletions

View File

@@ -1,5 +1,6 @@
# GitHub integration modules: # GitHub integration modules:
PyGithub~=1.57 PyGithub~=1.57
GitPython~=3.1
# Markdown visualization modules: # Markdown visualization modules:
pytz~=2022.7 pytz~=2022.7

View File

@@ -143,7 +143,7 @@ async def get_stats() -> str:
if EM.SHOW_PROFILE_VIEWS: if EM.SHOW_PROFILE_VIEWS:
DBM.i("Adding profile views info...") DBM.i("Adding profile views info...")
data = GHM.REPO.get_views_traffic(per="week") data = GHM.REMOTE.get_views_traffic(per="week")
stats += f"![Profile Views](http://img.shields.io/badge/{quote(LM.t('Profile Views'))}-{data['count']}-blue)\n\n" stats += f"![Profile Views](http://img.shields.io/badge/{quote(LM.t('Profile Views'))}-{data['count']}-blue)\n\n"
if EM.SHOW_LINES_OF_CODE: if EM.SHOW_LINES_OF_CODE:

View File

@@ -1,6 +1,7 @@
from base64 import b64decode from base64 import b64decode
from re import sub from re import sub
from git import Repo
from github import Github, AuthenticatedUser, Repository, ContentFile, InputGitAuthor, UnknownObjectException from github import Github, AuthenticatedUser, Repository, ContentFile, InputGitAuthor, UnknownObjectException
from manager_environment import EnvironmentManager as EM from manager_environment import EnvironmentManager as EM
@@ -18,10 +19,12 @@ def init_github_manager():
class GitHubManager: class GitHubManager:
USER: AuthenticatedUser USER: AuthenticatedUser
REPO: Repository REPO: Repo
REMOTE: Repository
_README: ContentFile _README: ContentFile
_README_CONTENTS: str _README_CONTENTS: str
_REPO_PATH = "repo"
_START_COMMENT = f"<!--START_SECTION:{EM.SECTION_NAME}-->" _START_COMMENT = f"<!--START_SECTION:{EM.SECTION_NAME}-->"
_END_COMMENT = f"<!--END_SECTION:{EM.SECTION_NAME}-->" _END_COMMENT = f"<!--END_SECTION:{EM.SECTION_NAME}-->"
_README_REGEX = f"{_START_COMMENT}[\\s\\S]+{_END_COMMENT}" _README_REGEX = f"{_START_COMMENT}[\\s\\S]+{_END_COMMENT}"
@@ -37,8 +40,9 @@ class GitHubManager:
""" """
github = Github(EM.GH_TOKEN) github = Github(EM.GH_TOKEN)
GitHubManager.USER = github.get_user() GitHubManager.USER = github.get_user()
GitHubManager.REPO = github.get_repo(f"{GitHubManager.USER.login}/{GitHubManager.USER.login}") GitHubManager.REMOTE = github.get_repo(f"{GitHubManager.USER.login}/{GitHubManager.USER.login}")
GitHubManager._README = GitHubManager.REPO.get_readme() GitHubManager.REPO = Repo.clone_from(GitHubManager.REMOTE.clone_url, to_path=GitHubManager._REPO_PATH)
GitHubManager._README = GitHubManager.REMOTE.get_readme()
GitHubManager._README_CONTENTS = str(b64decode(GitHubManager._README.content), "utf-8") GitHubManager._README_CONTENTS = str(b64decode(GitHubManager._README.content), "utf-8")
@staticmethod @staticmethod
@@ -73,7 +77,7 @@ class GitHubManager:
:returns: Commit author. :returns: Commit author.
""" """
return GitHubManager.REPO.default_branch if EM.BRANCH_NAME == "" else EM.BRANCH_NAME return GitHubManager.REMOTE.default_branch if EM.BRANCH_NAME == "" else EM.BRANCH_NAME
@staticmethod @staticmethod
def update_readme(stats: str) -> bool: def update_readme(stats: str) -> bool:
@@ -86,7 +90,7 @@ class GitHubManager:
DBM.i("Updating README...") DBM.i("Updating README...")
new_readme = GitHubManager._generate_new_readme(stats) new_readme = GitHubManager._generate_new_readme(stats)
if new_readme != GitHubManager._README_CONTENTS: if new_readme != GitHubManager._README_CONTENTS:
GitHubManager.REPO.update_file( GitHubManager.REMOTE.update_file(
path=GitHubManager._README.path, path=GitHubManager._README.path,
message=EM.COMMIT_MESSAGE, message=EM.COMMIT_MESSAGE,
content=new_readme, content=new_readme,
@@ -112,9 +116,13 @@ class GitHubManager:
with open(chart_path, "rb") as input_file: with open(chart_path, "rb") as input_file:
data = input_file.read() data = input_file.read()
try: try:
contents = GitHubManager.REPO.get_contents(chart_path) contents = GitHubManager.REMOTE.get_contents(chart_path)
GitHubManager.REPO.update_file(contents.path, "Charts Updated", data, contents.sha, committer=GitHubManager._get_author()) GitHubManager.REMOTE.update_file(contents.path, "Charts Updated", data, contents.sha, committer=GitHubManager._get_author())
DBM.g("Lines of code chart updated!") DBM.g("Lines of code chart updated!")
except UnknownObjectException: except UnknownObjectException:
GitHubManager.REPO.create_file(chart_path, "Charts Added", data, committer=GitHubManager._get_author()) GitHubManager.REMOTE.create_file(chart_path, "Charts Added", data, committer=GitHubManager._get_author())
DBM.g("Lines of code chart created!") DBM.g("Lines of code chart created!")
@staticmethod
def commit_repo():
pass