This commit is contained in:
parent
108a90abce
commit
980f869999
78
main.py
78
main.py
@ -28,7 +28,7 @@ Contents := Title + Byline + Body
|
||||
"""
|
||||
|
||||
# standard
|
||||
from base64 import b64encode
|
||||
from base64 import b64encode, b64decode
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from functools import partial
|
||||
@ -49,7 +49,6 @@ from requests.exceptions import RequestException
|
||||
|
||||
################### setup ###################
|
||||
|
||||
|
||||
print()
|
||||
# hush existing loggers
|
||||
for lgr_name in logger.root.manager.loggerDict:
|
||||
@ -70,9 +69,12 @@ try:
|
||||
|
||||
# comment this out to disable colored logging
|
||||
from loguru import logger
|
||||
logger.debug("loguru loaded")
|
||||
|
||||
# load from .env before class def gets parsed
|
||||
load_dotenv()
|
||||
logger.debug("dotenv loaded")
|
||||
waka_key: str | None = os.getenv("INPUT_WAKATIME_API_KEY")
|
||||
except ImportError as im_err:
|
||||
logger.warning(im_err)
|
||||
|
||||
@ -129,6 +131,7 @@ class WakaInput:
|
||||
# mapped environment variables
|
||||
# # required
|
||||
gitea_token: str | None = os.getenv("INPUT_GITEA_TOKEN")
|
||||
gitea_url: str | None = os.getenv("INPUT_URL", "https://gitea.com")
|
||||
waka_key: str | None = os.getenv("INPUT_WAKATIME_API_KEY")
|
||||
api_base_url: str | None = os.getenv("INPUT_API_BASE_URL", "https://wakatime.com/api")
|
||||
repository: str | None = os.getenv("INPUT_REPOSITORY")
|
||||
@ -408,53 +411,62 @@ def churn(old_readme: str, /):
|
||||
repl=f"{wk_i.start_comment}\n\n```{wk_i.code_lang}\n{generated_content}\n```\n\n{wk_i.end_comment}",
|
||||
string=old_readme,
|
||||
)
|
||||
logger.debug(new_readme)
|
||||
if len(sys.argv) == 2 and sys.argv[1] == "--dev":
|
||||
logger.debug("Detected run in `dev` mode.")
|
||||
# to avoid accidentally writing back to Github
|
||||
# when developing or testing waka-readme
|
||||
return None
|
||||
|
||||
return None if new_readme == old_readme else new_readme
|
||||
|
||||
|
||||
def qualify_target(gitea_repo: Repository.Repository):
|
||||
# def qualify_target(gitea_repo: Repository.Repository):
|
||||
def qualify_target(gitea_repo: Repository, gitea_connect: Gitea):
|
||||
"""Qualify target repository defaults."""
|
||||
|
||||
@dataclass
|
||||
class TargetRepository:
|
||||
this: ContentFile.ContentFile
|
||||
# this: ContentFile.ContentFile
|
||||
this: Content
|
||||
path: str
|
||||
commit_message: str
|
||||
sha: str
|
||||
branch: str
|
||||
committer: InputGitAuthor | None
|
||||
author: InputGitAuthor | None
|
||||
# committer: InputGitAuthor | None
|
||||
# author: InputGitAuthor | None
|
||||
committer: None
|
||||
author: None
|
||||
|
||||
gitea_branch = gitea_repo.default_branch
|
||||
if wk_i.target_branch != "NOT_SET":
|
||||
gitea_branch = gitea_repo.get_branch(wk_i.target_branch)
|
||||
|
||||
target = gitea_repo.get_readme()
|
||||
if wk_i.target_path != "NOT_SET":
|
||||
target = gitea_repo.get_contents(
|
||||
path=wk_i.target_path,
|
||||
ref=gitea_branch if isinstance(gitea_branch, str) else gitea_branch.commit.sha,
|
||||
)
|
||||
gitea_branch = gitea_repo.get_branches()[0].name
|
||||
# if wk_i.target_branch != "NOT_SET":
|
||||
# gitea_branch = gitea_repo.get_branch(wk_i.target_branch)
|
||||
|
||||
readme_content = Content(gitea_connect)
|
||||
readme_content.path = "README.md"
|
||||
readme_content.type = Content.FILE
|
||||
target = gitea_repo.get_file_content(readme_content) # base64 encoded
|
||||
# target = gitea_repo.get_readme()
|
||||
|
||||
# if wk_i.target_path != "NOT_SET":
|
||||
# target = gitea_repo.get_contents(
|
||||
# path=wk_i.target_path,
|
||||
# ref=gitea_branch if isinstance(gitea_branch, str) else gitea_branch.commit.sha,
|
||||
# )
|
||||
if isinstance(target, list):
|
||||
raise RuntimeError("Cannot handle multiple files.")
|
||||
|
||||
committer, author = None, None
|
||||
if wk_i.committer_name != "NOT_SET" and wk_i.committer_email != "NOT_SET":
|
||||
committer = InputGitAuthor(name=wk_i.committer_name, email=wk_i.committer_email)
|
||||
if wk_i.author_name != "NOT_SET" and wk_i.author_email != "NOT_SET":
|
||||
author = InputGitAuthor(name=wk_i.author_name, email=wk_i.author_email)
|
||||
# if wk_i.committer_name != "NOT_SET" and wk_i.committer_email != "NOT_SET":
|
||||
# committer = InputGitAuthor(name=wk_i.committer_name, email=wk_i.committer_email)
|
||||
# if wk_i.author_name != "NOT_SET" and wk_i.author_email != "NOT_SET":
|
||||
# author = InputGitAuthor(name=wk_i.author_name, email=wk_i.author_email)
|
||||
sha = ""
|
||||
|
||||
return TargetRepository(
|
||||
this=target,
|
||||
path=target.path,
|
||||
path="README.md",
|
||||
commit_message=wk_i.commit_message,
|
||||
sha=target.sha,
|
||||
sha=sha,
|
||||
branch=gitea_branch if isinstance(gitea_branch, str) else gitea_branch.name,
|
||||
committer=committer,
|
||||
author=author,
|
||||
@ -464,15 +476,19 @@ def qualify_target(gitea_repo: Repository.Repository):
|
||||
def genesis():
|
||||
"""Run Program."""
|
||||
logger.debug("Connecting to Gitea")
|
||||
gitea_connect = Gitea(wk_i.gitea_token)
|
||||
gitea_connect = Gitea(wk_i.gitea_url, wk_i.gitea_token)
|
||||
|
||||
# since a validator is being used earlier, casting
|
||||
# `wk_i.ENV_VARIABLE` to a string here, is okay
|
||||
gitea_repo = gitea_connect.get_repo(str(wk_i.repository))
|
||||
target = qualify_target(gitea_repo)
|
||||
# gitea_repo = gitea_connect.get_repo(str(wk_i.repository))
|
||||
owner = "sangge"
|
||||
repo_name = ".profile"
|
||||
gitea_repo = Repository.request(gitea_connect, owner, repo_name)
|
||||
target = qualify_target(gitea_repo, gitea_connect)
|
||||
logger.debug("Decoding readme contents\n")
|
||||
|
||||
readme_contents = str(target.this.decoded_content, encoding="utf-8")
|
||||
|
||||
readme_contents = str(b64decode(target.this), encoding="utf-8")
|
||||
if not (new_content := churn(readme_contents)):
|
||||
logger.info("WakaReadme was not updated")
|
||||
return
|
||||
@ -505,7 +521,7 @@ if __name__ == "__main__":
|
||||
|
||||
# initial waka-readme setup
|
||||
logger.debug("Initialize WakaReadme")
|
||||
wk_i = WakaInput()
|
||||
wk_i = WakaInput()
|
||||
if not wk_i.validate_input():
|
||||
logger.error("Environment variables are misconfigured\n")
|
||||
sys.exit(1)
|
||||
@ -520,7 +536,7 @@ if __name__ == "__main__":
|
||||
except RuntimeError as err:
|
||||
logger.error(f"{type(err).__name__}: {err}\n")
|
||||
sys.exit(1)
|
||||
except (GithubException, RequestException) as rq_exp:
|
||||
logger.critical(f"{rq_exp}\n")
|
||||
sys.exit(1)
|
||||
# except (GithubException, RequestException) as rq_exp:
|
||||
# logger.critical(f"{rq_exp}\n")
|
||||
# sys.exit(1)
|
||||
print("\nThanks for using WakaReadme!\n")
|
||||
|
Loading…
x
Reference in New Issue
Block a user