Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions .github/workflows/sync-beman-standard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

name: Sync Beman Standard

on:
schedule:
- cron: '0 6 * * 0' # Weekly on Sunday 09:00 EEST
workflow_dispatch:

permissions:
contents: read
issues: write

jobs:
check-standard-drift:
name: Check beman-standard.yaml drift
runs-on: ubuntu-latest
steps:
- name: Checkout beman-tidy
uses: actions/checkout@v5

- name: Install uv
uses: astral-sh/setup-uv@v6
with:
enable-cache: true

- name: Fetch upstream Beman Standard
Comment on lines +22 to +27
run: |
curl -fsSL \
https://raw.githubusercontent.com/bemanproject/beman/main/docs/beman_standard.md \
-o beman_standard.md

- name: Check for drift
id: drift
run: |
uv run python - << 'EOF'
import json
import os
from pathlib import Path

from beman_tidy.lib.utils.standard_drift import check_standard_drift

report = check_standard_drift(
"beman_standard.md",
"beman_tidy/.beman-standard.yaml",
)

output_path = Path(os.environ["GITHUB_OUTPUT"])
with output_path.open("a") as f:
f.write(f"has_drift={'true' if report.has_drift else 'false'}\n")
f.write(
"report_json="
+ json.dumps(
{
"added": report.added,
"removed": report.removed,
"type_changed": {
k: list(v) for k, v in report.type_changed.items()
},
}
)
+ "\n"
)

if report.has_drift:
body_path = Path("drift-report.md")
body_path.write_text(
report.format_markdown(
"https://github.com/bemanproject/beman/blob/main/docs/beman_standard.md"
),
encoding="utf-8",
)
EOF

- name: Open or update drift issue
if: steps.drift.outputs.has_drift == 'true'
env:
GH_TOKEN: ${{ github.token }}
run: |
TITLE="[standard-sync] beman-standard.yaml out of sync with beman_standard.md"
MARKER="<!-- beman-standard-drift -->"

{
echo "${MARKER}"
cat drift-report.md
} > issue-body.md

EXISTING=$(gh issue list \
--repo "${{ github.repository }}" \
--search "in:title ${TITLE}" \
--state open \
--json number \
--jq '.[0].number // empty')
Comment on lines +88 to +93

if [ -n "${EXISTING}" ]; then
gh issue edit "${EXISTING}" \
--repo "${{ github.repository }}" \
--body-file issue-body.md
gh issue comment "${EXISTING}" \
--repo "${{ github.repository }}" \
--body "Drift still present after scheduled check on $(date -u +%Y-%m-%d)."
else
gh issue create \
--repo "${{ github.repository }}" \
--title "${TITLE}" \
--label "standard-sync" \
--body-file issue-body.md || \
gh issue create \
--repo "${{ github.repository }}" \
--title "${TITLE}" \
--body-file issue-body.md
fi

- name: Close drift issue when in sync
if: steps.drift.outputs.has_drift != 'true'
env:
GH_TOKEN: ${{ github.token }}
run: |
TITLE="[standard-sync] beman-standard.yaml out of sync with beman_standard.md"

EXISTING=$(gh issue list \
--repo "${{ github.repository }}" \
--search "in:title ${TITLE}" \
--state open \
--json number \
--jq '.[0].number // empty')
Comment on lines +121 to +126

if [ -n "${EXISTING}" ]; then
gh issue close "${EXISTING}" \
--repo "${{ github.repository }}" \
--comment "beman-standard.yaml is back in sync with the upstream standard."
fi
4 changes: 4 additions & 0 deletions beman_tidy/.beman-standard.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ repository.disallow_git_submodules:
# RELEASE
release.github:
- type: Requirement
release.version:
- type: Recommendation
release.notes:
- type: Recommendation
release.godbolt_trunk_version:
Expand Down Expand Up @@ -141,3 +143,5 @@ cpp.no_flag_forking:
- type: Requirement
cpp.extension_identifiers:
- type: Recommendation
cpp.min_std_version:
- type: Recommendation
154 changes: 154 additions & 0 deletions beman_tidy/lib/utils/standard_drift.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

"""Compare beman_standard.md rules against beman_tidy/.beman-standard.yaml."""

from __future__ import annotations

import re
from dataclasses import dataclass, field
from pathlib import Path

import yaml

RULE_HEADING_RE = re.compile(r"^###\s+\*\*\[([^\]]+)\]\*\*", re.MULTILINE)
RULE_TYPE_RE = re.compile(r"^\*\*(Requirement|Recommendation)\*\*:", re.MULTILINE)


