Skip to content

Commit

Permalink
Merge pull request #106 from projectsyn/feat/post-gen-project-hook-pa…
Browse files Browse the repository at this point in the history
…ckage-rules

Generate component `renovate.json` in post-gen-project hook
  • Loading branch information
simu authored Jun 7, 2024
2 parents 58c9655 + 7bc527b commit c07c837
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 46 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,11 @@ See https://syn.tools/syn/how-tos/prepare_for_component_sync.html
* Push or merge to `main`
* Via workflow dispatch (https://github.com/projectsyn/commodore-component-template/actions/workflows/sync.yaml)
You can provide a regex to only sync selected components via workflow dispatch.

## Notes regarding the template

The base template is stored in `{{ cookiecutter.slug }}`.
However, a part of the template is managed by a custom Python hook in `hooks/post_gen_project.py`

Notably, the Python hook will generate a suitable `renovate.json` for the rendered template.
We've switched to this approach since the logic that defines the contents of the `renovate.json` has become complex enough that templating JSON with Jinja2 isn't readable anymore.
61 changes: 61 additions & 0 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os
import shutil

Expand All @@ -7,6 +8,9 @@

create_lib = "{{ cookiecutter.add_lib }}" == "y"
add_golden = "{{ cookiecutter.add_golden }}" == "y"
add_matrix = "{{ cookiecutter.add_matrix }}" == "y"
automerge_patch = "{{ cookiecutter.automerge_patch }}" == "y"
automerge_patch_v0 = "{{ cookiecutter.automerge_patch_v0 }}" == "y"

if not create_lib:
shutil.rmtree("lib")
Expand Down Expand Up @@ -35,5 +39,62 @@
if not add_golden:
shutil.rmtree("tests/golden")

renovatejson = {
"extends": [
"config:base",
":gitSignOff",
":disableDependencyDashboard",
],
"ignorePaths": [".github/**"],
"labels": ["dependency"],
"separateMinorPatch": True,
}

if add_golden:
if add_matrix:
cmd = "make gen-golden-all"
else:
cmd = "make gen-golden"

# Add postUpgradeTask to run make gen-golden(-all) if golden tests are enabled.
renovatejson["postUpgradeTasks"] = {
"commands": [cmd],
"fileFilters": ["tests/golden/**"],
"executionMode": "update",
}
# suppress artifact error notifications if postUpgradeTask is configured, since upstream hosted
# renovate will complain when any postUpgradeTasks are present.
renovatejson["suppressNotifications"] = ["artifactErrors"]

# We always add an empty package rules list
renovatejson["packageRules"] = []

if automerge_patch:
# automerge patch PRs
patch_rule = {
"matchUpdateTypes": ["patch"],
# negative match: do not match versions that match regex `^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"],
}
if automerge_patch_v0:
# remove match current version if we want v0.x patch automerge
del patch_rule["matchCurrentVersion"]

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)
f.write("\n")

if Path(".sync.yml").is_file():
os.unlink(Path(".sync.yml"))
46 changes: 0 additions & 46 deletions {{ cookiecutter.slug }}/renovate.json

This file was deleted.

0 comments on commit c07c837

Please sign in to comment.