From f58f61ec664c06443076290eb27476425849893f Mon Sep 17 00:00:00 2001 From: Simon Gerber Date: Thu, 6 Jun 2024 16:24:55 +0200 Subject: [PATCH 1/3] Allow disabling patch automerge for selected dependencies We introduce a new cookiecutter argument which allows users to specify zero or more regex patterns for dependencies whose patch level updates shouldn't be automerged. The template expects that the contents of the new argument is a string which contains zero or more regex patterns separated by semicolons. The template will use these patterns as the values for the package rule field `excludePackagePatterns`. --- cookiecutter.json | 2 ++ hooks/post_gen_project.py | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/cookiecutter.json b/cookiecutter.json index fe0722a..0b5845d 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -14,6 +14,8 @@ "automerge_patch": "y", "automerge_patch_v0": "n", + "automerge_patch_regexp_blocklist": "", + "copyright_holder": "VSHN AG ", "copyright_year": "1950", diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index 475ec22..f17f904 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -12,6 +12,8 @@ automerge_patch = "{{ cookiecutter.automerge_patch }}" == "y" automerge_patch_v0 = "{{ cookiecutter.automerge_patch_v0 }}" == "y" +automerge_patch_regexp_blocklist = "{{ cookiecutter.automerge_patch_regexp_blocklist }}" + if not create_lib: shutil.rmtree("lib") @@ -88,6 +90,13 @@ # remove match current version if we want v0.x patch automerge del patch_rule["matchCurrentVersion"] + # Set excludePackagePatterns to the provided list of package patterns for which patch PRs + # shouldn't be automerged. Only set the field if the provided list isn't empty. + if len(automerge_patch_regexp_blocklist) > 0: + # NOTE: We expect that the provided pattern list is a string with patterns separated by ; + patterns = automerge_patch_regexp_blocklist.split(";") + patch_rule["excludePackagePatterns"] = patterns + renovatejson["packageRules"].append(patch_rule) # NOTE: Later rules in `packageRules` take precedence From e67a246389f2bf5141b9d954a14b52a20170dc59 Mon Sep 17 00:00:00 2001 From: Simon Gerber Date: Fri, 7 Jun 2024 13:11:10 +0200 Subject: [PATCH 2/3] Allow selectively enabling patch dependency automerge for v0.x dependencies This commit introduces a new cookiecutter argument `automerge_patch_v0_regexp_allowlist` which can be used to selectively enable patch automerging for v0.x dependencies. The template expects that the content of the new argument is a string which contains zero or more regexp patterns separated by semicolons. If global automerging of patch updates for v0.x dependencies is disabled, but `automerge_patch_v0_regexp_allowlist` isn't empty, the template generates an additional package rule that enables automerge only for dependencies that are currently v0.x and which match one of the patterns provided in the argument. This rule is not required and therefore not generated when `automerge_patch_v0` is enabled. If you want to disable automerging for some v0.x patch level updates but automerge patch level updates for v0.x dependencies by default, you can specify patterns for which patch level updates shouldn't be automerged in cookiecutter argument `automerge_patch_regexp_blocklist`. --- cookiecutter.json | 1 + hooks/post_gen_project.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/cookiecutter.json b/cookiecutter.json index 0b5845d..b9d4149 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -15,6 +15,7 @@ "automerge_patch_v0": "n", "automerge_patch_regexp_blocklist": "", + "automerge_patch_v0_regexp_allowlist": "", "copyright_holder": "VSHN AG ", "copyright_year": "1950", diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index f17f904..5c12405 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -13,6 +13,9 @@ automerge_patch_v0 = "{{ cookiecutter.automerge_patch_v0 }}" == "y" automerge_patch_regexp_blocklist = "{{ cookiecutter.automerge_patch_regexp_blocklist }}" +automerge_patch_v0_regexp_allowlist = ( + "{{ cookiecutter.automerge_patch_v0_regexp_allowlist }}" +) if not create_lib: shutil.rmtree("lib") @@ -99,6 +102,32 @@ renovatejson["packageRules"].append(patch_rule) +# If global automerging of patch updates for v0.x dependencies is disabled, but automerging is +# requested for some v0.x package patterns via `automerge_patch_v0_regexp_allowlist`, we generate an +# additional package rule that enables automerge only for dependencies that are currently v0.x and +# which match one of the patterns provided in `automerge_patch_v0_regexp_allowlist`. +# This rule is not required and therefore not generated when `automerge_patch_v0` is enabled. If you +# want to disable automerging for some v0.x patch level updates but automerge patch level updates +# for v0.x dependencies by default, you can specify patterns for which patch level updates shouldn't +# be automerged in cookiecutter argument `automerge_patch_regexp_blocklist`. +if not automerge_patch_v0 and len(automerge_patch_v0_regexp_allowlist) > 0: + patterns = automerge_patch_v0_regexp_allowlist.split(";") + patch_v0_rule = { + "matchUpdateTypes": ["patch"], + # only apply for packages whose current version matches `v?0\.` + "matchCurrentVersion": "/^v?0\\./", + "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"], + "matchPackagePatterns": patterns, + } + renovatejson["packageRules"].append(patch_v0_rule) + # NOTE: Later rules in `packageRules` take precedence with open("renovate.json", "w", encoding="utf-8") as f: From bb95794b5b6ecb8dd71e6ec3894f8b76f7b161ae Mon Sep 17 00:00:00 2001 From: Simon Gerber Date: Fri, 7 Jun 2024 13:44:36 +0200 Subject: [PATCH 3/3] Refactor package rule generation Move default configuration that's shared between all rules into a function instead of repeating the raw dict for each rule. --- hooks/post_gen_project.py | 42 ++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index 5c12405..ff187a5 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -74,10 +74,14 @@ # We always add an empty package rules list renovatejson["packageRules"] = [] -if automerge_patch: - # automerge patch PRs - patch_rule = { - "matchUpdateTypes": ["patch"], + +def rule_defaults(updateTypes, extraLabels=[]): + """Returns a dict containing a base package rule configuration for automerging. + + By default automerging is disabled for dependencies whose current version matches `^v?0\.` + """ + return { + "matchUpdateTypes": updateTypes, # negative match: do not match versions that match regex `^v?0\.` "matchCurrentVersion": "!/^v?0\\./", "automerge": True, @@ -87,10 +91,16 @@ "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"], + "labels": ["dependency", "automerge"] + extraLabels, } + + +# automerge patch PRs if `automerge_patch` is True +if automerge_patch: + patch_rule = rule_defaults(["patch"]) if automerge_patch_v0: - # remove match current version if we want v0.x patch automerge + # If automerging patch updates for v0.x dependencies is enabled, remove field + # `matchCurrentVersion` del patch_rule["matchCurrentVersion"] # Set excludePackagePatterns to the provided list of package patterns for which patch PRs @@ -112,20 +122,12 @@ # be automerged in cookiecutter argument `automerge_patch_regexp_blocklist`. if not automerge_patch_v0 and len(automerge_patch_v0_regexp_allowlist) > 0: patterns = automerge_patch_v0_regexp_allowlist.split(";") - patch_v0_rule = { - "matchUpdateTypes": ["patch"], - # only apply for packages whose current version matches `v?0\.` - "matchCurrentVersion": "/^v?0\\./", - "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"], - "matchPackagePatterns": patterns, - } + + patch_v0_rule = rule_defaults(["patch"]) + # only apply for packages whose current version matches `v?0\.` + patch_v0_rule["matchCurrentVersion"] = "/^v?0\\./" + patch_v0_rule["matchPackagePatterns"] = patterns + renovatejson["packageRules"].append(patch_v0_rule) # NOTE: Later rules in `packageRules` take precedence