From 2dc7322cce0fe0dc0da7a512ae90c4dc38b0939f Mon Sep 17 00:00:00 2001 From: Mimmy <45905800+MimmyJau@users.noreply.github.com> Date: Tue, 18 Jul 2023 16:15:22 -0400 Subject: [PATCH] feat(barcalendar): auto-populate current versions from Tutor (#414) * fix: Update Elasticsearch support dates Add support date for Elasticsearch version 7.17. Did not include support dates for previous versions between 7.14 and 7.16 inclusive. * chore: Add auto pull version from tutor's config Add functions for automatically pulling version number from tutor's default config file and choosing which bars to bold and outline. This pulls the name and / or number for the current version of openedx, MongoDB, Elasticsearch, MySQL, and Redis. It does not pull the current version of Python, Django, Ubuntu, Node, or Ruby. To be clear, this commit doesn't add new bars to the calendar, it only automates how we choose which bars to bold and outline. For example, if we were to switch to Elasticsearch 8.x.x, we'll still need to manually add the release and EOL dates. * fix: Regex to match version name if suffix is word See here for original comment: https://github.com/openedx/repo-tools/pull/414#discussion_r1264784334 Version name might be something like "palm.master", in which case we don't just want to capture a version number. * refactor: Import yaml library for consistency Was previously importing a specific function but this was not consistent with style. See here for original comment: https://github.com/openedx/repo-tools/pull/414#discussion_r1264784617 * fix: Raise error if status code isn't 200 Before, if http request to tutor's defaults.yml returned status code != 200, function would return None and continue. No error would be raised. This fix now raises a RuntimError. See here for original comment: https://github.com/openedx/repo-tools/pull/414#discussion_r1264784796 * refactor: Remove f-string for single expressions f-string is unnecessary if the string is a single Python expression. Replaced f-string with just the expression where possible. See here for original comment: https://github.com/openedx/repo-tools/pull/414#discussion_r1264784942 * fix: Simplify regex for matching version name * fix: Use requests' built-in method to raise exc * refactor: Separate third-party imports from built-in Co-authored-by: Ned Batchelder * fix: Raise error if version name/number not found Before, if version name or number wasn't found, will continue to run program and generate a bar calendar that doesn't have bolded bars. Want to instead raise an error. See here for original comment: https://github.com/openedx/repo-tools/pull/414#discussion_r1266989079 --------- Co-authored-by: Ned Batchelder --- barcalendar.py | 65 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 59 insertions(+), 6 deletions(-) diff --git a/barcalendar.py b/barcalendar.py index 6ddc8091..0fe7f66c 100644 --- a/barcalendar.py +++ b/barcalendar.py @@ -19,7 +19,11 @@ import colorsys import datetime import itertools +import re +import time +import requests +import yaml def css_to_rgb(hex): assert hex[0] == "#" @@ -245,6 +249,52 @@ def column_marker(self, column): def write(self): self.epilog() +def get_defaults_from_tutor(): + """ + Fetches default configurations from tutor repository. + Returns: + object: Default configurations as Python object + """ + url = "https://raw.githubusercontent.com/overhangio/tutor/master/tutor/templates/config/defaults.yml" + while True: + try: + resp = requests.get(url, timeout=10) + except requests.RequestException as exc: + print(f"Couldn't fetch {url}: {exc}") + raise + if resp.status_code == 429: + wait = int(resp.headers.get("Retry-After", 10)) + time.sleep(wait + 1) + else: + break + + if resp.status_code == 200: + return yaml.safe_load(resp.text) + resp.raise_for_status() + +def parse_version_number(line): + """ + Get version number in line from YAML file. + Note that this only captures major and minor version (not patch number). + e.g. "docker.io/elasticsearch:7.17.9" -> "7.17" + """ + match = re.search(r'(?P\d+(\.\d+)?)', line) + if match is not None: + version_number = match.group("version_number") + return version_number + raise ValueError(f"Couldn't get version number from {line!r}") + +def parse_version_name(line): + """ + Get openedx version name in line from YAML file. + e.g.1 "open-release/palm.1" -> "Palm" + e.g.2 "open-release/palm.master" -> "Palm" + """ + match = re.search(r'/(?P[A-Za-z]+)\.', line) + if match is not None: + version_name = match.group("version_name") + return version_name.capitalize() + raise ValueError(f"Couldn't get version name from {line!r}") # ==== Editable content ==== @@ -253,17 +303,19 @@ def write(self): END_YEAR = 2027 LTS_ONLY = True +versions = get_defaults_from_tutor() + # The current versions of everything. Use the same strings as the keys in the various sections below. CURRENT = { - "Open edX": "Palm", + "Open edX": parse_version_name(versions['OPENEDX_COMMON_VERSION']), "Python": "3.8", "Django": "3.2", "Ubuntu": "20.04", "Node": "16.x", - "Mongo": "4.2", - "MySQL": "5.7", - "Elasticsearch": "7.10", - "Redis": "5.6", + "Mongo": parse_version_number(versions['DOCKER_IMAGE_MONGODB']), + "MySQL": parse_version_number(versions['DOCKER_IMAGE_MYSQL']), + "Elasticsearch": parse_version_number(versions['DOCKER_IMAGE_ELASTICSEARCH']), + "Redis": parse_version_number(versions['DOCKER_IMAGE_REDIS']), "Ruby": "3.0", } @@ -440,11 +492,12 @@ def write(self): # ('2.4', 2016, 8, 2018, 2), # ('5.6', 2017, 9, 2019, 3), # ('6.8', 2019, 5, 2020, 11), - ('7.8', 2020, 6, 2021, 12), + # ('7.8', 2020, 6, 2021, 12), ('7.10', 2020, 11, 2022, 5), ('7.11', 2021, 2, 2022, 8), ('7.12', 2021, 3, 2022, 9), ('7.13', 2021, 5, 2022, 11), + ('7.17', 2022, 2, 2023, 8), ] for name, syear, smonth, eyear, emonth in es_releases: cal.bar(f"Elasticsearch {name}", start=(syear, smonth), end=(eyear, emonth), color="#4595ba", current=(name==CURRENT["Elasticsearch"]))