Skip to content

Commit 8c21ad0

Browse files
committed
migrate: Fix version ruleset copilot removal
The v0.15.0 migration for the Protect version branches ruleset did not properly remove the Copilot review rule in existing repositories. Add a dedicated migration step to detect and remove the `copilot_code_review` rule. Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
1 parent 75090ee commit 8c21ad0

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ But you might still need to adapt your code:
3737
- Added a migration step for api repositories to fix `mkdocs.yml` when the previous `mkdocstrings-python` v2 migration moved only `paths: ["src"]` under `handlers.python.options` but not `paths: ["py"]`.
3838
- Fixed runners for jobs that require Docker and where wrongly converted to `ubuntu-slim` in v0.15.0, changing them back to `ubuntu-24.04` to avoid Docker-related failures. The template and the migration script were both updated to reflect this change.
3939
- Updated the repo-config migration workflow template and migration script so existing repositories also add the `merge_group` trigger and skip the job unless the event is `pull_request_target`, allowing the workflow to be used as a required merge-queue check.
40+
- Added a migration step to remove the copilot review request from the Protect version branch protection rules. This was also done by v0.15.0 in theory, but the migration step was wrong and didn't update it properly.

cookiecutter/migrate.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ def main() -> None:
4747
print("Migrating protolint and publish-to-pypi runners to ubuntu-24.04...")
4848
migrate_docker_based_runners()
4949
print("=" * 72)
50+
print("Updating 'Protect version branches' GitHub ruleset...")
51+
migrate_protect_version_branches_ruleset()
52+
print("=" * 72)
5053
print()
5154

5255
if _manual_steps:
@@ -303,6 +306,68 @@ def migrate_repo_config_migration_merge_group_trigger() -> None:
303306
)
304307

305308

309+
def migrate_protect_version_branches_ruleset() -> None:
310+
"""Update the 'Protect version branches' GitHub ruleset.
311+
312+
Uses the GitHub API (via ``gh`` CLI) to check whether the
313+
'Protect version branches' ruleset on the current repository is aligned
314+
with the current template. Recent template changes include:
315+
316+
* Removing the ``copilot_code_review`` rule.
317+
318+
If the ruleset is already aligned, prints an informational message.
319+
If it needs updating, applies the changes via the API without removing
320+
any existing required status checks.
321+
If the ruleset is not found at all, issues a manual-step message that
322+
points the user to the docs.
323+
"""
324+
rule_name = "Protect version branches"
325+
docs_url = (
326+
"https://frequenz-floss.github.io/frequenz-repo-config-python/"
327+
"user-guide/start-a-new-project/configure-github/#rulesets"
328+
)
329+
330+
# Build a link to the repo's ruleset settings for manual-step messages.
331+
ruleset_url = get_ruleset_settings_url() or docs_url
332+
333+
# ── Fetch ruleset details ────────────────────────────────────────
334+
ruleset = get_ruleset(rule_name)
335+
if ruleset is None:
336+
manual_step(
337+
f"The '{rule_name}' GitHub ruleset was not found (or the gh CLI "
338+
"is not available / the API call failed). "
339+
"Please check whether it should exist for this repository. "
340+
f"If it should, import it following the instructions at: {docs_url}"
341+
)
342+
return
343+
344+
# ── Detect and apply changes in-memory ───────────────────────────────
345+
changes: list[str] = []
346+
updated_rules = []
347+
348+
for rule in ruleset.get("rules", []):
349+
if rule.get("type") == "copilot_code_review":
350+
changes.append("remove copilot_code_review")
351+
continue
352+
updated_rules.append(rule)
353+
354+
if not changes:
355+
print(f" Ruleset '{rule_name}' is already up to date")
356+
return
357+
358+
# ── Push the update ───────────────────────────────────────────────────
359+
ruleset["rules"] = updated_rules
360+
if not update_ruleset(ruleset["id"], ruleset):
361+
manual_step(
362+
f"Failed to update the '{rule_name}' ruleset via the GitHub API. "
363+
f"Please apply the following changes manually at {ruleset_url}: "
364+
+ "; ".join(changes)
365+
)
366+
return
367+
368+
print(f" Updated ruleset '{rule_name}': " + ", ".join(changes))
369+
370+
306371
def apply_patch(patch_content: str) -> None:
307372
"""Apply a patch using the patch utility."""
308373
subprocess.run(["patch", "-p1"], input=patch_content.encode(), check=True)

0 commit comments

Comments
 (0)