From 00286c47e3b04b824be6a29962b24e199dbcf3ea Mon Sep 17 00:00:00 2001 From: Simon Gerber Date: Thu, 6 Jun 2024 13:36:23 +0200 Subject: [PATCH] Move generating patch automerge package rules to post-gen-project hook --- hooks/post_gen_project.py | 33 +++++++++++++++++++++++++++ {{ cookiecutter.slug }}/renovate.json | 20 +--------------- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index 4d1ccd8..1eec94c 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -1,3 +1,4 @@ +import json import os import shutil @@ -7,6 +8,8 @@ create_lib = "{{ cookiecutter.add_lib }}" == "y" add_golden = "{{ cookiecutter.add_golden }}" == "y" +automerge_patch = "{{ cookiecutter.automerge_patch }}" == "y" +automerge_patch_v0 = "{{ cookiecutter.automerge_patch_v0 }}" == "y" if not create_lib: shutil.rmtree("lib") @@ -35,5 +38,35 @@ if not add_golden: shutil.rmtree("tests/golden") +with open("renovate.json", "r", encoding="utf-8") as f: + renovatejson = json.load(f) + +# We always add an empty package rules list +renovatejson["packageRules"] = [] + +if automerge_patch: + # automerge patch PRs + patch_rule = { + "matchUpdateTypes": ["patch"], + "automerge": True, + # NOTE: We can't use Platform Automerge because the repositories are configured manually, + # so we can't be sure the "require status checks" option is always enabled, and without + # that, platformAutomerge does not wait for tests to pass. + "platformAutomerge": False, + # NOTE: We need to add all the labels we want here, renovate doesn't inherit globally + # specified labels for package rules + "labels": ["dependency", "automerge"], + } + if not automerge_patch_v0: + # negative match: do not match versions that match regex `^v?0\.` + patch_rule["matchCurrentVersion"] = "!/^v?0\\./" + + renovatejson["packageRules"].append(patch_rule) + +# NOTE: Later rules in `packageRules` take precedence + +with open("renovate.json", "w", encoding="utf-8") as f: + json.dump(renovatejson, f, indent=2) + if Path(".sync.yml").is_file(): os.unlink(Path(".sync.yml")) diff --git a/{{ cookiecutter.slug }}/renovate.json b/{{ cookiecutter.slug }}/renovate.json index 04905bc..f435d02 100644 --- a/{{ cookiecutter.slug }}/renovate.json +++ b/{{ cookiecutter.slug }}/renovate.json @@ -24,23 +24,5 @@ "labels": [ "dependency" ], - "separateMinorPatch": true, - "packageRules": [ - {%- if cookiecutter.automerge_patch == "y" %} - { - "matchUpdateTypes": ["patch"], - {%- if cookiecutter.automerge_patch_v0 != "y" %} - "matchCurrentVersion": "!/^v?0\\./",{# negative match: do not match versions that match regex `^v?0\.` #} - {%- endif %} - "automerge": true, - {#- We can't use Platform Automerge because the repositories are configured manually, so we can't be sure the "require status checks" option is always enabled, and without that, platformAutomerge does not wait for tests to pass. #} - "platformAutomerge": false, - "labels": [ - "dependency", - "automerge" - ] - } - {%- endif %} - {#- re: SYN-785 - Later rules take precedence, so add more specific package rules below. #} - ] + "separateMinorPatch": true }