From 1825eafab68090b58bf3c31b641deff918b2e801 Mon Sep 17 00:00:00 2001 From: Gabor Boros Date: Tue, 14 Nov 2023 15:55:53 +0400 Subject: [PATCH] refactor: use MFE refs caching for master branches --- tutormfe/plugin.py | 37 +++++++++++++-------- tutormfe/templates/mfe/build/mfe/Dockerfile | 2 +- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/tutormfe/plugin.py b/tutormfe/plugin.py index 7df6b8f3..37280db4 100644 --- a/tutormfe/plugin.py +++ b/tutormfe/plugin.py @@ -2,12 +2,14 @@ from glob import glob import os +import re import typing as t import pkg_resources from tutor import fmt from tutor import hooks as tutor_hooks +from tutor import config as tutor_config from tutor.hooks import priorities from tutor.types import Config, get_typed from .__about__ import __version__, __version_suffix__ @@ -24,55 +26,41 @@ } -# If the package version suffix is set (for instance, in the nightly branch) use the "heads" Github refs API endpoint by default. -def gh_refs_path() -> str: - return "heads" if __version_suffix__ else "tags" - - CORE_MFE_APPS: dict[str, MFE_ATTRS_TYPE] = { "authn": { "repository": "https://github.com/openedx/frontend-app-authn", - "refs": "https://api.github.com/repos/openedx/frontend-app-authn/git/refs/" + gh_refs_path(), "port": 1999, }, "account": { "repository": "https://github.com/openedx/frontend-app-account", - "refs": "https://api.github.com/repos/openedx/frontend-app-account/git/refs/" + gh_refs_path(), "port": 1997, }, "communications": { "repository": "https://github.com/openedx/frontend-app-communications", - "refs": "https://api.github.com/repos/openedx/frontend-app-communications/git/refs/" + gh_refs_path(), "port": 1984, }, "course-authoring": { "repository": "https://github.com/openedx/frontend-app-course-authoring", - "refs": "https://api.github.com/repos/openedx/frontend-app-course-authoring/git/refs/" + gh_refs_path(), "port": 2001, }, "discussions": { "repository": "https://github.com/openedx/frontend-app-discussions", - "refs": "https://api.github.com/repos/openedx/frontend-app-discussions/git/refs/" + gh_refs_path(), "port": 2002, }, "gradebook": { "repository": "https://github.com/openedx/frontend-app-gradebook", - "refs": "https://api.github.com/repos/openedx/frontend-app-gradebook/git/refs/" + gh_refs_path(), "port": 1994, }, "learning": { "repository": "https://github.com/openedx/frontend-app-learning", - "refs": "https://api.github.com/repos/openedx/frontend-app-learning/git/refs/" + gh_refs_path(), "port": 2000, }, "ora-grading": { "repository": "https://github.com/openedx/frontend-app-ora-grading", - "refs": "https://api.github.com/repos/openedx/frontend-app-ora-grading/git/refs/" + gh_refs_path(), "port": 1993, }, "profile": { "repository": "https://github.com/openedx/frontend-app-profile", - "refs": "https://api.github.com/repos/openedx/frontend-app-profile/git/refs/" + gh_refs_path(), "port": 1995, }, } @@ -86,6 +74,27 @@ def _add_core_mfe_apps(apps: dict[str, MFE_ATTRS_TYPE]) -> dict[str, MFE_ATTRS_T return apps +@hooks.Actions.PROJECT_ROOT_READY.add() +def _modify_mfes(root: str): + """ + Set the GitHub ref for OpenEdX MFEs that are using the *.master branch. + + If the repository of the MFE is not of openedx or the branch is not a + *.master branch, we are not setting the ref, hence the build won't benefit + from build caches. + """ + config = tutor_config.load_minimal(root) + + for mfe, app in iter_mfes(): + mfe_app_version = app.get("version", config.get("MFE_COMMON_VERSION")) + is_openedx_repo = app["repository"].startswith("https://github.com/openedx/") + is_master_branch = re.match(r"(^open-release\/\w+\.)?master$", mfe_app_version) is not None + + if is_openedx_repo and is_master_branch: + refs_api_url_base = app["repository"].replace("github.com", "api.github.com/repos") + apps[mfe]["refs"] = f"{refs_api_url_base}/git/refs/heads/{mfe_app_version}" + + def iter_mfes() -> t.Iterable[tuple[str, MFE_ATTRS_TYPE]]: """ Yield: diff --git a/tutormfe/templates/mfe/build/mfe/Dockerfile b/tutormfe/templates/mfe/build/mfe/Dockerfile index f0d74c26..88013e37 100644 --- a/tutormfe/templates/mfe/build/mfe/Dockerfile +++ b/tutormfe/templates/mfe/build/mfe/Dockerfile @@ -35,7 +35,7 @@ RUN echo "copying i18n data" \ FROM base AS {{ app_name }}-git {#- Invalidate the build cache if a change is detected upstream #} {%- if app.get("refs") %} -ADD {{ app["refs"] }}/{{ app.get("version", MFE_COMMON_VERSION) }} /tmp/gitref-{{ app_name }} +ADD {{ app["refs"] }} /tmp/gitref-{{ app_name }} {%- endif %} RUN git clone {{ app["repository"] }} --branch {{ app.get("version", MFE_COMMON_VERSION) }} --depth 1 .