Skip to content

Commit

Permalink
Merge pull request #74 from srvanrell/stop-github-workflow-no-updates
Browse files Browse the repository at this point in the history
Stop GitHub workflow no updates
  • Loading branch information
srvanrell authored Jun 18, 2024
2 parents 0f49850 + 62e1ded commit 7b1b585
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "ranking_table_tennis"
version = "2024.6.17"
version = "2024.6.18"
description = "A ranking table tennis system"
readme = "README.md"
authors = [ "Sebastian Vanrell srvanrell_gmail_com" ]
Expand Down
1 change: 1 addition & 0 deletions ranking_table_tennis/helpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
save_ranking_sheet,
save_raw_ranking,
)
from ranking_table_tennis.helpers.github import no_updates_stop_workflow
from ranking_table_tennis.helpers.gspread import publish_to_web
from ranking_table_tennis.helpers.initial_rating import print_rating_context
from ranking_table_tennis.helpers.pickle import load_from_pickle, save_to_pickle
Expand Down
25 changes: 25 additions & 0 deletions ranking_table_tennis/helpers/github.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import logging
import os

from ranking_table_tennis.helpers.gspread import days_since_last_update

logger = logging.getLogger(__name__)


def no_updates_stop_workflow(spreadsheet_id, max_days_since_last_update=3):
"""If spreasheet_id has not been modified recently GITHUB_OUTPUT env var will stop workflow"""

days_since_update = days_since_last_update(spreadsheet_id)
stop_workflow = days_since_update > max_days_since_last_update
logger.info(
"%s days since last update. Should stop workflow? %s", days_since_update, stop_workflow
)

GITHUB_OUTPUT_VAR_NAME = "GITHUB_OUTPUT"
var_value = f"stop_workflow={str(stop_workflow).lower()}" # true or false

logger.debug("Set env var '%s': '%s'", GITHUB_OUTPUT_VAR_NAME, var_value)
os.environ[GITHUB_OUTPUT_VAR_NAME] = var_value

if stop_workflow:
exit(0)
21 changes: 19 additions & 2 deletions ranking_table_tennis/helpers/gspread.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import logging
import os
from datetime import datetime, timezone
from typing import List

import gspread
Expand Down Expand Up @@ -104,6 +105,21 @@ def publish_to_web(tid: str, show_on_web=False) -> None:
create_n_tour_sheet(spreadsheet_id, tid)


def days_since_last_update(spreadsheet_id) -> str:
try:
gc = _get_gc()
spreadsheet = gc.open_by_key(spreadsheet_id)
last_update_time = spreadsheet.get_lastUpdateTime()
except PermissionError:
logger.warn("!! Permission Error. FAIL TO GET last updated time @ '%s'", spreadsheet_id)

# Parse the ISO 8601 datetime string into a datetime at UTC
last_update_utc = datetime.fromisoformat(last_update_time[:-1]).replace(tzinfo=timezone.utc)
difference = datetime.now(timezone.utc) - last_update_utc

return difference.days


def _in_colab() -> bool:
# Verify if it is running on colab
try:
Expand All @@ -123,11 +139,11 @@ def _get_gc() -> gspread.Client:
github_secret_name = "GCP_SA_KEY"
credentials_str = os.getenv(github_secret_name)
if credentials_str:
logger.debug("Loading GCP credentials from %s", github_secret_name)
logger.debug("Loading GCP SA credentials from %s", github_secret_name)
credentials_dict = json.loads(credentials_str)
gc = gspread.service_account_from_dict(credentials_dict)
else:
logger.debug("Loading GCP credentials from standard path")
logger.debug("Loading GCP SA credentials from standard path")
gc = gspread.service_account()
except FileNotFoundError:
logger.warn(
Expand All @@ -136,6 +152,7 @@ def _get_gc() -> gspread.Client:

try:
if gc is None:
logger.debug("Loading GCP End User credentials from standard path")
gc = gspread.oauth()
except FileNotFoundError:
logger.warn("!! The end user .json key file has not been configured. Upload might fail.")
Expand Down
12 changes: 4 additions & 8 deletions ranking_table_tennis/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,18 @@ def main(offline=True, assume_yes=False, config_initial_date="220101"):
xlsx_file = cfg.io.data_folder + cfg.io.xlsx.tournaments_filename

if not offline:
# Stop preprocess if there were no recent updates on the spreadsheet
helpers.no_updates_stop_workflow(cfg.io.tournaments_spreadsheet_id)

if assume_yes:
retrieve = "yes"
else:
retrieve = input("\nDo you want to retrieve online sheet [Y/n]? ")

if retrieve.lower() != "n":
logger.info("Downloading and saving '%s'" % xlsx_file)
request.urlretrieve(cfg.io.tournaments_gdrive, xlsx_file)

# Help to stop the workflow if there are no updates on the file
# TODO exit if there are no updates.
new_data_available = True
if new_data_available:
print("::set-output name=stop_workflow::false")
else:
print("::set-output name=stop_workflow::true")

# Loading all tournament data
tournaments = helpers.load_tournaments_sheets()

Expand Down
11 changes: 3 additions & 8 deletions ranking_table_tennis/preprocess_unattended.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,12 @@ def main(config_initial_date="220101"):
ConfigManager().set_current_config(date=config_initial_date)
cfg = ConfigManager().current_config

xlsx_file = cfg.io.data_folder + cfg.io.xlsx.tournaments_filename
# Stop preprocess if there were no recent updates on the spreadsheet
helpers.no_updates_stop_workflow(cfg.io.tournaments_spreadsheet_id)

xlsx_file = cfg.io.data_folder + cfg.io.xlsx.tournaments_filename
logger.info("Downloading and saving '%s'" % xlsx_file)
request.urlretrieve(cfg.io.tournaments_gdrive, xlsx_file)
# Help to stop the workflow if there are no updates on the file
# TODO exit if there are no updates.
new_data_available = True
if new_data_available:
print("::set-output name=stop_workflow::false")
else:
print("::set-output name=stop_workflow::true")

# Loading all tournament data
tournaments = helpers.load_tournaments_sheets()
Expand Down

0 comments on commit 7b1b585

Please sign in to comment.