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