From cd6377a956bf985488fa5b0effc714725026d5ef Mon Sep 17 00:00:00 2001 From: pseusys Date: Sun, 26 Feb 2023 12:45:17 +0100 Subject: [PATCH] inlining LOC chart --- sources/manager_file.py | 2 +- sources/manager_github.py | 31 ++++++++++++++++++++++--------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/sources/manager_file.py b/sources/manager_file.py index 20248ab..741b681 100644 --- a/sources/manager_file.py +++ b/sources/manager_file.py @@ -1,5 +1,5 @@ from json import load -from os.path import join, dirname +from os.path import join from typing import Dict from manager_environment import EnvironmentManager as EM diff --git a/sources/manager_github.py b/sources/manager_github.py index 0f1660e..2b7c1e7 100644 --- a/sources/manager_github.py +++ b/sources/manager_github.py @@ -1,4 +1,4 @@ -from base64 import b64decode +from base64 import b64decode, b64encode from os import environ from random import choice from re import sub @@ -123,20 +123,33 @@ class GitHubManager: DBM.g("Action output set!") @staticmethod - def update_chart(chart_path: str): + def update_chart(chart_path: str) -> str: """ Updates lines of code chart. + Inlines data into readme if in debug mode, commits otherwise. Uses commit author, commit message and branch name specified by environmental variables. :param chart_path: path to saved lines of code chart. + :returns: string to add to README file. """ DBM.i("Updating lines of code chart...") with open(chart_path, "rb") as input_file: data = input_file.read() - try: - contents = GitHubManager.REPO.get_contents(chart_path) - GitHubManager.REPO.update_file(contents.path, "Charts Updated", data, contents.sha, committer=GitHubManager._get_author()) - DBM.g("Lines of code chart updated!") - except UnknownObjectException: - GitHubManager.REPO.create_file(chart_path, "Charts Added", data, committer=GitHubManager._get_author()) - DBM.g("Lines of code chart created!") + + if not EM.DEBUG_RUN: + DBM.i("Pushing chart to repo...") + chart_path = f"https://raw.githubusercontent.com/{GitHubManager.USER.login}/{GitHubManager.USER.login}/{GitHubManager.branch()}/{chart_path}" + + try: + contents = GitHubManager.REPO.get_contents(chart_path) + GitHubManager.REPO.update_file(contents.path, "Charts Updated", data, contents.sha, committer=GitHubManager._get_author()) + DBM.g("Lines of code chart updated!") + except UnknownObjectException: + GitHubManager.REPO.create_file(chart_path, "Charts Added", data, committer=GitHubManager._get_author()) + DBM.g("Lines of code chart created!") + + else: + DBM.i("Inlining chart...") + chart_path = f"data:image/png;base64,{b64encode(data)}" + + return f"**{FM.t('Timeline')}**\n\n![Lines of Code chart]({chart_path})\n\n"