Skip to content

Commit 3ba3c88

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 f97e24a commit 3ba3c88

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
@@ -36,3 +36,4 @@ But you might still need to adapt your code:
3636

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.
39+
- 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 i 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
@@ -44,6 +44,9 @@ def main() -> None:
4444
print("Migrating protolint and publish-to-pypi runners to ubuntu-24.04...")
4545
migrate_docker_based_runners()
4646
print("=" * 72)
47+
print("Updating 'Protect version branches' GitHub ruleset...")
48+
migrate_protect_version_branches_ruleset()
49+
print("=" * 72)
4750
print()
4851

4952
if _manual_steps:
@@ -233,6 +236,68 @@ def migrate_docker_based_runners() -> None:
233236
)
234237

235238

239+
def migrate_protect_version_branches_ruleset() -> None:
240+
"""Update the 'Protect version branches' GitHub ruleset.
241+
242+
Uses the GitHub API (via ``gh`` CLI) to check whether the
243+
'Protect version branches' ruleset on the current repository is aligned
244+
with the current template. Recent template changes include:
245+
246+
* Removing the ``copilot_code_review`` rule.
247+
248+
If the ruleset is already aligned, prints an informational message.
249+
If it needs updating, applies the changes via the API without removing
250+
any existing required status checks.
251+
If the ruleset is not found at all, issues a manual-step message that
252+
points the user to the docs.
253+
"""
254+
rule_name = "Protect version branches"
255+
docs_url = (
256+
"https://frequenz-floss.github.io/frequenz-repo-config-python/"
257+
"user-guide/start-a-new-project/configure-github/#rulesets"
258+
)
259+
260+
# Build a link to the repo's ruleset settings for manual-step messages.
261+
ruleset_url = get_ruleset_settings_url() or docs_url
262+
263+
# ── Fetch ruleset details ────────────────────────────────────────
264+
ruleset = get_ruleset(rule_name)
265+
if ruleset is None:
266+
manual_step(
267+
f"The '{rule_name}' GitHub ruleset was not found (or the gh CLI "
268+
"is not available / the API call failed). "
269+
"Please check whether it should exist for this repository. "
270+
f"If it should, import it following the instructions at: {docs_url}"
271+
)
272+
return
273+
274+
# ── Detect and apply changes in-memory ───────────────────────────────
275+
changes: list[str] = []
276+
updated_rules = []
277+
278+
for rule in ruleset.get("rules", []):
279+
if rule.get("type") == "copilot_code_review":
280+
changes.append("remove copilot_code_review")
281+
continue
282+
updated_rules.append(rule)
283+
284+
if not changes:
285+
print(f" Ruleset '{rule_name}' is already up to date")
286+
return
287+
288+
# ── Push the update ───────────────────────────────────────────────────
289+
ruleset["rules"] = updated_rules
290+
if not update_ruleset(ruleset["id"], ruleset):
291+
manual_step(
292+
f"Failed to update the '{rule_name}' ruleset via the GitHub API. "
293+
f"Please apply the following changes manually at {ruleset_url}: "
294+
+ "; ".join(changes)
295+
)
296+
return
297+
298+
print(f" Updated ruleset '{rule_name}': " + ", ".join(changes))
299+
300+
236301
def apply_patch(patch_content: str) -> None:
237302
"""Apply a patch using the patch utility."""
238303
subprocess.run(["patch", "-p1"], input=patch_content.encode(), check=True)

0 commit comments

Comments
 (0)