Skip to content

Commit 8c41775

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. Note that this reorders the rule contents in the generated JSON since by default Python dict serialization is done by insertion order.
1 parent e67a246 commit 8c41775

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

hooks/post_gen_project.py

+20-24
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,28 @@
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"],
81-
# negative match: do not match versions that match regex `^v?0\.`
82-
"matchCurrentVersion": "!/^v?0\\./",
77+
78+
def rule_defaults(updateTypes, extraLabels=[]):
79+
return {
80+
"matchUpdateTypes": updateTypes,
8381
"automerge": True,
8482
# NOTE: We can't use Platform Automerge because the repositories are configured manually,
8583
# so we can't be sure the "require status checks" option is always enabled, and without
8684
# that, platformAutomerge does not wait for tests to pass.
8785
"platformAutomerge": False,
8886
# NOTE: We need to add all the labels we want here, renovate doesn't inherit globally
8987
# specified labels for package rules
90-
"labels": ["dependency", "automerge"],
88+
"labels": ["dependency", "automerge"] + extraLabels,
9189
}
92-
if automerge_patch_v0:
93-
# remove match current version if we want v0.x patch automerge
94-
del patch_rule["matchCurrentVersion"]
90+
91+
92+
if automerge_patch:
93+
# automerge patch PRs
94+
patch_rule = rule_defaults(["patch"])
95+
if not automerge_patch_v0:
96+
# If automerging patch updates for v0.x dependencies isn't enabled, do not match versions
97+
# that match regex `^v?0\.`
98+
patch_rule["matchCurrentVersion"] = "!/^v?0\\./"
9599

96100
# Set excludePackagePatterns to the provided list of package patterns for which patch PRs
97101
# shouldn't be automerged. Only set the field if the provided list isn't empty.
@@ -112,20 +116,12 @@
112116
# be automerged in cookiecutter argument `automerge_patch_regexp_blocklist`.
113117
if not automerge_patch_v0 and len(automerge_patch_v0_regexp_allowlist) > 0:
114118
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-
}
119+
120+
patch_v0_rule = rule_defaults(["patch"])
121+
# only apply for packages whose current version matches `v?0\.`
122+
patch_v0_rule["matchCurrentVersion"] = "/^v?0\\./"
123+
patch_v0_rule["matchPackagePatterns"] = patterns
124+
129125
renovatejson["packageRules"].append(patch_v0_rule)
130126

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

0 commit comments

Comments
 (0)