Skip to content

Commit bb95794

Browse files
committed
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.
1 parent e67a246 commit bb95794

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

hooks/post_gen_project.py

+22-20
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,14 @@
7474
# We always add an empty package rules list
7575
renovatejson["packageRules"] = []
7676

77-
if automerge_patch:
78-
# automerge patch PRs
79-
patch_rule = {
80-
"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,
8185
# negative match: do not match versions that match regex `^v?0\.`
8286
"matchCurrentVersion": "!/^v?0\\./",
8387
"automerge": True,
@@ -87,10 +91,16 @@
8791
"platformAutomerge": False,
8892
# NOTE: We need to add all the labels we want here, renovate doesn't inherit globally
8993
# specified labels for package rules
90-
"labels": ["dependency", "automerge"],
94+
"labels": ["dependency", "automerge"] + extraLabels,
9195
}
96+
97+
98+
# automerge patch PRs if `automerge_patch` is True
99+
if automerge_patch:
100+
patch_rule = rule_defaults(["patch"])
92101
if automerge_patch_v0:
93-
# 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`
94104
del patch_rule["matchCurrentVersion"]
95105

96106
# Set excludePackagePatterns to the provided list of package patterns for which patch PRs
@@ -112,20 +122,12 @@
112122
# be automerged in cookiecutter argument `automerge_patch_regexp_blocklist`.
113123
if not automerge_patch_v0 and len(automerge_patch_v0_regexp_allowlist) > 0:
114124
patterns = automerge_patch_v0_regexp_allowlist.split(";")
115-
patch_v0_rule = {
116-
"matchUpdateTypes": ["patch"],
117-
# only apply for packages whose current version matches `v?0\.`
118-
"matchCurrentVersion": "/^v?0\\./",
119-
"automerge": True,
120-
# NOTE: We can't use Platform Automerge because the repositories are configured manually,
121-
# so we can't be sure the "require status checks" option is always enabled, and without
122-
# that, platformAutomerge does not wait for tests to pass.
123-
"platformAutomerge": False,
124-
# NOTE: We need to add all the labels we want here, renovate doesn't inherit globally
125-
# specified labels for package rules
126-
"labels": ["dependency", "automerge"],
127-
"matchPackagePatterns": patterns,
128-
}
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+
129131
renovatejson["packageRules"].append(patch_v0_rule)
130132

131133
# NOTE: Later rules in `packageRules` take precedence

0 commit comments

Comments
 (0)