update readme

This commit is contained in:
sangge-redmi 2024-01-15 23:46:39 +08:00
parent 76d3c7249a
commit d1558ad6db
2 changed files with 31 additions and 19 deletions

48
main.py
View File

@ -42,6 +42,7 @@ from typing import Any
# external
from faker import Faker
# from github import ContentFile, Github, GithubException, InputGitAuthor, Repository
from gitea import *
from requests import get as rq_get
@ -69,6 +70,7 @@ try:
# comment this out to disable colored logging
from loguru import logger
logger.debug("loguru loaded")
# load from .env before class def gets parsed
@ -165,7 +167,12 @@ class WakaInput:
def validate_input(self):
"""Validate Input Env Variables."""
logger.debug("Validating input variables")
if not self.gitea_token or not self.waka_key or not self.api_base_url or not self.repository:
if (
not self.gitea_token
or not self.waka_key
or not self.api_base_url
or not self.repository
):
logger.error("Invalid inputs")
logger.info("Refer https://github.com/athul/waka-readme")
return False
@ -444,9 +451,9 @@ def qualify_target(gitea_repo: Repository, gitea_connect: Gitea):
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_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,
@ -487,26 +494,31 @@ def genesis():
target = qualify_target(gitea_repo, gitea_connect)
logger.debug("Decoding readme contents\n")
readme_contents = str(b64decode(target.this), encoding="utf-8")
if not (new_content := churn(readme_contents)):
logger.info("WakaReadme was not updated")
return
logger.debug("WakaReadme stats has changed")
update_metric = partial(
gitea_repo.update_file,
path=target.path,
message=target.commit_message,
content=new_content,
sha=target.sha,
branch=target.branch,
)
if target.committer:
update_metric = partial(update_metric, committer=target.committer)
if target.author:
update_metric = partial(update_metric, author=target.author)
update_metric()
# update_metric = partial(
# gitea_repo.update_file,
# path=target.path,
# message=target.commit_message,
# content=new_content,
# sha=target.sha,
# branch=target.branch,
# )
# if target.committer:
# update_metric = partial(update_metric, committer=target.committer)
# if target.author:
# update_metric = partial(update_metric, author=target.author)
# update_metric()
b64_new_content = b64encode(bytes(new_content, "utf-8"))
repo_content = gitea_repo.get_git_content()
readmes = [c for c in repo_content if c.name == "README.md"]
sha = readmes[0].sha
str_new_content = b64_new_content.decode("utf-8")
gitea_repo.change_file(readmes[0].name, readmes[0].sha, str_new_content)
logger.info("Stats updated successfully")
@ -521,7 +533,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)

View File

@ -16,7 +16,7 @@ classifiers = [
"Typing :: Typed",
]
requires-python = ">=3.12"
dependencies = ["faker>=21.0.0", "pygithub>=2.1.1", "requests>=2.31.0", "py-gitea">=0.2.6]
dependencies = ["faker>=21.0.0", "pygithub>=2.1.1", "requests>=2.31.0", "py-gitea>=0.2.6"]
[project.urls]
Homepage = "https://github.com/athul/waka-readme"