Skip to content

Commit

Permalink
Merge pull request #983 from projectsyn/feat/template/minor-automerge…
Browse files Browse the repository at this point in the history
…-allowlist

 Add command line flags for configuring automerging of minor updates for selected dependencies
  • Loading branch information
simu authored Jun 14, 2024
2 parents 0cfba55 + 137d757 commit f7418e1
Show file tree
Hide file tree
Showing 4 changed files with 417 additions and 2 deletions.
67 changes: 67 additions & 0 deletions commodore/cli/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ def _generate_automerge_pattern_help(level: str, remove: bool = False) -> str:
+ f"See '--{op}-automerge-patch-v0-allow-depname' for a variant of "
+ "this flag which allows specifying dependency names."
)
if level == "minor":
return (
f"{opc} regex pattern for dependencies for which minor updates should be "
+ "automerged. Can be repeated. Commodore will deduplicate patterns. "
+ removenote
+ f"See '--{op}-automerge-minor-allow-depname' for a variant of "
+ "this flag which allows specifying dependency names."
)

raise ValueError(
f"Expected 'level' to be one of ['patch', 'patch_v0', 'minor'], got {level}"
Expand Down Expand Up @@ -105,6 +113,15 @@ def _generate_automerge_depname_help(level: str, remove: bool = False) -> str:
+ "flag which allows specifying regex patterns. "
+ implnote
)
if level == "minor":
return (
f"{opc} dependency name for which minor updates should be automerged. "
+ "Can be repeated. Commodore will deduplicate dependency names. "
+ removenote
+ f"See '--{op}-automerge-minor-allow-pattern' for a variant of "
+ "this flag which allows specifying regex patterns. "
+ implnote
)

