From 93c7d8483e8a5aea9d7f1e814e911f61f47ed688 Mon Sep 17 00:00:00 2001 From: Zubair Shakoor <57657330+zubairshakoorarbisoft@users.noreply.github.com> Date: Mon, 19 Jun 2023 20:27:37 +0500 Subject: [PATCH] Added github action moderniser for django 42 only in matrix (#395) * fix: inprogress stuff pushed for github action moderniser --------- Co-authored-by: Usama Sadiq --- edx_repo_tools/__init__.py | 2 +- .../github_actions_modernizer_django42.py | 53 +++++++++++++++++++ .../django42/tox_moderniser_django42.py | 13 +++-- setup.py | 1 + 4 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 edx_repo_tools/codemods/django42/github_actions_modernizer_django42.py diff --git a/edx_repo_tools/__init__.py b/edx_repo_tools/__init__.py index a337602f..43619bfc 100644 --- a/edx_repo_tools/__init__.py +++ b/edx_repo_tools/__init__.py @@ -1,2 +1,2 @@ -__version__ = '0.8.0' +__version__ = '0.8.1' diff --git a/edx_repo_tools/codemods/django42/github_actions_modernizer_django42.py b/edx_repo_tools/codemods/django42/github_actions_modernizer_django42.py new file mode 100644 index 00000000..0498b488 --- /dev/null +++ b/edx_repo_tools/codemods/django42/github_actions_modernizer_django42.py @@ -0,0 +1,53 @@ +""" +Modernizer for Github Actions CI Django 4.2 support +""" +from copy import deepcopy +import click +from edx_repo_tools.utils import YamlLoader + +ALLOWED_DJANGO_VERSIONS = ['django32', 'django40', 'django41', 'django42'] + + +class GithubCIModernizer(YamlLoader): + def __init__(self, file_path): + super().__init__(file_path) + + def _update_django_in_matrix(self): + django_versions = list() + matrix_elements = dict() + section_key = None + + for key in ['build', 'tests', 'run_tests', 'run_quality', 'pytest']: + if key in self.elements['jobs']: + section_key = key + matrix_elements = deepcopy(self.elements['jobs'][section_key]['strategy']['matrix']) + + for key, value in matrix_elements.items(): + if key == 'django-version': + django_versions = value + django_versions.extend(filter( + lambda version: version not in value, ALLOWED_DJANGO_VERSIONS)) + if not section_key: + return + if django_versions: + self.elements['jobs'][section_key]['strategy']['matrix']['django-version'] = django_versions + + def _update_github_actions(self): + self._update_django_in_matrix() + + def modernize(self): + self._update_github_actions() + self.update_yml_file() + + +@click.command() +@click.option( + '--path', default='.github/workflows/ci.yml', + help="Path to default CI workflow file") +def main(path): + modernizer = GithubCIModernizer(path) + modernizer.modernize() + + +if __name__ == '__main__': + main() diff --git a/edx_repo_tools/codemods/django42/tox_moderniser_django42.py b/edx_repo_tools/codemods/django42/tox_moderniser_django42.py index f3525e10..61139152 100644 --- a/edx_repo_tools/codemods/django42/tox_moderniser_django42.py +++ b/edx_repo_tools/codemods/django42/tox_moderniser_django42.py @@ -9,9 +9,10 @@ TEST_ENV_SECTION = "testenv" TEST_ENV_DEPS = "deps" PYTHON_SUBSTITUTE = "py38" -DJANGO_SUBSTITUTE = "django{32,40,42}" +DJANGO_SUBSTITUTE = "django{32,40, 41,42}" DJANGO_40_DEPENDENCY = "django40: Django>=4.0,<4.1\n" +DJANGO_41_DEPENDENCY = "django41: Django>=4.1,<4.2\n" DJANGO_42_DEPENDENCY = "django42: Django>=4.2,<4.3\n" NEW_DJANGO_DEPENDENCIES = DJANGO_40_DEPENDENCY + DJANGO_42_DEPENDENCY @@ -74,10 +75,16 @@ def _replace_matches(pattern, substitute, target, matches): occurrences_to_replace = len(matches) - 1 if occurrences_to_replace > 0: target = re.sub(pattern, '', target, occurrences_to_replace) - + # checking if there is any dependency for django32 dont override it if matches[0].startswith('django32:'): - substitute = matches[0] + substitute + substitute = matches[0] + if 'django40:' not in target: + substitute += DJANGO_40_DEPENDENCY + if 'django41:' not in target: + substitute += DJANGO_41_DEPENDENCY + if 'django42:' not in target: + substitute += DJANGO_42_DEPENDENCY target = re.sub(pattern, substitute, target) return target diff --git a/setup.py b/setup.py index adf92d5e..f808d8c6 100644 --- a/setup.py +++ b/setup.py @@ -110,6 +110,7 @@ def is_requirement(line): 'show_hooks = edx_repo_tools.dev.show_hooks:main', 'tag_release = edx_repo_tools.release.tag_release:main', 'modernize_tox_django42 = edx_repo_tools.codemods.django42.tox_moderniser_django42:main', + 'modernize_github_actions_django42 = edx_repo_tools.codemods.django42.github_actions_modernizer_django42:main', ], }, package_data={