feat: adds show-time flag for unclassified langs

- Adds hardcoded defaults for envars
- Updates default commit message
- Uses __slots__ for constants
This commit is contained in:
Jovial Joe Jayarson 2022-08-06 12:27:53 +05:30
parent 9704223eb6
commit 9a92d7a3c8
2 changed files with 42 additions and 14 deletions

View File

@ -24,7 +24,7 @@ inputs:
COMMIT_MESSAGE: COMMIT_MESSAGE:
description: "Add a commit message of your choice" description: "Add a commit message of your choice"
default: "Updated the Graph with new Metrics" default: "Updated WakaReadme graph with new metrics"
required: false required: false
# content tweaks # content tweaks
@ -54,6 +54,11 @@ inputs:
default: "false" default: "false"
required: false required: false
SHOW_MASKED_TIME:
description: "Displays total coding time including unclassified languages"
default: "false"
required: false
runs: runs:
using: "docker" using: "docker"
image: "Dockerfile" image: "Dockerfile"

47
main.py
View File

@ -52,7 +52,7 @@ import requests
################### data ################### ################### data ###################
@dataclass @dataclass(frozen=True, slots=True)
class WakaConstants: class WakaConstants:
""" """
WakaConstants WakaConstants
@ -80,16 +80,25 @@ class WakaInput:
# # required # # required
self.gh_token: str = os.getenv('INPUT_GH_TOKEN') self.gh_token: str = os.getenv('INPUT_GH_TOKEN')
self.waka_key: str = os.getenv('INPUT_WAKATIME_API_KEY') self.waka_key: str = os.getenv('INPUT_WAKATIME_API_KEY')
self.api_base_url: str = os.getenv('INPUT_API_BASE_URL') self.api_base_url: str = os.getenv(
'INPUT_API_BASE_URL', 'https://wakatime.com/api'
)
self.repository: str = os.getenv('INPUT_REPOSITORY') self.repository: str = os.getenv('INPUT_REPOSITORY')
# # depends # # depends
self.commit_message: str = os.getenv("INPUT_COMMIT_MESSAGE") self.commit_message: str = os.getenv(
'INPUT_COMMIT_MESSAGE', 'Updated WakaReadme graph with new metrics'
)
# # optional # # optional
self.show_title: str | bool = os.getenv("INPUT_SHOW_TITLE") self.show_title: str | bool = os.getenv('INPUT_SHOW_TITLE', 'False')
self.block_style: str = os.getenv("INPUT_BLOCKS") self.block_style: str = os.getenv('INPUT_BLOCKS', '░▒▓█')
self.time_range: str = os.getenv("INPUT_TIME_RANGE") self.time_range: str = os.getenv('INPUT_TIME_RANGE', 'last_7_days')
self.show_time: str | bool = os.getenv("INPUT_SHOW_TIME") self.show_time: str | bool = os.getenv('INPUT_SHOW_TIME', 'False')
self.show_total_time: str | bool = os.getenv("INPUT_SHOW_TOTAL") self.show_total_time: str | bool = os.getenv(
'INPUT_SHOW_TOTAL', 'False'
)
self.show_masked_time: str | bool = os.getenv(
'INPUT_SHOW_MASKED_TIME', 'False'
)
def validate_input(self) -> bool: def validate_input(self) -> bool:
""" """
@ -102,19 +111,24 @@ class WakaInput:
return False return False
if len(self.commit_message) < 1: if len(self.commit_message) < 1:
logger.error('Invalid commit message') logger.error(
'Commit message length must be greater than 1 character long'
)
return False return False
try: try:
self.show_title: bool = strtobool(self.show_title) self.show_title: bool = strtobool(self.show_title)
self.show_time: bool = strtobool(self.show_time) self.show_time: bool = strtobool(self.show_time)
self.show_total_time: bool = strtobool(self.show_total_time) self.show_total_time: bool = strtobool(self.show_total_time)
self.show_masked_time: bool = strtobool(self.show_masked_time)
except (ValueError, AttributeError) as err: except (ValueError, AttributeError) as err:
logger.error(err) logger.error(err)
return False return False
if len(self.block_style) < 2: if len(self.block_style) < 2:
logger.warning('Invalid block length') logger.warning(
'Block length should be greater than 2 characters long'
)
logger.debug('Using default blocks: ░▒▓█') logger.debug('Using default blocks: ░▒▓█')
# 'all_time' is un-documented, should it be used? # 'all_time' is un-documented, should it be used?
@ -226,12 +240,17 @@ def prep_content(stats: dict | None, /) -> str:
total_time := stats.get('human_readable_total') total_time := stats.get('human_readable_total')
): ):
contents += f'Total Time: {total_time}\n\n' contents += f'Total Time: {total_time}\n\n'
# # overrides 'human_readable_total'
if wk_i.show_masked_time and (
total_time := stats.get('human_readable_total_including_other_language')
):
contents += f'Total Time: {total_time}\n\n'
# make content # make content
logger.debug('Making contents') logger.debug('Making contents')
pad_len = len( pad_len = len(
max((str(l.get('name')) for l in lang_info), key=len)
# comment if it feels way computationally expensive # comment if it feels way computationally expensive
max((str(l.get('name')) for l in lang_info), key=len)
# and then don't for get to set pad_len to say 13 :) # and then don't for get to set pad_len to say 13 :)
) )
for idx, lang in enumerate(lang_info): for idx, lang in enumerate(lang_info):
@ -271,6 +290,8 @@ def fetch_stats() -> Any:
# why session? read @ # why session? read @
# https://docs.python-requests.org/en/latest/user/advanced/#session-objects # https://docs.python-requests.org/en/latest/user/advanced/#session-objects
while tries > 0: while tries > 0:
# TODO: instead of manual retry use requests.adapters.Retry with back-off and
# requests.adapters.HTTPAdapter: see https://stackoverflow.com/questions/15431044/
resp = rqs.get( resp = rqs.get(
url=f'{wk_i.api_base_url.rstrip("/")}/v1/users/current/stats/{wk_i.time_range}', url=f'{wk_i.api_base_url.rstrip("/")}/v1/users/current/stats/{wk_i.time_range}',
headers={'Authorization': f'Basic {encoded_key}'} headers={'Authorization': f'Basic {encoded_key}'}
@ -313,8 +334,10 @@ def churn(old_readme: str, /) -> str | None:
repl=f'{wk_c.start_comment}\n\n```text\n{generated_content}\n```\n\n{wk_c.end_comment}', repl=f'{wk_c.start_comment}\n\n```text\n{generated_content}\n```\n\n{wk_c.end_comment}',
string=old_readme string=old_readme
) )
# return None # un-comment when testing with --dev if len(sys.argv) == 2 and sys.argv[1] == '--dev':
# to avoid accidentally writing back to Github # to avoid accidentally writing back to Github
# when developing and testing WakaReadme
return None
return None if new_readme == old_readme else new_readme return None if new_readme == old_readme else new_readme