raise ValueError(
f"Expected 'level' to be one of ['patch', 'patch_v0', 'minor'], got {level}"
Expand All @@ -124,6 +141,22 @@ def new_update_options(new_cmd: bool):
add_text, test_case_help = _generate_option_text_snippets(new_cmd)

def decorator(cmd):
click.option(
"--add-automerge-minor-allow-pattern",
metavar="PATTERN",
default=[],
show_default=True,
multiple=True,
help=_generate_automerge_pattern_help(level="minor"),
)(cmd)
click.option(
"--add-automerge-minor-allow-depname",
metavar="NAME",
default=[],
show_default=True,
multiple=True,
help=_generate_automerge_depname_help(level="minor"),
)(cmd)
click.option(
"--add-automerge-patch-v0-allow-pattern",
metavar="PATTERN",
Expand Down Expand Up @@ -281,6 +314,8 @@ def component_new(
add_automerge_patch_block_pattern: Iterable[str],
add_automerge_patch_v0_allow_depname: Iterable[str],
add_automerge_patch_v0_allow_pattern: Iterable[str],
add_automerge_minor_allow_depname: Iterable[str],
add_automerge_minor_allow_pattern: Iterable[str],
):
config.update_verbosity(verbose)
t = ComponentTemplater(
Expand All @@ -303,6 +338,10 @@ def component_new(
t.add_automerge_patch_v0_allow_depname(name)
for pattern in add_automerge_patch_v0_allow_pattern:
t.add_automerge_patch_v0_allow_pattern(pattern)
for name in add_automerge_minor_allow_depname:
t.add_automerge_minor_allow_depname(name)
for pattern in add_automerge_minor_allow_pattern:
t.add_automerge_minor_allow_pattern(pattern)
t.create()


Expand Down Expand Up @@ -359,6 +398,22 @@ def component_new(
multiple=True,
help=_generate_automerge_pattern_help(level="patch_v0", remove=True),
)
@click.option(
"--remove-automerge-minor-allow-depname",
metavar="NAME",
default=[],
show_default=True,
multiple=True,
help=_generate_automerge_depname_help(level="minor", remove=True),
)
@click.option(
"--remove-automerge-minor-allow-pattern",
metavar="PATTERN",
default=[],
show_default=True,
multiple=True,
help=_generate_automerge_pattern_help(level="minor", remove=True),
)
@click.option(
"--commit / --no-commit",
is_flag=True,
Expand Down Expand Up @@ -386,10 +441,14 @@ def component_update(
add_automerge_patch_block_pattern: Iterable[str],
add_automerge_patch_v0_allow_depname: Iterable[str],
add_automerge_patch_v0_allow_pattern: Iterable[str],
add_automerge_minor_allow_depname: Iterable[str],
add_automerge_minor_allow_pattern: Iterable[str],
remove_automerge_patch_block_depname: Iterable[str],
remove_automerge_patch_block_pattern: Iterable[str],
remove_automerge_patch_v0_allow_depname: Iterable[str],
remove_automerge_patch_v0_allow_pattern: Iterable[str],
remove_automerge_minor_allow_depname: Iterable[str],
remove_automerge_minor_allow_pattern: Iterable[str],
):
"""This command updates the component at COMPONENT_PATH to the latest version of the
template which was originally used to create it, if the template version is given as
Expand Down Expand Up @@ -433,6 +492,10 @@ def component_update(
t.add_automerge_patch_v0_allow_depname(name)
for pattern in add_automerge_patch_v0_allow_pattern:
t.add_automerge_patch_v0_allow_pattern(pattern)
for name in add_automerge_minor_allow_depname:
t.add_automerge_minor_allow_depname(name)
for pattern in add_automerge_minor_allow_pattern:
t.add_automerge_minor_allow_pattern(pattern)

for name in remove_automerge_patch_block_depname:
t.remove_automerge_patch_block_depname(name)
Expand All @@ -442,6 +505,10 @@ def component_update(
t.remove_automerge_patch_v0_allow_depname(name)
for pattern in remove_automerge_patch_v0_allow_pattern:
t.remove_automerge_patch_v0_allow_pattern(pattern)
for name in remove_automerge_minor_allow_depname:
t.remove_automerge_minor_allow_depname(name)
for pattern in remove_automerge_minor_allow_pattern:
t.remove_automerge_minor_allow_pattern(pattern)

t.update(commit=commit)

Expand Down
70 changes: 68 additions & 2 deletions commodore/component/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ComponentTemplater(Templater):
_matrix_tests: bool
_automerge_patch_blocklist: set[str]
_automerge_patch_v0_allowlist: set[str]
_automerge_minor_allowlist: set[str]

def __init__(
self,
Expand All @@ -42,6 +43,7 @@ def __init__(
)
self._automerge_patch_blocklist = set()
self._automerge_patch_v0_allowlist = set()
self._automerge_minor_allowlist = set()

@classmethod
def from_existing(cls, config: Config, path: Path):
Expand Down Expand Up @@ -106,6 +108,16 @@ def _initialize_from_cookiecutter_args(self, cookiecutter_args: dict[str, str]):
self.matrix_tests = cookiecutter_args["add_matrix"] == "y"
self.automerge_patch = cookiecutter_args["automerge_patch"] == "y"
self.automerge_patch_v0 = cookiecutter_args["automerge_patch_v0"] == "y"

self._initialize_automerge_pattern_lists_from_cookiecutter_args(
cookiecutter_args
)

return update_cruft_json

def _initialize_automerge_pattern_lists_from_cookiecutter_args(
self, cookiecutter_args: dict[str, str]
):
args_patch_blocklist = cookiecutter_args.get(
"automerge_patch_regexp_blocklist", ""
)
Expand All @@ -120,8 +132,13 @@ def _initialize_from_cookiecutter_args(self, cookiecutter_args: dict[str, str]):
self._automerge_patch_v0_allowlist = set(args_patch_v0_allowlist.split(";"))
else:
self._automerge_patch_v0_allowlist = set()

return update_cruft_json
args_minor_allowlist = cookiecutter_args.get(
"automerge_minor_regexp_allowlist", ""
)
if args_minor_allowlist:
self._automerge_minor_allowlist = set(args_minor_allowlist.split(";"))
else:
self._automerge_minor_allowlist = set()

@property
def cookiecutter_args(self) -> dict[str, str]:
Expand All @@ -137,6 +154,9 @@ def cookiecutter_args(self) -> dict[str, str]:
args["automerge_patch_v0_regexp_allowlist"] = ";".join(
sorted(self._automerge_patch_v0_allowlist)
)
args["automerge_minor_regexp_allowlist"] = ";".join(
sorted(self._automerge_minor_allowlist)
)
return args

@property
Expand Down Expand Up @@ -257,6 +277,52 @@ def remove_automerge_patch_v0_allow_depname(self, name: str):
+ "patch v0 allowlist"
)

def add_automerge_minor_allow_pattern(self, pattern: str):
"""Add pattern to the minor automerge allowlist.
`pattern` is expected to be a valid regex pattern.
See `add_automerge_minor_allow_depname()` for a variant of this method which
will generate an anchored regex pattern for a particular dependency name.
"""
self._automerge_minor_allowlist.add(pattern)

def remove_automerge_minor_allow_pattern(self, pattern: str):
"""Remove the given pattern from the minor allowlist."""
try:
self._automerge_minor_allowlist.remove(pattern)
except KeyError:
if self.config.verbose:
click.echo(
f" > Pattern '{pattern}' isn't present in the automerge "
+ "minor allowlist"
)

def add_automerge_minor_allow_depname(self, name: str):
"""Add dependency to the minor automerge allowlist.
This method generates an anchored regex pattern for the provided name and adds
that pattern to the allow list. See `add_automerge_minor_allow_pattern()` for a
variant which allows providing regex patterns directly.
"""
self._automerge_minor_allowlist.add(f"^{name}$")

def remove_automerge_minor_allow_depname(self, name: str):
"""Remove the given dependency name from the minor allowlist.
The function converts the dependency name into an anchored pattern to match the
pattern that's added `add_automerge_minor_allow_depname()` for the same value of
`name`.
"""
try:
self._automerge_minor_allowlist.remove(f"^{name}$")
except KeyError:
if self.config.verbose:
click.echo(
f" > Dependency name '{name}' isn't present in the automerge "
+ "minor allowlist"
)

@property
def deptype(self) -> str:
return "component"
Expand Down
41 changes: 41 additions & 0 deletions docs/modules/ROOT/pages/reference/cli.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,19 @@ NOTE: Enabling automerging of patch-level dependency PRs for v0.x dependencies i
Commodore will deduplicate patterns.
See `--add-automerge-patch-v0-allow- depname` for a variant of this flag which allows specifying dependency names.

*--add-automerge-minor-allow-depname* NAME::
Add dependency name for which minor updates should be automerged.
Can be repeated.
Commodore will deduplicate dependency names.
See `--add-automerge-minor-allow-pattern` for a variant of this flag which allows specifying regex patterns.
Commodore will convert the provided dependency names into a list of anchored regex patterns.

*--add-automerge-minor-allow-pattern* PATTERN::
Add regex pattern for dependencies for which minor updates should be automerged.
Can be repeated.
Commodore will deduplicate patterns.
See `--add-automerge-minor-allow-depname` for a variant of this flag which allows specifying dependency names.

*--help*::
Show component new usage and options then exit.

Expand Down Expand Up @@ -357,6 +370,34 @@ NOTE: Enabling automerging of patch-level dependency PRs for v0.x dependencies i
This flag has no effect if the provided pattern isn't part of the currently configured patterns.
See `--remove-automerge-patch-v0-allow-depname` for a variant of this flag which allows specifying dependency names.

*--add-automerge-minor-allow-depname* NAME::
Add dependency name for which minor updates should be automerged.
Can be repeated.
Commodore will deduplicate dependency names.
See `--add-automerge-minor-allow-pattern` for a variant of this flag which allows specifying regex patterns.
Commodore will convert the provided dependency names into a list of anchored regex patterns.

*--add-automerge-minor-allow-pattern* PATTERN::
Add regex pattern for dependencies for which minor updates should be automerged.
Can be repeated.
Commodore will deduplicate patterns.
See `--add-automerge-minor-allow-depname` for a variant of this flag which allows specifying dependency names.

*--remove-automerge-minor-allow-depname* NAME::
Remove dependency name for which minor updates should be automerged.
Can be repeated.
Commodore will deduplicate dependency names.
This flag has no effect if the provided name isn't part of the currently configured dependency names.
See `-- remove-automerge-minor-allow-pattern` for a variant of this flag which allows specifying regex patterns.
Commodore will convert the provided dependency names into a list of anchored regex patterns.

*--remove-automerge-minor-allow-pattern* PATTERN::
Remove regex pattern for dependencies for which minor updates should be automerged.
Can be repeated.
Commodore will deduplicate patterns.
This flag has no effect if the provided pattern isn't part of the currently configured patterns.
See `--remove-automerge-minor-allow-depname` for a variant of this flag which allows specifying dependency names.

*--help*::
Show component new usage and options then exit.

Expand Down
Loading

0 comments on commit f7418e1

Please sign in to comment.