Skip to content

Commit 6bc56bd

Browse files
authored
Merge pull request #107 from projectsyn/feat/configurable-patch-automerge
Allow customizing patch automerge behavior for selected dependencies
2 parents c07c837 + bb95794 commit 6bc56bd

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

cookiecutter.json

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
"automerge_patch": "y",
1515
"automerge_patch_v0": "n",
1616

17+
"automerge_patch_regexp_blocklist": "",
18+
"automerge_patch_v0_regexp_allowlist": "",
19+
1720
"copyright_holder": "VSHN AG <info@vshn.ch>",
1821
"copyright_year": "1950",
1922

hooks/post_gen_project.py

+46-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
automerge_patch = "{{ cookiecutter.automerge_patch }}" == "y"
1313
automerge_patch_v0 = "{{ cookiecutter.automerge_patch_v0 }}" == "y"
1414

15+
automerge_patch_regexp_blocklist = "{{ cookiecutter.automerge_patch_regexp_blocklist }}"
16+
automerge_patch_v0_regexp_allowlist = (
17+
"{{ cookiecutter.automerge_patch_v0_regexp_allowlist }}"
18+
)
19+
1520
if not create_lib:
1621
shutil.rmtree("lib")
1722

@@ -69,10 +74,14 @@
6974
# We always add an empty package rules list
7075
renovatejson["packageRules"] = []
7176

72-
if automerge_patch:
73-
# automerge patch PRs
74-
patch_rule = {
75-
"matchUpdateTypes": ["patch"],
77+
78+
def rule_defaults(updateTypes, extraLabels=[]):
79+
"""Returns a dict containing a base package rule configuration for automerging.
80+
81+
By default automerging is disabled for dependencies whose current version matches `^v?0\.`
82+
"""
83+
return {
84+
"matchUpdateTypes": updateTypes,
7685
# negative match: do not match versions that match regex `^v?0\.`
7786
"matchCurrentVersion": "!/^v?0\\./",
7887
"automerge": True,
@@ -82,14 +91,45 @@
8291
"platformAutomerge": False,
8392
# NOTE: We need to add all the labels we want here, renovate doesn't inherit globally
8493
# specified labels for package rules
85-
"labels": ["dependency", "automerge"],
94+
"labels": ["dependency", "automerge"] + extraLabels,
8695
}
96+
97+
98+
# automerge patch PRs if `automerge_patch` is True
99+
if automerge_patch:
100+
patch_rule = rule_defaults(["patch"])
87101
if automerge_patch_v0:
88-
# remove match current version if we want v0.x patch automerge
102+
# If automerging patch updates for v0.x dependencies is enabled, remove field
103+
# `matchCurrentVersion`
89104
del patch_rule["matchCurrentVersion"]
90105

106+
# Set excludePackagePatterns to the provided list of package patterns for which patch PRs
107+
# shouldn't be automerged. Only set the field if the provided list isn't empty.
108+
if len(automerge_patch_regexp_blocklist) > 0:
109+
# NOTE: We expect that the provided pattern list is a string with patterns separated by ;
110+
patterns = automerge_patch_regexp_blocklist.split(";")
111+
patch_rule["excludePackagePatterns"] = patterns
112+
91113
renovatejson["packageRules"].append(patch_rule)
92114

115+
# If global automerging of patch updates for v0.x dependencies is disabled, but automerging is
116+
# requested for some v0.x package patterns via `automerge_patch_v0_regexp_allowlist`, we generate an
117+
# additional package rule that enables automerge only for dependencies that are currently v0.x and
118+
# which match one of the patterns provided in `automerge_patch_v0_regexp_allowlist`.
119+
# This rule is not required and therefore not generated when `automerge_patch_v0` is enabled. If you
120+
# want to disable automerging for some v0.x patch level updates but automerge patch level updates
121+
# for v0.x dependencies by default, you can specify patterns for which patch level updates shouldn't
122+
# be automerged in cookiecutter argument `automerge_patch_regexp_blocklist`.
123+
if not automerge_patch_v0 and len(automerge_patch_v0_regexp_allowlist) > 0:
124+
patterns = automerge_patch_v0_regexp_allowlist.split(";")
125+
126+
patch_v0_rule = rule_defaults(["patch"])
127+
# only apply for packages whose current version matches `v?0\.`
128+
patch_v0_rule["matchCurrentVersion"] = "/^v?0\\./"
129+
patch_v0_rule["matchPackagePatterns"] = patterns
130+
131+
renovatejson["packageRules"].append(patch_v0_rule)
132+
93133
# NOTE: Later rules in `packageRules` take precedence
94134

95135
with open("renovate.json", "w", encoding="utf-8") as f:

0 commit comments

Comments
 (0)