@dataclass
class DriftReport:
added: dict[str, str] = field(default_factory=dict)
removed: list[str] = field(default_factory=list)
type_changed: dict[str, tuple[str, str]] = field(default_factory=dict)

@property
def has_drift(self) -> bool:
return bool(self.added or self.removed or self.type_changed)

def format_markdown(self, standard_url: str) -> str:
lines = [
"The upstream [Beman Standard](" + standard_url + ") "
"and [`beman_tidy/.beman-standard.yaml`]"
"(https://github.com/bemanproject/beman-tidy/blob/main/beman_tidy/.beman-standard.yaml) "
"are out of sync.",
"",
]

if self.added:
lines.append("## New rules in the standard (missing from YAML)")
lines.append("")
for rule_id, rule_type in sorted(self.added.items()):
lines.append(f"- `{rule_id}` — **{rule_type}**")
lines.append("")

if self.removed:
lines.append("## Rules removed from the standard (still in YAML)")
lines.append("")
for rule_id in sorted(self.removed):
lines.append(f"- `{rule_id}`")
lines.append("")

if self.type_changed:
lines.append("## Category mismatches")
lines.append("")
for rule_id, (md_type, yaml_type) in sorted(self.type_changed.items()):
lines.append(
f"- `{rule_id}`: standard says **{md_type}**, "
f"YAML has **{yaml_type}**"
)
lines.append("")

lines.extend(
[
"## Action",
"",
"Update `beman_tidy/.beman-standard.yaml` to match the standard, "
"then implement or track any new checks as needed.",
"",
"_This issue was opened automatically by the "
"[Sync Beman Standard workflow]"
"(https://github.com/bemanproject/beman-tidy/actions/workflows/sync-beman-standard.yml)._",
]
)
return "\n".join(lines)


def parse_beman_standard_markdown(text: str) -> dict[str, str]:
"""Extract check identifiers and types from beman_standard.md."""
rules: dict[str, str] = {}

for match in RULE_HEADING_RE.finditer(text):
rule_id = match.group(1).strip()
section_start = match.end()
next_heading = RULE_HEADING_RE.search(text, section_start)
section_end = next_heading.start() if next_heading else len(text)
section = text[section_start:section_end]

type_match = RULE_TYPE_RE.search(section)
if type_match is None:
raise ValueError(
f"No Requirement/Recommendation line found for rule '{rule_id}'"
)

rules[rule_id] = type_match.group(1)

return rules


def parse_beman_standard_yaml(text: str) -> dict[str, str]:
"""Extract check identifiers and types from .beman-standard.yaml."""
data = yaml.safe_load(text)
if not isinstance(data, dict):
raise ValueError("Beman Standard YAML must be a mapping")

rules: dict[str, str] = {}
for rule_id, entries in data.items():
if not isinstance(entries, list):
raise ValueError(f"Invalid config for rule '{rule_id}'")

rule_type = None
for entry in entries:
if isinstance(entry, dict) and "type" in entry:
rule_type = entry["type"]
break

if rule_type is None:
raise ValueError(f"No type entry found for rule '{rule_id}'")

rules[rule_id] = rule_type

return rules


def compare_standard_to_yaml(
markdown_rules: dict[str, str],
yaml_rules: dict[str, str],
) -> DriftReport:
"""Compare upstream standard rules against the local YAML snapshot."""
markdown_ids = set(markdown_rules)
yaml_ids = set(yaml_rules)

added = {
rule_id: markdown_rules[rule_id]
for rule_id in sorted(markdown_ids - yaml_ids)
}
removed = sorted(yaml_ids - markdown_ids)
type_changed = {
rule_id: (markdown_rules[rule_id], yaml_rules[rule_id])
for rule_id in sorted(markdown_ids & yaml_ids)
if markdown_rules[rule_id] != yaml_rules[rule_id]
}

return DriftReport(added=added, removed=removed, type_changed=type_changed)


def check_standard_drift(
markdown_path: Path | str,
yaml_path: Path | str,
) -> DriftReport:
markdown_text = Path(markdown_path).read_text(encoding="utf-8")
yaml_text = Path(yaml_path).read_text(encoding="utf-8")
return compare_standard_to_yaml(
parse_beman_standard_markdown(markdown_text),
parse_beman_standard_yaml(yaml_text),
)
Loading
Loading